(no commit message)
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / app / wp6 / palmae / PalmaePostImportUpdater.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.app.wp6.palmae;
12
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.springframework.transaction.TransactionStatus;
19
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;
36
37 /**
38 * @author a.mueller
39 * @created 01.10.2009
40 * @version 1.0
41 */
42 public class PalmaePostImportUpdater {
43 private static final Logger logger = Logger.getLogger(PalmaePostImportUpdater.class);
44
45 static final ICdmDataSource cdmDestination = CdmDestinations.localH2Palmae();
46
47
48 private String relationships = "relationships";
49 private String taxonomicAccounts = "taxonomic accounts";
50 private String fossilRecord = "fossil record";
51
52 public boolean updateMissingFeatures(ICdmDataSource dataSource) {
53 try{
54 int count = 0;
55 UUID featureTreeUuid = PalmaeActivator.featureTreeUuid;
56 CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
57
58 TransactionStatus tx = cdmApp.startTransaction();
59
60 FeatureTree tree = cdmApp.getFeatureTreeService().find(featureTreeUuid);
61 FeatureNode root = tree.getRoot();
62
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);
69 count++;
70 }else if(taxonomicAccounts.equals(label)){
71 FeatureNode newNode = FeatureNode.NewInstance((Feature)feature);
72 root.addChild(newNode);
73 count++;
74 }else if(fossilRecord.equals(label)){
75 FeatureNode newNode = FeatureNode.NewInstance((Feature)feature);
76 root.addChild(newNode);
77 count++;
78 }
79 }
80 cdmApp.commitTransaction(tx);
81 if (count != 3){
82 logger.warn("Did not find 3 additional features but " + count);
83 return false;
84 }
85 logger.info("Feature tree updated!");
86 return true;
87 } catch (Exception e) {
88 e.printStackTrace();
89 logger.error("ERROR in feature tree update");
90 return false;
91 }
92
93 }
94
95 public boolean updateNameUsage(ICdmDataSource dataSource) {
96 try{
97 boolean result = true;
98 CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
99
100 TransactionStatus tx = cdmApp.startTransaction();
101
102 int page = 0;
103 int count = cdmApp.getTaxonService().count(Taxon.class);
104 List<TaxonBase> taxonList = cdmApp.getTaxonService().list(TaxonBase.class, 100000, page, null, null);
105 int i = 0;
106
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");
110 result = false;
111 }else{
112 for (TaxonBase nameUsage : taxonList){
113 if ((i++ % 100) == 0){System.out.println(i);};
114
115 try {
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);
124 }else{
125 logger.warn("Non treatment taxon has no accepted taxon in treatment: " + nameUsage + " (" + nameUsage.getId() +")" );
126 }
127 }
128 } catch (Exception e) {
129 result = false;
130 e.printStackTrace();
131 }
132 }
133 }
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);
139 count = 0;
140 for (DefinedTermBase feature : featureList){
141 if (feature.equals(Feature.CITATION())){
142 FeatureNode newNode = FeatureNode.NewInstance((Feature)feature);
143 root.addChild(newNode);
144 count++;
145 }
146 }
147 if (count != 1){
148 logger.warn("Did not add exactly 1 features to the feature tree but " + count);
149 result = false;
150 }
151 //commit
152 cdmApp.commitTransaction(tx);
153 logger.info("NameUsage updated!");
154 return result;
155 } catch (Exception e) {
156 e.printStackTrace();
157 logger.error("ERROR in name usage update");
158 return false;
159 }
160
161 }
162
163 /**
164 * @param nameUsage
165 * @return
166 */
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;
175 }
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;
181 }
182 for (Taxon accTaxon : accTaxa){
183 if (isInTreatment(accTaxon, treatmentReference, false)){
184 return accTaxon;
185 }
186 }
187 }else{
188 throw new IllegalStateException("TaxonBase should be either a Taxon or a Synonym but was " + nameUsage.getClass().getName());
189 }
190 }
191 if (hasSynonymInTreatment){
192 logger.warn("Non treatment taxon has synonym in treatment but no accepted taxon: " + nameUsage + " (" + nameUsage.getId() +")" );
193 }
194 return null;
195 }
196
197 /**
198 * @param taxonBase
199 * @param treatmentReference
200 * @return
201 */
202 private boolean isInTreatment(TaxonBase taxonBase, IReferenceBase treatmentReference, boolean silent) {
203 if (taxonBase.getSec().equals(treatmentReference)){
204 //treatment taxa
205 if (! silent){
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() +")" );
209 }
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);
215 }
216 if (hasAccTaxonInTreatment == false){
217 logger.warn("Synonym has treatment reference but has no accepted taxon in tree: " + taxonBase + " (" + taxonBase.getId() +")" );
218 }
219 }else{
220 throw new IllegalStateException("TaxonBase should be either Taxon or Synonym");
221 }
222 }
223 return true;
224 }else{
225 //taxon not in treatment
226 if (! silent){
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() +")" );
230 }
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);
236 }
237 if (hasAccTaxonInTreatment == true){
238 logger.warn("Synonym has no treatment reference but has accepted taxon in treatment: " + taxonBase + " (" + taxonBase.getId() +")" );
239 }
240 }else{
241 throw new IllegalStateException("TaxonBase should be either Taxon or Synonym but was ");
242 }
243 }
244 return false;
245 }
246 }
247
248 /**
249 * @param taxonCandidate
250 * @param taxon
251 */
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;
257 break;
258 }
259 }
260 if (myDescription == null){
261 return false;
262 }
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);
268 return true;
269 }
270
271
272 /**
273 * @param args
274 */
275 public static void main(String[] args) {
276 PalmaePostImportUpdater updater = new PalmaePostImportUpdater();
277 try {
278 updater.updateMissingFeatures(cdmDestination);
279 updater.updateNameUsage(cdmDestination);
280 } catch (Exception e) {
281 e.printStackTrace();
282 logger.error("ERROR in feature tree update");
283 }
284 }
285 }