Project

General

Profile

Download (16.6 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
		if (state.getConfig().getSourceReference().getId() == 0){
79
		    Reference sourceRef = getReferenceService().find(state.getConfig().getSourceRefUuid());
80
		    state.getConfig().setSourceReference(sourceRef);
81
		}
82
		Set<UUID> taxonUuids = null;
83
		Map<Integer, Reference<?>> references = null;
84
		Map<String,TeamOrPersonBase<?>> authors = null;
85
		Map<UUID, FaunaEuropaeaReferenceTaxon> fauEuTaxonMap = null;
86
		Map<Integer, UUID> referenceUuids = new HashMap<Integer, UUID>();
87
		Set<Integer> referenceIDs = null;
88
		int limit = state.getConfig().getLimitSave();
89

    
90
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
91
		Source source = fauEuConfig.getSource();
92

    
93
		String namespace = "Reference";
94
		int i = 0;
95

    
96
		String selectCountTaxRefs =
97
			" SELECT count(*) ";
98

    
99
		String selectColumnsTaxRefs =
100
			" SELECT Reference.*, TaxRefs.*, Taxon.UUID  ";
101

    
102
		String fromClauseTaxRefs =
103
			" FROM TaxRefs " +
104
			" INNER JOIN Reference ON Reference.ref_id = TaxRefs.trf_ref_id " +
105
			" INNER JOIN Taxon ON TaxRefs.trf_tax_id = Taxon.TAX_ID ";
106

    
107
		String orderClauseTaxRefs =
108
			" ORDER BY TaxRefs.trf_tax_id";
109

    
110
		String selectCountRefs =
111
			" SELECT count(*) FROM Reference";
112

    
113
		String selectColumnsRefs =
114
			" SELECT * FROM Reference order by ref_author";
115

    
116

    
117
		String countQueryTaxRefs =
118
			selectCountTaxRefs + fromClauseTaxRefs;
119

    
120
		String selectQueryTaxRefs =
121
			selectColumnsTaxRefs + fromClauseTaxRefs + orderClauseTaxRefs;
122

    
123
		String countQueryRefs =
124
			selectCountRefs;
125

    
126
		String selectQueryRefs =
127
			selectColumnsRefs;
128

    
129

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

    
136
	    /*
137
		logger.warn("Start ref taxon relationships");
138
		ProfilerController.memorySnapshot();
139
	 	*/
140
	 //create the relationships between references and taxa
141

    
142
        createTaxonReferenceRel(state, taxonUuids, fauEuTaxonMap,
143
				referenceUuids, referenceIDs, limit, source,
144
				countQueryTaxRefs, selectQueryTaxRefs);
145

    
146
        /*
147
		logger.warn("End RefImport doInvoke");
148
		ProfilerController.memorySnapshot();
149
		*/
150
		if(logger.isInfoEnabled()) { logger.info("End making references ..."); }
151

    
152
		return;
153
	}
154

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

    
169
			rsRefs = source.getResultSet(selectQueryRefs);
170

    
171
	        if (logger.isInfoEnabled()) {
172
	        	logger.info("Get all References...");
173
				logger.info("Number of rows: " + count);
174
				logger.info("Count Query: " + countQueryRefs);
175
				logger.info("Select Query: " + selectQueryRefs);
176
			}
