Project

General

Profile

Download (9.86 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9

    
10
package eu.etaxonomy.cdm.app.wp6.palmae;
11

    
12
import java.util.List;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.transaction.TransactionStatus;
18

    
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;
35

    
36
/**
37
 * @author a.mueller
38
 * @since 01.10.2009
39
 */
40
public class PalmaePostImportUpdater {
41
	private static final Logger logger = Logger.getLogger(PalmaePostImportUpdater.class);
42

    
43
	static final ICdmDataSource cdmDestination = CdmDestinations.localH2Palmae();
44

    
45

    
46
	private String relationships = "relationships";
47
	private String taxonomicAccounts = "taxonomic accounts";
48
	private String fossilRecord = "fossil record";
49

    
50
	public boolean updateMissingFeatures(ICdmDataSource dataSource) {
51
		try{
52
			int count = 0;
53
			UUID featureTreeUuid = PalmaeActivator.featureTreeUuid;
54
			CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
55

    
56
			TransactionStatus tx = cdmApp.startTransaction();
57

    
58
			FeatureTree tree = cdmApp.getFeatureTreeService().find(featureTreeUuid);
59
			FeatureNode root = tree.getRoot();
60

    
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);
67
					count++;
68
				}else if(taxonomicAccounts.equals(label)){
69
					FeatureNode newNode = FeatureNode.NewInstance(feature);
70
					root.addChild(newNode);
71
					count++;
72
				}else if(fossilRecord.equals(label)){
73
					FeatureNode newNode = FeatureNode.NewInstance(feature);
74
					root.addChild(newNode);
75
					count++;
76
				}
77
			}
78
			cdmApp.commitTransaction(tx);
79
			if (count != 3){
80
				logger.warn("Did not find 3 additional features but " + count);
81
				return false;
82
			}
83
			logger.info("Feature tree updated!");
84
			return true;
85
		} catch (Exception e) {
86
			e.printStackTrace();
87
			logger.error("ERROR in feature tree update");
88
			return false;
89
		}
90

    
91
	}
92

    
93
	public boolean updateNameUsage(ICdmDataSource dataSource) {
94
		try{
95
			boolean result = true;
96
			CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
97

    
98
			TransactionStatus tx = cdmApp.startTransaction();
99

    
100
			int page = 0;
101
			int count = cdmApp.getTaxonService().count(Taxon.class);
102
			List<TaxonBase> taxonList = cdmApp.getTaxonService().list(TaxonBase.class, 100000, page, null, null);
103
			int i = 0;
104

    
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");
108
				result = false;
109
			}else{
110
				for (TaxonBase nameUsage : taxonList){
111
					if ((i++ % 100) == 0){System.out.println(i);};
112

    
113
					try {
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);
122
							}else{
123
								logger.warn("Non treatment taxon has no accepted taxon in treatment: " +  nameUsage + " (" + nameUsage.getId() +")" );
124
							}
125
						}
126
					} catch (Exception e) {
127
						result = false;
128
						e.printStackTrace();
129
					}
130
				}
131
			}
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);
137
			count = 0;
138
			for (Feature feature : featureList){
139
				if (feature.equals(Feature.CITATION())){
140
					FeatureNode newNode = FeatureNode.NewInstance(feature);
141
					root.addChild(newNode);
142
					count++;
143
				}
144
			}
145
			if (count != 1){
146
				logger.warn("Did not add exactly 1 features to the feature tree but " + count);
147
				result = false;
148
			}
149
			//commit
150
			cdmApp.commitTransaction(tx);
151
			logger.info("NameUsage updated!");
152
			return result;
153
		} catch (Exception e) {
154
			e.printStackTrace();
155
			logger.error("ERROR in name usage update");
156
			return false;
157
		}
158

    
159
	}
160

    
161
	/**
162
	 * @param nameUsage
163
	 * @return
164
	 */
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;
173
				}
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;
179
				}
180
				if (accTaxon != null){
181
					if (isInTreatment(accTaxon, treatmentReference, false)){
182
						return accTaxon;
183
					}
184
				}
185
			}else{
186
				throw new IllegalStateException("TaxonBase should be either a Taxon or a Synonym but was " + nameUsage.getClass().getName());
187
			}
188
		}
189
		if (hasSynonymInTreatment){
190
			logger.warn("Non treatment taxon has synonym in treatment but no accepted taxon: " +  nameUsage + " (" + nameUsage.getId() +")" );
191
		}
192
		return null;
193
	}
194

    
195
	/**
196
	 * @param taxonBase
197
	 * @param treatmentReference
198
	 * @return
199
	 */
200
	private boolean isInTreatment(TaxonBase taxonBase, IReference treatmentReference, boolean silent) {
201
		if (taxonBase.getSec().equals(treatmentReference)){
202
			//treatment taxa
203
			if (! silent){
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() +")" );
207
					}
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);
214
					}
215
					if (hasAccTaxonInTreatment == false){
216
						logger.warn("Synonym has treatment reference but has no accepted taxon in tree: " +  taxonBase + " (" + taxonBase.getId() +")" );
217
					}
218
				}else{
219
					throw new IllegalStateException("TaxonBase should be either Taxon or Synonym");
220
				}
221
			}
222
			return true;
223
		}else{
224
			//taxon not in treatment
225
			if (! silent){
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() +")" );
229
					}
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);
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(OriginalSourceType.PrimaryTaxonomicSource, 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
}
(4-4/8)