Project

General

Profile

Download (32.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 static eu.etaxonomy.cdm.io.pesi.faunaEuropaea.FaunaEuropaeaTransformer.A_AUCT;
13

    
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Set;
21
import java.util.UUID;
22

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

    
28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.io.common.ICdmIO;
30
import eu.etaxonomy.cdm.io.common.MapWrapper;
31
import eu.etaxonomy.cdm.io.common.Source;
32
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33
import eu.etaxonomy.cdm.model.common.Annotation;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.Language;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37
import eu.etaxonomy.cdm.model.taxon.Classification;
38
import eu.etaxonomy.cdm.model.taxon.Synonym;
39
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
43
//import eu.etaxonomy.cdm.profiler.ProfilerController;
44
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
45

    
46

    
47

    
48
/**
49
 * @author a.babadshanjan
50
 * @created 12.05.2009
51
 */
52
@Component
53
public class FaunaEuropaeaRelTaxonIncludeImport extends FaunaEuropaeaImportBase  {
54

    
55
	public static final String OS_NAMESPACE_TAXON = "Taxon";
56
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaRelTaxonIncludeImport.class);
57
	//private static final String acceptedTaxonUUID = "A9C24E42-69F5-4681-9399-041E652CF338"; // any accepted taxon uuid, taken from original fauna europaea database
58
	//private static final String acceptedTaxonUUID = "E23E6295-836A-4332-BF72-7D29949C7C60"; //faunaEu_1_3
59
	//private static final String acceptedTaxonUUID = "bab7642e-f733-4a21-848d-a15250d2f4ed"; //for faunEu (2.4)
60
	private static final String acceptedTaxonUUID = "DADA6F44-B7B5-4C0A-9F32-980F54B02C36"; // for MfNFaunaEuropaea
61

    
62

    
63
	private Reference<?> sourceRef;
64
	private static String ALL_SYNONYM_FROM_CLAUSE = " FROM Taxon INNER JOIN Taxon AS Parent " +
65
	" ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
66
	" WHERE (Taxon.TAX_VALID = 0) " +
67
	" AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";
68

    
69
	@Override
70
	protected boolean doCheck(FaunaEuropaeaImportState state) {
71
		boolean result = true;
72
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
73
		logger.warn("Checking for Taxa not yet fully implemented");
74
		result &= checkTaxonStatus(fauEuConfig);
75

    
76
		return result;
77
	}
78

    
79
	@Override
80
	protected boolean isIgnore(FaunaEuropaeaImportState state) {
81
		return ! (state.getConfig().isDoTaxonomicallyIncluded() ||
82
		state.getConfig().isDoMisappliedNames() || state.getConfig().isDoHeterotypicSynonyms());
83
	}
84

    
85
	private boolean checkTaxonStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
86
		boolean result = true;
87
//		try {
88
			Source source = fauEuConfig.getSource();
89
			String sqlStr = "";
90
			ResultSet rs = source.getResultSet(sqlStr);
91
			return result;
92
//		} catch (SQLException e) {
93
//			e.printStackTrace();
94
//			return false;
95
//		}
96
	}
97

    
98
	@Override
99
	protected void doInvoke(FaunaEuropaeaImportState state) {
100

    
101
		/*logger.warn("Start RelTaxon doInvoke");
102
		ProfilerController.memorySnapshot();
103
		*/
104
		
105
		if (!state.getConfig().isDoTaxonomicallyIncluded()){
106
			return;
107
		}
108
		Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
109

    
110
		MapWrapper<TeamOrPersonBase> authorStore = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.TEAM_STORE);
111
		authorStore.makeEmpty();
112

    
113
		if(logger.isInfoEnabled()) { logger.info("Start making relationships..."); }
114

    
115
		TransactionStatus txStatus = startTransaction();
116

    
117
		// the uuid of an accepted taxon is needed here. any accepted taxon will do.
118
		TaxonBase taxon = getTaxonService().find(UUID.fromString(acceptedTaxonUUID));
119
		sourceRef = taxon.getSec();
120

    
121
		Classification tree = getClassificationFor(state, sourceRef);
122
		commitTransaction(txStatus);
123

    
124
		logger.warn("Before processParentsChildren" + state.getConfig().isDoTaxonomicallyIncluded());
125

    
126
		//ProfilerController.memorySnapshot();
127

    
128
		if (state.getConfig().isDoTaxonomicallyIncluded())  {
129
			processParentsChildren(state);
130
			
131
		}
132
		if (state.getConfig().isDoAssociatedSpecialists()){
133
			processAssociatedSpecialists(state);
134
		}
135

    
136
		logger.warn("Before processMissappliedNames" + state.getConfig().isDoMisappliedNames());
137

    
138
		//ProfilerController.memorySnapshot();
139

    
140
		if (state.getConfig().isDoMisappliedNames()) {
141
			processMisappliedNames(state);
142
		}
143
		/*
144
		logger.warn("Before heterotypic synonyms");
145
		ProfilerController.memorySnapshot();
146
		*/