177

    
178
	        while (rsRefs.next()){
179
	        	int refId = rsRefs.getInt("ref_id");
180
	        	String var = "\u00A7";
181
				String refAuthor = deleteSymbol(var,rsRefs.getString("ref_author"));
182

    
183
				String year = deleteSymbol(var, rsRefs.getString("ref_year"));
184
				String title = deleteSymbol(var, rsRefs.getString("ref_title"));
185

    
186
				if (year == null){
187
					try{
188
						year = String.valueOf((Integer.parseInt(title)));
189
					}
190
					catch(Exception ex)
191
					{
192
						logger.info("year is empty and " +title + " contains no integer");
193
				    }
194
				}
195
				String refSource = deleteSymbol(var, rsRefs.getString("ref_source"));
196

    
197
				if ((i++ % limit) == 0) {
198

    
199
					txStatus = startTransaction();
200
					references = new HashMap<Integer,Reference<?>>(limit);
201
					authors = new HashMap<String,TeamOrPersonBase<?>>(limit);
202

    
203
					if(logger.isInfoEnabled()) {
204
						logger.info("i = " + i + " - Reference import transaction started");
205
					}
206
				}
207

    
208
				Reference<?> reference = null;
209
				TeamOrPersonBase<?> author = null;
210
				//ReferenceFactory refFactory = ReferenceFactory.newInstance();
211
				reference = ReferenceFactory.newGeneric();
212

    
213
//				reference.setTitleCache(title);
214
				reference.setTitle(title);
215
				reference.setDatePublished(ImportHelper.getDatePublished(year));
216
				reference.setTitleCache(title + " " +refSource , true);
217

    
218
				if (!authors.containsKey(refAuthor)) {
219
					if (refAuthor == null) {
220
						logger.warn("Reference author is null");
221
					}
222
					author = Team.NewInstance();
223
					author.setTitleCache(refAuthor, true);
224
					authors.put(refAuthor,author);
225
					if (logger.isTraceEnabled()) {
226
						logger.trace("Stored author (" + refAuthor + ")");
227
					}
228
				//}
229

    
230
				} else {
231
					author = authors.get(refAuthor);
232
					if (logger.isDebugEnabled()) {
233
						logger.debug("Not imported author with duplicated aut_id (" + refId +
234
							") " + refAuthor);
235
					}
236
				}
237

    
238
				reference.setAuthorship(author);
239

    
240
				ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
241
				ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
242

    
243
				// Store reference
244

    
245

    
246
				if (!references.containsKey(refId)) {
247

    
248
					if (reference == null) {
249
						logger.warn("Reference is null");
250
					}
251
					references.put(refId, reference);
252
					if (logger.isTraceEnabled()) {
253
						logger.trace("Stored reference (" + refAuthor + ")");
254
					}
255
				} else {
256
					if (logger.isDebugEnabled()) {
257
						logger.debug("Duplicated reference (" + refId + ", " + refAuthor + ")");
258
					}
259
					//continue;
260
				}
261

    
262
				if (((i % limit) == 0 && i > 1 ) || i == count ) {
263

    
264
					commitReferences(references, authors, referenceUuids, i,
265
							txStatus);
266
					references= null;
267
					authors = null;
268
				}
269

    
270

    
271

    
272
	        }
273
	        if (references != null){
274
	        	commitReferences(references, authors, referenceUuids, i, txStatus);
275
	        	references= null;
276
				authors = null;
277
	        }
278
		}catch(SQLException e) {
279
			logger.error("SQLException:" +  e);
280
			state.setUnsuccessfull();
281
		}
282

    
283
	}
284

    
285
	private void commitReferences(Map<Integer, Reference<?>> references,
286
			Map<String, TeamOrPersonBase<?>> authors,
287
			Map<Integer, UUID> referenceUuids, int i, TransactionStatus txStatus) {
288

    
289
		Map <UUID, Reference> referenceMap =getReferenceService().save((Collection)references.values());
290
		logger.info("i = " + i + " - references saved");
291

    
292
		Iterator<Entry<UUID, Reference>> it = referenceMap.entrySet().iterator();
293
		while (it.hasNext()){
294
			Reference<?> ref = it.next().getValue();
295
			int refID = Integer.valueOf((ref.getSources().iterator().next()).getIdInSource());
296
			UUID uuid = ref.getUuid();
297
			referenceUuids.put(refID, uuid);
298
		}
299

    
300
		getAgentService().save((Collection)authors.values());
301
		commitTransaction(txStatus);
302
	}
