Project

General

Profile

Download (9.62 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.description.Feature;
24
import eu.etaxonomy.cdm.model.description.TaxonDescription;
25
import eu.etaxonomy.cdm.model.description.TextData;
26
import eu.etaxonomy.cdm.model.name.TaxonName;
27
import eu.etaxonomy.cdm.model.reference.IReference;
28
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.taxon.Synonym;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
33
import eu.etaxonomy.cdm.model.term.TermNode;
34
import eu.etaxonomy.cdm.model.term.TermTree;
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
			TermTree<Feature> tree = cdmApp.getFeatureTreeService().find(featureTreeUuid);
59
			TermNode<Feature> 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
					root.addChild(feature);
66
					count++;
67
				}else if(taxonomicAccounts.equals(label)){
68
					root.addChild(feature);
69
					count++;
70
				}else if(fossilRecord.equals(label)){
71
					root.addChild(feature);
72
					count++;
73
				}
74
			}
75
			cdmApp.commitTransaction(tx);
76
			if (count != 3){
77
				logger.warn("Did not find 3 additional features but " + count);
78
				return false;
79
			}
80
			logger.info("Feature tree updated!");
81
			return true;
82
		} catch (Exception e) {
83
			e.printStackTrace();
84
			logger.error("ERROR in feature tree update");
85
			return false;
86
		}
87

    
88
	}
89

    
90
	public boolean updateNameUsage(ICdmDataSource dataSource) {
91
		try{
92
			boolean result = true;
93
			CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
94

    
95
			TransactionStatus tx = cdmApp.startTransaction();
96

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

    
102
			IReference treatmentReference = (IReference) cdmApp.getCommonService().getSourcedObjectByIdInSource(Reference.class, "palm_pub_ed_999999", "PublicationCitation");
103
			if (treatmentReference == null){
104
				logger.error("Treatment reference could not be found");
105
				result = false;
106
			}else{
107
				for (TaxonBase nameUsage : taxonList){
108
					if ((i++ % 100) == 0){System.out.println(i);};
109

    
110
					try {
111
						//if not in treatment
112
						if (! isInTreatment(nameUsage, treatmentReference, false)){
113
							//if connected treatment taxon can be found
114
							Taxon acceptedTaxon = getAcceptedTreatmentTaxon(nameUsage, treatmentReference);
115
							if (acceptedTaxon != null){
116
								//add as citation and delete
117
								addNameUsage(acceptedTaxon, nameUsage);
118
								cdmApp.getTaxonService().delete(nameUsage);
119
							}else{
120
								logger.warn("Non treatment taxon has no accepted taxon in treatment: " +  nameUsage + " (" + nameUsage.getId() +")" );
121
							}
122
						}
123
					} catch (Exception e) {
124
						result = false;
125
						e.printStackTrace();
126
					}
127
				}
128
			}
129
			//add citation feature to feature tree
130
			UUID featureTreeUuid = PalmaeActivator.featureTreeUuid;
131
			TermTree<Feature> tree = cdmApp.getFeatureTreeService().find(featureTreeUuid);
132
			TermNode<Feature> root = tree.getRoot();
133
			List<Feature> featureList = cdmApp.getTermService().list(Feature.class, null, null, null, null);
134
			count = 0;
135
			for (Feature feature : featureList){
136
				if (feature.equals(Feature.CITATION())){
137
					root.addChild(feature);
138
					count++;
139
				}
140
			}
141
			if (count != 1){
142
				logger.warn("Did not add exactly 1 features to the feature tree but " + count);
143
				result = false;
144
			}
145
			//commit
146
			cdmApp.commitTransaction(tx);
147
			logger.info("NameUsage updated!");
148
			return result;
149
		} catch (Exception e) {
150
			e.printStackTrace();
151
			logger.error("ERROR in name usage update");
152
			return false;
153
		}
154

    
155
	}
156

    
157
	/**
158
	 * @param nameUsage
159
	 * @return
160
	 */
161
	private Taxon getAcceptedTreatmentTaxon(TaxonBase nameUsage, IReference treatmentReference) {
162
		boolean hasSynonymInTreatment = false;
163
		TaxonName name = nameUsage.getName();
164
		Set<TaxonBase> candidateList = name.getTaxonBases();
165
		for (TaxonBase candidate : candidateList){
166
			if (candidate instanceof Taxon){
167
				if (isInTreatment(candidate, treatmentReference, false)){
168
					return (Taxon)candidate;
169
				}
170
			}else if (candidate instanceof Synonym){
171
				Synonym synonym = (Synonym)candidate;
172
				Taxon accTaxon = synonym.getAcceptedTaxon();
173
				if (isInTreatment(synonym, treatmentReference, true)){
174
					hasSynonymInTreatment = true;
175
				}
176
				if (accTaxon != null){
177
					if (isInTreatment(accTaxon, treatmentReference, false)){
178
						return accTaxon;
179
					}
180
				}
181
			}else{
182
				throw new IllegalStateException("TaxonBase should be either a Taxon or a Synonym but was " + nameUsage.getClass().getName());
183
			}
184
		}
185
		if (hasSynonymInTreatment){
186
			logger.warn("Non treatment taxon has synonym in treatment but no accepted taxon: " +  nameUsage + " (" + nameUsage.getId() +")" );
187
		}
188
		return null;
189
	}
