Project

General

Profile

« Previous | Next » 

Revision a124222b

Added by Katja Luther over 9 years ago

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiTaxonExport.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
22 22
import java.util.List;
23 23
import java.util.Set;
24 24
import java.util.UUID;
25
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
25 27

  
26 28
import org.apache.commons.lang.StringUtils;
27 29
import org.apache.log4j.Logger;
......
116 118
	private PreparedStatement rankUpdateStmt;
117 119
	private NomenclaturalCode nomenclaturalCode;
118 120
	private Integer kingdomFk;
119
	private HashMap<Rank, Rank> rank2endRankMap = new HashMap<Rank, Rank>();
120
	private List<Rank> rankList = new ArrayList<Rank>();
121
	private final HashMap<Rank, Rank> rank2endRankMap = new HashMap<Rank, Rank>();
122
	private final List<Rank> rankList = new ArrayList<Rank>();
121 123
	private static final UUID uuidTreeIndex = UUID.fromString("28f4e205-1d02-4d3a-8288-775ea8413009");
122 124
	private AnnotationType treeIndexAnnotationType;
123 125
	private static ExtensionType lastActionExtensionType;
......
130 132
	public static NonViralNameDefaultCacheStrategy<?> nonViralNameStrategy = NonViralNameDefaultCacheStrategy.NewInstance();
131 133
	public static NonViralNameDefaultCacheStrategy<?> bacterialNameStrategy = BacterialNameDefaultCacheStrategy.NewInstance();
132 134
	private static int currentTaxonId;
133
	
134
	
135

  
136

  
135 137
	/**
136 138
	 * @return the treeIndexAnnotationType
137 139
	 */
......
174 176
		boolean result = true;
175 177
		return result;
176 178
	}
177
	
178
	
179

  
180

  
179 181
	/* (non-Javadoc)
180 182
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
181 183
	 */
......
185 187
			logger.info("*** Started Making " + pluralString + " ...");
186 188

  
187 189
			initPreparedStatements(state);
188
			
190

  
189 191
			// Stores whether this invoke was successful or not.
190 192
			boolean success = true;
191
	
193

  
192 194
			// PESI: Clear the database table Taxon.
193 195
//			doDelete(state);
194
			
196

  
195 197
			// Get specific mappings: (CDM) Taxon -> (PESI) Taxon
196 198
			PesiExportMapping mapping = getMapping();
197 199
			PesiExportMapping synonymRelMapping = getSynRelMapping();
198
			PesiExportMapping additionalSourceMapping = getAdditionalSourceMapping(state); 
199
			
200
			PesiExportMapping additionalSourceMapping = getAdditionalSourceMapping(state);
201

  
200 202
			// Initialize the db mapper
201 203
			mapping.initialize(state);
202 204
			synonymRelMapping.initialize(state);
203 205
			additionalSourceMapping.initialize(state);
204
			
206

  
205 207
			// Find extensionTypes
206 208
			lastActionExtensionType = (ExtensionType)getTermService().find(PesiTransformer.lastActionUuid);
207 209
			lastActionDateExtensionType = (ExtensionType)getTermService().find(PesiTransformer.lastActionDateUuid);
208 210
			expertNameExtensionType = (ExtensionType)getTermService().find(PesiTransformer.expertNameUuid);
209 211
			speciesExpertNameExtensionType = (ExtensionType)getTermService().find(PesiTransformer.speciesExpertNameUuid);
210 212
			cacheCitationExtensionType = (ExtensionType)getTermService().find(PesiTransformer.cacheCitationUuid);
211
			
213

  
212 214
			//Export Taxa..
213 215
			success &= doPhase01(state, mapping, additionalSourceMapping);
214 216

  
215 217
			//"PHASE 1b: Handle names without taxa ...
216 218
			success &= doNames(state, additionalSourceMapping);
217 219

  
218
			
220

  
219 221
			// 2nd Round: Add ParentTaxonFk to each taxon
220 222
			success &= doPhase02(state);
221
			
223

  
222 224
			//PHASE 3: Add Rank data, KingdomFk, TypeNameFk ...
223 225
			success &= doPhase03(state);
224
			
226

  
225 227
			// 4nd Round: Add TreeIndex to each taxon
226 228
			success &= doPhase04(state);
227
						
228
			
229

  
230

  
229 231
			//"PHASE 4: Creating Inferred Synonyms...
230 232
			success &= doPhase05(state, mapping, synonymRelMapping);
231
			
233

  
232 234
			logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
233 235

  
234 236
			if (!success){
......
242 244
		}
243 245
	}
244 246

  
245
	
247

  
246 248
	private void initPreparedStatements(PesiExportState state) throws SQLException {
247 249
		initTreeIndexStatement(state);
248 250
		initRankExpertsUpdateStmt(state);
249 251
		initRankUpdateStatement(state);
250
		
252

  
251 253
		initParentFkStatement(state);
252 254
	}
253 255

  
254 256
	// Prepare TreeIndex-And-KingdomFk-Statement
255 257
	private void initTreeIndexStatement(PesiExportState state) throws SQLException {
256 258
		Connection connection = state.getConfig().getDestination().getConnection();
257
		String parentTaxonFk_TreeIndex_KingdomFkSql = "UPDATE Taxon SET ParentTaxonFk = ?, TreeIndex = ? WHERE TaxonId = ?"; 
259
		String parentTaxonFk_TreeIndex_KingdomFkSql = "UPDATE Taxon SET ParentTaxonFk = ?, TreeIndex = ? WHERE TaxonId = ?";
258 260
		parentTaxonFk_TreeIndex_KingdomFkStmt = connection.prepareStatement(parentTaxonFk_TreeIndex_KingdomFkSql);
259 261
	}
260 262

  
261 263
	// Prepare TreeIndex-And-KingdomFk-Statement
262 264
	private void initParentFkStatement(PesiExportState state) throws SQLException {
263 265
		Connection connection = state.getConfig().getDestination().getConnection();
264
		String parentTaxonFkSql = "UPDATE Taxon SET ParentTaxonFk = ? WHERE TaxonId = ?"; 
266
		String parentTaxonFkSql = "UPDATE Taxon SET ParentTaxonFk = ? WHERE TaxonId = ?";
265 267
		parentTaxonFkStmt = connection.prepareStatement(parentTaxonFkSql);
266 268
	}
267
	
