Project

General

Profile

Download (9.63 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
			@SuppressWarnings("unchecked")
59
            TermTree<Feature> tree = cdmApp.getFeatureTreeService().find(featureTreeUuid);
60
			TermNode<Feature> root = tree.getRoot();
61

    
62
			List<Feature> featureList = cdmApp.getTermService().list(Feature.class, null, null, null, null);
63
			for (Feature feature : featureList){
64
				String label = feature.getLabel();
65
				if (relationships.equals(label)){
66
					root.addChild(feature);
67
					count++;
68
				}else if(taxonomicAccounts.equals(label)){
69
					root.addChild(feature);
70
					count++;
71
				}else if(fossilRecord.equals(label)){
72
					root.addChild(feature);
73
					count++;
74
				}
75
			}
76
			cdmApp.commitTransaction(tx);
77
			if (count != 3){
78
				logger.warn("Did not find 3 additional features but " + count);
79
				return false;
80
			}
81
			logger.info("Feature tree updated!");
82
			return true;
83
		} catch (Exception e) {
84
			e.printStackTrace();
85
			logger.error("ERROR in feature tree update");
86
			return false;
87
		}
88

    
89
	}
90

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

    
96
			TransactionStatus tx = cdmApp.startTransaction();
97

    
98
			int page = 0;
99
			int count = cdmApp.getTaxonService().count(Taxon.class);
100
			@SuppressWarnings("rawtypes")
101
            List<TaxonBase> taxonList = cdmApp.getTaxonService().list(TaxonBase.class, 100000, page, null, null);
102
			int i = 0;
103

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

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

    
158
	}
159

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

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

    
262
	public static void main(String[] args) {
263
		PalmaePostImportUpdater updater = new PalmaePostImportUpdater();
264
		try {
265
			updater.updateMissingFeatures(cdmDestination);
266
			updater.updateNameUsage(cdmDestination);
267
		} catch (Exception e) {
268
			e.printStackTrace();
269
			logger.error("ERROR in feature tree update");
270
		}
271
	}
272
}
(4-4/8)