3 * Copyright (C) 2007 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.
11 package eu
.etaxonomy
.cdm
.app
.wp6
.palmae
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.transaction
.TransactionStatus
;
20 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
21 import eu
.etaxonomy
.cdm
.app
.common
.CdmDestinations
;
22 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
23 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
24 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
26 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
27 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
29 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.IReferenceBase
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
42 public class PalmaePostImportUpdater
{
43 private static final Logger logger
= Logger
.getLogger(PalmaePostImportUpdater
.class);
45 static final ICdmDataSource cdmDestination
= CdmDestinations
.localH2Palmae();
48 private String relationships
= "relationships";
49 private String taxonomicAccounts
= "taxonomic accounts";
50 private String fossilRecord
= "fossil record";
52 public boolean updateMissingFeatures(ICdmDataSource dataSource
) {
55 UUID featureTreeUuid
= PalmaeActivator
.featureTreeUuid
;
56 CdmApplicationController cdmApp
= CdmApplicationController
.NewInstance(dataSource
, DbSchemaValidation
.VALIDATE
);
58 TransactionStatus tx
= cdmApp
.startTransaction();
60 FeatureTree tree
= cdmApp
.getFeatureTreeService().find(featureTreeUuid
);
61 FeatureNode root
= tree
.getRoot();
63 List
<DefinedTermBase
> featureList
= cdmApp
.getTermService().list(Feature
.class, null, null, null, null);
64 for (DefinedTermBase feature
: featureList
){
65 String label
= feature
.getLabel();
66 if (relationships
.equals(label
)){
67 FeatureNode newNode
= FeatureNode
.NewInstance((Feature
)feature
);
68 root
.addChild(newNode
);
70 }else if(taxonomicAccounts
.equals(label
)){
71 FeatureNode newNode
= FeatureNode
.NewInstance((Feature
)feature
);
72 root
.addChild(newNode
);
74 }else if(fossilRecord
.equals(label
)){
75 FeatureNode newNode
= FeatureNode
.NewInstance((Feature
)feature
);
76 root
.addChild(newNode
);
80 cdmApp
.commitTransaction(tx
);
82 logger
.warn("Did not find 3 additional features but " + count
);
85 logger
.info("Feature tree updated!");
87 } catch (Exception e
) {
89 logger
.error("ERROR in feature tree update");
95 public boolean updateNameUsage(ICdmDataSource dataSource
) {
97 boolean result
= true;
98 CdmApplicationController cdmApp
= CdmApplicationController
.NewInstance(dataSource
, DbSchemaValidation
.VALIDATE
);
100 TransactionStatus tx
= cdmApp
.startTransaction();
103 int count
= cdmApp
.getTaxonService().count(Taxon
.class);
104 List
<TaxonBase
> taxonList
= cdmApp
.getTaxonService().list(TaxonBase
.class, 100000, page
, null, null);
107 IReferenceBase treatmentReference
= (IReferenceBase
) cdmApp
.getCommonService().getSourcedObjectByIdInSource(ReferenceBase
.class, "palm_pub_ed_999999", "PublicationCitation");
108 if (treatmentReference
== null){
109 logger
.error("Treatment reference could not be found");
112 for (TaxonBase nameUsage
: taxonList
){
113 if ((i
++ % 100) == 0){System
.out
.println(i
);};
116 //if not in treatment
117 if (! isInTreatment(nameUsage
, treatmentReference
, false)){
118 //if connected treatment taxon can be found
119 Taxon acceptedTaxon
= getAcceptedTreatmentTaxon(nameUsage
, treatmentReference
);
120 if (acceptedTaxon
!= null){
121 //add as citation and delete
122 addNameUsage(acceptedTaxon
, nameUsage
);
123 cdmApp
.getTaxonService().delete(nameUsage
);
125 logger
.warn("Non treatment taxon has no accepted taxon in treatment: " + nameUsage
+ " (" + nameUsage
.getId() +")" );
128 } catch (Exception e
) {
134 //add citation feature to feature tree
135 UUID featureTreeUuid
= PalmaeActivator
.featureTreeUuid
;
136 FeatureTree tree
= cdmApp
.getFeatureTreeService().find(featureTreeUuid
);
137 FeatureNode root
= tree
.getRoot();
138 List
<DefinedTermBase
> featureList
= cdmApp
.getTermService().list(Feature
.class, null, null, null, null);
140 for (DefinedTermBase feature
: featureList
){
141 if (feature
.equals(Feature
.CITATION())){
142 FeatureNode newNode
= FeatureNode
.NewInstance((Feature
)feature
);
143 root
.addChild(newNode
);
148 logger
.warn("Did not add exactly 1 features to the feature tree but " + count
);
152 cdmApp
.commitTransaction(tx
);
153 logger
.info("NameUsage updated!");
155 } catch (Exception e
) {
157 logger
.error("ERROR in name usage update");
167 private Taxon
getAcceptedTreatmentTaxon(TaxonBase nameUsage
, IReferenceBase treatmentReference
) {
168 boolean hasSynonymInTreatment
= false;
169 TaxonNameBase name
= nameUsage
.getName();
170 Set
<TaxonBase
> candidateList
= name
.getTaxonBases();
171 for (TaxonBase candidate
: candidateList
){
172 if (candidate
instanceof Taxon
){
173 if (isInTreatment(candidate
, treatmentReference
, false)){
174 return (Taxon
)candidate
;
176 }else if (candidate
instanceof Synonym
){
177 Synonym synonym
= (Synonym
)candidate
;
178 Set
<Taxon
> accTaxa
= synonym
.getAcceptedTaxa();
179 if (isInTreatment(synonym
, treatmentReference
, true)){
180 hasSynonymInTreatment
= true;
182 for (Taxon accTaxon
: accTaxa
){
183 if (isInTreatment(accTaxon
, treatmentReference
, false)){
188 throw new IllegalStateException("TaxonBase should be either a Taxon or a Synonym but was " + nameUsage
.getClass().getName());
191 if (hasSynonymInTreatment
){
192 logger
.warn("Non treatment taxon has synonym in treatment but no accepted taxon: " + nameUsage
+ " (" + nameUsage
.getId() +")" );
199 * @param treatmentReference
202 private boolean isInTreatment(TaxonBase taxonBase
, IReferenceBase treatmentReference
, boolean silent
) {
203 if (taxonBase
.getSec().equals(treatmentReference
)){
206 if (taxonBase
instanceof Taxon
){
207 if (((Taxon
)taxonBase
).getTaxonNodes().size()< 1){
208 logger
.warn("Taxon has treatment sec but is not in tree: " + taxonBase
+ " (" + taxonBase
.getId() +")" );
210 }else if (taxonBase
instanceof Synonym
){
211 Synonym synonym
= (Synonym
)taxonBase
;
212 boolean hasAccTaxonInTreatment
= false;
213 for (Taxon accTaxon
: synonym
.getAcceptedTaxa()){
214 hasAccTaxonInTreatment
|= isInTreatment(accTaxon
, treatmentReference
, false);
216 if (hasAccTaxonInTreatment
== false){
217 logger
.warn("Synonym has treatment reference but has no accepted taxon in tree: " + taxonBase
+ " (" + taxonBase
.getId() +")" );
220 throw new IllegalStateException("TaxonBase should be either Taxon or Synonym");
225 //taxon not in treatment
227 if (taxonBase
instanceof Taxon
){
228 if (((Taxon
)taxonBase
).getTaxonNodes().size()> 0){
229 logger
.warn("Taxon has no treatment sec but is in tree: " + taxonBase
+ " (" + taxonBase
.getId() +")" );
231 }else if (taxonBase
instanceof Synonym
){
232 Synonym synonym
= (Synonym
)taxonBase
;
233 boolean hasAccTaxonInTreatment
= false;
234 for (Taxon accTaxon
: synonym
.getAcceptedTaxa()){
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(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");