147
		if (state.getConfig().isDoHeterotypicSynonyms()) {
148
			if(logger.isInfoEnabled()) {
149
				logger.info("Start making heterotypic synonym relationships...");
150
			}
151
			processHeterotypicSynonyms(state, ALL_SYNONYM_FROM_CLAUSE);
152
		}
153
		/*
154
		logger.warn("End RelTaxon doInvoke");
155
		ProfilerController.memorySnapshot();
156
		*/
157
		logger.info("End making relationships......");
158

    
159
		return;
160
	}
161

    
162
	/** Retrieve child-parent uuid map from CDM DB */
163
	private void processParentsChildren(FaunaEuropaeaImportState state) {
164

    
165
		int limit = state.getConfig().getLimitSave();
166

    
167
		TransactionStatus txStatus = null;
168

    
169
		Map<UUID, UUID> childParentMap = null;
170

    
171
		Map<UUID, UUID> taxonSpecialistMap = null;
172
		Map<UUID, UUID> taxonGroupCoordinatorMap = null;
173
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
174
		Source source = fauEuConfig.getSource();
175
		int i = 0;
176

    
177
		String selectCount =
178
			" SELECT count(*) ";
179

    
180
		String selectColumns = " SELECT Taxon.UUID AS ChildUuid, Taxon.tax_usr_idgc as group_coordinator, Taxon.tax_usr_idsp as specialist, Parent.UUID AS ParentUuid, Parent.tax_usr_idgc as parent_group_coordinator, Parent.tax_usr_idsp as parent_specialist";
181

    
182
		String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +
183
		" ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
184
		" WHERE (Taxon.TAX_VALID <> 0) AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL )";
185

    
186
		String orderClause = " ORDER BY Taxon.TAX_RNK_ID ASC";
187

    
188
		String countQuery =
189
			selectCount + fromClause;
190

    
191
		String selectQuery =
192
			selectColumns + fromClause + orderClause;
193

    
194
		if(logger.isInfoEnabled()) { logger.info("Start making taxonomically included relationships..."); }
195

    
196
		try {
197

    
198
			ResultSet rs = source.getResultSet(countQuery);
199
			rs.next();
200
			int count = rs.getInt(1);
201

    
202
			rs = source.getResultSet(selectQuery);
203

    
204
	        if (logger.isInfoEnabled()) {
205
				logger.info("Number of rows: " + count);
206
				logger.info("Count Query: " + countQuery);
207
				logger.info("Select Query: " + selectQuery);
208
			}
209

    
210
	        while (rs.next()) {
211

    
212
				if ((i++ % limit) == 0) {
213

    
214
					txStatus = startTransaction();
215
					childParentMap = new HashMap<UUID, UUID>(limit);
216
					taxonSpecialistMap = new HashMap<UUID,UUID>(limit);
217
					taxonGroupCoordinatorMap = new HashMap<UUID, UUID>(limit);
218
					if(logger.isInfoEnabled()) {
219
						logger.info("Taxonomically included retrieved: " + (i-1));
220
					}
221
				}
222

    
223
				String childUuidStr = rs.getString("ChildUuid");
224
				String parentUuidStr = rs.getString("ParentUuid");
225

    
226
				int group_coordinator_id = rs.getInt("group_coordinator");
227
				int specialist_id = rs.getInt("specialist");
228
				int parent_group_coordinator_id = rs.getInt("parent_group_coordinator");
229
                int parent_specialist_id = rs.getInt("parent_specialist");
230
				UUID childUuid = UUID.fromString(childUuidStr);
231
				UUID parentUuid = UUID.fromString(parentUuidStr);
232

    
233
				if (!childParentMap.containsKey(childUuid)) {
234

    
235
						childParentMap.put(childUuid, parentUuid);
236

    
237

    
238
				} else {
239
					if(logger.isDebugEnabled()) {
240
						logger.debug("Duplicated child UUID (" + childUuid + ")");
241
					}
242
				}
243
				if (!taxonSpecialistMap.containsKey(childUuid)) {
244

    
245
				    taxonSpecialistMap.put(childUuid, state.getAgentMap().get(specialist_id));
246
				}
247
				if (!taxonSpecialistMap.containsKey(parentUuid)) {
248

    
249
                    taxonSpecialistMap.put(parentUuid, state.getAgentMap().get(parent_specialist_id));
250
                }
251
				if (taxonGroupCoordinatorMap.containsKey(childUuid)) {
252

    
253
				    taxonGroupCoordinatorMap.put(childUuid, state.getAgentMap().get(group_coordinator_id));
254
                }
255
                if (!taxonGroupCoordinatorMap.containsKey(parentUuid)) {
256

    
257
                    taxonGroupCoordinatorMap.put(parentUuid, state.getAgentMap().get(parent_group_coordinator_id));
258
                }
259

    
260

    
261
				if (((i % limit) == 0 && i != 1 ) || i == count ) {
262

    
263
					createAndCommitParentChildRelationships(
264
							state, txStatus, childParentMap, taxonGroupCoordinatorMap, taxonSpecialistMap);
265
					childParentMap = null;
266

    
267
					if(logger.isInfoEnabled()) {
268
						logger.info("i = " + i + " - Transaction committed");
269
					}
270
				}
271
			}
272
	        rs = null;
273
	        if (childParentMap != null){
274
	        	logger.info("processParentsChildren... last commit");
275
	        	createAndCommitParentChildRelationships(
276
						state, txStatus, childParentMap,taxonSpecialistMap, taxonGroupCoordinatorMap);
277
	        	childParentMap = null;
278
	        }
279

    
280
		} catch (SQLException e) {
281
			logger.error("SQLException:" +  e);
282
			state.setUnsuccessfull();
283
		}
284
		childParentMap = null;
285
		return;
286
	}
