2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.app
.wp6
.palmae
;
12 import java
.util
.List
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.springframework
.transaction
.TransactionStatus
;
19 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
20 import eu
.etaxonomy
.cdm
.app
.common
.CdmDestinations
;
21 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
22 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
23 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
24 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
25 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
26 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.IReference
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
40 public class PalmaePostImportUpdater
{
41 private static final Logger logger
= Logger
.getLogger(PalmaePostImportUpdater
.class);
43 static final ICdmDataSource cdmDestination
= CdmDestinations
.localH2Palmae();
46 private String relationships
= "relationships";
47 private String taxonomicAccounts
= "taxonomic accounts";
48 private String fossilRecord
= "fossil record";
50 public boolean updateMissingFeatures(ICdmDataSource dataSource
) {
53 UUID featureTreeUuid
= PalmaeActivator
.featureTreeUuid
;
54 CdmApplicationController cdmApp
= CdmApplicationController
.NewInstance(dataSource
, DbSchemaValidation
.VALIDATE
);
56 TransactionStatus tx
= cdmApp
.startTransaction();
58 FeatureTree tree
= cdmApp
.getFeatureTreeService().find(featureTreeUuid
);
59 FeatureNode root
= tree
.getRoot();
61 List
<Feature
> featureList
= cdmApp
.getTermService().list(Feature
.class, null, null, null, null);
62 for (Feature feature
: featureList
){
63 String label
= feature
.getLabel();
64 if (relationships
.equals(label
)){
65 FeatureNode newNode
= FeatureNode
.NewInstance(feature
);
66 root
.addChild(newNode
);
68 }else if(taxonomicAccounts
.equals(label
)){
69 FeatureNode newNode
= FeatureNode
.NewInstance(feature
);
70 root
.addChild(newNode
);
72 }else if(fossilRecord
.equals(label
)){
73 FeatureNode newNode
= FeatureNode
.NewInstance(feature
);
74 root
.addChild(newNode
);
78 cdmApp
.commitTransaction(tx
);
80 logger
.warn("Did not find 3 additional features but " + count
);
83 logger
.info("Feature tree updated!");
85 } catch (Exception e
) {
87 logger
.error("ERROR in feature tree update");
93 public boolean updateNameUsage(ICdmDataSource dataSource
) {
95 boolean result
= true;
96 CdmApplicationController cdmApp
= CdmApplicationController
.NewInstance(dataSource
, DbSchemaValidation
.VALIDATE
);
98 TransactionStatus tx
= cdmApp
.startTransaction();
101 int count
= cdmApp
.getTaxonService().count(Taxon
.class);
102 List
<TaxonBase
> taxonList
= cdmApp
.getTaxonService().list(TaxonBase
.class, 100000, page
, null, null);
105 IReference treatmentReference
= (IReference
) cdmApp
.getCommonService().getSourcedObjectByIdInSource(Reference
.class, "palm_pub_ed_999999", "PublicationCitation");
106 if (treatmentReference
== null){
107 logger
.error("Treatment reference could not be found");
110 for (TaxonBase nameUsage
: taxonList
){
111 if ((i
++ % 100) == 0){System
.out
.println(i
);};
114 //if not in treatment
115 if (! isInTreatment(nameUsage
, treatmentReference
, false)){
116 //if connected treatment taxon can be found
117 Taxon acceptedTaxon
= getAcceptedTreatmentTaxon(nameUsage
, treatmentReference
);
118 if (acceptedTaxon
!= null){
119 //add as citation and delete
120 addNameUsage(acceptedTaxon
, nameUsage
);
121 cdmApp
.getTaxonService().delete(nameUsage
);
123 logger
.warn("Non treatment taxon has no accepted taxon in treatment: " + nameUsage
+ " (" + nameUsage
.getId() +")" );
126 } catch (Exception e
) {
132 //add citation feature to feature tree
133 UUID featureTreeUuid
= PalmaeActivator
.featureTreeUuid
;
134 FeatureTree tree
= cdmApp
.getFeatureTreeService().find(featureTreeUuid
);
135 FeatureNode root
= tree
.getRoot();
136 List
<Feature
> featureList
= cdmApp
.getTermService().list(Feature
.class, null, null, null, null);
138 for (Feature feature
: featureList
){
139 if (feature
.equals(Feature
.CITATION())){
140 FeatureNode newNode
= FeatureNode
.NewInstance(feature
);
141 root
.addChild(newNode
);
146 logger
.warn("Did not add exactly 1 features to the feature tree but " + count
);
150 cdmApp
.commitTransaction(tx
);
151 logger
.info("NameUsage updated!");
153 } catch (Exception e
) {
155 logger
.error("ERROR in name usage update");
165 private Taxon
getAcceptedTreatmentTaxon(TaxonBase nameUsage
, IReference treatmentReference
) {
166 boolean hasSynonymInTreatment
= false;
167 TaxonName name
= nameUsage
.getName();
168 Set
<TaxonBase
> candidateList
= name
.getTaxonBases();
169 for (TaxonBase candidate
: candidateList
){
170 if (candidate
instanceof Taxon
){
171 if (isInTreatment(candidate
, treatmentReference
, false)){
172 return (Taxon
)candidate
;
174 }else if (candidate
instanceof Synonym
){
175 Synonym synonym
= (Synonym
)candidate
;
176 Taxon accTaxon
= synonym
.getAcceptedTaxon();
177 if (isInTreatment(synonym
, treatmentReference
, true)){
178 hasSynonymInTreatment
= true;
180 if (accTaxon
!= null){
181 if (isInTreatment(accTaxon
, treatmentReference
, false)){
186 throw new IllegalStateException("TaxonBase should be either a Taxon or a Synonym but was " + nameUsage
.getClass().getName());
189 if (hasSynonymInTreatment
){
190 logger
.warn("Non treatment taxon has synonym in treatment but no accepted taxon: " + nameUsage
+ " (" + nameUsage
.getId() +")" );
197 * @param treatmentReference
200 private boolean isInTreatment(TaxonBase taxonBase
, IReference treatmentReference
, boolean silent
) {
201 if (taxonBase
.getSec().equals(treatmentReference
)){
204 if (taxonBase
instanceof Taxon
){
205 if (((Taxon
)taxonBase
).getTaxonNodes().size()< 1){
206 logger
.warn("Taxon has treatment sec but is not in tree: " + taxonBase
+ " (" + taxonBase
.getId() +")" );
208 }else if (taxonBase
instanceof Synonym
){
209 Synonym synonym
= (Synonym
)taxonBase
;
210 boolean hasAccTaxonInTreatment
= false;
211 Taxon accTaxon
= synonym
.getAcceptedTaxon();
212 if (accTaxon
!= null){
213 hasAccTaxonInTreatment
|= isInTreatment(accTaxon
, treatmentReference
, false);
215 if (hasAccTaxonInTreatment
== false){
216 logger
.warn("Synonym has treatment reference but has no accepted taxon in tree: " + taxonBase
+ " (" + taxonBase
.getId() +")" );
219 throw new IllegalStateException("TaxonBase should be either Taxon or Synonym");
224 //taxon not in treatment
226 if (taxonBase
instanceof Taxon
){
227 if (((Taxon
)taxonBase
).getTaxonNodes().size()> 0){
228 logger
.warn("Taxon has no treatment sec but is in tree: " + taxonBase
+ " (" + taxonBase
.getId() +")" );
230 }else if (taxonBase
instanceof Synonym
){
231 Synonym synonym
= (Synonym
)taxonBase
;
232 boolean hasAccTaxonInTreatment
= false;
233 Taxon accTaxon
= synonym
.getAcceptedTaxon();
234 if (accTaxon
!= null){
235 hasAccTaxonInTreatment
|= isInTreatment(accTaxon
, treatmentReference
, false);
237 if (hasAccTaxonInTreatment
== true){
238 logger
.warn("Synonym has no treatment reference but has accepted taxon in treatment: " + taxonBase
+ " (" + taxonBase
.getId() +")" );
241 throw new IllegalStateException("TaxonBase should be either Taxon or Synonym but was ");
249 * @param taxonCandidate
252 private boolean addNameUsage(Taxon taxon
, TaxonBase nameUsageTaxon
) {
253 TaxonDescription myDescription
= null;
254 for (TaxonDescription desc
: taxon
.getDescriptions()){
255 if (! desc
.isImageGallery()){
256 myDescription
= desc
;
260 if (myDescription
== null){
263 TextData textData
= TextData
.NewInstance(Feature
.CITATION());
264 //creates text (name: reference)
265 //textData.putText(nameUsageTaxon.getName().getTitleCache()+": " + nameUsageTaxon.getSec().getTitleCache(), Language.DEFAULT());
266 textData
.addSource(OriginalSourceType
.PrimaryTaxonomicSource
, null, null, nameUsageTaxon
.getSec(), null, nameUsageTaxon
.getName(), nameUsageTaxon
.getName().getTitleCache());
267 myDescription
.addElement(textData
);
275 public static void main(String
[] args
) {
276 PalmaePostImportUpdater updater
= new PalmaePostImportUpdater();
278 updater
.updateMissingFeatures(cdmDestination
);
279 updater
.updateNameUsage(cdmDestination
);
280 } catch (Exception e
) {
282 logger
.error("ERROR in feature tree update");