303

    
304
	private void createTaxonReferenceRel(FaunaEuropaeaImportState state,
305
			Set<UUID> taxonUuids,
306
			Map<UUID, FaunaEuropaeaReferenceTaxon> fauEuTaxonMap,
307
			Map<Integer, UUID> referenceUuids, Set<Integer> referenceIDs,
308
			int limit, Source source, String countQueryTaxRefs,
309
			String selectQueryTaxRefs) {
310

    
311
		TransactionStatus txStatus = null;
312
		int i;
313
		int count;
314
		Taxon taxon = null;
315
		i = 0;
316
		try{
317
			ResultSet rsTaxRefs = source.getResultSet(countQueryTaxRefs);
318
			rsTaxRefs.next();
319
			count = rsTaxRefs.getInt(1);
320

    
321
			rsTaxRefs = source.getResultSet(selectQueryTaxRefs);
322

    
323
			logger.info("Start taxon reference-relationships");
324
			FaunaEuropaeaReference fauEuReference;
325
			FaunaEuropaeaReferenceTaxon fauEuReferenceTaxon;
326
			while (rsTaxRefs.next()) {
327

    
328

    
329
				if ((i++ % limit) == 0) {
330

    
331
					txStatus = startTransaction();
332
					taxonUuids = new HashSet<UUID>(limit);
333
					referenceIDs = new HashSet<Integer>(limit);
334
					fauEuTaxonMap = new HashMap<UUID, FaunaEuropaeaReferenceTaxon>(limit);
335

    
336
					if(logger.isInfoEnabled()) {
337
						logger.info("i = " + i + " - Reference import transaction started");
338
					}
339
				}
340

    
341

    
342
				int taxonId = rsTaxRefs.getInt("trf_tax_id");
343
				int refId = rsTaxRefs.getInt("ref_id");
344
				String refAuthor = rsTaxRefs.getString("ref_author");
345
				String year = rsTaxRefs.getString("ref_year");
346
				String title = rsTaxRefs.getString("ref_title");
347

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

    
367
				fauEuReference = new FaunaEuropaeaReference();
368
				fauEuReference.setTaxonUuid(currentTaxonUuid);
369
				fauEuReference.setReferenceId(refId);
370
				fauEuReference.setReferenceAuthor(refAuthor);
371
				fauEuReference.setReferenceYear(year);
372
				fauEuReference.setReferenceTitle(title);
373
				fauEuReference.setReferenceSource(refSource);
374
				fauEuReference.setPage(page);
375

    
376
				if (!taxonUuids.contains(currentTaxonUuid)) {
377
					taxonUuids.add(currentTaxonUuid);
378
					fauEuReferenceTaxon =
379
						new FaunaEuropaeaReferenceTaxon(currentTaxonUuid);
380
					fauEuTaxonMap.put(currentTaxonUuid, fauEuReferenceTaxon);
381
				} else {
382
					if (logger.isTraceEnabled()) {
383
						logger.trace("Taxon (" + currentTaxonUuid + ") already stored.");
384
						//continue; ein Taxon kann mehr als eine Referenz haben
385
					}
386
				}
387

    
388
				if (!referenceIDs.contains(refId)) {
389

    
390

    
391
					referenceIDs.add(refId);
392
					if (logger.isTraceEnabled()) {
393
						logger.trace("Stored reference (" + refAuthor + ")");
394
					}
395
				} else {
396
					if (logger.isDebugEnabled()) {
397
						logger.debug("Duplicated reference (" + refId + ", " + refAuthor + ")");
398
					}
399
					//continue;
400
				}
401

    
402
				fauEuTaxonMap.get(currentTaxonUuid).addReference(fauEuReference);
403

    
404
				if (((i % limit) == 0 && i > 1 ) || i == count) {
405

    
406
					try {
407
						commitTaxaReferenceRel(taxonUuids, fauEuTaxonMap,
408
								referenceUuids, referenceIDs, limit, txStatus, i,
409
								taxon);
410

    
411
						taxonUuids = null;
412

    
413
						fauEuTaxonMap = null;
414
						referenceIDs = null;
415

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

    
433
		fauEuTaxonMap = null;
434
		referenceIDs = null;
435

    
436
	}
437

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

    
455
		referenceList = getReferenceService().find(uuidSet);
456

    
457
		references = new HashMap<Integer, Reference>(limit);
458
		for (Reference<?> ref : referenceList){
459
			references.put(Integer.valueOf(((OriginalSourceBase)ref.getSources().iterator().next()).getIdInSource()), ref);
460
		}
461
		for (TaxonBase<?> taxonBase : taxonList) {
462

    
463
			// Create descriptions
464

    
465
			if (taxonBase == null) {
466
				if (logger.isDebugEnabled()) {
467
					logger.debug("TaxonBase is null ");
468
				}
469
				continue;
470
			}
471
			boolean isSynonym = taxonBase.isInstanceOf(Synonym.class);
472
			if (isSynonym) {
473
				Synonym syn = CdmBase.deproxy(taxonBase, Synonym.class);
474
				Set<Taxon> acceptedTaxa = syn.getAcceptedTaxa();
475
				if (acceptedTaxa.size() > 0) {
476
					taxon = syn.getAcceptedTaxa().iterator().next();
477
					//logger.warn("Synonym (" + taxonBase.getUuid() + ") has accepted taxon" + taxon.getUuid());
478
				} else {
479
//								if (logger.isDebugEnabled()) {
480
					logger.warn("Synonym (" + taxonBase.getUuid() + ") does not have accepted taxa");
481
//								}
482
				}
483
			} else {
484
				taxon = CdmBase.deproxy(taxonBase, Taxon.class);
485
			}
486

    
487
			if (taxon != null) {
488
				TaxonDescription taxonDescription;
489
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
490
				if (descriptions.size() > 0) {
491
					taxonDescription = descriptions.iterator().next();
492
				} else {
493
					taxonDescription = TaxonDescription.NewInstance();
494
					taxon.addDescription(taxonDescription);
495
				}
496

    
497

    
498
				UUID taxonUuid = taxonBase.getUuid();
499
				FaunaEuropaeaReferenceTaxon fauEuHelperTaxon = fauEuTaxonMap.get(taxonUuid);
500
				Reference<?> citation;
501
				String microCitation;
502
				DescriptionElementSource originalSource;
503
				Synonym syn;
504
				for (FaunaEuropaeaReference storedReference : fauEuHelperTaxon.getReferences()) {
505

    
506
					TextData textData = TextData.NewInstance(Feature.CITATION());
507

    
508
					citation = references.get(storedReference.getReferenceId());
509
					microCitation = null;
510
					originalSource = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, microCitation, null, null);
511
					if (isSynonym){
512
						syn = CdmBase.deproxy(taxonBase, Synonym.class);
513
						originalSource.setNameUsedInSource(syn.getName());
514
					}
515
					textData.addSource(originalSource);
516
					taxonDescription.addElement(textData);
517
				}
518
			}
519
		}
520
		if(logger.isInfoEnabled()) {
521
			logger.info("i = " + i + " - Transaction committed");
522
		}
523

    
524
		// save taxa
525
		getTaxonService().save(taxonList);
526
		commitTransaction(txStatus);
527

    
528
	}
529

    
530
	@Override
531
	protected boolean isIgnore(FaunaEuropaeaImportState state){
532
		return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
533
	}
534

    
535
	private String deleteSymbol(String symbol, String stringVar){
536
		if (stringVar.startsWith(symbol)){
537
			if (stringVar.endsWith(symbol)){
538
				stringVar = stringVar.substring(1,stringVar.length()-1);
539
			}else{
540
				stringVar = stringVar.substring(1);
541
			}
542
		} else if (stringVar.endsWith(symbol)){
543
			stringVar = stringVar.substring(0, stringVar.length()-1);
544
		}
545
		return stringVar;
546
	}
547

    
548
}
(10-10/20)