287

    
288
	private Map<UUID, UUID> createAndCommitParentChildRelationships(
289
			FaunaEuropaeaImportState state, TransactionStatus txStatus,
290
			Map<UUID, UUID> childParentMap, Map<UUID, UUID> taxonSpecialistMap, Map<UUID, UUID> taxonGroupCoordinatorMap) {
291
		createParentChildRelationships(state, childParentMap, taxonSpecialistMap, taxonGroupCoordinatorMap);
292

    
293

    
294
		commitTransaction(txStatus);
295
		return childParentMap;
296
	}
297

    
298

    
299
	/** Retrieve misapplied name / accepted taxon uuid map from CDM DB */
300
	private void processMisappliedNames(FaunaEuropaeaImportState state) {
301

    
302
		int limit = state.getConfig().getLimitSave();
303

    
304
		TransactionStatus txStatus = null;
305

    
306
		Map<UUID, UUID> childParentMap = null;
307
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
308
		Source source = fauEuConfig.getSource();
309
		int i = 0;
310

    
311
		String selectCount =
312
			" SELECT count(*) ";
313

    
314
		String selectColumns = " SELECT Taxon.UUID AS MisappliedUuid, Parent.UUID AS AcceptedUuid ";
315

    
316
		String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +
317
		" ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +
318
		" WHERE (Taxon.TAX_VALID = 0) AND (Taxon.TAX_AUT_ID = " + A_AUCT + ")";
319
		String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC ";
320

    
321

    
322

    
323

    
324
		String countQuery =
325
			selectCount + fromClause;
326

    
327
		String selectQuery =
328
			selectColumns + fromClause + orderClause;
329

    
330
		if(logger.isInfoEnabled()) { logger.info("Start making misapplied name relationships..."); }
331

    
332
		try {
333

    
334
			ResultSet rs = source.getResultSet(countQuery);
335
			rs.next();
336
			int count = rs.getInt(1);
337

    
338
			rs = source.getResultSet(selectQuery);
339

    
340
	        if (logger.isInfoEnabled()) {
341
				logger.info("Number of rows: " + count);
342
				logger.info("Count Query: " + countQuery);
343
				logger.info("Select Query: " + selectQuery);
344
			}
345

    
346
			while (rs.next()) {
347

    
348
				if ((i++ % limit) == 0) {
349

    
350
					txStatus = startTransaction();
351
					childParentMap = new HashMap<UUID, UUID>(limit);
352

    
353
					if(logger.isInfoEnabled()) {
354
						logger.info("Misapplied names retrieved: " + (i-1) );
355
					}
356
				}
357

    
358
				String childUuidStr = rs.getString("MisappliedUuid");
359
				String parentUuidStr = rs.getString("AcceptedUuid");
360
				UUID childUuid = UUID.fromString(childUuidStr);
361
				UUID parentUuid = UUID.fromString(parentUuidStr);
362

    
363
				if (!childParentMap.containsKey(childUuid)) {
364

    
365
						childParentMap.put(childUuid, parentUuid);
366

    
367
				} else {
368
					if(logger.isDebugEnabled()) {
369
						logger.debug("Duplicated child UUID (" + childUuid + ")");
370
					}
371
				}
372

    
373
				if (((i % limit) == 0 && i != 1 ) || i == count) {
374

    
375
					createAndCommitMisappliedNameRelationships(state, txStatus,
376
							childParentMap);
377
					childParentMap = null;
378

    
379

    
380
					if(logger.isInfoEnabled()) {
381
						logger.info("i = " + i + " - Transaction committed");
382
					}
383
				}
384
			}
385
			if (childParentMap != null){
386
				logger.info("processMisappliedNames... last commit");
387
				createAndCommitMisappliedNameRelationships(state, txStatus,
388
						childParentMap);
389
				childParentMap = null;
390
			}
391

    
392
		} catch (SQLException e) {
393
			logger.error("SQLException:" +  e);
394
			state.setUnsuccessfull();
395
		}
396
		return;
397
	}
398

    
399
	private void createAndCommitMisappliedNameRelationships(
400
			FaunaEuropaeaImportState state, TransactionStatus txStatus,
401
			Map<UUID, UUID> childParentMap) {
402
		createMisappliedNameRelationships(state, childParentMap);
403
		commitTransaction(txStatus);
404
	}
405

    
406

    
407

    
408
	/** Retrieve synonyms from FauEuDB DB */
