Project

General

Profile

Download (9.66 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.api.application.ICdmRepository;
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.description.Feature;
25
import eu.etaxonomy.cdm.model.description.TaxonDescription;
26
import eu.etaxonomy.cdm.model.description.TextData;
27
import eu.etaxonomy.cdm.model.name.TaxonName;
28
import eu.etaxonomy.cdm.model.reference.IReference;
29
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.model.taxon.Synonym;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34
import eu.etaxonomy.cdm.model.term.TermNode;
35
import eu.etaxonomy.cdm.model.term.TermTree;
36

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

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

    
46

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

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

    
57
			TransactionStatus tx = cdmApp.startTransaction();
58

    
59
			@SuppressWarnings("unchecked")
60
            TermTree<Feature> tree = cdmApp.getTermTreeService().find(featureTreeUuid);
61
			TermNode<Feature> root = tree.getRoot();
62

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

    
90
	}
91

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

    
97
			TransactionStatus tx = cdmApp.startTransaction();
98

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

    
105
			IReference treatmentReference = 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
			@SuppressWarnings("unchecked")
135
            TermTree<Feature> tree = cdmApp.getTermTreeService().find(featureTreeUuid);
136
			TermNode<Feature> root = tree.getRoot();
137
			List<Feature> featureList = cdmApp.getTermService().list(Feature.class, null, null, null, null);
138
			count = 0;
139
			for (Feature feature : featureList){
140
				if (feature.equals(Feature.CITATION())){
141
					root.addChild(feature);
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
		@SuppressWarnings("rawtypes")
169
        Set<TaxonBase> candidateList = name.getTaxonBases();
170
		for (TaxonBase<?> candidate : candidateList){
171
			if (candidate instanceof Taxon){
172
				if (isInTreatment(candidate, treatmentReference, false)){
173
					return (Taxon)candidate;
174
				}
175
			}else if (candidate instanceof Synonym){
176
				Synonym synonym = (Synonym)candidate;
177
				Taxon accTaxon = synonym.getAcceptedTaxon();
178
				if (isInTreatment(synonym, treatmentReference, true)){
179
					hasSynonymInTreatment = true;
180
				}
181
				if (accTaxon != null){
182
					if (isInTreatment(accTaxon, treatmentReference, false)){
183
						return accTaxon;
184
					}
185
				}
186
			}else{
187
				throw new IllegalStateException("TaxonBase should be either a Taxon or a Synonym but was " + nameUsage.getClass().getName());
188
			}
189
		}
190
		if (hasSynonymInTreatment){
191
			logger.warn("Non treatment taxon has synonym in treatment but no accepted taxon: " +  nameUsage + " (" + nameUsage.getId() +")" );
192
		}
193
		return null;
194
	}
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
	private boolean addNameUsage(Taxon taxon, TaxonBase<?> nameUsageTaxon) {
245
		TaxonDescription myDescription = null;
246
		for (TaxonDescription desc : taxon.getDescriptions()){
247
			if (! desc.isImageGallery()){
248
				myDescription = desc;
249
				break;
250
			}
251
		}
252
		if (myDescription == null){
253
			return false;
254
		}
255
		TextData textData = TextData.NewInstance(Feature.CITATION());
256
		//creates text (name: reference)
257
		//textData.putText(nameUsageTaxon.getName().getTitleCache()+": " + nameUsageTaxon.getSec().getTitleCache(), Language.DEFAULT());
258
		textData.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, nameUsageTaxon.getSec(), null, nameUsageTaxon.getName(), nameUsageTaxon.getName().getTitleCache());
259
		myDescription.addElement(textData);
260
		return true;
261
	}
262

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