Project

General

Profile

Download (16.2 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.ImportHelper;
30
import eu.etaxonomy.cdm.io.common.Source;
31
import eu.etaxonomy.cdm.model.agent.Team;
32
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
35
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
36
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
37
import eu.etaxonomy.cdm.model.description.Feature;
38
import eu.etaxonomy.cdm.model.description.TaxonDescription;
39
import eu.etaxonomy.cdm.model.description.TextData;
40
import eu.etaxonomy.cdm.model.reference.Reference;
41
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
42
import eu.etaxonomy.cdm.model.taxon.Synonym;
43
import eu.etaxonomy.cdm.model.taxon.Taxon;
44
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
45

    
46

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

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

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

    
65
		return result;
66
	}
67

    
68
	@Override
69
	protected void doInvoke(FaunaEuropaeaImportState state) {
70
		/*
71
		logger.warn("Start RefImport doInvoke");
72
		ProfilerController.memorySnapshot();
73
		*/
74

    
75
		Set<UUID> taxonUuids = null;
76
		Map<Integer, Reference<?>> references = null;
77
		Map<String,TeamOrPersonBase<?>> authors = null;
78
		Map<UUID, FaunaEuropaeaReferenceTaxon> fauEuTaxonMap = null;
79
		Map<Integer, UUID> referenceUuids = new HashMap<Integer, UUID>();
80
		Set<Integer> referenceIDs = null;
81
		int limit = state.getConfig().getLimitSave();
82

    
83
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
84
		Source source = fauEuConfig.getSource();
85

    
86
		String namespace = "Reference";
87
		int i = 0;
88

    
89
		String selectCountTaxRefs =
90
			" SELECT count(*) ";
91

    
92
		String selectColumnsTaxRefs =
93
			" SELECT Reference.*, TaxRefs.*, Taxon.UUID  ";
94

    
95
		String fromClauseTaxRefs =
96
			" FROM TaxRefs " +
97
			" INNER JOIN Reference ON Reference.ref_id = TaxRefs.trf_ref_id " +
98
			" INNER JOIN Taxon ON TaxRefs.trf_tax_id = Taxon.TAX_ID ";
99

    
100
		String orderClauseTaxRefs =
101
			" ORDER BY TaxRefs.trf_tax_id";
102

    
103
		String selectCountRefs =
104
			" SELECT count(*) FROM Reference";
105

    
106
		String selectColumnsRefs =
107
			" SELECT * FROM Reference order by ref_author";
108

    
109

    
110
		String countQueryTaxRefs =
111
			selectCountTaxRefs + fromClauseTaxRefs;
112

    
113
		String selectQueryTaxRefs =
114
			selectColumnsTaxRefs + fromClauseTaxRefs + orderClauseTaxRefs;
115

    
116
		String countQueryRefs =
117
			selectCountRefs;
118

    
119
		String selectQueryRefs =
120
			selectColumnsRefs;
121

    
122

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

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

    
135
        createTaxonReferenceRel(state, taxonUuids, fauEuTaxonMap,
136
				referenceUuids, referenceIDs, limit, source,
137
				countQueryTaxRefs, selectQueryTaxRefs);
138

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

    
145
		return;
146
	}
147

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

    
162
			rsRefs = source.getResultSet(selectQueryRefs);
163

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

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

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

    
190
				if ((i++ % limit) == 0) {
191

    
192
					txStatus = startTransaction();
193
					references = new HashMap<Integer,Reference<?>>(limit);
194
					authors = new HashMap<String,TeamOrPersonBase<?>>(limit);
195

    
196
					if(logger.isInfoEnabled()) {
197
						logger.info("i = " + i + " - Reference import transaction started");
198
					}
199
				}
200

    
201
				Reference<?> reference = null;
202
				TeamOrPersonBase<?> author = null;
203
				//ReferenceFactory refFactory = ReferenceFactory.newInstance();
204
				reference = ReferenceFactory.newGeneric();
205

    
206
//				reference.setTitleCache(title);
207
				reference.setTitle(title);
208
				reference.setDatePublished(ImportHelper.getDatePublished(year));
209

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

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

    
230
				reference.setAuthorship(author);
231

    
232
				ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
233
				ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
234

    
235
				// Store reference
236

    
237

    
238
				if (!references.containsKey(refId)) {
239

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

    
254
				if (((i % limit) == 0 && i > 1 ) || i == count ) {
255

    
256
					commitReferences(references, authors, referenceUuids, i,
257
							txStatus);
258
					references= null;
259
					authors = null;
260
				}
261

    
262

    
263

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

    
275
	}
276

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

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

    
291
		getAgentService().save((Collection)authors.values());
292
		commitTransaction(txStatus);
293
	}
294

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

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

    
312
			rsTaxRefs = source.getResultSet(selectQueryTaxRefs);
313

    
314
			logger.info("Start taxon reference-relationships");
315
			FaunaEuropaeaReference fauEuReference;
316
			FaunaEuropaeaReferenceTaxon fauEuReferenceTaxon;
317
			while (rsTaxRefs.next()) {
318

    
319

    
320
				if ((i++ % limit) == 0) {
321

    
322
					txStatus = startTransaction();
323
					taxonUuids = new HashSet<UUID>(limit);
324
					referenceIDs = new HashSet<Integer>(limit);
325
					fauEuTaxonMap = new HashMap<UUID, FaunaEuropaeaReferenceTaxon>(limit);
326

    
327
					if(logger.isInfoEnabled()) {
328
						logger.info("i = " + i + " - Reference import transaction started");
329
					}
330
				}
331

    
332

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

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

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

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

    
379
				if (!referenceIDs.contains(refId)) {
380

    
381

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

    
393
				fauEuTaxonMap.get(currentTaxonUuid).addReference(fauEuReference);
394

    
395
				if (((i % limit) == 0 && i > 1 ) || i == count) {
396

    
397
					try {
398
						commitTaxaReferenceRel(taxonUuids, fauEuTaxonMap,
399
								referenceUuids, referenceIDs, limit, txStatus, i,
400
								taxon);
401

    
402
						taxonUuids = null;
403

    
404
						fauEuTaxonMap = null;
405
						referenceIDs = null;
406

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

    
424
		fauEuTaxonMap = null;
425
		referenceIDs = null;
426

    
427
	}
428

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

    
452
			// Create descriptions
453

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

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

    
486

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

    
495
					TextData textData = TextData.NewInstance(Feature.CITATION());
496

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

    
513
		// save taxa
514
		getTaxonService().save(taxonList);
515
		commitTransaction(txStatus);
516

    
517
	}
518

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

    
537
}
(10-10/20)