409
	private void processHeterotypicSynonyms(FaunaEuropaeaImportState state, String fromClause) {
410

    
411
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
412
		Source source = fauEuConfig.getSource();
413

    
414
		String selectCount =
415
			" SELECT count(*) ";
416

    
417
		String selectColumns = " SELECT Taxon.UUID AS SynonymUuid, Parent.UUID AS AcceptedUuid ";
418

    
419
		String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC ";
420

    
421
		String countQuery =
422
			selectCount + fromClause;
423

    
424
		String selectQuery =
425
			selectColumns + fromClause + orderClause;
426
		logger.debug(selectQuery);
427

    
428
		try {
429

    
430
			ResultSet rs = source.getResultSet(countQuery);
431
			rs.next();
432
			int count = rs.getInt(1);
433

    
434
			rs = source.getResultSet(selectQuery);
435

    
436
	        if (logger.isInfoEnabled()) {
437
				logger.info("Number of rows: " + count);
438
				logger.info("Count Query: " + countQuery);
439
				logger.info("Select Query: " + selectQuery);
440
			}
441

    
442
	        storeSynonymRelationships(rs, count, state);
443
	        rs = null;
444
		} catch (SQLException e) {
445
			logger.error("SQLException:" +  e);
446
			state.setUnsuccessfull();
447
		}
448
		return;
449
	}
450

    
451

    
452

    
453

    
454
	private void storeSynonymRelationships(ResultSet rs, int count, FaunaEuropaeaImportState state)
455
	throws SQLException {
456

    
457
		TransactionStatus txStatus = null;
458
		Map<UUID, UUID> synonymAcceptedMap = null;
459
		int i = 0;
460
		int limit = state.getConfig().getLimitSave();
461

    
462
		while (rs.next()) {
463

    
464
			if ((i++ % limit) == 0) {
465

    
466
				txStatus = startTransaction();
467
				synonymAcceptedMap = new HashMap<UUID, UUID>(limit);
468

    
469
				if(logger.isInfoEnabled()) {
470
					logger.info("Synonyms retrieved: " + (i-1));
471
				}
472
			}
473

    
474
			String synonymUuidStr = rs.getString("SynonymUuid");
475
			String acceptedUuidStr = rs.getString("AcceptedUuid");
476
			UUID synonymUuid = UUID.fromString(synonymUuidStr);
477
			UUID acceptedUuid = UUID.fromString(acceptedUuidStr);
478

    
479
			if (!synonymAcceptedMap.containsKey(synonymUuid)) {
480

    
481
				synonymAcceptedMap.put(synonymUuid, acceptedUuid);
482

    
483
			} else {
484
				if(logger.isDebugEnabled()) {
485
					logger.debug("Duplicated synonym UUID (" + synonymUuid + ")");
486
				}
487
			}
488

    
489
			if (((i % limit) == 0 && i != 1 ) || i == count ) {
490

    
491
				synonymAcceptedMap = createAndCommitHeterotypicSynonyms(state,
492
						txStatus, synonymAcceptedMap);
493

    
494
				if(logger.isInfoEnabled()) {
495
					logger.info("i = " + i + " - Transaction committed");
496
				}
497
			}
498
		}
499
		if (synonymAcceptedMap != null){
500
			logger.info("processHeterotypicSynonyms... last commit");
501
			synonymAcceptedMap = createAndCommitHeterotypicSynonyms(state,
502
					txStatus, synonymAcceptedMap);
503
		}
504
		return;
505
	}
506

    
507
	private Map<UUID, UUID> createAndCommitHeterotypicSynonyms(
508
			FaunaEuropaeaImportState state, TransactionStatus txStatus,
509
			Map<UUID, UUID> synonymAcceptedMap) {
510
		createHeterotypicSynonyms(state, synonymAcceptedMap);
511

    
512
		synonymAcceptedMap = null;
513
		commitTransaction(txStatus);
514
		return synonymAcceptedMap;
515
	}
516

    
517

    
518

    
519

    
520

    
521
	/* Creates parent-child relationships.
522
	 * Parent-child pairs are retrieved in blocks via findByUUID(Set<UUID>) from CDM DB.
523
	 */