190

    
191
	/**
192
	 * @param taxonBase
193
	 * @param treatmentReference
194
	 * @return
195
	 */
196
	private boolean isInTreatment(TaxonBase taxonBase, IReference treatmentReference, boolean silent) {
197
		if (taxonBase.getSec().equals(treatmentReference)){
198
			//treatment taxa
199
			if (! silent){
200
				if (taxonBase instanceof Taxon){
201
					if (((Taxon)taxonBase).getTaxonNodes().size()< 1){
202
						logger.warn("Taxon has treatment sec but is not in tree: " +  taxonBase + " (" + taxonBase.getId() +")" );
203
					}
204
				}else if (taxonBase instanceof Synonym){
205
					Synonym synonym = (Synonym)taxonBase;
206
					boolean hasAccTaxonInTreatment = false;
207
					Taxon accTaxon = synonym.getAcceptedTaxon();
208
					if (accTaxon != null){
209
						hasAccTaxonInTreatment |= isInTreatment(accTaxon, treatmentReference, false);
210
					}
211
					if (hasAccTaxonInTreatment == false){
212
						logger.warn("Synonym has treatment reference but has no accepted taxon in tree: " +  taxonBase + " (" + taxonBase.getId() +")" );
213
					}
214
				}else{
215
					throw new IllegalStateException("TaxonBase should be either Taxon or Synonym");
216
				}
217
			}
218
			return true;
219
		}else{
220
			//taxon not in treatment
221
			if (! silent){
222
				if (taxonBase instanceof Taxon){
223
					if (((Taxon)taxonBase).getTaxonNodes().size()> 0){
224
						logger.warn("Taxon has no treatment sec but is in tree: " +  taxonBase + " (" + taxonBase.getId() +")" );
225
					}
226
				}else if (taxonBase instanceof Synonym){
227
					Synonym synonym = (Synonym)taxonBase;
228
					boolean hasAccTaxonInTreatment = false;
229
					Taxon accTaxon = synonym.getAcceptedTaxon();
230
					if (accTaxon != null){
231
						hasAccTaxonInTreatment |= isInTreatment(accTaxon, treatmentReference, false);
232
					}
233
					if (hasAccTaxonInTreatment == true){
234
						logger.warn("Synonym has no treatment reference but has accepted taxon in treatment: " +  taxonBase + " (" + taxonBase.getId() +")" );
235
					}
236
				}else{
237
					throw new IllegalStateException("TaxonBase should be either Taxon or Synonym but was ");
238
				}
239
			}
240
			return false;
241
		}
242
	}
243

    
244
	/**
245
	 * @param taxonCandidate
246
	 * @param taxon
247
	 */
248
	private boolean addNameUsage(Taxon taxon, TaxonBase nameUsageTaxon) {
249
		TaxonDescription myDescription = null;
250
		for (TaxonDescription desc : taxon.getDescriptions()){
251
			if (! desc.isImageGallery()){
252
				myDescription = desc;
253
				break;
254
			}
255
		}
256
		if (myDescription == null){
257
			return false;
258
		}
259
		TextData textData = TextData.NewInstance(Feature.CITATION());
260
		//creates text (name: reference)
261
		//textData.putText(nameUsageTaxon.getName().getTitleCache()+": " + nameUsageTaxon.getSec().getTitleCache(), Language.DEFAULT());
262
		textData.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, nameUsageTaxon.getSec(), null, nameUsageTaxon.getName(), nameUsageTaxon.getName().getTitleCache());
263
		myDescription.addElement(textData);
264
		return true;
265
	}
266

    
267

    
268
	/**
269
	 * @param args
270
	 */
271
	public static void main(String[] args) {
272
		PalmaePostImportUpdater updater = new PalmaePostImportUpdater();
273
		try {
274
			updater.updateMissingFeatures(cdmDestination);
275
			updater.updateNameUsage(cdmDestination);
276
		} catch (Exception e) {
277
			e.printStackTrace();
278
			logger.error("ERROR in feature tree update");
279
		}
280
	}
281
}
(4-4/8)