3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.out
;
12 import java
.sql
.Connection
;
13 import java
.sql
.PreparedStatement
;
14 import java
.sql
.SQLException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
19 import java
.util
.UUID
;
20 import java
.util
.regex
.Matcher
;
21 import java
.util
.regex
.Pattern
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.joda
.time
.DateTime
;
25 import org
.springframework
.stereotype
.Component
;
26 import org
.springframework
.transaction
.TransactionStatus
;
28 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
29 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.IdMapper
;
30 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.MethodMapper
;
31 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
32 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
33 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
36 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
37 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
38 import eu
.etaxonomy
.cdm
.model
.common
.VersionableEntity
;
39 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignation
;
40 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignationStatus
;
41 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
42 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
43 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
44 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
45 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
46 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
47 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
60 @SuppressWarnings("unchecked")
61 public class PesiTaxonExport
extends PesiExportBase
{
62 private static final Logger logger
= Logger
.getLogger(PesiTaxonExport
.class);
63 private static final Class
<?
extends CdmBase
> standardMethodParameter
= TaxonNameBase
.class;
65 private static int modCount
= 1000;
66 private static final String dbTableName
= "Taxon";
67 private static final String pluralString
= "Taxa";
68 private PreparedStatement parentTaxonFk_TreeIndex_KingdomFkStmt
;
69 private NomenclaturalCode nomenclaturalCode
;
70 private Integer kingdomFk
;
71 private HashMap
<Rank
, Rank
> rankMap
= new HashMap
<Rank
, Rank
>();
72 private List
<Rank
> rankList
= new ArrayList
<Rank
>();
73 private static final UUID uuidTreeIndex
= UUID
.fromString("28f4e205-1d02-4d3a-8288-775ea8413009");
74 private AnnotationType treeIndexAnnotationType
;
77 * @return the treeIndexAnnotationType
79 protected AnnotationType
getTreeIndexAnnotationType() {
80 return treeIndexAnnotationType
;
84 * @param treeIndexAnnotationType the treeIndexAnnotationType to set
86 protected void setTreeIndexAnnotationType(AnnotationType treeIndexAnnotationType
) {
87 this.treeIndexAnnotationType
= treeIndexAnnotationType
;
98 public PesiTaxonExport() {
103 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
106 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
107 return standardMethodParameter
;
111 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
114 protected boolean doCheck(PesiExportState state
) {
115 boolean result
= true;
120 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
123 protected boolean doInvoke(PesiExportState state
) {
125 logger
.error("*** Started Making " + pluralString
+ " ...");
127 // Prepare TreeIndex-And-KingdomFk-Statement
128 Connection connection
= state
.getConfig().getDestination().getConnection();
129 String parentTaxonFk_TreeIndex_KingdomFkSql
= "UPDATE Taxon SET ParentTaxonFk = ?, TreeIndex = ?, " +
130 "KingdomFk = ?, RankFk = ?, RankCache = ? WHERE TaxonId = ?";
131 parentTaxonFk_TreeIndex_KingdomFkStmt
= connection
.prepareStatement(parentTaxonFk_TreeIndex_KingdomFkSql
);
133 // Get the limit for objects to save within a single transaction.
134 int limit
= state
.getConfig().getLimitSave();
136 // Stores whether this invoke was successful or not.
137 boolean success
= true;
139 // PESI: Clear the database table Taxon.
142 // CDM: Get the number of all available taxa.
143 // int maxCount = getTaxonService().count(null);
144 // logger.error("Total amount of " + maxCount + " " + pluralString + " will be exported.");
146 // Get specific mappings: (CDM) Taxon -> (PESI) Taxon
147 PesiExportMapping mapping
= getMapping();
149 // Initialize the db mapper
150 mapping
.initialize(state
);
154 TransactionStatus txStatus
= null;
155 List
<TaxonNameBase
> list
= null;
157 // 1st Round: Make Taxa
158 logger
.error("PHASE 1...");
160 txStatus
= startTransaction(true);
161 logger
.error("Started new transaction. Fetching some " + pluralString
+ " (max: " + limit
+ ") ...");
162 while ((list
= getNameService().list(null, limit
, count
, null, null)).size() > 0) {
164 logger
.error("Fetched " + list
.size() + " " + pluralString
+ ". Exporting...");
165 for (TaxonNameBase taxonName
: list
) {
166 doCount(count
++, modCount
, pluralString
);
167 success
&= mapping
.invoke(taxonName
);
169 // Check whether some rules are violated
170 nomenclaturalCode
= taxonName
.getNomenclaturalCode();
171 String genusOrUninomial
= getGenusOrUninomial(taxonName
);
172 String specificEpithet
= getSpecificEpithet(taxonName
);
173 String infraSpecificEpithet
= getInfraSpecificEpithet(taxonName
);
174 String infraGenericEpithet
= getInfraGenericEpithet(taxonName
);
175 Integer rank
= getRankFk(taxonName
, nomenclaturalCode
);
178 logger
.error("Rank was not determined: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
180 if (infraGenericEpithet
== null && rank
.intValue() == 190) {
181 logger
.error("InfraSpecificEpithet was not determined although it should exist for rank 190: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
183 if (specificEpithet
!= null && rank
.intValue() < 220) {
184 logger
.error("SpecificEpithet was determined for rank " + rank
+ " although it should only exist for ranks higher or equal to 220: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
186 if (infraSpecificEpithet
!= null && rank
.intValue() < 230) {
187 logger
.error("InfraSpecificEpithet was determined for rank " + rank
+ " although it should only exist for ranks higher or equal to 230: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
190 if (infraSpecificEpithet
!= null && specificEpithet
== null) {
191 logger
.error("An infraSpecificEpithet was determined, but a specificEpithet was not determined: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
193 if (genusOrUninomial
== null) {
194 logger
.error("GenusOrUninomial was not determined: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
199 // Commit transaction
200 commitTransaction(txStatus
);
201 logger
.error("Committed transaction.");
202 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ". Total: " + count
);
206 txStatus
= startTransaction(true);
207 logger
.error("Started new transaction. Fetching some " + pluralString
+ " (max: " + limit
+ ") ...");
209 if (list
.size() == 0) {
210 logger
.error("No " + pluralString
+ " left to fetch.");
212 // Commit transaction
213 commitTransaction(txStatus
);
214 logger
.error("Committed transaction.");
218 List
<TaxonomicTree
> taxonomicTreeList
= null;
219 // 2nd Round: Add ParentTaxonFk, TreeIndex, Rank and KingdomFk to each Taxon
220 logger
.error("PHASE 2...");
222 // Specify starting ranks for tree traversing
223 rankList
.add(Rank
.KINGDOM());
224 rankList
.add(Rank
.GENUS());
226 // Specify where to stop traversing (value) when starting at a specific Rank (key)
227 rankMap
.put(Rank
.GENUS(), null); // Since NULL does not match an existing Rank, traverse all the way down to the leafs
228 rankMap
.put(Rank
.KINGDOM(), Rank
.GENUS()); // excludes rank genus
230 StringBuffer treeIndex
= new StringBuffer();
232 // Retrieve list of Taxonomic Trees
233 txStatus
= startTransaction(true);
234 logger
.error("Started transaction. Fetching all Taxonomic Trees...");
235 taxonomicTreeList
= getTaxonTreeService().listTaxonomicTrees(null, 0, null, null);
236 commitTransaction(txStatus
);
237 logger
.error("Committed transaction.");
239 logger
.error("Fetched " + taxonomicTreeList
.size() + " Taxonomic Tree.");
241 setTreeIndexAnnotationType(getAnnotationType(uuidTreeIndex
, "TreeIndex", "", "TI"));
243 for (TaxonomicTree taxonomicTree
: taxonomicTreeList
) {
244 for (Rank rank
: rankList
) {
246 txStatus
= startTransaction(true);
247 logger
.error("Started transaction to fetch all rootNodes specific to Rank " + rank
.getLabel() + " ...");
249 List
<TaxonNode
> rankSpecificRootNodes
= getTaxonTreeService().loadRankSpecificRootNodes(taxonomicTree
, rank
, null);
250 logger
.error("Fetched " + rankSpecificRootNodes
.size() + " RootNodes for Rank " + rank
.getLabel());
252 commitTransaction(txStatus
);
253 logger
.error("Committed transaction.");
255 // int elementCount = 0;
256 // int halfCount = rankSpecificRootNodes.size() / 2;
258 for (TaxonNode rootNode
: rankSpecificRootNodes
) {
259 txStatus
= startTransaction(false);
260 Rank endRank
= rankMap
.get(rank
);
261 if (endRank
!= null) {
262 logger
.error("Started transaction to traverse childNodes of rootNode (" + rootNode
.getUuid() + ") till Rank " + endRank
.getLabel() + " ...");
264 logger
.error("Started transaction to traverse childNodes of rootNode (" + rootNode
.getUuid() + ") till leafs are reached ...");
267 TaxonNode newNode
= getTaxonNodeService().load(rootNode
.getUuid());
269 TaxonNode parentNode
= newNode
.getParent();
270 if (rank
.equals(Rank
.KINGDOM())) {
271 treeIndex
= new StringBuffer();
272 treeIndex
.append("#");
274 // Get treeIndex from parentNode
275 if (parentNode
!= null) {
276 boolean annotationFound
= false;
277 Set
<Annotation
> annotations
= parentNode
.getAnnotations();
278 for (Annotation annotation
: annotations
) {
279 AnnotationType annotationType
= annotation
.getAnnotationType();
280 if (annotationType
!= null && annotationType
.equals(getTreeIndexAnnotationType())) {
281 // It is assumed that there is only one annotation using AnnotationType TREEINDEX. If there are more only one is noticed.
282 treeIndex
= new StringBuffer(CdmUtils
.Nz(annotation
.getText()));
283 annotationFound
= true;
284 logger
.error("treeIndex: " + treeIndex
);
288 if (!annotationFound
) {
289 // This should not happen because it means that the treeIndex was not set correctly as an annotation to parentNode
290 logger
.error("TreeIndex could not be read from annotation of this TaxonNode: " + parentNode
.getUuid());
291 treeIndex
= new StringBuffer();
292 treeIndex
.append("#");
295 // TreeIndex could not be determined, but it's unclear how to proceed to generate a correct treeIndex if the parentNode is NULL
296 logger
.error("ParentNode for RootNode is NULL. TreeIndex could not be determined: " + newNode
.getUuid());
297 treeIndex
= new StringBuffer(); // This just prevents growing of the treeIndex in a wrong manner
298 treeIndex
.append("#");
302 nomenclaturalCode
= newNode
.getTaxon().getName().getNomenclaturalCode();
303 kingdomFk
= PesiTransformer
.nomenClaturalCode2Kingdom(nomenclaturalCode
);
304 traverseTree(newNode
, parentNode
, treeIndex
, rankMap
.get(rank
), state
);
306 commitTransaction(txStatus
);
307 logger
.error("Committed transaction.");
310 // if (elementCount == halfCount) {
311 // logger.error("50% of " + rank.getLabel() + " RootNodes processed...");
317 logger
.error("*** Finished Making " + pluralString
+ " ..." + getSuccessString(success
));
320 } catch (SQLException e
) {
322 logger
.error(e
.getMessage());
328 * Returns the AnnotationType for a given UUID.
336 protected AnnotationType
getAnnotationType(UUID uuid
, String label
, String text
, String labelAbbrev
){
337 AnnotationType annotationType
= (AnnotationType
)getTermService().find(uuid
);
338 if (annotationType
== null) {
339 annotationType
= AnnotationType
.NewInstance(label
, text
, labelAbbrev
);
340 annotationType
.setUuid(uuid
);
341 // annotationType.setVocabulary(AnnotationType.EDITORIAL().getVocabulary());
342 getTermService().save(annotationType
);
344 return annotationType
;
348 * Traverses the TaxonTree recursively and stores determined values for every Taxon.
355 private void traverseTree(TaxonNode childNode
, TaxonNode parentNode
, StringBuffer treeIndex
, Rank fetchLevel
, PesiExportState state
) {
356 // Traverse all branches from this childNode until specified fetchLevel is reached.
357 StringBuffer localTreeIndex
= new StringBuffer(treeIndex
);
358 if (childNode
.getTaxon() != null) {
359 TaxonNameBase taxonName
= childNode
.getTaxon().getName();
360 Integer taxonNameId
= state
.getDbId(taxonName
);
361 if (taxonNameId
!= null) {
362 Rank childTaxonNameRank
= taxonName
.getRank();
363 if (childTaxonNameRank
!= null) {
364 if (! childTaxonNameRank
.equals(fetchLevel
)) {
366 localTreeIndex
.append(taxonNameId
);
367 localTreeIndex
.append("#");
369 saveData(childNode
, parentNode
, localTreeIndex
, state
, taxonNameId
);
371 // Store treeIndex as annotation for further use
372 Annotation annotation
= Annotation
.NewInstance(localTreeIndex
.toString(), getTreeIndexAnnotationType(), Language
.DEFAULT());
373 childNode
.addAnnotation(annotation
);
375 for (TaxonNode newNode
: childNode
.getChildNodes()) {
376 traverseTree(newNode
, childNode
, localTreeIndex
, fetchLevel
, state
);
380 // logger.error("Target Rank " + fetchLevel.getLabel() + " reached");
384 logger
.error("Rank is NULL. FetchLevel can not be checked: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
387 logger
.error("TaxonName can not be found in State: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
391 logger
.error("Taxon is NULL for TaxonNode: " + childNode
.getUuid());
396 * Stores values in database for every recursive round.
401 * @param currentTaxonFk
403 private void saveData(TaxonNode childNode
, TaxonNode parentNode
, StringBuffer treeIndex
, PesiExportState state
, Integer currentTaxonFk
) {
404 // We are differentiating kingdoms by the nomenclatural code for now.
405 // This needs to be handled in a better way as soon as we know how to differentiate between more kingdoms.
406 Taxon childNodeTaxon
= childNode
.getTaxon();
407 TaxonNameBase childNodeTaxonName
= childNode
.getTaxon().getName();
408 if (childNodeTaxon
!= null && childNodeTaxonName
!= null) {
409 TaxonNameBase parentNodeTaxonName
= null;
410 if (parentNode
!= null) {
411 Taxon parentNodeTaxon
= parentNode
.getTaxon();
412 if (parentNodeTaxon
!= null) {
413 parentNodeTaxonName
= parentNodeTaxon
.getName();
417 invokeParentTaxonFkAndTreeIndexAndKingdomFk(childNodeTaxonName
,
420 state
.getDbId(parentNodeTaxonName
),
428 * Inserts values into the Taxon database table.
429 * @param taxonNameBase
434 protected boolean invokeParentTaxonFkAndTreeIndexAndKingdomFk(TaxonNameBase taxonName
, NomenclaturalCode nomenclaturalCode
, Integer kingdomFk
, Integer parentTaxonFk
, Integer currentTaxonFk
, StringBuffer treeIndex
) {
436 if (parentTaxonFk
!= null) {
437 parentTaxonFk_TreeIndex_KingdomFkStmt
.setInt(1, parentTaxonFk
);
439 parentTaxonFk_TreeIndex_KingdomFkStmt
.setObject(1, parentTaxonFk
);
441 parentTaxonFk_TreeIndex_KingdomFkStmt
.setString(2, treeIndex
.toString());
442 parentTaxonFk_TreeIndex_KingdomFkStmt
.setInt(3, kingdomFk
);
443 parentTaxonFk_TreeIndex_KingdomFkStmt
.setInt(4, getRankFk(taxonName
, nomenclaturalCode
));
444 parentTaxonFk_TreeIndex_KingdomFkStmt
.setString(5, getRankCache(taxonName
, nomenclaturalCode
));
445 parentTaxonFk_TreeIndex_KingdomFkStmt
.setInt(6, currentTaxonFk
);
446 parentTaxonFk_TreeIndex_KingdomFkStmt
.executeUpdate();
448 } catch (SQLException e
) {
449 logger
.error("SQLException during treeIndex invoke for taxonName - " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + "): " + e
.getMessage());
450 logger
.error("parentTaxonFk: " + parentTaxonFk
);
451 logger
.error("treeIndex: " + treeIndex
);
452 logger
.error("kingdomFk: " + kingdomFk
);
453 logger
.error("rankFk: " + getRankFk(taxonName
, nomenclaturalCode
));
454 logger
.error("rankCache: " + getRankCache(taxonName
, nomenclaturalCode
));
455 logger
.error("taxonFk: " + currentTaxonFk
);
456 // e.printStackTrace();
462 * Deletes all entries of database tables related to <code>Taxon</code>.
463 * @param state The PesiExportState
464 * @return Whether the delete operation was successful or not.
466 protected boolean doDelete(PesiExportState state
) {
467 PesiExportConfigurator pesiConfig
= (PesiExportConfigurator
) state
.getConfig();
470 Source destination
= pesiConfig
.getDestination();
473 sql
= "DELETE FROM " + dbTableName
;
474 destination
.setQuery(sql
);
475 destination
.update(sql
);
480 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
483 protected boolean isIgnore(PesiExportState state
) {
484 return ! state
.getConfig().isDoTaxa();
488 * Returns the <code>RankFk</code> attribute.
489 * @param taxon The {@link TaxonBase Taxon}.
490 * @return The <code>RankFk</code> attribute.
493 private static Integer
getRankFk(TaxonNameBase taxonName
, NomenclaturalCode nomenclaturalCode
) {
494 Integer result
= null;
495 if (nomenclaturalCode
!= null) {
496 if (taxonName
!= null && taxonName
.getRank() == null) {
497 logger
.warn("Rank is null: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
499 result
= PesiTransformer
.rank2RankId(taxonName
.getRank(), PesiTransformer
.nomenClaturalCode2Kingdom(nomenclaturalCode
));
500 if (result
== null) {
501 logger
.warn("Rank " + taxonName
.getRank().getLabel() + " could not be determined for PESI-Kingdom-Id " + PesiTransformer
.nomenClaturalCode2Kingdom(nomenclaturalCode
));
508 * Returns the <code>RankCache</code> attribute.
509 * @param taxon The {@link TaxonBase Taxon}.
510 * @return The <code>RankCache</code> attribute.
513 private static String
getRankCache(TaxonNameBase taxonName
, NomenclaturalCode nomenclaturalCode
) {
514 String result
= null;
515 if (nomenclaturalCode
!= null) {
516 result
= PesiTransformer
.rank2RankCache(taxonName
.getRank(), PesiTransformer
.nomenClaturalCode2Kingdom(nomenclaturalCode
));
523 * @param taxon The {@link TaxonBase Taxon}.
524 * @return Whether it's genus or uninomial.
527 private static String
getGenusOrUninomial(TaxonNameBase taxonName
) {
528 String result
= null;
529 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
530 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
531 result
= nonViralName
.getGenusOrUninomial();
537 * Returns the <code>InfraGenericEpithet</code> attribute.
538 * @param taxon The {@link TaxonBase Taxon}.
539 * @return The <code>InfraGenericEpithet</code> attribute.
542 private static String
getInfraGenericEpithet(TaxonNameBase taxonName
) {
543 String result
= null;
544 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
545 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
546 result
= nonViralName
.getInfraGenericEpithet();
552 * Returns the <code>SpecificEpithet</code> attribute.
553 * @param taxon The {@link TaxonBase Taxon}.
554 * @return The <code>SpecificEpithet</code> attribute.
557 private static String
getSpecificEpithet(TaxonNameBase taxonName
) {
558 String result
= null;
559 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
560 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
561 result
= nonViralName
.getSpecificEpithet();
567 * Returns the <code>InfraSpecificEpithet</code> attribute.
568 * @param taxon The {@link TaxonBase Taxon}.
569 * @return The <code>InfraSpecificEpithet</code> attribute.
572 private static String
getInfraSpecificEpithet(TaxonNameBase taxonName
) {
573 String result
= null;
574 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
575 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
576 result
= nonViralName
.getInfraSpecificEpithet();
582 * Returns the <code>WebSearchName</code> attribute.
583 * @param taxon The {@link TaxonBase Taxon}.
584 * @return The <code>WebSearchName</code> attribute.
587 @SuppressWarnings("unused")
588 private static String
getWebSearchName(TaxonNameBase taxonName
) {
589 String result
= null;
590 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
591 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
592 result
= nonViralName
.getNameCache();
598 * Returns the <code>WebShowName</code> attribute.
599 * @param taxon The {@link TaxonBase Taxon}.
600 * @return The <code>WebShowName</code> attribute.
603 @SuppressWarnings("unused")
604 private static String
getWebShowName(TaxonNameBase taxonName
) {
605 String result
= null;
607 if (taxonName
!= null) {
608 if (taxonName
!= null && taxonName
.isInstanceOf(NonViralName
.class)) {
609 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
610 String singleWhitespace
= "\\s";
611 String multipleWhitespaces
= "\\s*";
612 String whitespaceOrNothing
= "\\s?";
613 String singleBlank
= " ";
614 String anyNumberOfCharacters
= ".*";
615 String backSlashRegEx
= "\\";
616 // String periodRegEx = "\\.";
617 // String asterixRegEx = "\\*";
618 String questionMarkRegEx
= "\\?";
619 // String plusRegEx = "\\+";
620 // String squareBracketRegEx = "\\[";
621 // String curlyBracketRegEx = "\\{";
622 // String pipeRegEx = "\\|";
623 // String accentRegEx = "\\^";
624 // String dollarSignRegEx = "\\$";
625 String openingParenthesisRegEx
= "\\(";
626 String closingParenthesisRegEx
= "\\)";
627 String openParenthesis
= "(";
628 String closeParenthesis
= ")";
629 String italicBeginTag
= "<i>";
630 String italicEndTag
= "</i>";
631 String endAnchor
= "$";
633 String questionMarkReplacement
= backSlashRegEx
+ questionMarkRegEx
;
634 // String backSlashReplacement = backSlashRegEx + backSlashRegEx;
635 // String periodReplacement = backSlashRegEx + periodRegEx;
636 // String asterixReplacement = backSlashRegEx + asterixRegEx;
637 // String plusReplacement = backSlashRegEx + plusRegEx;
638 // String squareBracketReplacement = backSlashRegEx + squareBracketRegEx;
639 // String curlyBracketReplacement = backSlashRegEx + curlyBracketRegEx;
640 // String pipeReplacement = backSlashRegEx + pipeRegEx;
641 // String accentReplacement = backSlashRegEx + accentRegEx;
642 // String dollarSignReplacement = backSlashRegEx + dollarSignRegEx;
644 if (nonViralName
!= null) {
645 if (nonViralName
.getTitleCache() != null) {
647 String fullName
= nonViralName
.getTitleCache();
649 // This deals with question marks that are reserved in regular expressions
650 fullName
= fullName
.replaceAll(questionMarkRegEx
, questionMarkReplacement
);
652 String genusOrUninomial
= nonViralName
.getGenusOrUninomial();
653 String infraGenericEpithet
= nonViralName
.getInfraGenericEpithet();
654 if (infraGenericEpithet
!= null) {
655 infraGenericEpithet
= openParenthesis
+ infraGenericEpithet
+ closeParenthesis
;
657 String specificEpithet
= nonViralName
.getSpecificEpithet();
658 String infraSpecificEpithet
= nonViralName
.getInfraSpecificEpithet();
660 StringBuffer replaceFullName
= new StringBuffer(fullName
);
661 List
<NamePosition
> genusOrUninomialPosition
= getPosition(genusOrUninomial
, fullName
);
662 List
<NamePosition
> infraGenericEpithetPosition
= getPosition(infraGenericEpithet
, fullName
);
663 List
<NamePosition
> specificEpithetPosition
= getPosition(specificEpithet
, fullName
);
664 List
<NamePosition
> authorshipPosition
= getPosition(specificEpithet
, fullName
);
665 if (nameExists(genusOrUninomialPosition
) || nameExists(infraGenericEpithetPosition
) || nameExists(specificEpithetPosition
)) {
666 replaceFullName
.insert(0, italicBeginTag
);
668 if (nameExists(specificEpithetPosition
)) {
669 boolean insertSpecificEpithetEndTag
= true;
671 if ((specificEpithet
.equals(infraSpecificEpithet
) && countPattern(infraSpecificEpithet
, fullName
) == 2) |
672 (! specificEpithet
.equals(infraSpecificEpithet
) && countPattern(infraSpecificEpithet
, fullName
) == 1)) {
673 // infraSpecificEpithet exists
675 // Determine position of infraSpecificEpithet
676 int infraSpecificEpithetLocation
= replaceFullName
.lastIndexOf(infraSpecificEpithet
);
677 int infraSpecificEpithetBeginLocation
= infraSpecificEpithetLocation
;
678 int infraSpecificEpithetEndLocation
= infraSpecificEpithetLocation
+ infraSpecificEpithet
.length();
680 // Check whether rank information exists between specificEpithet and infraSpecificEpithet
681 String rankExistsRegEx
= specificEpithet
+ singleWhitespace
+ anyNumberOfCharacters
+ singleWhitespace
+ infraSpecificEpithet
;
682 List
<NamePosition
> rankPosition
= getPosition(rankExistsRegEx
, fullName
);
683 if (rankPosition
!= null && nameExists(rankPosition
)) {
684 // Rank information exists
686 // Insert italicBeginTag
687 replaceFullName
.insert(infraSpecificEpithetBeginLocation
, italicBeginTag
);
689 // Check position of infraSpecificEpithet
690 List
<NamePosition
> infraSpecificEpithetPosition
= getPosition(infraSpecificEpithet
, fullName
);
691 if (infraSpecificEpithetPosition
!= null) {
692 if (infraSpecificEpithetPosition
.contains(NamePosition
.end
)) {
693 // Append italicEndTag
694 replaceFullName
.append(italicEndTag
);
696 // Insert italicEndTag
697 replaceFullName
.insert(infraSpecificEpithetEndLocation
+ italicBeginTag
.length(), italicEndTag
);
700 } else if (rankPosition
!= null && ! nameExists(rankPosition
)) {
701 // Rank information does not exist
702 insertSpecificEpithetEndTag
= false;
704 // Insert italicEndTag
705 replaceFullName
.insert(infraSpecificEpithetEndLocation
+ italicBeginTag
.length(), italicEndTag
);
709 // Insert an italicEndTag for the specificEpithet
710 if (insertSpecificEpithetEndTag
) {
711 int specificEpithetLocation
= replaceFullName
.indexOf(specificEpithet
) + specificEpithet
.length();
712 if (specificEpithetPosition
.contains(NamePosition
.end
)) {
713 replaceFullName
.append(italicEndTag
);
715 replaceFullName
.insert(specificEpithetLocation
, italicEndTag
);
718 } else if (nameExists(infraGenericEpithetPosition
)) {
719 int infraGenericEpithetLocation
= replaceFullName
.indexOf(infraGenericEpithet
) +
720 openParenthesis
.length() + infraGenericEpithet
.length() + closeParenthesis
.length();
721 if (infraGenericEpithetPosition
.contains(NamePosition
.end
)) {
722 replaceFullName
.append(italicEndTag
);
724 replaceFullName
.insert(infraGenericEpithetLocation
, italicEndTag
);
726 } else if (nameExists(genusOrUninomialPosition
)) {
727 replaceFullName
.insert(genusOrUninomial
.length() + italicBeginTag
.length(), italicEndTag
);
731 result
= replaceFullName
.toString();
733 } catch (Exception e
) {
734 // This needs a workaround since very likely there is a character in the fullName that is reserved in regular expressions
735 logger
.error("WebShowName could not be determined for NonViralName " + nonViralName
.getUuid() + " (" + nonViralName
.getTitleCache() + "): " + e
.getMessage());
736 // e.printStackTrace();
737 result
= nonViralName
.getTitleCache();
740 logger
.error("WebShowName could not be determined: TitleCache is NULL for NonViralName " + nonViralName
.getUuid() + " (" + nonViralName
.getTitleCache() + ")");
743 logger
.error("WebShowName could not be determined: NonViralName is NULL for TaxonName" + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
748 logger
.warn("WebShowName could not be determined: TaxonName is NULL");
751 // logger.error("final result: " + result);
752 } catch (Exception e
) {
760 * @param searchString
761 * @param searchedString
764 private static Integer
countPattern(String searchString
, String searchedString
) {
766 if (searchString
!= null && searchedString
!= null) {
768 while ((index
= searchedString
.indexOf(searchString
, index
)) != -1) {
773 // logger.error("Either searchString or searchedString is NULL");
779 * @param namePosition
782 private static boolean nameExists(List
<NamePosition
> namePosition
) {
783 boolean result
= false;
784 if (namePosition
!= null) {
785 if (namePosition
.contains(NamePosition
.nowhere
)) {
795 private static List
<NamePosition
> getPosition(String name
, String targetString
) {
796 List
<NamePosition
> result
= new ArrayList
<NamePosition
>();
797 boolean touched
= false;
798 if (name
!= null && targetString
!= null) {
799 if ("".equals(name
.trim())) {
800 result
.add(NamePosition
.nowhere
);
802 String beginningAnchor
= "^";
803 String endAnchor
= "$";
804 String anyNumberOfCharacters
= ".*";
806 String beginningRegEx
= beginningAnchor
+ name
;
807 String endRegEx
= name
+ endAnchor
;
808 String middleRegEx
= anyNumberOfCharacters
+ name
+ anyNumberOfCharacters
;
809 if (stringExists(beginningRegEx
, targetString
)) {
810 result
.add(NamePosition
.beginning
);
813 if (stringExists(endRegEx
, targetString
)) {
814 result
.add(NamePosition
.end
);
817 if (stringExists(middleRegEx
, targetString
)) {
818 if (result
.contains(NamePosition
.beginning
) && result
.contains(NamePosition
.end
)) {
819 result
.add(NamePosition
.alone
);
821 result
.add(NamePosition
.between
);
826 result
.add(NamePosition
.nowhere
);
830 // logger.error("Either name or targetString is NULL");
838 * @param beginningRegEx
839 * @param targetString
841 private static boolean stringExists(String regEx
, String targetString
) {
842 Pattern pattern
= Pattern
.compile(regEx
);
843 Matcher matcher
= pattern
.matcher(targetString
);
844 if (matcher
.find()) {
852 * Returns the <code>AuthorString</code> attribute.
853 * @param taxon The {@link TaxonBase Taxon}.
854 * @return The <code>AuthorString</code> attribute.
857 @SuppressWarnings("unused")
858 private static String
getAuthorString(TaxonNameBase taxonName
) {
859 String result
= null;
860 if (taxonName
!= null && taxonName
!= null) {
861 if (taxonName
.isInstanceOf(NonViralName
.class)) {
862 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
863 result
= nonViralName
.getAuthorshipCache();
865 logger
.warn("TaxonName is not of instance NonViralName: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
872 * Returns the <code>FullName</code> attribute.
873 * @param taxon The {@link TaxonBase Taxon}.
874 * @return The <code>FullName</code> attribute.
877 @SuppressWarnings("unused")
878 private static String
getFullName(TaxonNameBase taxonName
) {
879 if (taxonName
!= null) {
880 return taxonName
.getTitleCache();
887 * Returns the <code>NomRefString</code> attribute.
888 * @param taxon The {@link TaxonBase Taxon}.
889 * @return The <code>NomRefString</code> attribute.
892 @SuppressWarnings("unused")
893 private static String
getNomRefString(TaxonNameBase taxonName
) {
894 String result
= null;
895 if (taxonName
!= null) {
897 result
= taxonName
.getNomenclaturalMicroReference();
898 } catch (Exception e
) {
899 logger
.error("While getting NomRefString");
907 * Returns the <code>DisplayName</code> attribute.
908 * @param taxon The {@link TaxonBase Taxon}.
909 * @return The <code>DisplayName</code> attribute.
912 @SuppressWarnings("unused")
913 private static String
getDisplayName(TaxonNameBase taxonName
) {
915 if (taxonName
!= null) {
916 return taxonName
.getFullTitleCache();
923 * Returns the <code>FuzzyName</code> attribute.
924 * @param taxon The {@link TaxonBase Taxon}.
925 * @return The <code>FuzzyName</code> attribute.
928 @SuppressWarnings("unused")
929 private static String
getFuzzyName(TaxonNameBase taxonName
) {
935 * Returns the <code>NameStatusFk</code> attribute.
936 * @param taxon The {@link TaxonBase Taxon}.
937 * @return The <code>NameStatusFk</code> attribute.
940 @SuppressWarnings("unused")
941 private static Integer
getNameStatusFk(TaxonNameBase taxonName
) {
942 Integer result
= null;
945 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
946 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
947 Set
<NomenclaturalStatus
> states
= nonViralName
.getStatus();
948 if (states
.size() == 1) {
949 NomenclaturalStatus state
= states
.iterator().next();
950 NomenclaturalStatusType statusType
= null;
952 statusType
= state
.getType();
954 if (statusType
!= null) {
955 result
= PesiTransformer
.nomStatus2nomStatusFk(statusType
);
957 } else if (states
.size() > 1) {
958 logger
.error("This TaxonName has more than one Nomenclatural Status: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
962 } catch (Exception e
) {
969 * Returns the <code>NameStatusCache</code> attribute.
970 * @param taxon The {@link TaxonBase Taxon}.
971 * @return The <code>NameStatusCache</code> attribute.
974 @SuppressWarnings("unused")
975 private static String
getNameStatusCache(TaxonNameBase taxonName
) {
976 String result
= null;
979 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
980 NonViralName nonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
981 Set
<NomenclaturalStatus
> states
= nonViralName
.getStatus();
982 if (states
.size() == 1) {
983 NomenclaturalStatus state
= states
.iterator().next();
985 result
= PesiTransformer
.nomStatus2NomStatusCache(state
.getType());
987 } else if (states
.size() > 1) {
988 logger
.error("This TaxonName has more than one Nomenclatural Status: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
992 } catch (Exception e
) {
999 * Returns the <code>TaxonStatusFk</code> attribute.
1000 * @param taxon The {@link TaxonBase Taxon}.
1001 * @return The <code>TaxonStatusFk</code> attribute.
1004 @SuppressWarnings("unused")
1005 private static Integer
getTaxonStatusFk(TaxonNameBase taxonName
) {
1006 Integer result
= null;
1009 Set taxa
= taxonName
.getTaxa();
1010 if (taxa
.size() == 1) {
1011 result
= PesiTransformer
.taxonBase2statusFk((TaxonBase
<?
>) taxa
.iterator().next());
1012 } else if (taxa
.size() > 1) {
1013 logger
.warn("This TaxonName has " + taxa
.size() + " Taxa: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1016 Set synonyms
= taxonName
.getSynonyms();
1017 if (synonyms
.size() == 1) {
1018 result
= PesiTransformer
.taxonBase2statusFk((TaxonBase
<?
>) synonyms
.iterator().next());
1019 } else if (synonyms
.size() > 1) {
1020 logger
.warn("This TaxonName has " + synonyms
.size() + " Synonyms: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1023 } catch (Exception e
) {
1024 e
.printStackTrace();
1030 * Returns the <code>TaxonStatusCache</code> attribute.
1031 * @param taxon The {@link TaxonBase Taxon}.
1032 * @return The <code>TaxonStatusCache</code> attribute.
1035 @SuppressWarnings("unused")
1036 private static String
getTaxonStatusCache(TaxonNameBase taxonName
) {
1037 String result
= null;
1040 Set taxa
= taxonName
.getTaxa();
1041 if (taxa
.size() == 1) {
1042 result
= PesiTransformer
.taxonBase2statusCache((TaxonBase
<?
>) taxa
.iterator().next());
1043 } else if (taxa
.size() > 1) {
1044 logger
.warn("This TaxonName has " + taxa
.size() + " Taxa: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1047 Set synonyms
= taxonName
.getSynonyms();
1048 if (synonyms
.size() == 1) {
1049 result
= PesiTransformer
.taxonBase2statusCache((TaxonBase
<?
>) synonyms
.iterator().next());
1050 } else if (synonyms
.size() > 1) {
1051 logger
.warn("This TaxonName has " + synonyms
.size() + " Synonyms: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1054 } catch (Exception e
) {
1055 e
.printStackTrace();
1061 * Returns the <code>TypeNameFk</code> attribute.
1062 * @param taxon The {@link TaxonBase Taxon}.
1063 * @return The <code>TypeNameFk</code> attribute.
1066 @SuppressWarnings("unused")
1067 private static Integer
getTypeNameFk(TaxonNameBase taxonNameBase
, PesiExportState state
) {
1068 Integer result
= null;
1069 // if (taxonNameBase != null) {
1070 // Set<NameTypeDesignation> nameTypeDesignations = taxonNameBase.getNameTypeDesignations();
1071 // if (nameTypeDesignations.size() == 1) {
1072 // NameTypeDesignation nameTypeDesignation = nameTypeDesignations.iterator().next();
1073 // if (nameTypeDesignation != null) {
1074 // TaxonNameBase typeName = nameTypeDesignation.getTypeName();
1075 // if (typeName != null) {
1076 // Set<TaxonBase> taxa = typeName.getTaxa();
1077 // if (taxa.size() == 1) {
1078 // TaxonBase singleTaxon = taxa.iterator().next();
1079 // result = state.getDbId(singleTaxon.getName());
1080 // } else if (taxa.size() > 1) {
1081 // logger.warn("This TaxonName has " + taxa.size() + " Taxa: " + taxonNameBase.getUuid() + " (" + taxonNameBase.getTitleCache() + ")");
1085 // } else if (nameTypeDesignations.size() > 1) {
1086 // logger.warn("This TaxonName has " + nameTypeDesignations.size() + " NameTypeDesignations: " + taxonNameBase.getUuid() + " (" + taxonNameBase.getTitleCache() + ")");
1089 // if (result != null) {
1090 // logger.error("Taxon Id: " + result);
1091 // logger.error("TaxonName: " + taxonNameBase.getUuid() + " (" + taxonNameBase.getTitleCache() +")");
1097 * Returns the <code>TypeFullnameCache</code> attribute.
1098 * @param taxon The {@link TaxonBase Taxon}.
1099 * @return The <code>TypeFullnameCache</code> attribute.
1102 @SuppressWarnings("unused")
1103 private static String
getTypeFullnameCache(TaxonNameBase taxonName
) {
1104 String result
= null;
1107 if (taxonName
!= null) {
1108 Set
<NameTypeDesignation
> nameTypeDesignations
= taxonName
.getNameTypeDesignations();
1109 if (nameTypeDesignations
.size() == 1) {
1110 NameTypeDesignation nameTypeDesignation
= nameTypeDesignations
.iterator().next();
1111 if (nameTypeDesignation
!= null) {
1112 TaxonNameBase typeName
= nameTypeDesignation
.getTypeName();
1113 if (typeName
!= null) {
1114 result
= typeName
.getTitleCache();
1117 } else if (nameTypeDesignations
.size() > 1) {
1118 logger
.warn("This TaxonName has " + nameTypeDesignations
.size() + " NameTypeDesignations: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1122 } catch (Exception e
) {
1123 e
.printStackTrace();
1129 * Returns the <code>QualityStatusFk</code> attribute.
1130 * @param taxon The {@link TaxonBase Taxon}.
1131 * @return The <code>QualityStatusFk</code> attribute.
1134 @SuppressWarnings("unused")
1135 private static Integer
getQualityStatusFk(TaxonNameBase taxonName
) {
1136 // TODO: Not represented in CDM right now. Depends on import.
1137 Integer result
= null;
1142 * Returns the <code>QualityStatusCache</code> attribute.
1143 * @param taxon The {@link TaxonBase Taxon}.
1144 * @return The <code>QualityStatusCache</code> attribute.
1147 @SuppressWarnings("unused")
1148 private static String
getQualityStatusCache(TaxonNameBase taxonName
) {
1149 // TODO: Not represented in CDM right now. Depends on import.
1150 String result
= null;
1155 * Returns the <code>TypeDesignationStatusFk</code> attribute.
1156 * @param taxon The {@link TaxonBase Taxon}.
1157 * @return The <code>TypeDesignationStatusFk</code> attribute.
1160 @SuppressWarnings("unused")
1161 private static Integer
getTypeDesignationStatusFk(TaxonNameBase taxonName
) {
1162 Integer result
= null;
1165 if (taxonName
!= null) {
1166 Set
<NameTypeDesignation
> typeDesignations
= taxonName
.getNameTypeDesignations();
1167 if (typeDesignations
.size() == 1) {
1168 Object obj
= typeDesignations
.iterator().next().getTypeStatus();
1169 NameTypeDesignationStatus designationStatus
= CdmBase
.deproxy(obj
, NameTypeDesignationStatus
.class);
1170 result
= PesiTransformer
.nameTypeDesignationStatus2TypeDesignationStatusId(designationStatus
);
1171 } else if (typeDesignations
.size() > 1) {
1172 logger
.error("Found a TaxonName with more than one NameTypeDesignation: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1176 } catch (Exception e
) {
1177 e
.printStackTrace();
1183 * Returns the <code>TypeDesignationStatusCache</code> attribute.
1184 * @param taxon The {@link TaxonBase Taxon}.
1185 * @return The <code>TypeDesignationStatusCache</code> attribute.
1188 @SuppressWarnings("unused")
1189 private static String
getTypeDesignationStatusCache(TaxonNameBase taxonName
) {
1190 String result
= null;
1193 if (taxonName
!= null) {
1194 Set
<NameTypeDesignation
> typeDesignations
= taxonName
.getNameTypeDesignations();
1195 if (typeDesignations
.size() == 1) {
1196 Object obj
= typeDesignations
.iterator().next().getTypeStatus();
1197 NameTypeDesignationStatus designationStatus
= CdmBase
.deproxy(obj
, NameTypeDesignationStatus
.class);
1198 result
= PesiTransformer
.nameTypeDesignationStatus2TypeDesignationStatusCache(designationStatus
);
1199 } else if (typeDesignations
.size() > 1) {
1200 logger
.error("Found a TaxonName with more than one NameTypeDesignation: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1204 } catch (Exception e
) {
1205 e
.printStackTrace();
1211 * Returns the <code>FossilStatusFk</code> attribute.
1212 * @param taxon The {@link TaxonBase Taxon}.
1213 * @return The <code>FossilStatusFk</code> attribute.
1216 @SuppressWarnings("unused")
1217 private static Integer
getFossilStatusFk(TaxonNameBase taxonNameBase
) {
1218 Integer result
= null;
1220 // if (taxonBase.isInstanceOf(Taxon.class)) {
1221 // taxon = CdmBase.deproxy(taxonBase, Taxon.class);
1222 // Set<TaxonDescription> specimenDescription = taxon.;
1223 // result = PesiTransformer.fossil2FossilStatusId(fossil);
1229 * Returns the <code>FossilStatusCache</code> attribute.
1230 * @param taxon The {@link TaxonBase Taxon}.
1231 * @return The <code>FossilStatusCache</code> attribute.
1234 @SuppressWarnings("unused")
1235 private static String
getFossilStatusCache(TaxonNameBase taxonName
) {
1237 String result
= null;
1242 * Returns the <code>IdInSource</code> attribute.
1243 * @param taxon The {@link TaxonBase Taxon}.
1244 * @return The <code>IdInSource</code> attribute.
1247 @SuppressWarnings("unused")
1248 private static String
getIdInSource(TaxonNameBase taxonName
) {
1250 // String result = "Nominal Taxon from TAX_ID: ";
1251 // boolean set = false;
1254 // if (taxon != null) {
1255 // TaxonNameBase taxonName = taxon.getName();
1256 // Set taxa = taxonName.getTaxa();
1257 // if (taxa.size() == 1) {
1258 // IdentifiableEntity singleTaxon = (IdentifiableEntity) taxa.iterator().next();
1259 // if (singleTaxon != null) {
1260 // Set<IdentifiableSource> sources = singleTaxon.getSources();
1261 // if (sources.size() == 1) {
1262 // IdentifiableSource source = sources.iterator().next();
1263 // if (source != null) {
1264 // result += source.getIdInSource();
1267 // } else if (sources.size() > 1) {
1268 // logger.warn("Taxon has multiple IdentifiableSources: " + singleTaxon.getUuid() + " (" + singleTaxon.getTitleCache() + ")");
1270 // for (IdentifiableSource source : sources) {
1271 // result += source.getIdInSource();
1272 // if (count < sources.size()) {
1282 // } else if (taxa.size() > 1) {
1283 // logger.warn("This TaxonName has " + taxa.size() + " Taxa: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() +")");
1287 // } catch (Exception e) {
1288 // e.printStackTrace();
1299 * Returns the <code>GUID</code> attribute.
1300 * @param taxon The {@link TaxonBase Taxon}.
1301 * @return The <code>GUID</code> attribute.
1304 @SuppressWarnings("unused")
1305 private static String
getGUID(TaxonNameBase taxonName
) {
1307 String result
= null;
1312 * Returns the <code>DerivedFromGuid</code> attribute.
1313 * @param taxon The {@link TaxonBase Taxon}.
1314 * @return The <code>DerivedFromGuid</code> attribute.
1317 @SuppressWarnings("unused")
1318 private static String
getDerivedFromGuid(TaxonNameBase taxonName
) {
1320 String result
= null;
1325 * Returns the <code>OriginalDB</code> attribute.
1326 * @param taxon The {@link TaxonBase Taxon}.
1327 * @return The <code>OriginalDB</code> attribute.
1330 @SuppressWarnings("unused")
1331 private static String
getOriginalDB(TaxonNameBase taxonName
) {
1334 IdentifiableEntity taxonBase
= null;
1335 Set taxa
= taxonName
.getTaxa();
1336 if (taxa
.size() == 1) {
1337 taxonBase
= (IdentifiableEntity
) taxa
.iterator().next();
1338 } else if (taxa
.size() > 1) {
1339 logger
.warn("This TaxonName has " + taxa
.size() + " Taxa: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() +")");
1341 Set synonyms
= taxonName
.getSynonyms();
1342 if (synonyms
.size() == 1) {
1343 taxonBase
= (IdentifiableEntity
) synonyms
.iterator().next();
1344 } else if (synonyms
.size() > 1) {
1345 logger
.warn("This TaxonName has " + synonyms
.size() + " Synonyms: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() +")");
1348 if (taxonBase
!= null) {
1349 Set
<IdentifiableSource
> sources
= taxonBase
.getSources();
1350 if (sources
.size() == 1) {
1351 IdentifiableSource source
= sources
.iterator().next();
1352 if (source
!= null) {
1353 ReferenceBase citation
= source
.getCitation();
1354 if (citation
!= null) {
1355 result
= PesiTransformer
.databaseString2Abbreviation(citation
.getTitleCache());
1358 } else if (sources
.size() > 1) {
1359 logger
.warn("Taxon has multiple IdentifiableSources: " + taxonBase
.getUuid() + " (" + taxonBase
.getTitleCache() + ")");
1361 for (IdentifiableSource source
: sources
) {
1362 result
+= PesiTransformer
.databaseString2Abbreviation(source
.getCitation().getTitleCache());
1363 if (count
< sources
.size()) {
1373 } catch (Exception e
) {
1374 e
.printStackTrace();
1376 if ("".equals(result
)) {
1384 * Returns the <code>LastAction</code> attribute.
1385 * @param taxon The {@link TaxonBase Taxon}.
1386 * @return The <code>LastAction</code> attribute.
1389 @SuppressWarnings("unused")
1390 private static String
getLastAction(TaxonNameBase taxonName
) {
1396 * Returns the <code>LastActionDate</code> attribute.
1397 * @param taxon The {@link TaxonBase Taxon}.
1398 * @return The <code>LastActionDate</code> attribute.
1401 @SuppressWarnings("unused")
1402 private static DateTime
getLastActionDate(TaxonNameBase taxonNameBase
) {
1403 DateTime result
= null;
1406 if (taxonNameBase
!= null) {
1407 VersionableEntity taxonBase
= null;
1408 Set taxa
= taxonNameBase
.getTaxa();
1409 if (taxa
.size() == 1) {
1410 taxonBase
= (VersionableEntity
) taxa
.iterator().next();
1411 } else if (taxa
.size() > 1) {
1412 logger
.warn("This TaxonName has " + taxa
.size() + " Taxa: " + taxonNameBase
.getUuid() + " (" + taxonNameBase
.getTitleCache() + ")");
1414 Set synonyms
= taxonNameBase
.getSynonyms();
1415 if (synonyms
.size() == 1) {
1416 taxonBase
= (VersionableEntity
) synonyms
.iterator().next();
1417 } else if (synonyms
.size() > 1) {
1418 logger
.warn("This TaxonName has " + synonyms
.size() + " Synonyms: " + taxonNameBase
.getUuid() + " (" + taxonNameBase
.getTitleCache() + ")");
1421 if (taxonBase
!= null) {
1422 DateTime updated
= taxonBase
.getUpdated();
1423 if (updated
!= null) {
1424 // logger.error("Taxon Updated: " + updated);
1425 result
= new DateTime(updated
.toDate()); // Unfortunately the time information gets lost here.
1430 } catch (Exception e
) {
1431 e
.printStackTrace();
1437 * Returns the <code>ExpertName</code> attribute.
1438 * @param taxon The {@link TaxonBase Taxon}.
1439 * @return The <code>ExpertName</code> attribute.
1442 @SuppressWarnings("unused")
1443 private static String
getExpertName(TaxonNameBase taxonName
) {
1449 * Returns the <code>ExpertFk</code> attribute.
1450 * @param taxon The {@link TaxonBase Taxon}.
1451 * @return The <code>ExpertFk</code> attribute.
1454 @SuppressWarnings("unused")
1455 private static String
getExpertFk(TaxonNameBase taxonName
) {
1460 @SuppressWarnings("unused")
1461 private static Integer
getSourceFk(TaxonNameBase taxonName
, PesiExportState state
) {
1462 Integer result
= null;
1465 TaxonBase taxonBase
= null;
1466 Set taxa
= taxonName
.getTaxa();
1467 if (taxa
.size() == 1) {
1468 taxonBase
= CdmBase
.deproxy(taxa
.iterator().next(), TaxonBase
.class);
1469 } else if (taxa
.size() > 1) {
1470 logger
.warn("This TaxonName has " + taxa
.size() + " Taxa: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1473 Set synonyms
= taxonName
.getSynonyms();
1474 if (synonyms
.size() == 1) {
1475 taxonBase
= CdmBase
.deproxy(synonyms
.iterator().next(), TaxonBase
.class);
1476 } else if (synonyms
.size() > 1) {
1477 logger
.warn("This TaxonName has " + synonyms
.size() + " Synonyms: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
1480 if (taxonBase
!= null) {
1481 result
= state
.getDbId(taxonBase
.getSec());
1483 } catch (Exception e
) {
1484 e
.printStackTrace();
1490 * Returns the CDM to PESI specific export mappings.
1491 * @return The {@link PesiExportMapping PesiExportMapping}.
1493 private PesiExportMapping
getMapping() {
1494 PesiExportMapping mapping
= new PesiExportMapping(dbTableName
);
1496 mapping
.addMapper(IdMapper
.NewInstance("TaxonId"));
1497 mapping
.addMapper(MethodMapper
.NewInstance("SourceFK", this.getClass(), "getSourceFk", standardMethodParameter
, PesiExportState
.class));
1498 // mapping.addMapper(MethodMapper.NewInstance("KingdomFk", this.getClass(), "getKingdomFk", standardMethodParameter, PesiExportState.class));
1499 // mapping.addMapper(MethodMapper.NewInstance("RankFk", this));
1500 // mapping.addMapper(MethodMapper.NewInstance("RankCache", this));
1501 mapping
.addMapper(MethodMapper
.NewInstance("GenusOrUninomial", this));
1502 mapping
.addMapper(MethodMapper
.NewInstance("InfraGenericEpithet", this));
1503 mapping
.addMapper(MethodMapper
.NewInstance("SpecificEpithet", this));
1504 mapping
.addMapper(MethodMapper
.NewInstance("InfraSpecificEpithet", this));
1505 mapping
.addMapper(MethodMapper
.NewInstance("WebSearchName", this));
1506 mapping
.addMapper(MethodMapper
.NewInstance("WebShowName", this));
1507 mapping
.addMapper(MethodMapper
.NewInstance("AuthorString", this));
1508 mapping
.addMapper(MethodMapper
.NewInstance("FullName", this));
1509 mapping
.addMapper(MethodMapper
.NewInstance("NomRefString", this));
1510 mapping
.addMapper(MethodMapper
.NewInstance("DisplayName", this));
1511 mapping
.addMapper(MethodMapper
.NewInstance("FuzzyName", this));
1512 mapping
.addMapper(MethodMapper
.NewInstance("NameStatusFk", this));
1513 mapping
.addMapper(MethodMapper
.NewInstance("NameStatusCache", this));
1514 mapping
.addMapper(MethodMapper
.NewInstance("TaxonStatusFk", this));
1515 mapping
.addMapper(MethodMapper
.NewInstance("TaxonStatusCache", this));
1516 mapping
.addMapper(MethodMapper
.NewInstance("TypeNameFk", this.getClass(), "getTypeNameFk", standardMethodParameter
, PesiExportState
.class));
1517 mapping
.addMapper(MethodMapper
.NewInstance("TypeFullnameCache", this));
1518 mapping
.addMapper(MethodMapper
.NewInstance("QualityStatusFk", this));
1519 mapping
.addMapper(MethodMapper
.NewInstance("QualityStatusCache", this));
1520 mapping
.addMapper(MethodMapper
.NewInstance("TypeDesignationStatusFk", this));
1521 mapping
.addMapper(MethodMapper
.NewInstance("TypeDesignationStatusCache", this));
1522 // mapping.addMapper(MethodMapper.NewInstance("TreeIndex", this.getClass(), "getTreeIndex", standardMethodParameter, PesiExportState.class));
1523 mapping
.addMapper(MethodMapper
.NewInstance("FossilStatusFk", this));
1524 mapping
.addMapper(MethodMapper
.NewInstance("FossilStatusCache", this));
1525 mapping
.addMapper(MethodMapper
.NewInstance("IdInSource", this));
1526 mapping
.addMapper(MethodMapper
.NewInstance("GUID", this)); // TODO
1527 mapping
.addMapper(MethodMapper
.NewInstance("DerivedFromGuid", this)); // TODO
1528 mapping
.addMapper(MethodMapper
.NewInstance("OriginalDB", this));
1529 mapping
.addMapper(MethodMapper
.NewInstance("LastAction", this));
1530 // mapping.addMapper(DbTimePeriodMapper.NewInstance("updated", "LastActionDate"));
1531 mapping
.addMapper(MethodMapper
.NewInstance("LastActionDate", this));
1532 mapping
.addMapper(MethodMapper
.NewInstance("ExpertName", this)); // TODO
1533 mapping
.addMapper(MethodMapper
.NewInstance("ExpertFk", this)); // TODO