524
	private void createParentChildRelationships(FaunaEuropaeaImportState state, Map<UUID, UUID> childParentMap, Map<UUID, UUID> taxonSpecialistMap, Map<UUID, UUID> taxonGroupCoordinatorMap) {
525
		//gets the taxon "Hydroscaphidae"(family)
526
		TaxonBase taxon = getTaxonService().find(UUID.fromString(acceptedTaxonUUID));
527
		sourceRef = taxon.getSec();
528
		int limit = state.getConfig().getLimitSave();
529

    
530
		Classification tree = getClassificationFor(state, sourceRef);
531

    
532
		Set<TaxonBase> childSet = new HashSet<TaxonBase>(limit);
533

    
534
		Set<UUID> childKeysSet = childParentMap.keySet();
535
		Set<UUID> parentValuesSet = new HashSet<UUID>(childParentMap.values());
536

    
537
		if (logger.isTraceEnabled()) {
538
			logger.trace("Start reading children and parents");
539
		}
540
		List<TaxonBase> children = getTaxonService().find(childKeysSet);
541
		List<TaxonBase> parents = getTaxonService().find(parentValuesSet);
542
		Map<UUID, TaxonBase> parentsMap = new HashMap<UUID, TaxonBase>(parents.size());
543

    
544
		for (TaxonBase taxonBase : parents){
545
			parentsMap.put(taxonBase.getUuid(), taxonBase);
546
		}
547

    
548
		if (logger.isTraceEnabled()) {
549
			logger.debug("End reading children and parents");
550
			for (UUID uuid : childKeysSet) {
551
				logger.trace("child uuid query: " + uuid);
552
			}
553
			for (UUID uuid : parentValuesSet) {
554
				logger.trace("parent uuid query: " + uuid);
555
			}
556
			for (TaxonBase tb : children) {
557
				logger.trace("child uuid result: " + tb.getUuid());
558
			}
559
			for (TaxonBase tb : parents) {
560
				logger.trace("parent uuid result: " + tb.getUuid());
561
			}
562
		}
563

    
564
		UUID mappedParentUuid = null;
565
		UUID childUuid = null;
566
		TaxonNode childNode;
567
		TeamOrPersonBase taxonomicSpecialist = null;
568
		TeamOrPersonBase groupCoordinator = null;
569
		UUID agentUuid = null;
570
		for (TaxonBase child : children) {
571

    
572
			try {
573
				Taxon childTaxon = child.deproxy(child, Taxon.class);
574
				childUuid = childTaxon.getUuid();
575
				mappedParentUuid = childParentMap.get(childUuid);
576
				TaxonBase parent = null;
577

    
578
				TaxonBase potentialParent = parentsMap.get(mappedParentUuid);
579
//					for (TaxonBase potentialParent : parents ) {
580
//						parentUuid = potentialParent.getUuid();
581
//						if(parentUuid.equals(mappedParentUuid)) {
582
						parent = potentialParent;
583
						if (logger.isDebugEnabled()) {
584
							logger.debug("Parent (" + mappedParentUuid + ") found for child (" + childUuid + ")");
585
						}
586
//							break;
587
//						}
588
//					}
589

    
590
				Taxon parentTaxon = parent.deproxy(parent, Taxon.class);
591

    
592
				if (childTaxon != null && parentTaxon != null) {
593

    
594
					childNode = tree.addParentChild(parentTaxon, childTaxon, sourceRef, null);
595
					agentUuid = taxonSpecialistMap.get(childTaxon.getUuid());
596
					taxonomicSpecialist = null;
597
					if (agentUuid != null){
598
					    taxonomicSpecialist = (TeamOrPersonBase) getAgentService().find(agentUuid);
599

    
600
    					if (taxonomicSpecialist != null){
601
    					    childNode.addAgentRelation(FaunaEuropaeaTransformer.getTaxonomicSpecialistType(getTermService()),  taxonomicSpecialist);
602
    					}
603
    				}
604

    
605
					agentUuid = taxonGroupCoordinatorMap.get(childTaxon.getUuid());
606
					groupCoordinator = (TeamOrPersonBase) getAgentService().find(agentUuid);
607
					if (groupCoordinator != null){
608
                        childNode.addAgentRelation(FaunaEuropaeaTransformer.getGroupCoordinatorType(getTermService()),  groupCoordinator);
609
                    }
610

    
611
					//TODO: add the specialist and the group coordinator to the node!!
612
					if (logger.isDebugEnabled()) {
613
						logger.debug("Parent-child (" + mappedParentUuid + "-" + childUuid +
614
						") relationship created");
615
					}
616
					if (childTaxon != null && !childSet.contains(childTaxon)) {
617

    
618
						childSet.add(childTaxon);
619

    
620
						if (logger.isTraceEnabled()) {
621
							logger.trace("Child taxon (" + childUuid + ") added to Set");
622
						}
623

    
624
					} else {
625
						if (logger.isDebugEnabled()) {
626
							logger.debug("Duplicated child taxon (" + childUuid + ")");
627
						}
628
					}
629
				} else {
630
					if (logger.isDebugEnabled()) {
631
						logger.debug("Parent(" + mappedParentUuid + ") or child (" + childUuid + " is null");
632
					}
633
				}
634

    
635
			} catch (Exception e) {
636
				logger.error("Error creating taxonomically included relationship parent-child (" +
637
					mappedParentUuid + " <----> " + childUuid + ")", e);
638
			}
639

    
640
		}
641
		if (logger.isTraceEnabled()) {
642
			logger.trace("Start saving childSet");
643
		}
644
		getTaxonService().save(childSet);
645
		if (logger.isTraceEnabled()) {
646
			logger.trace("End saving childSet");
647
		}
648

    
649
		parentValuesSet = null;
650
		childSet = null;
651
		children = null;
652
		parents = null;
653
		tree = null;
654

    
655
		return;
656
	}
657

    
658
	/* Creates misapplied name relationships.
659
	 * Misapplied name-accepted taxon pairs are retrieved in blocks via findByUUID(Set<UUID>) from CDM DB.
660
	 */