269

  
268 270
	private void initRankUpdateStatement(PesiExportState state) throws SQLException {
269 271
		Connection connection = state.getConfig().getDestination().getConnection();
270 272
		String rankSql = "UPDATE Taxon SET RankFk = ?, RankCache = ?, KingdomFk = ? WHERE TaxonId = ?";
......
276 278
//				"ExpertFk = ?, SpeciesExpertFk = ? WHERE TaxonId = ?";
277 279
		//TODO handle experts GUIDs
278 280
		Connection connection = state.getConfig().getDestination().getConnection();
279
		
281

  
280 282
		String sql = "UPDATE Taxon SET RankFk = ?, RankCache = ?, TypeNameFk = ?, KingdomFk = ? " +
281 283
				" WHERE TaxonId = ?";
282 284
		rankTypeExpertsUpdateStmt = connection.prepareStatement(sql);
......
290 292
		// Get the limit for objects to save within a single transaction.
291 293
		int limit = state.getConfig().getLimitSave();
292 294

  
293
		
295

  
294 296
		logger.info("PHASE 1: Export Taxa...limit is " + limit);
295 297
		// Start transaction
296 298
		TransactionStatus txStatus = startTransaction(true);
297 299
		logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
298
		
299
		
300
		
300

  
301

  
302

  
301 303
		int partitionCount = 0;
302 304

  
303 305
		logger.info("Taking snapshot at the beginning of phase 1 of taxonExport");
304
		ProfilerController.memorySnapshot();
306
		//ProfilerController.memorySnapshot();
305 307
		while ((list = getNextTaxonPartition(null, limit, partitionCount++, null)) != null   ) {
306
			
308

  
307 309
			logger.debug("Fetched " + list.size() + " " + pluralString + ". Exporting...");
310

  
308 311
			for (TaxonBase<?> taxon : list) {
309 312
				doCount(count++, modCount, pluralString);
310 313
				TaxonNameBase<?,?> taxonName = taxon.getName();
314

  
311 315
				NonViralName<?> nvn = CdmBase.deproxy(taxonName, NonViralName.class);
312
								
316
				System.err.println(nvn.getTitleCache());
313 317
				if (! nvn.isProtectedTitleCache()){
314
					nvn.setTitleCache(null, false);	
318
					nvn.setTitleCache(null, false);
315 319
				}
316 320
				if (! nvn.isProtectedNameCache()){
317
					nvn.setNameCache(null, false);	
321
					nvn.setNameCache(null, false);
318 322
				}
319 323
				if (! nvn.isProtectedFullTitleCache()){
320
					nvn.setFullTitleCache(null, false);	
324
					nvn.setFullTitleCache(null, false);
321 325
				}
322 326
				if (! nvn.isProtectedAuthorshipCache()){
323
					nvn.setAuthorshipCache(null, false);	
327
					nvn.setAuthorshipCache(null, false);
328
				}
329
				try{
330
				if (nvn.getRank().equals(Rank.KINGDOM())){
331
				    String treeIndex = ((Taxon)taxon).getTaxonNodes().iterator().next().treeIndex();
332
				    Integer kingdomId = PesiTransformer.pesiKingdomMap.get(nvn.getGenusOrUninomial());
333
				    state.getTreeIndexKingdomMap().put(treeIndex, kingdomId);
334
				}}catch(NullPointerException e){
335
				    logger.error(nvn.getTitleCache() + "has no Rank!");
336
				    System.err.println(nvn.getTitleCache() + "has no Rank!");
324 337
				}
325
				
326 338
				//core mapping
327 339
				success &= mapping.invoke(taxon);
328 340
				//additional source
329 341
				if (nvn.getNomenclaturalReference() != null || StringUtils.isNotBlank(nvn.getNomenclaturalMicroReference() )){
330 342
					additionalSourceMapping.invoke(taxon);
331 343
				}
332
				
344

  
333 345
				validatePhaseOne(taxon, nvn);
334 346
				taxon = null;
335 347
				nvn = null;
336 348
				taxonName = null;
337 349

  
338
				
339
				
350

  
351

  
340 352
			}
341
			
353

  
342 354

  
343 355
			// Commit transaction
344 356
			commitTransaction(txStatus);
......
346 358
			logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
347 359
			pastCount = count;
348 360
			/*logger.warn("Taking snapshot at the end of the loop of phase 1 of taxonExport");
349
			ProfilerController.memorySnapshot();
361
			//ProfilerController.memorySnapshot();
350 362
			*/
351 363
			// Start transaction
352 364
			txStatus = startTransaction(true);
353 365
			logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
354
			
366

  
355 367
		}
356 368
		if (list == null ) {
357 369
			logger.info("No " + pluralString + " left to fetch.");
358 370
		}
359
		
360
		
361
		
371

  
372

  
373

  
362 374
		// Commit transaction
363 375
		commitTransaction(txStatus);
364 376
		txStatus = null;
......
380 392
		String infraSpecificEpithet = taxonName.getInfraSpecificEpithet();
381 393
		String infraGenericEpithet = taxonName.getInfraGenericEpithet();
382 394
		Integer rank = getRankFk(taxonName, nomenclaturalCode);
383
		
395

  
384 396
		if (rank == null) {
385 397
			logger.error("Rank was not determined: " + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
386 398
		} else {
387
			
399

  
388 400
			// Check whether infraGenericEpithet is set correctly
389 401
			// 1. Childs of an accepted taxon of rank subgenus that are accepted taxa of rank species have to have an infraGenericEpithet
390 402
			// 2. Grandchilds of an accepted taxon of rank subgenus that are accepted taxa of rank subspecies have to have an infraGenericEpithet
391
			
403

  
392 404
			int ancestorLevel = 0;
393 405
			if (taxonName.getRank().equals(Rank.SUBSPECIES())) {
394 406
				// The accepted taxon two rank levels above should be of rank subgenus
......
407 419
					}
408 420
				}
409 421
			}
410
			
422

  
411 423
			if (infraGenericEpithet == null && rank.intValue() == 190) {
412 424
				logger.warn("InfraGenericEpithet was not determined although it should exist for rank 190: " + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
413 425
			}
......
415 427
				logger.warn("SpecificEpithet was determined for rank " + rank + " although it should only exist for ranks higher or equal to 220: TaxonName " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
416 428
			}
417 429
			if (infraSpecificEpithet != null && rank.intValue() < 225) {
418
				String message = "InfraSpecificEpithet '" +infraSpecificEpithet + "' was determined for rank " + rank + " although it should only exist for ranks higher or equal to 230: "  + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")"; 
430
				String message = "InfraSpecificEpithet '" +infraSpecificEpithet + "' was determined for rank " + rank + " although it should only exist for ranks higher or equal to 230: "  + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")";
419 431
				if (StringUtils.isNotBlank(infraSpecificEpithet)){
420 432
					logger.warn(message);
421 433
				}else{
......
431 443
		}
432 444
	}
433 445

  
434
	
435
	
446

  
447

  
436 448
	/**
437 449
	 * 2nd Round: Add ParentTaxonFk to each taxon and add Biota if not exists
438 450
	 * @param state
......
446 458
			logger.info ("Ignore PHASE 2: Make ParentFk and Biota...");
447 459
			return success;
448 460
		}
449
		
461

  
450 462
		List<Taxon> list;
451
		
463

  
452 464
		// Get the limit for objects to save within a single transaction.
453 465
		int limit = state.getConfig().getLimitSave();
454
		
466

  
455 467
		insertBiota(state);
456
		
468

  
457 469
		logger.info("PHASE 2: Make ParentFk and Biota ... limit is " + limit);
458 470
		// Start transaction
459 471
		TransactionStatus txStatus = startTransaction(true);
......
461 473

  
462 474
//		ProfilerController.memorySnapshot();
463 475
		while ((list = getNextTaxonPartition(Taxon.class, limit, partitionCount++, null)) != null   ) {
464
			
476

  
465 477
			logger.info("Fetched " + list.size() + " " + pluralString + ". Exporting...");
466 478
			for (Taxon taxon : list) {
467 479
				for (TaxonNode node : taxon.getTaxonNodes()){
468 480
					doCount(count++, modCount, pluralString);
469 481
					TaxonNode parentNode = node.getParent();
470 482
					if (parentNode != null && parentNode.getTaxon() != null){  //new root node handling requires has root taxon with taxon == null
471
						int childId = state.getDbId( taxon); 
483
						int childId = state.getDbId( taxon);
472 484
						int parentId = state.getDbId(parentNode.getTaxon());
473 485
						success &= invokeParentTaxonFk(parentId, childId);
474 486
					}
475 487
				}
476
				
488

  
477 489
			}
478
			
490

  
479 491
			// Commit transaction
480 492
			commitTransaction(txStatus);
481 493
			logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
......
483 495
			// Start transaction
484 496
			txStatus = startTransaction(true);
485 497
			logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
486
			
498

  
487 499
		}
488 500
		if (list == null ) {
489 501
			logger.info("No " + pluralString + " left to fetch.");
......
491 503
		list = null;
492 504
		// Commit transaction
493 505
		commitTransaction(txStatus);
494
		
506

  
495 507
		return success;
496
		
508

  
497 509
	}
498 510

  
499 511
	/**
......
514 526
		} catch (SQLException e) {
515 527
			logger.warn ("Biota could not be requested or inserted");
516 528
		}
517
		
529

  
518 530
	}
519
	
531

  
520 532
	// 4th round: Add TreeIndex to each taxon
521 533
	private boolean doPhase04(PesiExportState state) {
522 534
		boolean success = true;
523
		
535

  
524 536
		logger.info("PHASE 4: Make TreeIndex ... ");
525
	
526
		//TODO test if possible to move to phase 02 
537

  
538
		//TODO test if possible to move to phase 02
527 539
		String sql = " UPDATE Taxon SET ParentTaxonFk = (Select TaxonId from Taxon where RankFk = 0) " +
528 540
				" WHERE (RankFk = 10) and TaxonStatusFk = 1 ";
529 541
		state.getConfig().getDestination().update(sql);
530
		
542

  
531 543
		state.getConfig().getDestination().update("EXEC dbo.recalculateallstoredpaths");
532
		
544

  
533 545
		return success;
534
		
546

  
535 547
	}
536
	
537
	
548

  
549

  
538 550
	// 2nd Round: Add ParentTaxonFk, TreeIndex to each Taxon
539 551
	private boolean doPhase02_OLD(PesiExportState state) {
540 552
		boolean success = true;
......
542 554
			logger.info ("Ignore PHASE 2: ParentTaxonFk and TreeIndex");
543 555
			return success;
544 556
		}
545
		
557

  
546 558
		List<Classification> classificationList = null;
547 559
		logger.info("PHASE 2: Add ParenTaxonFk and TreeIndex...");
548
		
560

  
549 561
		// Specify starting ranks for tree traversing
550 562
		rankList.add(Rank.KINGDOM());
551 563
		rankList.add(Rank.GENUS());
......
553 565
		// Specify where to stop traversing (value) when starting at a specific Rank (key)
554 566
		rank2endRankMap.put(Rank.GENUS(), null); // Since NULL does not match an existing Rank, traverse all the way down to the leaves
555 567
		rank2endRankMap.put(Rank.KINGDOM(), Rank.GENUS()); // excludes rank genus
556
		
568

  
557 569
		StringBuffer treeIndex = new StringBuffer();
558
		
570

  
559 571
		// Retrieve list of classifications
560 572
		TransactionStatus txStatus = startTransaction(true);
561 573
		logger.info("Started transaction for parentFk and treeIndex. Fetching all classifications...");
......
569 581
		List<TaxonNode> rankSpecificRootNodes;
570 582
		for (Classification classification : classificationList) {
571 583
			for (Rank rank : rankList) {
572
				
584

  
573 585
				txStatus = startTransaction(true);
574 586
				logger.info("Started transaction to fetch all rootNodes specific to Rank " + rank.getLabel() + " ...");
575 587

  
......
629 641
					}else{
630 642
						logger.debug("Taxon is not a PESI taxon: " + newNode.getTaxon().getUuid());
631 643
					}
632
					
644

  
633 645
					newNode = null;
634
					
646

  
635 647
					try {
636 648
						commitTransaction(txStatus);
637 649
						logger.debug("Committed transaction.");
......
643 655
				}
644 656
				rankSpecificRootNodes = null;
645 657
			}
646
			
658

  
647 659
		}
648
		
660

  
649 661
		logger.warn("Taking snapshot at the end of phase 2 of taxonExport");
650
		ProfilerController.memorySnapshot();
662
		//ProfilerController.memorySnapshot();
651 663
		return success;
652
	}	
664
	}
653 665

  
654 666
	//PHASE 3: Add Rank data, KingdomFk, TypeNameFk, expertFk and speciesExpertFk...
655 667
	private boolean doPhase03(PesiExportState state) {
......
666 678
		List<TaxonBase> list;
667 679
		logger.info("PHASE 3: Add Rank data, KingdomFk, TypeNameFk, expertFk and speciesExpertFk...");
668 680
		// Be sure to add rank information, KingdomFk, TypeNameFk, expertFk and speciesExpertFk to every taxonName
669
		
681

  
670 682
		// Start transaction
671 683
		TransactionStatus txStatus = startTransaction(true);
672 684
		logger.info("Started new transaction for rank, kingdom, typeName, expertFk and speciesExpertFK. Fetching some " + pluralString + " (max: " + limit + ") ...");
......
684 696

  
685 697
				doCount(count++, modCount, pluralString);
686 698
				Integer typeNameFk = getTypeNameFk(taxonName, state);
687
				kingdomFk = PesiTransformer.nomenClaturalCode2Kingdom(nomenclaturalCode);
688
				
699
				kingdomFk = findKingdomIdFromTreeIndex(taxon, state);
700
				 //       PesiTransformer.nomenClaturalCode2Kingdom(nomenclaturalCode);
701

  
689 702
				//TODO why are expertFks needed? (Andreas M.)
690 703
//				if (expertFk != null || speciesExpertFk != null) {
691
					invokeRankDataAndTypeNameFkAndKingdomFk(taxonName, nomenclaturalCode, state.getDbId(taxon), 
704
					invokeRankDataAndTypeNameFkAndKingdomFk(taxonName, nomenclaturalCode, state.getDbId(taxon),
692 705
							typeNameFk, kingdomFk, state);
693 706
//				}
694
					
707

  
695 708
					taxon = null;
696 709
					taxonName = null;
697 710
			}
......
709 722
		if (list == null) {
710 723
			logger.info("No " + pluralString + " left to fetch.");
711 724
		}
712
		
725

  
713 726
		list = null;
714
		
727

  
715 728
		// Commit transaction
716 729
		commitTransaction(txStatus);
717
		
730

  
718 731
		logger.debug("Committed transaction.");
719 732
		logger.debug("Try to take snapshot at the end of phase 3 of taxonExport, number of partitions: " + partitionCount);
720
		ProfilerController.memorySnapshot();
733
		//ProfilerController.memorySnapshot();
721 734
		return success;
722 735
	}
723
	
724
	//	"PHASE 5: Creating Inferred Synonyms..."
736

  
737
	/**
738
     * @param taxon
739
     * @return
740
     */
741
    private Integer findKingdomIdFromTreeIndex(TaxonBase<?> taxon,PesiExportState state) {
742
        if (taxon instanceof Taxon){
743
            Set<TaxonNode> nodes = ((Taxon)taxon).getTaxonNodes();
744
            if (!nodes.isEmpty()){
745
                String treeIndex = nodes.iterator().next().treeIndex();
746

  
747
                Pattern pattern = Pattern.compile("#t[0-9]+#[0-9]+#[0-9]+#");
748
                Matcher matcher = pattern.matcher(treeIndex);
749
                Integer kingdomID = null;
750
                if(matcher.find()) {
751
                    String treeIndexKingdom = matcher.group(0);
752
                    kingdomID = state.getTreeIndexKingdomMap().get(treeIndexKingdom);
753
                }
754
                return kingdomID;
755
            } else {
756
                logger.error("The taxon has no nodes: " + taxon.getTitleCache() + " the kingdom is taken from the nomenclatural code: " + PesiTransformer.nomenClaturalCode2Kingdom(nomenclaturalCode));
757
                return PesiTransformer.nomenClaturalCode2Kingdom(nomenclaturalCode);
758
        }} else{
759
           return PesiTransformer.nomenClaturalCode2Kingdom(nomenclaturalCode);
760
        }
761
    }
762

  
763
    //	"PHASE 5: Creating Inferred Synonyms..."
725 764
	private boolean doPhase05(PesiExportState state, PesiExportMapping mapping, PesiExportMapping synRelMapping) throws SQLException {
726 765
		int count;
727 766
		int pastCount;
......
731 770
			logger.info ("Ignore PHASE 5: Creating Inferred Synonyms...");
732 771
			return success;
733 772
		}
734
		
773

  
735 774
		int limit = state.getConfig().getLimitSave();
736 775
		// Create inferred synonyms for accepted taxa
737 776
		logger.info("PHASE 4: Creating Inferred Synonyms...");
......
742 781

  
743 782
		count = 0;
744 783
		pastCount = 0;
745
		int pageSize = limit;
784
		int pageSize = limit/10;
746 785
		int pageNumber = 1;
747 786
		String inferredSynonymPluralString = "Inferred Synonyms";
748
		
787

  
749 788
		// Start transaction
750 789
		TransactionStatus txStatus = startTransaction(true);
751 790
		logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
752 791
		List<TaxonBase> taxonList = null;
753
		
754
		
755
		
792

  
793

  
794

  
756 795
		while ((taxonList  = getTaxonService().listTaxaByName(Taxon.class, "*", "*", "*", "*", Rank.SPECIES(), pageSize, pageNumber)).size() > 0) {
757 796
			HashMap<Integer, TaxonNameBase<?,?>> inferredSynonymsDataToBeSaved = new HashMap<Integer, TaxonNameBase<?,?>>();
758 797

  
759 798
			logger.info("Fetched " + taxonList.size() + " " + parentPluralString + ". Exporting...");
760 799
			inferredSynonymsDataToBeSaved.putAll(createInferredSynonymsForTaxonList(state, mapping,
761 800
					synRelMapping, taxonList));
762
			
801

  
763 802
			doCount(count += taxonList.size(), modCount, inferredSynonymPluralString);
764 803
			// Commit transaction
765 804
			commitTransaction(txStatus);
766 805
			logger.debug("Committed transaction.");
767 806
			logger.info("Exported " + (taxonList.size()) + " " + inferredSynonymPluralString + ". Total: " + count);
768 807
			//pastCount = count;
769
			
808

  
770 809
			// Save Rank Data and KingdomFk for inferred synonyms
771 810
			for (Integer taxonFk : inferredSynonymsDataToBeSaved.keySet()) {
772 811
				invokeRankDataAndKingdomFk(inferredSynonymsDataToBeSaved.get(taxonFk), nomenclaturalCode, taxonFk, kingdomFk, state);
......
775 814
			// Start transaction
776 815
			txStatus = startTransaction(true);
777 816
			logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
778
			
817

  
779 818
			// Increment pageNumber
780 819
			pageNumber++;
781 820
		}
......
793 832
			logger.debug("Committed transaction.");
794 833
			logger.info("Exported " + taxonList.size()+ " " + inferredSynonymPluralString + ". Total: " + count);
795 834
			//pastCount = count;
796
			
835

  
797 836
			// Save Rank Data and KingdomFk for inferred synonyms
798 837
			for (Integer taxonFk : inferredSynonymsDataToBeSaved.keySet()) {
799 838
				invokeRankDataAndKingdomFk(inferredSynonymsDataToBeSaved.get(taxonFk), nomenclaturalCode, taxonFk, kingdomFk, state);
......
802 841
			// Start transaction
803 842
			txStatus = startTransaction(true);
804 843
			logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
805
			
844

  
806 845
			// Increment pageNumber
807 846
			pageNumber++;
808 847
		}
809 848
		if (taxonList.size() == 0) {
810 849
			logger.info("No " + parentPluralString + " left to fetch.");
811 850
		}
812
		
851

  
813 852
		taxonList = null;
814 853
//		logger.warn("Taking snapshot at the end of phase 4 of taxonExport");
815 854
//		ProfilerController.memorySnapshot();
816
		
855

  
817 856
		// Commit transaction
818 857
		commitTransaction(txStatus);
819 858
		System.gc();
820 859
		logger.debug("Taking snapshot at the end of phase 4 after gc() of taxonExport");
821
		ProfilerController.memorySnapshot();
860
		//ProfilerController.memorySnapshot();
822 861
		logger.debug("Committed transaction.");
823 862
		return success;
824 863
	}
......
834 873
	 */
835 874
	private HashMap<Integer, TaxonNameBase<?, ?>> createInferredSynonymsForTaxonList(PesiExportState state,
836 875
			PesiExportMapping mapping, PesiExportMapping synRelMapping,	 List<TaxonBase> taxonList) {
837
		
876

  
838 877
		Taxon acceptedTaxon;
839 878
		Classification classification = null;
840 879
		List<Synonym> inferredSynonyms = null;
841 880
		boolean localSuccess = true;
842
		
881

  
843 882
		HashMap<Integer, TaxonNameBase<?,?>> inferredSynonymsDataToBeSaved = new HashMap<Integer, TaxonNameBase<?,?>>();
844
		
883

  
845 884
		for (TaxonBase<?> taxonBase : taxonList) {
846
		
885

  
847 886
			if (taxonBase.isInstanceOf(Taxon.class)) { // this should always be the case since we should have fetched accepted taxon only, but you never know...
848 887
				acceptedTaxon = CdmBase.deproxy(taxonBase, Taxon.class);
849 888
				TaxonNameBase<?,?> taxonName = acceptedTaxon.getName();
850
				
889

  
851 890
				if (taxonName.isInstanceOf(ZoologicalName.class)) {
852 891
					nomenclaturalCode  = taxonName.getNomenclaturalCode();
853 892
					kingdomFk = PesiTransformer.nomenClaturalCode2Kingdom(nomenclaturalCode);
854 893

  
855 894
					Set<TaxonNode> taxonNodes = acceptedTaxon.getTaxonNodes();
856 895
					TaxonNode singleNode = null;
857
					
896

  
858 897
					if (taxonNodes.size() > 0) {
859 898
						// Determine the classification of the current TaxonNode
860
						
899

  
861 900
						singleNode = taxonNodes.iterator().next();
862 901
						if (singleNode != null) {
863 902
							classification = singleNode.getClassification();
......
869 908
						// The stored classification from another TaxonNode is used. It's a simple, but not a failsafe fallback solution.
870 909
						if (taxonNodes.size() == 0) {
871 910
							//logger.error("Classification could not be determined directly from this Taxon: " + acceptedTaxon.getUuid() + " is misapplication? "+acceptedTaxon.isMisapplication()+ "). The classification of the last taxon is used");
872
						
911

  
873 912
						}
874 913
					}
875
					
914

  
876 915
					if (classification != null) {
877 916
						try{
878 917
							TaxonNameBase name = acceptedTaxon.getName();
......
887 926
									synonym.addMarker(Marker.NewInstance(markerType, true));
888 927
									// Both Synonym and its TaxonName have no valid Id yet
889 928
									synonym.setId(currentTaxonId++);
890
									
891
									
929

  
930

  
892 931
									localSuccess &= mapping.invoke(synonym);
893 932
									//get SynonymRelationship and export
894 933
									if (synonym.getSynonymRelations().isEmpty() ){
895
										SynonymRelationship synRel;					
934
										SynonymRelationship synRel;
896 935
										IdentifiableSource source = synonym.getSources().iterator().next();
897 936
										if (source.getIdNamespace().contains("Potential combination")){
898 937
											synRel = acceptedTaxon.addSynonym(synonym, SynonymRelationshipType.POTENTIAL_COMBINATION_OF());
......
907 946
											synRel = acceptedTaxon.addSynonym(synonym, SynonymRelationshipType.INFERRED_SYNONYM_OF());
908 947
											logger.error(synonym.getTitleCache() + " has no synonym relationship to " + acceptedTaxon.getTitleCache() + " type is set to inferred synonym");
909 948
										}
910
										
949

  
911 950
										localSuccess &= synRelMapping.invoke(synRel);
912 951
										if (!localSuccess) {
913 952
											logger.error("Synonym relationship export failed " + synonym.getTitleCache() + " accepted taxon: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache()+")");
......
924 963
											synRel = null;
925 964
										}
926 965
									}
927
									
966

  
928 967
									inferredSynonymsDataToBeSaved.put(synonym.getId(), synonym.getName());
929 968
								}
930 969
							}
......
945 984
		taxonList = null;
946 985
		return inferredSynonymsDataToBeSaved;
947 986
	}
948
	
987

  
949 988

  
950 989
	/**
951 990
	 * Handles names that do not appear in taxa
......
954 993
	 * @return
955 994
	 */
956 995
	private boolean doNames(PesiExportState state, PesiExportMapping additionalSourceMapping)  throws SQLException {
957
		
996

  
958 997
		boolean success = true;
959 998
		if (! state.getConfig().isDoPureNames()){
960 999
			logger.info ("Ignore PHASE 1b: PureNames");
961 1000
			return success;
962 1001
		}
963
		
1002

  
964 1003
		try {
965 1004
			PesiExportMapping mapping = getPureNameMapping(state);
966 1005
			mapping.initialize(state);
......
971 1010
			// Get the limit for objects to save within a single transaction.
972 1011
			int limit = state.getConfig().getLimitSave();
973 1012

  
974
			
1013

  
975 1014
			logger.info("PHASE 1b: Export Pure Names ...");
976 1015
			// Start transaction
977 1016
			TransactionStatus txStatus = startTransaction(true);
978 1017
			logger.info("Started new transaction for Pure Names. Fetching some " + pluralString + " (max: " + limit + ") ...");
979
			
1018

  
980 1019
			int partitionCount = 0;
981 1020
			while ((list = getNextPureNamePartition(null, limit, partitionCount++)) != null   ) {
982 1021

  
......
1021 1060
	 */
1022 1061
	private Integer determineTaxonCount(PesiExportState state) {
1023 1062
		Integer result = null;
1024
		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
1025
		
1063
		PesiExportConfigurator pesiConfig = state.getConfig();
1064

  
1026 1065
		String sql;
1027 1066
		Source destination =  pesiConfig.getDestination();
1028 1067
		sql = "SELECT max(taxonId) FROM Taxon";
......
1038 1077
		resultSet = null;
1039 1078
		return result;
1040 1079
	}
1041
	
1080

  
1042 1081
	/**
1043 1082
	 * Checks whether a parent at specific level has a specific Rank.
1044 1083
	 * @param taxonName A {@link TaxonNameBase TaxonName}.
......
1124 1163
					Rank childRank = childName.getRank();
1125 1164
					if (childRank != null) {
1126 1165
						if (! childRank.equals(fetchLevel)) {
1127
	
1166

  
1128 1167
							localTreeIndex.append(taxonId + "#");
1129
							
1168

  
1130 1169
							saveData(childNode, parentNode, localTreeIndex, state, taxonId);
1131
	
1170

  
1132 1171
							// Store treeIndex as annotation for further use
1133 1172
							Annotation annotation = Annotation.NewInstance(localTreeIndex.toString(), getTreeIndexAnnotationType(), Language.DEFAULT());
1134 1173
							childNode.addAnnotation(annotation);
1135
	
1174

  
1136 1175
							for (TaxonNode newNode : childNode.getChildNodes()) {
1137 1176
								if (newNode.getTaxon() != null && isPesiTaxon(newNode.getTaxon())){
1138 1177
									traverseTree(newNode, childNode, localTreeIndex, fetchLevel, state);
1139 1178
								}
1140 1179
							}
1141
							
1180

  
1142 1181
						} else {
1143 1182
	//						logger.debug("Target Rank " + fetchLevel.getLabel() + " reached");
1144 1183
							return;
......
1150 1189
					logger.error("Taxon can not be found in state: " + childTaxon.getUuid() + " (" + childTaxon.getTitleCache() + ")");
1151 1190
				}
1152 1191
			}else{
1153
				if (logger.isDebugEnabled()){ 
1192
				if (logger.isDebugEnabled()){
1154 1193
					logger.debug("Taxon is not a PESI taxon: " + childTaxon.getUuid());
1155 1194
				}
1156 1195
			}
......
1176 1215
			TaxonBase<?> parentTaxon = null;
1177 1216
			if (parentNode != null) {
1178 1217
				parentTaxon = parentNode.getTaxon();
1179
				
1218

  
1180 1219
			}
1181 1220

  
1182 1221
			invokeParentTaxonFkAndTreeIndex(state.getDbId(parentTaxon), currentTaxonFk,	treeIndex);
1183 1222
		}
1184
		
1223

  
1185 1224
	}
1186 1225

  
1187 1226
	/**
......
1210 1249
			} else {
1211 1250
				parentTaxonFk_TreeIndex_KingdomFkStmt.setObject(3, null);
1212 1251
			}
1213
			
1252

  
1214 1253
			parentTaxonFk_TreeIndex_KingdomFkStmt.executeUpdate();
1215 1254
			return true;
1216 1255
		} catch (SQLException e) {
......
1219 1258
			return false;
1220 1259
		}
1221 1260
	}
1222
	
1261

  
1223 1262
	protected boolean invokeParentTaxonFk(Integer parentId, Integer childId) {
1224 1263
		try {
1225 1264
			parentTaxonFkStmt.setInt(1, parentId);
......
1239 1278
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
1240 1279
	 * @param nomenclaturalCode The {@link NomenclaturalCode NomenclaturalCode}.
1241 1280
	 * @param taxonFk The TaxonFk to store the values for.
1242
	 * @param state 
1281
	 * @param state
1243 1282
	 * @param kindomFk The KingdomFk.
1244 1283
	 * @return Whether save was successful or not.
1245 1284
	 */
......
1251 1290
			} else {
1252 1291
				rankUpdateStmt.setObject(1, null);
1253 1292
			}
1254
	
1293

  
1255 1294
			String rankCache = getRankCache(taxonName, nomenclaturalCode, state);
1256 1295
			if (rankCache != null) {
1257 1296
				rankUpdateStmt.setString(2, rankCache);
1258 1297
			} else {
1259 1298
				rankUpdateStmt.setObject(2, null);
1260 1299
			}
1261
			
1300

  
1262 1301
			if (kingdomFk != null) {
1302

  
1263 1303
				rankUpdateStmt.setInt(3, kingdomFk);
1264 1304
			} else {
1265 1305
				rankUpdateStmt.setObject(3, null);
1266 1306
			}
1267
			
1307

  
1268 1308
			if (taxonFk != null) {
1269 1309
				rankUpdateStmt.setInt(4, taxonFk);
1270 1310
			} else {
1271 1311
				rankUpdateStmt.setObject(4, null);
1272 1312
			}
1273
			
1313

  
1274 1314
			rankUpdateStmt.executeUpdate();
1275 1315
			return true;
1276 1316
		} catch (SQLException e) {
......
1286 1326
	 * @param nomenclaturalCode The {@link NomenclaturalCode NomenclaturalCode}.
1287 1327
	 * @param taxonFk The TaxonFk to store the values for.
1288 1328
	 * @param typeNameFk The TypeNameFk.
1289
	 * @param state 
1329
	 * @param state
1290 1330
	 * @param kindomFk The KingdomFk.
1291 1331
	 * @param expertFk The ExpertFk.
1292 1332
	 * @param speciesExpertFk The SpeciesExpertFk.
1293 1333
	 * @return Whether save was successful or not.
1294 1334
	 */
1295
	private boolean invokeRankDataAndTypeNameFkAndKingdomFk(TaxonNameBase<?,?> taxonName, NomenclaturalCode nomenclaturalCode, 
1335
	private boolean invokeRankDataAndTypeNameFkAndKingdomFk(TaxonNameBase<?,?> taxonName, NomenclaturalCode nomenclaturalCode,
1296 1336
			Integer taxonFk, Integer typeNameFk, Integer kingdomFkk, PesiExportState state) {
1297 1337
		try {
1298 1338
			int index = 1;
......
1302 1342
			} else {
1303 1343
				rankTypeExpertsUpdateStmt.setObject(index++, null);
1304 1344
			}
1305
	
1345

  
1306 1346
			String rankCache = getRankCache(taxonName, nomenclaturalCode, state);
1307 1347
			if (rankCache != null) {
1308 1348
				rankTypeExpertsUpdateStmt.setString(index++, rankCache);
1309 1349
			} else {
1310 1350
				rankTypeExpertsUpdateStmt.setObject(index++, null);
1311 1351
			}
1312
			
1352

  
1313 1353
			if (typeNameFk != null) {
1314 1354
				rankTypeExpertsUpdateStmt.setInt(index++, typeNameFk);
1315 1355
			} else {
1316 1356
				rankTypeExpertsUpdateStmt.setObject(index++, null);
1317 1357
			}
1318
			
1319
			if (kingdomFk != null) {
1320
				rankTypeExpertsUpdateStmt.setInt(index++, kingdomFk);
1358

  
1359
			if (kingdomFkk != null) {
1360
				rankTypeExpertsUpdateStmt.setInt(index++, kingdomFkk);
1321 1361
			} else {
1322 1362
				rankTypeExpertsUpdateStmt.setObject(index++, null);
1323 1363
			}
1324
			
1364

  
1325 1365
//			if (expertFk != null) {
1326 1366
//				rankTypeExpertsUpdateStmt.setInt(5, expertFk);
1327 1367
//			} else {
......
1334 1374
//			} else {
1335 1375
//				rankTypeExpertsUpdateStmt.setObject(6, null);
1336 1376
//			}
1337
//			
1377
//
1338 1378
			if (taxonFk != null) {
1339 1379
				rankTypeExpertsUpdateStmt.setInt(index++, taxonFk);
1340 1380
			} else {
......
1360 1400
	 * @return Whether the delete operation was successful or not.
1361 1401
	 */
1362 1402
	protected boolean doDelete(PesiExportState state) {
1363
		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
1364
		
1403
		PesiExportConfigurator pesiConfig = state.getConfig();
1404

  
1365 1405
		String sql;
1366 1406
		Source destination =  pesiConfig.getDestination();
1367 1407

  
......
1380 1420
		return ! state.getConfig().isDoTaxa();
1381 1421
	}
1382 1422

  
1383
	
1423

  
1384 1424
	/**
1385 1425
	 * Creates the kingdom fk.
1386 1426
	 * @param taxonName
......
1390 1430
	private static Integer getKingdomFk(TaxonNameBase taxonName){
1391 1431
		return PesiTransformer.nomenClaturalCode2Kingdom(taxonName.getNomenclaturalCode());
1392 1432
	}
1393
	
1433

  
1394 1434
	/**
1395 1435
	 * Creates the parent fk.
1396 1436
	 * @param taxonName
......
1427 1467
	private static Integer getRankFk(TaxonNameBase<?,?> taxonName) {
1428 1468
		return getRankFk(taxonName, taxonName.getNomenclaturalCode());
1429 1469
	}
1430
		
1431
	
1470

  
1471

  
1432 1472
	/**
1433 1473
	 * Returns the <code>RankFk</code> attribute.
1434 1474
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1468 1508
		return getRankCache(taxonName, taxonName.getNomenclaturalCode(), state);
1469 1509
	}
1470 1510

  
1471
	
1511

  
1472 1512
	/**
1473 1513
	 * Returns the <code>RankCache</code> attribute.
1474 1514
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
1475 1515
	 * @param nomenclaturalCode The {@link NomenclaturalCode NomenclaturalCode}.
1476
	 * @param state 
1516
	 * @param state
1477 1517
	 * @return The <code>RankCache</code> attribute.
1478 1518
	 * @see MethodMapper
1479 1519
	 */
......
1484 1524
			logger.warn("No nomenclatural code defined for name " + taxonName.getUuid());
1485 1525
			return null;
1486 1526
		}
1487
		
1527

  
1488 1528
	}
1489 1529

  
1490
	
1530

  
1491 1531
	/**
1492 1532
	 * Returns the <code>DisplayName</code> attribute.
1493 1533
	 * @param taxon The {@link TaxonBase Taxon}.
......
1503 1543
		}
1504 1544
		return result;
1505 1545
	}
1506
	
1546

  
1507 1547
	/**
1508 1548
	 * Returns the <code>AuthorString</code> attribute.
1509 1549
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1522 1562
				isNonViralName = true;
1523 1563
			}
1524 1564
			result = authorshipCache;
1525
			
1565

  
1526 1566
			// For a misapplied names there are special rules
1527 1567
			if (isMisappliedName(taxon)){
1528 1568
				if (taxon.getSec() != null){
......
1541 1581
//					result = authorshipCache;
1542 1582
				}
1543 1583
			}
1544
			
1584

  
1545 1585
			if (taxonName == null){
1546 1586
				logger.warn("TaxonName does not exist for taxon: " + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
1547 1587
			}else if (! isNonViralName){
1548 1588
				logger.warn("TaxonName is not of instance NonViralName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1549 1589
			}
1550
			
1590

  
1551 1591
			if (StringUtils.isBlank(result)) {
1552 1592
				return null;
1553 1593
			} else {
......
1557 1597
			e.printStackTrace();
1558 1598
			return null;
1559 1599
		}
1560
		
1600

  
1561 1601
	}
1562
		
1563
	
1602

  
1603

  
1564 1604
	/**
1565 1605
	 * Returns the <code>DisplayName</code> attribute.
1566 1606
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1577 1617
			HTMLTagRules tagRules = new HTMLTagRules().
1578 1618
					addRule(TagEnum.name, "i").
1579 1619
					addRule(TagEnum.nomStatus, "@status@");
1580
			
1620

  
1581 1621
			NonViralName<?> nvn = CdmBase.deproxy(taxonName, NonViralName.class);
1582 1622
			String result = cacheStrategy.getFullTitleCache(nvn, tagRules);
1583 1623
			cacheStrategy = null;
......
1585 1625
			return result.replaceAll(",?\\<@status@\\>.*\\</@status@\\>", "");
1586 1626
		}
1587 1627
	}
1588
	
1628

  
1589 1629
	@SuppressWarnings("unused")
1590 1630
	private static String getGUID(TaxonNameBase<?,?> taxonName) {
1591 1631
		UUID uuid = taxonName.getUuid();
1592 1632
		String result = "NameUUID:" + uuid.toString();
1593 1633
		return result;
1594 1634
	}
1595
	
1635

  
1596 1636

  
1597 1637
	/**
1598 1638
	 * Returns the <code>WebShowName</code> attribute for a taxon.
......
1609 1649
		}
1610 1650
		return result;
1611 1651
	}
1612
	
1652

  
1613 1653
	/**
1614 1654
	 * Returns the <code>WebShowName</code> attribute.
1615 1655
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1622 1662
			return null;
1623 1663
		}else{
1624 1664
			INonViralNameCacheStrategy<NonViralName<?>> cacheStrategy = getCacheStrategy(taxonName);
1625
		
1665

  
1626 1666
			HTMLTagRules tagRules = new HTMLTagRules().addRule(TagEnum.name, "i");
1627 1667
			NonViralName<?> nvn = CdmBase.deproxy(taxonName, NonViralName.class);
1628 1668
			String result = cacheStrategy.getTitleCache(nvn, tagRules);
......
1632 1672
		}
1633 1673
	}
1634 1674

  
1635
 	
1675

  
1636 1676
	/**
1637 1677
	 * Returns the <code>WebSearchName</code> attribute.
1638 1678
	 * @param taxonName The {@link NonViralName NonViralName}.
......
1676 1716
		nvn = null;
1677 1717
		return result;
1678 1718
	}
1679
	
1719

  
1680 1720
	/**
1681 1721
	 * Returns the SourceNameCache for the AdditionalSource table
1682 1722
	 * @param taxonName
......
1690 1730
			if (nomRef != null){
1691 1731
				return nomRef.getAbbrevTitleCache();
1692 1732
			}
1693
			
1733

  
1694 1734
		}
1695 1735
		return null;
1696 1736
	}
1697
	
1698
	
1699
	
1737

  
1738

  
1739

  
1700 1740
	/**
1701 1741
	 * Returns the <code>FullName</code> attribute.
1702 1742
	 * @param taxon The {@link TaxonBase taxon}.
......
1711 1751
		if (isMisappliedName(taxon)){
1712 1752
			result = result + " " + getAuthorString(taxon);
1713 1753
		}
1714
		
1754

  
1715 1755
		return result;
1716 1756
	}
1717 1757
*/
1718
	
1758

  
1719 1759
	/**
1720 1760
	 * Returns the nomenclatural reference which is the reference
1721 1761
	 * including the detail (microreference).
......
1734 1774
		}
1735 1775
		return ref.getNomenclaturalCitation(taxonName.getNomenclaturalMicroReference());
1736 1776
	}
1737
	
1777

  
1738 1778

  
1739 1779
	/**
1740 1780
	 * Returns the <code>NameStatusFk</code> attribute.
......
1752 1792
		}
1753 1793
		return result;
1754 1794
	}
1755
	
1795

  
1756 1796
	/**
1757 1797
	 * Returns the <code>NameStatusCache</code> attribute.
1758 1798
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
1759 1799
	 * @return The <code>NameStatusCache</code> attribute.
1760
	 * @throws UndefinedTransformerMethodException 
1800
	 * @throws UndefinedTransformerMethodException
1761 1801
	 * @see MethodMapper
1762 1802
	 */
1763 1803
	@SuppressWarnings("unused")
......
1769 1809
		}
1770 1810
		return result;
1771 1811
	}
1772
	
1773
	
1812

  
1813

  
1774 1814
	private static NomenclaturalStatus getNameStatus(TaxonNameBase<?,?> taxonName) {
1775 1815
		try {
1776 1816
			if (taxonName != null && (taxonName.isInstanceOf(NonViralName.class))) {
......
1783 1823
					logger.error("This TaxonName has more than one Nomenclatural Status: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1784 1824
				}
1785 1825
			}
1786
		
1826

  
1787 1827
		} catch (Exception e) {
1788 1828
			e.printStackTrace();
1789 1829
		}
......
1798 1838
	 */
1799 1839
	private static Integer getTaxonStatusFk(TaxonBase<?> taxon, PesiExportState state) {
1800 1840
		Integer result = null;
1801
		
1841

  
1802 1842
		try {
1803 1843
			if (isMisappliedName(taxon)) {
1804 1844
				Synonym synonym = Synonym.NewInstance(null, null);
1805
				
1845

  
1806 1846
				// This works as long as only the instance is important to differentiate between TaxonStatus.
1807 1847
				result = PesiTransformer.taxonBase2statusFk(synonym); // Auct References are treated as Synonyms in Datawarehouse now.
1808 1848
			} else {
1809 1849
				result = PesiTransformer.taxonBase2statusFk(taxon);
1810 1850
			}
1811
		
1851

  
1812 1852
		} catch (Exception e) {
1813 1853
			e.printStackTrace();
1814 1854
		}
1815 1855
		return result;
1816 1856
	}
1817
	
1857

  
1818 1858
	/**
1819 1859
	 * Returns the <code>TaxonStatusCache</code> attribute.
1820 1860
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
1821 1861
	 * @param state The {@link PesiExportState PesiExportState}.
1822 1862
	 * @return The <code>TaxonStatusCache</code> attribute.
1823
	 * @throws UndefinedTransformerMethodException 
1863
	 * @throws UndefinedTransformerMethodException
1824 1864
	 * @see MethodMapper
1825 1865
	 */
1826 1866
	@SuppressWarnings("unused")
1827 1867
	private static String getTaxonStatusCache(TaxonBase<?> taxon, PesiExportState state) throws UndefinedTransformerMethodException {
1828 1868
		return state.getTransformer().getTaxonStatusCacheByKey(getTaxonStatusFk(taxon, state));
1829 1869
	}
1830
	
1870

  
1831 1871
	/**
1832 1872
	 * Returns the <code>TypeNameFk</code> attribute.
1833 1873
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1853 1893
		}
1854 1894
		return result;
1855 1895
	}
1856
	
1896

  
1857 1897
	/**
1858 1898
	 * Returns the <code>TypeFullnameCache</code> attribute.
1859 1899
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1863 1903
	@SuppressWarnings("unused")
1864 1904
	private static String getTypeFullnameCache(TaxonNameBase<?,?> taxonName) {
1865 1905
		String result = null;
1866
		
1906

  
1867 1907
		try {
1868 1908
		if (taxonName != null) {
1869 1909
			Set<NameTypeDesignation> nameTypeDesignations = taxonName.getNameTypeDesignations();
......
1879 1919
				logger.warn("This TaxonName has " + nameTypeDesignations.size() + " NameTypeDesignations: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1880 1920
			}
1881 1921
		}
1882
		
1922

  
1883 1923
		} catch (Exception e) {
1884 1924
			e.printStackTrace();
1885 1925
		}
1886 1926
		return result;
1887 1927
	}
1888 1928

  
1889
	
1929

  
1890 1930
	/**
1891 1931
	 * Returns the <code>QualityStatusFk</code> attribute.
1892 1932
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1898 1938
		return PesiTransformer.getQualityStatusKeyBySource(sources, taxonName);
1899 1939
	}
1900 1940

  
1901
	
1941

  
1902 1942
	/**
1903 1943
	 * Returns the <code>QualityStatusCache</code> attribute.
1904 1944
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
1905 1945
	 * @return The <code>QualityStatusCache</code> attribute.
1906
	 * @throws UndefinedTransformerMethodException 
1946
	 * @throws UndefinedTransformerMethodException
1907 1947
	 * @see MethodMapper
1908 1948
	 */
1909 1949
	@SuppressWarnings("unused")
......
1911 1951
		return state.getTransformer().getQualityStatusCacheByKey(getQualityStatusFk(taxonName));
1912 1952
	}
1913 1953

  
1914
	
1954

  
1915 1955
	/**
1916 1956
	 * Returns the <code>TypeDesignationStatusFk</code> attribute.
1917 1957
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1921 1961
	@SuppressWarnings("unused")
1922 1962
	private static Integer getTypeDesignationStatusFk(TaxonNameBase<?,?> taxonName) {
1923 1963
		Integer result = null;
1924
		
1964

  
1925 1965
		try {
1926 1966
		if (taxonName != null) {
1927 1967
			Set<NameTypeDesignation> typeDesignations = taxonName.getNameTypeDesignations();
......
1933 1973
				logger.error("Found a TaxonName with more than one NameTypeDesignation: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1934 1974
			}
1935 1975
		}
1936
		
1976

  
1937 1977
		} catch (Exception e) {
1938 1978
			e.printStackTrace();
1939 1979
		}
......
1949 1989
	@SuppressWarnings("unused")
1950 1990
	private static String getTypeDesignationStatusCache(TaxonNameBase<?,?> taxonName) {
1951 1991
		String result = null;
1952
		
1992

  
1953 1993
		try {
1954 1994
		if (taxonName != null) {
1955 1995
			Set<NameTypeDesignation> typeDesignations = taxonName.getNameTypeDesignations();
......
1961 2001
				logger.error("Found a TaxonName with more than one NameTypeDesignation: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1962 2002
			}
1963 2003
		}
1964
		
2004

  
1965 2005
		} catch (Exception e) {
1966 2006
			e.printStackTrace();
1967 2007
		}
1968 2008
		return result;
1969 2009
	}
1970
	
2010

  
1971 2011
	/**
1972 2012
	 * Returns the <code>FossilStatusFk</code> attribute.
1973 2013
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
1977 2017
	@SuppressWarnings("unused")
1978 2018
	private static Integer getFossilStatusFk(IdentifiableEntity<?> identEntity, PesiExportState state) {
1979 2019
		Integer result = null;
1980
		
2020

  
1981 2021
		Set<String> fossilStatuus = identEntity.getExtensions(ErmsTransformer.uuidFossilStatus);
1982 2022
		if (fossilStatuus.size() == 0){
1983 2023
			return null;
......
1985 2025
			logger.warn("More than 1 fossil status given for " +  identEntity.getTitleCache() + " " + identEntity.getUuid());
1986 2026
		}
1987 2027
		String fossilStatus = fossilStatuus.iterator().next();
1988
		
2028

  
1989 2029
		int statusFk = state.getTransformer().FossilStatusCache2FossilStatusFk(fossilStatus);
1990 2030
		return statusFk;
1991 2031
	}
1992
	
2032

  
1993 2033
	/**
1994 2034
	 * Returns the <code>FossilStatusCache</code> attribute.
1995 2035
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
2008 2048
		}
2009 2049
		return result;
2010 2050
	}
2011
	
2051

  
2012 2052
	/**
2013 2053
	 * Returns the <code>IdInSource</code> attribute.
2014 2054
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
2018 2058
	@SuppressWarnings("unused")
2019 2059
	private static String getIdInSource(IdentifiableEntity taxonName) {
2020 2060
		String result = null;
2021
		
2061

  
2022 2062
		try {
2023 2063
			Set<IdentifiableSource> sources = getPesiSources(taxonName);
2024 2064
			if (sources.size() > 1){
......
2042 2082
				}else{
2043 2083
					if (logger.isDebugEnabled()){logger.debug("Not a PESI source");};
2044 2084
				}
2045
				
2085

  
2046 2086
				String sourceIdNameSpace = source.getIdNamespace();
2047 2087
				if (sourceIdNameSpace != null) {
2048 2088
					if (sourceIdNameSpace.equals(PesiTransformer.STR_NAMESPACE_NOMINAL_TAXON)) {
......
2053 2093
						result =  idInSource != null ? ("Inferred genus from TAX_ID: " + source.getIdInSource()):null;
2054 2094
					} else if (sourceIdNameSpace.equals(TaxonServiceImpl.POTENTIAL_COMBINATION_NAMESPACE)) {
2055 2095
						result =  idInSource != null ? ("Potential combination from TAX_ID: " + source.getIdInSource()):null;
2056
					} 
2096
					}
2057 2097
				}
2058 2098
				if (result == null) {
2059 2099
					logger.warn("IdInSource is NULL for this taxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() +", sourceIdNameSpace: " + source.getIdNamespace()+")");
......
2069 2109
		}
2070 2110
		return result;
2071 2111
	}
2072
	
2112

  
2073 2113
	/**
2074 2114
	 * Returns the idInSource for a given TaxonName only.
2075 2115
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
2077 2117
	 */
2078 2118
	private static String getIdInSourceOnly(IdentifiableEntity identEntity) {
2079 2119
		String result = null;
2080
		
2120

  
2081 2121
		// Get the sources first
2082 2122
		Set<IdentifiableSource> sources = getPesiSources(identEntity);
2083 2123

  
......
2099 2139
			}
2100 2140

  
2101 2141
		}
2102
		
2142

  
2103 2143
		return result;
2104 2144
	}
2105
	
2145

  
2106 2146
	/**
2107 2147
	 * Returns the Sources for a given TaxonName only.
2108 2148
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
......
2117 2157
			TaxonNameBase taxonName = CdmBase.deproxy(identEntity, TaxonNameBase.class);
2118 2158
			Set<IdentifiableSource> testSources = identEntity.getSources();
2119 2159
			sources = filterPesiSources(identEntity.getSources());
2120
			
2160

  
2121 2161
			if (sources.size() == 0 && testSources.size()>0){
2122 2162
				IdentifiableSource source = testSources.iterator().next();
2123 2163
				logger.warn("There are sources, but they are no pesi sources!!!" + source.getIdInSource() + " - " + source.getIdNamespace() + " - " + source.getCitation().getTitleCache());
......
2125 2165
			if (sources.size() > 1) {
2126 2166
				logger.warn("This TaxonName has more than one Source: " + identEntity.getUuid() + " (" + identEntity.getTitleCache() + ")");
2127 2167
			}
2128
			
2168

  
2129 2169
			// name has no PESI source, take sources from TaxonBase
2130 2170
			if (sources == null || sources.isEmpty()) {
2131 2171
				Set<TaxonBase> taxa = taxonName.getTaxonBases();
......
2136 2176

  
2137 2177
		//for TaxonBases
2138 2178
		}else if (identEntity.isInstanceOf(TaxonBase.class)){
2139
			sources = filterPesiSources(identEntity.getSources());	
2179
			sources = filterPesiSources(identEntity.getSources());
2140 2180
		}
2141 2181

  
2142
		/*TODO: deleted only for testing the inferred synonyms 
2182
		/*TODO: deleted only for testing the inferred synonyms
2143 2183
		if (sources == null || sources.isEmpty()) {
2144 2184
			logger.warn("This TaxonName has no PESI Sources: " + identEntity.getUuid() + " (" + identEntity.getTitleCache() +")");
2145 2185
		}else if (sources.size() > 1){
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff