Project

General

Profile

Download (16.4 KB) Statistics
| Branch: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.cdm.io.pesi.faunaEuropaea;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.Collection;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Map.Entry;
21
import java.util.Set;
22
import java.util.UUID;
23

    
24
import org.apache.log4j.Logger;
25
import org.springframework.stereotype.Component;
26
import org.springframework.transaction.TransactionStatus;
27

    
28
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
29
import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
30
import eu.etaxonomy.cdm.io.common.ImportHelper;
31
import eu.etaxonomy.cdm.io.common.Source;
32
import eu.etaxonomy.cdm.model.agent.Team;
33
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
36
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
37
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
38
import eu.etaxonomy.cdm.model.description.Feature;
39
import eu.etaxonomy.cdm.model.description.TaxonDescription;
40
import eu.etaxonomy.cdm.model.description.TextData;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
43
import eu.etaxonomy.cdm.model.taxon.Synonym;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
46

    
47

    
48
/**
49
 * @author a.babadshanjan
50
 * @created 12.05.2009
51
 * @version 1.0
52
 */
53
@Component
54
public class FaunaEuropaeaRefImport extends FaunaEuropaeaImportBase {
55
    private static final long serialVersionUID = -586555645981648177L;
56

    
57
    private static final Logger logger = Logger.getLogger(FaunaEuropaeaRefImport.class);
58

    
59
	@Override
60
	protected boolean doCheck(FaunaEuropaeaImportState state) {
61
		boolean result = true;
62
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
63
		logger.warn("Checking for References not yet fully implemented");
64
//		result &= checkReferenceStatus(fauEuConfig);
65

    
66
		return result;
67
	}
68

    
69
	@Override
70
	protected void doInvoke(FaunaEuropaeaImportState state) {
71
		/*
72
		logger.warn("Start RefImport doInvoke");
73
		ProfilerController.memorySnapshot();
74
		*/
75
		if (state.getConfig().getDoReferences().equals(DO_REFERENCES.NONE)){
76
			return;
77
		}
78
		Set<UUID> taxonUuids = null;
79
		Map<Integer, Reference<?>> references = null;
80
		Map<String,TeamOrPersonBase<?>> authors = null;
81
		Map<UUID, FaunaEuropaeaReferenceTaxon> fauEuTaxonMap = null;
82
		Map<Integer, UUID> referenceUuids = new HashMap<Integer, UUID>();
83
		Set<Integer> referenceIDs = null;
84
		int limit = state.getConfig().getLimitSave();
85

    
86
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
87
		Source source = fauEuConfig.getSource();
88

    
89
		String namespace = "Reference";
90
		int i = 0;
91

    
92
		String selectCountTaxRefs =
93
			" SELECT count(*) ";
94

    
95
		String selectColumnsTaxRefs =
96
			" SELECT Reference.*, TaxRefs.*, Taxon.UUID  ";
97

    
98
		String fromClauseTaxRefs =
99
			" FROM TaxRefs " +
100
			" INNER JOIN Reference ON Reference.ref_id = TaxRefs.trf_ref_id " +
101
			" INNER JOIN Taxon ON TaxRefs.trf_tax_id = Taxon.TAX_ID ";
102

    
103
		String orderClauseTaxRefs =
104
			" ORDER BY TaxRefs.trf_tax_id";
105

    
106
		String selectCountRefs =
107
			" SELECT count(*) FROM Reference";
108

    
109
		String selectColumnsRefs =
110
			" SELECT * FROM Reference order by ref_author";
111

    
112

    
113
		String countQueryTaxRefs =
114
			selectCountTaxRefs + fromClauseTaxRefs;
115

    
116
		String selectQueryTaxRefs =
117
			selectColumnsTaxRefs + fromClauseTaxRefs + orderClauseTaxRefs;
118

    
119
		String countQueryRefs =
120
			selectCountRefs;
121

    
122
		String selectQueryRefs =
123
			selectColumnsRefs;
124

    
125

    
126
		if(logger.isInfoEnabled()) { logger.info("Start making References..."); }
127
		//first add all References to CDM
128
		processReferences(state, references, authors,
129
				referenceUuids, limit, fauEuConfig, source, namespace, i,
130
				countQueryRefs, selectQueryRefs);
131

    
132
	    /*
133
		logger.warn("Start ref taxon relationships");
134
		ProfilerController.memorySnapshot();
135
	 	*/
136
	 //create the relationships between references and taxa
137

    
138
        createTaxonReferenceRel(state, taxonUuids, fauEuTaxonMap,
139
				referenceUuids, referenceIDs, limit, source,
140
				countQueryTaxRefs, selectQueryTaxRefs);
141

    
142
        /*
143
		logger.warn("End RefImport doInvoke");
144
		ProfilerController.memorySnapshot();
145
		*/
146
		if(logger.isInfoEnabled()) { logger.info("End making references ..."); }
147

    
148
		return;
149
	}
150

    
151
	private void processReferences(FaunaEuropaeaImportState state,
152
			Map<Integer, Reference<?>> references,
153
			Map<String, TeamOrPersonBase<?>> authors,
154
			Map<Integer, UUID> referenceUuids, int limit,
155
			FaunaEuropaeaImportConfigurator fauEuConfig, Source source,
156
			String namespace, int i, String countQueryRefs,
157
			String selectQueryRefs) {
158
		TransactionStatus txStatus = null;
159
		int count;
160
		try {
161
			ResultSet rsRefs = source.getResultSet(countQueryRefs);
162
			rsRefs.next();
163
			count = rsRefs.getInt(1);
164

    
165
			rsRefs = source.getResultSet(selectQueryRefs);
166

    
167
	        if (logger.isInfoEnabled()) {
168
	        	logger.info("Get all References...");
169
				logger.info("Number of rows: " + count);
170
				logger.info("Count Query: " + countQueryRefs);
171
				logger.info("Select Query: " + selectQueryRefs);
172
			}
173

    
174
	        while (rsRefs.next()){
175
	        	int refId = rsRefs.getInt("ref_id");
176
	        	String var = "\u00A7";
177
				String refAuthor = deleteSymbol(var,rsRefs.getString("ref_author"));
178
				
179
				String year = deleteSymbol(var, rsRefs.getString("ref_year"));
180
				String title = deleteSymbol(var, rsRefs.getString("ref_title"));
181

    
182
				if (year == null){
183
					try{
184
						year = String.valueOf((Integer.parseInt(title)));
185
					}
186
					catch(Exception ex)
187
					{
188
						logger.info("year is empty and " +title + " contains no integer");
189
				    }
190
				}
191
				String refSource = rsRefs.getString("ref_source");
192

    
193
				if ((i++ % limit) == 0) {
194

    
195
					txStatus = startTransaction();
196
					references = new HashMap<Integer,Reference<?>>(limit);
197
					authors = new HashMap<String,TeamOrPersonBase<?>>(limit);
198

    
199
					if(logger.isInfoEnabled()) {
200
						logger.info("i = " + i + " - Reference import transaction started");
201
					}
202
				}
203

    
204
				Reference<?> reference = null;
205
				TeamOrPersonBase<?> author = null;
206
				//ReferenceFactory refFactory = ReferenceFactory.newInstance();
207
				reference = ReferenceFactory.newGeneric();
208

    
209
//				reference.setTitleCache(title);
210
				reference.setTitle(title);
211
				reference.setDatePublished(ImportHelper.getDatePublished(year));
212

    
213
				if (!authors.containsKey(refAuthor)) {
214
					if (refAuthor == null) {
215
						logger.warn("Reference author is null");
216
					}
217
					author = Team.NewInstance();
218
					author.setTitleCache(refAuthor, true);
219
					authors.put(refAuthor,author);
220
					if (logger.isTraceEnabled()) {
221
						logger.trace("Stored author (" + refAuthor + ")");
222
					}
223
				//}
224

    
225
				} else {
226
					author = authors.get(refAuthor);
227
					if (logger.isDebugEnabled()) {
228
						logger.debug("Not imported author with duplicated aut_id (" + refId +
229
							") " + refAuthor);
230
					}
231
				}
232

    
233
				reference.setAuthorship(author);
234

    
235
				ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
236
				ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
237

    
238
				// Store reference
239

    
240

    
241
				if (!references.containsKey(refId)) {
242

    
243
					if (reference == null) {
244
						logger.warn("Reference is null");
245
					}
246
					references.put(refId, reference);
247
					if (logger.isTraceEnabled()) {
248
						logger.trace("Stored reference (" + refAuthor + ")");
249
					}
250
				} else {
251
					if (logger.isDebugEnabled()) {
252
						logger.debug("Duplicated reference (" + refId + ", " + refAuthor + ")");
253
					}
254
					//continue;
255
				}
256

    
257
				if (((i % limit) == 0 && i > 1 ) || i == count ) {
258

    
259
					commitReferences(references, authors, referenceUuids, i,
260
							txStatus);
261
					references= null;
262
					authors = null;
263
				}
264

    
265

    
266

    
267
	        }
268
	        if (references != null){
269
	        	commitReferences(references, authors, referenceUuids, i, txStatus);
270
	        	references= null;
271
				authors = null;
272
	        }
273
		}catch(SQLException e) {
274
			logger.error("SQLException:" +  e);
275
			state.setUnsuccessfull();
276
		}
277

    
278
	}
279

    
280
	private void commitReferences(Map<Integer, Reference<?>> references,
281
			Map<String, TeamOrPersonBase<?>> authors,
282
			Map<Integer, UUID> referenceUuids, int i, TransactionStatus txStatus) {
283
		Map <UUID, Reference> referenceMap =getReferenceService().save((Collection)references.values());
284
		logger.info("i = " + i + " - references saved");
285

    
286
		Iterator<Entry<UUID, Reference>> it = referenceMap.entrySet().iterator();
287
		while (it.hasNext()){
288
			Reference<?> ref = it.next().getValue();
289
			int refID = Integer.valueOf((ref.getSources().iterator().next()).getIdInSource());
290
			UUID uuid = ref.getUuid();
291
			referenceUuids.put(refID, uuid);
292
		}
293

    
294
		getAgentService().save((Collection)authors.values());
295
		commitTransaction(txStatus);
296
	}
297

    
298
	private void createTaxonReferenceRel(FaunaEuropaeaImportState state,
299
			Set<UUID> taxonUuids,
300
			Map<UUID, FaunaEuropaeaReferenceTaxon> fauEuTaxonMap,
301
			Map<Integer, UUID> referenceUuids, Set<Integer> referenceIDs,
302
			int limit, Source source, String countQueryTaxRefs,
303
			String selectQueryTaxRefs) {
304

    
305
		TransactionStatus txStatus = null;
306
		int i;
307
		int count;
308
		Taxon taxon = null;
309
		i = 0;
310
		try{
311
			ResultSet rsTaxRefs = source.getResultSet(countQueryTaxRefs);
312
			rsTaxRefs.next();
313
			count = rsTaxRefs.getInt(1);
314

    
315
			rsTaxRefs = source.getResultSet(selectQueryTaxRefs);
316

    
317
			logger.info("Start taxon reference-relationships");
318
			FaunaEuropaeaReference fauEuReference;
319
			FaunaEuropaeaReferenceTaxon fauEuReferenceTaxon;
320
			while (rsTaxRefs.next()) {
321

    
322

    
323
				if ((i++ % limit) == 0) {
324

    
325
					txStatus = startTransaction();
326
					taxonUuids = new HashSet<UUID>(limit);
327
					referenceIDs = new HashSet<Integer>(limit);
328
					fauEuTaxonMap = new HashMap<UUID, FaunaEuropaeaReferenceTaxon>(limit);
329

    
330
					if(logger.isInfoEnabled()) {
331
						logger.info("i = " + i + " - Reference import transaction started");
332
					}
333
				}
334

    
335

    
336
				int taxonId = rsTaxRefs.getInt("trf_tax_id");
337
				int refId = rsTaxRefs.getInt("ref_id");
338
				String refAuthor = rsTaxRefs.getString("ref_author");
339
				String year = rsTaxRefs.getString("ref_year");
340
				String title = rsTaxRefs.getString("ref_title");
341

    
342
				if (year == null){
343
					try{
344
						year = String.valueOf((Integer.parseInt(title)));
345
					}
346
					catch(Exception ex)
347
					{
348
						logger.info("year is empty and " +title + " contains no integer");
349
				    }
350
				}
351
				String refSource = rsTaxRefs.getString("ref_source");
352
				String page = rsTaxRefs.getString("trf_page");
353
				UUID currentTaxonUuid = null;
354
				if (resultSetHasColumn(rsTaxRefs, "UUID")){
355
					currentTaxonUuid = UUID.fromString(rsTaxRefs.getString("UUID"));
356
				} else {
357
					logger.error("Taxon (" + taxonId + ") without UUID ignored");
358
					continue;
359
				}
360

    
361
				fauEuReference = new FaunaEuropaeaReference();
362
				fauEuReference.setTaxonUuid(currentTaxonUuid);
363
				fauEuReference.setReferenceId(refId);
364
				fauEuReference.setReferenceAuthor(refAuthor);
365
				fauEuReference.setReferenceYear(year);
366
				fauEuReference.setReferenceTitle(title);
367
				fauEuReference.setReferenceSource(refSource);
368
				fauEuReference.setPage(page);
369

    
370
				if (!taxonUuids.contains(currentTaxonUuid)) {
371
					taxonUuids.add(currentTaxonUuid);
372
					fauEuReferenceTaxon =
373
						new FaunaEuropaeaReferenceTaxon(currentTaxonUuid);
374
					fauEuTaxonMap.put(currentTaxonUuid, fauEuReferenceTaxon);
375
				} else {
376
					if (logger.isTraceEnabled()) {
377
						logger.trace("Taxon (" + currentTaxonUuid + ") already stored.");
378
						//continue; ein Taxon kann mehr als eine Referenz haben
379
					}
380
				}
381

    
382
				if (!referenceIDs.contains(refId)) {
383

    
384

    
385
					referenceIDs.add(refId);
386
					if (logger.isTraceEnabled()) {
387
						logger.trace("Stored reference (" + refAuthor + ")");
388
					}
389
				} else {
390
					if (logger.isDebugEnabled()) {
391
						logger.debug("Duplicated reference (" + refId + ", " + refAuthor + ")");
392
					}
393
					//continue;
394
				}
395

    
396
				fauEuTaxonMap.get(currentTaxonUuid).addReference(fauEuReference);
397

    
398
				if (((i % limit) == 0 && i > 1 ) || i == count) {
399

    
400
					try {
401
						commitTaxaReferenceRel(taxonUuids, fauEuTaxonMap,
402
								referenceUuids, referenceIDs, limit, txStatus, i,
403
								taxon);
404

    
405
						taxonUuids = null;
406

    
407
						fauEuTaxonMap = null;
408
						referenceIDs = null;
409

    
410
					} catch (Exception e) {
411
						logger.warn("An exception occurred when creating reference, reference could not be saved.");
412
					}
413
				}
414
			}
415
			if (taxonUuids != null){
416
				commitTaxaReferenceRel(taxonUuids, fauEuTaxonMap,
417
						referenceUuids, referenceIDs, limit, txStatus, i,
418
						taxon);
419
			}
420
			rsTaxRefs.close();
421
		} catch (SQLException e) {
422
				logger.error("SQLException:" +  e);
423
				state.setUnsuccessfull();
424
		}
425
		taxonUuids = null;
426

    
427
		fauEuTaxonMap = null;
428
		referenceIDs = null;
429

    
430
	}
431

    
432
	private void commitTaxaReferenceRel(Set<UUID> taxonUuids,
433
			Map<UUID, FaunaEuropaeaReferenceTaxon> fauEuTaxonMap,
434
			Map<Integer, UUID> referenceUuids, Set<Integer> referenceIDs,
435
			int limit, TransactionStatus txStatus, int i, Taxon taxon) {
436
		List<TaxonBase> taxonList;
437
		List<Reference> referenceList;
438
		Map<Integer, Reference> references;
439
		taxonList = getTaxonService().find(taxonUuids);
440
		//get UUIDs of used references
441
		Iterator<?> itRefs = referenceIDs.iterator();
442
		Set<UUID> uuidSet = new HashSet<UUID>(referenceIDs.size());
443
		UUID uuid;
444
		while (itRefs.hasNext()){
445
			uuid = referenceUuids.get(itRefs.next());
446
			uuidSet.add(uuid);
447
		}
448
		referenceList = getReferenceService().find(uuidSet);
449
		references = new HashMap<Integer, Reference>(limit);
450
		for (Reference<?> ref : referenceList){
451
			references.put(Integer.valueOf(((OriginalSourceBase)ref.getSources().iterator().next()).getIdInSource()), ref);
452
		}
453
		for (TaxonBase<?> taxonBase : taxonList) {
454

    
455
			// Create descriptions
456

    
457
			if (taxonBase == null) {
458
				if (logger.isDebugEnabled()) {
459
					logger.debug("TaxonBase is null ");
460
				}
461
				continue;
462
			}
463
			boolean isSynonym = taxonBase.isInstanceOf(Synonym.class);
464
			if (isSynonym) {
465
				Synonym syn = CdmBase.deproxy(taxonBase, Synonym.class);
466
				Set<Taxon> acceptedTaxa = syn.getAcceptedTaxa();
467
				if (acceptedTaxa.size() > 0) {
468
					taxon = syn.getAcceptedTaxa().iterator().next();
469
					//logger.warn("Synonym (" + taxonBase.getUuid() + ") has accepted taxon" + taxon.getUuid());
470
				} else {
471
//								if (logger.isDebugEnabled()) {
472
					logger.warn("Synonym (" + taxonBase.getUuid() + ") does not have accepted taxa");
473
//								}
474
				}
475
			} else {
476
				taxon = CdmBase.deproxy(taxonBase, Taxon.class);
477
			}
478

    
479
			if (taxon != null) {
480
				TaxonDescription taxonDescription;
481
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
482
				if (descriptions.size() > 0) {
483
					taxonDescription = descriptions.iterator().next();
484
				} else {
485
					taxonDescription = TaxonDescription.NewInstance();
486
					taxon.addDescription(taxonDescription);
487
				}
488

    
489

    
490
				UUID taxonUuid = taxonBase.getUuid();
491
				FaunaEuropaeaReferenceTaxon fauEuHelperTaxon = fauEuTaxonMap.get(taxonUuid);
492
				Reference<?> citation;
493
				String microCitation;
494
				DescriptionElementSource originalSource;
495
				Synonym syn;
496
				for (FaunaEuropaeaReference storedReference : fauEuHelperTaxon.getReferences()) {
497

    
498
					TextData textData = TextData.NewInstance(Feature.CITATION());
499

    
500
					citation = references.get(storedReference.getReferenceId());
501
					microCitation = storedReference.getPage();
502
					originalSource = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, microCitation, null, null);
503
					if (isSynonym){
504
						syn = CdmBase.deproxy(taxonBase, Synonym.class);
505
						originalSource.setNameUsedInSource(syn.getName());
506
					}
507
					textData.addSource(originalSource);
508
					taxonDescription.addElement(textData);
509
				}
510
			}
511
		}
512
		if(logger.isInfoEnabled()) {
513
			logger.info("i = " + i + " - Transaction committed");
514
		}
515

    
516
		// save taxa
517
		getTaxonService().save(taxonList);
518
		commitTransaction(txStatus);
519

    
520
	}
521

    
522
	@Override
523
	protected boolean isIgnore(FaunaEuropaeaImportState state){
524
		return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
525
	}
526
	
527
	private String deleteSymbol(String symbol, String stringVar){
528
		if (stringVar.startsWith(symbol)){
529
			if (stringVar.endsWith(symbol)){
530
				stringVar = stringVar.substring(1,stringVar.length()-1);
531
			}else{
532
				stringVar = stringVar.substring(1);
533
			}
534
		} else if (stringVar.endsWith(symbol)){
535
			stringVar = stringVar.substring(0, stringVar.length()-1);
536
		}
537
		return stringVar;
538
	}
539

    
540
}
(10-10/20)