661
	private void createMisappliedNameRelationships(FaunaEuropaeaImportState state, Map<UUID, UUID> fromToMap) {
662

    
663
		//gets the taxon "Hydroscaphidae" (family)
664

    
665
		TaxonBase taxon = getTaxonService().find(UUID.fromString(acceptedTaxonUUID));
666
		sourceRef = taxon.getSec();
667
		int limit = state.getConfig().getLimitSave();
668

    
669
			Set<TaxonBase> misappliedNameSet = new HashSet<TaxonBase>(limit);
670

    
671
			Set<UUID> misappliedNamesSet = fromToMap.keySet();
672
			Set<UUID> acceptedTaxaSet = new HashSet<UUID>(fromToMap.values());
673

    
674
			if (logger.isTraceEnabled()) {
675
				logger.trace("Start reading misapplied names and accepted taxa");
676
			}
677
			List<TaxonBase> misappliedNames = getTaxonService().find(misappliedNamesSet);
678
			List<TaxonBase> acceptedTaxa = getTaxonService().find(acceptedTaxaSet);
679
			Map<UUID, TaxonBase> acceptedTaxaMap = new HashMap<UUID, TaxonBase>(acceptedTaxa.size());
680
			for (TaxonBase taxonBase : acceptedTaxa){
681
				acceptedTaxaMap.put(taxonBase.getUuid(), taxonBase);
682
			}
683

    
684
			if (logger.isTraceEnabled()) {
685
				logger.info("End reading misapplied names and accepted taxa");
686
				for (UUID uuid : misappliedNamesSet) {
687
					logger.trace("misapplied name uuid query: " + uuid);
688
				}
689
				for (UUID uuid : acceptedTaxaSet) {
690
					logger.trace("accepted taxon uuid query: " + uuid);
691
				}
692
				for (TaxonBase tb : misappliedNames) {
693
					logger.trace("misapplied name uuid result: " + tb.getUuid());
694
				}
695
				for (TaxonBase tb : acceptedTaxa) {
696
					logger.trace("accepted taxon uuid result: " + tb.getUuid());
697
				}
698
			}
699

    
700
			UUID mappedAcceptedTaxonUuid = null;
701
			UUID misappliedNameUuid = null;
702
			Taxon misappliedNameTaxon = null;
703
			TaxonBase acceptedTaxonBase = null;
704
			Taxon acceptedTaxon = null;
705

    
706
			for (TaxonBase misappliedName : misappliedNames) {
707

    
708
				try {
709
					misappliedNameTaxon = misappliedName.deproxy(misappliedName, Taxon.class);
710
					misappliedNameUuid = misappliedNameTaxon.getUuid();
711
					mappedAcceptedTaxonUuid = fromToMap.get(misappliedNameUuid);
712
					acceptedTaxonBase = null;
713

    
714
					acceptedTaxonBase = acceptedTaxaMap.get(mappedAcceptedTaxonUuid);
715
							if (logger.isDebugEnabled()) {
716
								logger.debug("Parent (" + mappedAcceptedTaxonUuid + ") found for child (" + misappliedNameUuid + ")");
717
							}
718

    
719
							acceptedTaxon = acceptedTaxonBase.deproxy(acceptedTaxonBase, Taxon.class);
720

    
721
					if (misappliedNameTaxon != null && acceptedTaxon != null) {
722

    
723
						acceptedTaxon.addMisappliedName(misappliedNameTaxon, sourceRef, null);
724

    
725
						if (logger.isDebugEnabled()) {
726
							logger.debug("Accepted taxon / misapplied name (" + mappedAcceptedTaxonUuid + "-" + misappliedNameUuid +
727
							") relationship created");
728
						}
729
						if (!misappliedNameSet.contains(misappliedNameTaxon)) {
730

    
731
							misappliedNameSet.add(misappliedNameTaxon);
732

    
733
							if (logger.isTraceEnabled()) {
734
								logger.trace("Misapplied name taxon (" + misappliedNameUuid + ") added to Set");
735
							}
736

    
737
						} else {
738
							if (logger.isDebugEnabled()) {
739
								logger.debug("Duplicated misapplied name taxon (" + misappliedNameUuid + ")");
740
							}
741
						}
742
					} else {
743
						if (logger.isDebugEnabled()) {
744
							logger.debug("Accepted taxon (" + mappedAcceptedTaxonUuid + ") or misapplied name (" + misappliedNameUuid + " is null");
745
						}
746
					}
747

    
748
					if (misappliedNameTaxon != null && !misappliedNameSet.contains(misappliedNameTaxon)) {
749
						misappliedNameSet.add(misappliedNameTaxon);
750
						if (logger.isTraceEnabled()) {
751
							logger.trace("Misapplied name taxon (" + misappliedNameUuid + ") added to Set");
752
						}
753
					} else {
754
						if (logger.isDebugEnabled()) {
755
							logger.debug("Duplicated misapplied name taxon (" + misappliedNameUuid + ")");
756
						}
757
					}
758

    
759
				} catch (Exception e) {
760
					logger.error("Error creating misapplied name relationship accepted taxon-misapplied name (" +
761
						mappedAcceptedTaxonUuid + "-" + misappliedNameUuid + ")", e);
762
				}
763

    
764
			}
765
			if (logger.isTraceEnabled()) {
766
				logger.trace("Start saving misappliedNameSet");
767
			}
768
			getTaxonService().save(misappliedNameSet);
769
			if (logger.isTraceEnabled()) {
770
				logger.trace("End saving misappliedNameSet");
771
			}
772

    
773
			acceptedTaxaSet = null;
774
			misappliedNameSet = null;
775
			misappliedNames = null;
776
			acceptedTaxa = null;
777

    
778
		return;
779
	}
780

    
781

    
782
	/* Creates heterotypic synonym relationships.
783
	 * Synonym-accepted taxon pairs are retrieved in blocks via findByUUID(Set<UUID>) from CDM DB.
784
	 */
785
	private void createHeterotypicSynonyms(FaunaEuropaeaImportState state, Map<UUID, UUID> fromToMap) {
786

    
787
		int limit = state.getConfig().getLimitSave();
788

    
789
		Set<TaxonBase> synonymSet = new HashSet<TaxonBase>(limit);
790

    
791
		Set<UUID> synonymUuidSet = fromToMap.keySet();
792
		Set<UUID> acceptedTaxaUuidSet = new HashSet<UUID>(fromToMap.values());
793

    
794
		if (logger.isTraceEnabled()) {
795
			logger.trace("Reading synonym names and accepted taxa...");
796
		}
797
		List<TaxonBase> synonyms = getTaxonService().find(synonymUuidSet);
798
		List<TaxonBase> acceptedTaxa = getTaxonService().find(acceptedTaxaUuidSet);
799
		Map<UUID, TaxonBase> acceptedTaxaMap = new HashMap<UUID, TaxonBase>(acceptedTaxa.size());
800
		for (TaxonBase taxonBase : acceptedTaxa){
801
			acceptedTaxaMap.put(taxonBase.getUuid(), taxonBase);
802
		}
803

    
804
		if (logger.isTraceEnabled()) {
805
			logger.trace("End reading synonyms names and accepted taxa");
806
			for (UUID uuid : synonymUuidSet) {
807
				logger.trace("synonym uuid query: " + uuid);
808
			}
809
			for (UUID uuid : acceptedTaxaUuidSet) {
810
				logger.trace("accepted taxon uuid query: " + uuid);
811
			}
812
			for (TaxonBase tb : synonyms) {
813
				logger.trace("synonym uuid result: " + tb.getUuid());
814
			}
815
			for (TaxonBase tb : acceptedTaxa) {
816
				logger.trace("accepted taxon uuid result: " + tb.getUuid());
817
			}
818
		}
819

    
820
		UUID mappedAcceptedTaxonUuid = null;
821
		UUID synonymUuid = null;
822
		Synonym synonym = null;
823
		TaxonBase acceptedTaxonBase = null;
824
		Taxon acceptedTaxon = null;
825

    
826
		for (TaxonBase synonymTaxonBase : synonyms) {
827

    
828
			try {
829
				//check for misapplied names with nec (in Fauna Europaea they have a synonym relationship to the accepted taxon)
830
				if (synonymTaxonBase instanceof Taxon ){
831
					if (((Taxon)synonymTaxonBase).isMisapplication()){
832
						if (logger.isDebugEnabled()) {
833
							logger.debug("misapplied name with exclusion" +  synonymTaxonBase.getTitleCache());
834
						}
835
					}
836
					continue;
837
				}
838
				synonym = HibernateProxyHelper.deproxy(synonymTaxonBase, Synonym.class);
839
				synonymUuid = synonym.getUuid();
840
				mappedAcceptedTaxonUuid = fromToMap.get(synonymUuid);
841
				acceptedTaxonBase = null;
842

    
843
				acceptedTaxonBase = acceptedTaxaMap.get(mappedAcceptedTaxonUuid);
844
				if (logger.isDebugEnabled()) {
845
					logger.debug("Parent (" + mappedAcceptedTaxonUuid + ") found for child (" + synonymUuid + ")");
846
				}
847
				acceptedTaxon = CdmBase.deproxy(acceptedTaxonBase, Taxon.class);
848

    
849
				if (synonym != null && acceptedTaxon != null) {
850

    
851
					//TODO: in case original genus exists must add synonym to original genus instead of to accepted taxon
852
					acceptedTaxon.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
853

    
854
					if (logger.isDebugEnabled()) {
855
						logger.debug("Accepted taxon - synonym (" + mappedAcceptedTaxonUuid + " - " + synonymUuid +
856
						") relationship created");
857
					}
858
					if (synonym != null && !synonymSet.contains(synonym)) {
859

    
860
						synonymSet.add(synonym);
861

    
862
						if (logger.isTraceEnabled()) {
863
							logger.trace("Synonym (" + synonymUuid + ") added to Set");
864
						}
865

    
866
					} else {
867
						if (logger.isDebugEnabled()) {
868
							logger.debug("Duplicated synonym (" + synonymUuid + ")");
869
						}
870
					}
871
				} else {
872
					if (logger.isDebugEnabled()) {
873
						logger.debug("Accepted taxon (" + mappedAcceptedTaxonUuid + ") or misapplied name (" + synonymUuid + " is null");
874
					}
875
				}
876
			} catch (Exception e) {
877
				logger.error("Error creating synonym relationship: accepted taxon-synonym (" +
878
						mappedAcceptedTaxonUuid + "-" + synonymUuid + ": "+ synonymTaxonBase.getTitleCache() +")", e);
879
			}
880
		}
881
		if (logger.isTraceEnabled()) {
882
			logger.trace("Start saving synonymSet");
883
		}
884
		getTaxonService().save(synonymSet);
885
		if (logger.isTraceEnabled()) {
886
			logger.trace("End saving synonymSet");
887
		}
888

    
889
		acceptedTaxaUuidSet = null;
890
		synonymSet = null;
891
		synonyms = null;
892
		acceptedTaxa = null;
893

    
894
		return;
895
	}
896
	
897
	private void processAssociatedSpecialists(FaunaEuropaeaImportState state){
898
		int limit = state.getConfig().getLimitSave();
899

    
900
		TransactionStatus txStatus = null;
901

    
902
		Map<UUID, UUID> childParentMap = null;
903

    
904
		
905
		TaxonNode taxonNode = null;
906
		TeamOrPersonBase associatedSpecialist = null;
907
		TaxonNodeAgentRelation agentRel = null;
908
		UUID agentUuid = null;
909
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
910
		Source source = fauEuConfig.getSource();
911
		int i = 0;
912

    
913
		String selectCount =
914
			" SELECT count(*) ";
915
		
916
		
917
		
918
		String selectColumns = "SELECT  u.USR_GROUPNAME as groupName, u.USR_GROUPNOTE groupNote, u.USR_USR_ID as user_user_id, "
919
				+ "		u.usr_id user_id, user2.USR_ID as user2_id, taxon.UUID as tax_uuid ";
920
				
921
		String fromClause = " FROM USERROLE as userrole left join USERS u on userrole.URL_USR_ID = u.USR_ID left join USERS user2 on user2.USR_ID = u.USR_USR_ID "
922
						+ " left join TAXON taxon on (taxon.TAX_USR_IDSP= user2.USR_ID or taxon.TAX_USR_IDGC= user2.USR_ID) "
923
						+ " where USERROLE.URL_ROL_ID = 7 order by taxon.TAX_ID";
924
		String orderClause = " ORDER BY taxon.TAX_ID";
925

    
926
		String countQuery =
927
			selectCount + fromClause;
928

    
929
		String selectQuery =
930
			selectColumns + fromClause + orderClause;
931
		
932
		if(logger.isInfoEnabled()) { logger.info("Start making associated specialists..."); }
933

    
934
		try {
935

    
936
			ResultSet rs = source.getResultSet(countQuery);
937
			rs.next();
938
			int count = rs.getInt(1);
939

    
940
			rs = source.getResultSet(selectQuery);
941

    
942
	        if (logger.isInfoEnabled()) {
943
				logger.info("Number of rows: " + count);
944
				logger.info("Count Query: " + countQuery);
945
				logger.info("Select Query: " + selectQuery);
946
			}
947

    
948
	        while (rs.next()) {
949
				String taxonUUID = rs.getString("taxUuid");
950
				String oldTaxonUuidString = null;
951
				int userId = rs.getInt("user_id");
952
				int user2Id = rs.getInt("user2_id");
953
				String groupName = rs.getString("groupName");
954
				Taxon taxon = null;
955
				if (!taxonUUID.equals(oldTaxonUuidString)){
956
					if (taxonNode != null){
957
						getTaxonNodeService().saveOrUpdate(taxonNode);
958
					}
959
					commitTransaction(txStatus);
960
					txStatus = startTransaction();
961
					taxon = HibernateProxyHelper.deproxy(getTaxonService().find(UUID.fromString(taxonUUID)), Taxon.class);
962
					oldTaxonUuidString = taxonUUID;
963
					if (!taxon.getTaxonNodes().isEmpty()){
964
						taxonNode = taxon.getTaxonNodes().iterator().next();
965
					} else {
966
						taxonNode = null;
967
						logger.info("There is an associated specialist for a taxon which has no taxonnode.");
968
					}
969
 				}
970
				
971
				agentUuid = state.getAgentMap().get(userId);
972
				
973
				
974
				associatedSpecialist = (TeamOrPersonBase) getAgentService().find(agentUuid);
975
				if (associatedSpecialist != null && taxonNode != null){
976
					agentRel =taxonNode.addAgentRelation(FaunaEuropaeaTransformer.getAssociateSpecialistType(getTermService()), associatedSpecialist);
977
					if (!StringUtils.isBlank(groupName))
978
					agentRel.addAnnotation(Annotation.NewInstance(groupName, Language.DEFAULT()));
979
				}
980
				
981
				
982
	        }
983
	        getTaxonNodeService().saveOrUpdate(taxonNode);
984
			commitTransaction(txStatus);
985
		}catch(SQLException e){
986
			logger.info("Problems during creating associated specialists.", e);
987
		}
988
	}
989
	        
990

    
991
}
(13-13/20)