Project

General

Profile

Download (9.84 KB) Statistics
| Branch: | Revision:
1 cce11612 Andreas Müller
/**
2
* Copyright (C) 2007 EDIT
3 8422c0cd Andreas Müller
* European Distributed Institute of Taxonomy
4 cce11612 Andreas Müller
* http://www.e-taxonomy.eu
5 8422c0cd Andreas Müller
*
6 cce11612 Andreas Müller
* 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.cyprus;
11
12
import java.io.FileNotFoundException;
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import java.util.HashSet;
16 2a6e6815 Andreas Müller
import java.util.List;
17 539b7820 Andreas Müller
import java.util.Map;
18 cce11612 Andreas Müller
import java.util.Set;
19
import java.util.UUID;
20
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.log4j.Logger;
23
import org.springframework.transaction.TransactionStatus;
24
25
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
26
import eu.etaxonomy.cdm.app.common.CdmDestinations;
27
import eu.etaxonomy.cdm.common.ExcelUtils;
28
import eu.etaxonomy.cdm.database.DbSchemaValidation;
29
import eu.etaxonomy.cdm.database.ICdmDataSource;
30
import eu.etaxonomy.cdm.io.api.application.CdmIoApplicationController;
31
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
32
import eu.etaxonomy.cdm.model.common.CdmBase;
33
import eu.etaxonomy.cdm.model.description.Feature;
34
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
35
import eu.etaxonomy.cdm.model.description.QuantitativeData;
36
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
37
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
38
import eu.etaxonomy.cdm.model.description.TaxonDescription;
39 86536e03 Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonName;
40 de81ab2a Andreas Müller
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
41 cce11612 Andreas Müller
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
43
import eu.etaxonomy.cdm.model.taxon.Synonym;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
46
47
/**
48
 * @author a.mueller
49 a13538c8 Andreas Müller
 * @since 16.12.2010
50 cce11612 Andreas Müller
 */
51
public class CyprusAltitudeActivator {
52
	private static final Logger logger = Logger.getLogger(CyprusAltitudeActivator.class);
53 8422c0cd Andreas Müller
54 cce11612 Andreas Müller
	//database validation status (create, update, validate ...)
55
	static DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
56
	static final URI source = cyprus_altitude();
57
58 8422c0cd Andreas Müller
59 9cb92253 Andreas Müller
	static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
60
//	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_test();
61
//	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_cyprus_dev();
62 cce11612 Andreas Müller
//	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_cyprus_production();
63
64 8422c0cd Andreas Müller
65 cce11612 Andreas Müller
	//feature tree uuid
66
	public static final UUID featureTreeUuid = UUID.fromString("14d1e912-5ec2-4d10-878b-828788b70a87");
67 8422c0cd Andreas Müller
68 cce11612 Andreas Müller
	//classification
69
	static final UUID classificationUuid = UUID.fromString("0c2b5d25-7b15-4401-8b51-dd4be0ee5cab");
70 8422c0cd Andreas Müller
71 cce11612 Andreas Müller
	private static final String sourceReferenceTitle = "Cyprus Excel Altitude Import";
72 8422c0cd Andreas Müller
73
74 cce11612 Andreas Müller
	//TODO move to Feature vocabulary
75 8969b71c Andreas Müller
	private static final UUID uuidAltitudeFeature = UUID.fromString("1a28ed59-e15f-4001-b5c2-ea89f0012671");
76 8422c0cd Andreas Müller
77 cce11612 Andreas Müller
	//check - import
78
	static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
79 8422c0cd Andreas Müller
80 cce11612 Andreas Müller
	private void doImport(ICdmDataSource cdmDestination){
81 8422c0cd Andreas Müller
82 539b7820 Andreas Müller
		List<Map<String, String>> excel;
83 cce11612 Andreas Müller
		try {
84
			excel = ExcelUtils.parseXLS(source, "coreTax");
85
		} catch (FileNotFoundException e) {
86
			e.printStackTrace();
87
			return;
88
		}
89 8422c0cd Andreas Müller
90 cce11612 Andreas Müller
		CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, hbm2dll);
91
92 2e477cf4 Andreas Müller
		Set<TaxonBase> taxaToSave = new HashSet<>();
93 8422c0cd Andreas Müller
94 cce11612 Andreas Müller
		TransactionStatus tx = app.startTransaction();
95 8422c0cd Andreas Müller
96 cce11612 Andreas Müller
		UUID uuidMikle77 = UUID.fromString("9f5fa7ee-538b-4ae5-bd82-2a9503fea1d6");
97
		UUID uuidMikle85 = UUID.fromString("994403c4-c400-413d-9a1a-8531a40bfd8c");
98 8422c0cd Andreas Müller
99
		Reference mikle77 = app.getReferenceService().find(uuidMikle77);
100
		Reference mikle85 = app.getReferenceService().find(uuidMikle85);
101
102 cce11612 Andreas Müller
		Feature altitudeFeature = (Feature) app.getTermService().find(uuidAltitudeFeature);
103
		if (altitudeFeature == null){
104 8969b71c Andreas Müller
			throw new RuntimeException("Could not find altitudinal range feature");
105 cce11612 Andreas Müller
		}
106 8422c0cd Andreas Müller
107 cce11612 Andreas Müller
		MeasurementUnit meter = (MeasurementUnit)app.getTermService().find(UUID.fromString("8bef5055-789c-41e5-bea2-8dc2ea8ecdf6"));
108 8422c0cd Andreas Müller
109 cce11612 Andreas Müller
		int count =1;
110 539b7820 Andreas Müller
		for (Map<String, String> row : excel){
111 cce11612 Andreas Müller
			count++;
112
			UUID baseUuid = makeUuid(row, "uuid");
113
			UUID acceptedUuid = makeUuid(row, "acceptedNameUuid");
114
			UUID parentUuid = makeUuid(row, "parentUuid");
115 8422c0cd Andreas Müller
116 8969b71c Andreas Müller
			String altitudeMin = row.get("Min");
117
			String altitudeMax = row.get("Max");
118
			String acceptedName = row.get("AcceptedName");
119 8422c0cd Andreas Müller
120 cce11612 Andreas Müller
			String source = row.get("Source");
121 8422c0cd Andreas Müller
122 8969b71c Andreas Müller
			if (StringUtils.isBlank(altitudeMin)){
123 cce11612 Andreas Müller
				continue;
124
			}
125 8422c0cd Andreas Müller
126 cce11612 Andreas Müller
			boolean hasAltitude = false;
127 8422c0cd Andreas Müller
			Reference sourceRef = getSource(source, mikle77, mikle85);
128 8969b71c Andreas Müller
			Taxon taxon = getTaxon(app, baseUuid, acceptedUuid, parentUuid, acceptedName, count);
129 cce11612 Andreas Müller
			if (taxon != null){
130
				TaxonDescription desc = getDescription(taxon, sourceRef);
131 8422c0cd Andreas Müller
132 8969b71c Andreas Müller
				hasAltitude = makeAltitude(altitudeMin, altitudeMax, altitudeFeature, sourceRef, desc, meter, count);
133 cce11612 Andreas Müller
				if (hasAltitude){
134
					if(desc.getTaxon() == null){
135
						taxon.addDescription(desc);
136
					}
137
					taxaToSave.add(taxon);
138 8969b71c Andreas Müller
				}else{
139
					logger.warn("HasALtitude is false in " + count);
140 cce11612 Andreas Müller
				}
141
			}else{
142
				logger.warn("Taxon not recognized in line " + count);
143
			}
144
		}
145
146
		app.getTaxonService().saveOrUpdate(taxaToSave);
147 8422c0cd Andreas Müller
148 cce11612 Andreas Müller
		app.commitTransaction(tx);
149
	}
150
151 8969b71c Andreas Müller
152
	private Taxon getTaxon(CdmApplicationController app, UUID baseUuid, UUID acceptedUuid, UUID parentUuid, String acceptedName, int row) {
153 cce11612 Andreas Müller
		TaxonBase<?> base = app.getTaxonService().find(baseUuid);
154 8422c0cd Andreas Müller
155 cce11612 Andreas Müller
		Taxon result = null;
156
		if (base.isInstanceOf(Taxon.class)){
157
			Taxon t = CdmBase.deproxy(base, Taxon.class);
158
			if (t.getTaxonNodes().size() == 1 && t.getTaxonNodes().iterator().next().getClassification().getUuid().equals(classificationUuid)){
159
				result = t;
160
			}else{
161
				logger.warn("Base taxon (uuid) not in classification. Row: " + row +  ", Taxon: " + base.getTitleCache());
162
			}
163
		}
164
		if (result == null){
165
			TaxonBase<?> accepted = app.getTaxonService().find(acceptedUuid);
166
			Taxon t = CdmBase.deproxy(accepted, Taxon.class);;
167
			if (t.getTaxonNodes().size() == 1 && t.getTaxonNodes().iterator().next().getClassification().getUuid().equals(classificationUuid)){
168
				if (hasSynonym(t, base)){
169
					result = t;
170
				}else{
171
					logger.warn("Synonym relation has changed somehow. Row: " + row +  ", Taxon: " + base.getTitleCache());
172
				}
173 8422c0cd Andreas Müller
174 cce11612 Andreas Müller
			}else{
175
				logger.warn("Accepted taxon not in classification. Row: " + row +  ", Taxon: " + base.getTitleCache());
176
			}
177
		}
178 8422c0cd Andreas Müller
179 8969b71c Andreas Müller
		if (result != null){
180
			if (! result.getName().getTitleCache().equals(acceptedName)){
181
				logger.warn("AcceptedName and taxon name is not equal in " + row + ".\n" +
182
						" Accepted Name: " + acceptedName + ";\n" +
183
						" Taxon    Name: " + result.getName().getTitleCache());
184
			}
185
		}
186 8422c0cd Andreas Müller
187 cce11612 Andreas Müller
		return result;
188
	}
189
190
	private boolean hasSynonym(Taxon t, TaxonBase<?> base) {
191
		if (base.isInstanceOf(Synonym.class)){
192 3ef2e1bd Andreas Müller
			for (Synonym syn : t.getSynonyms()){
193
				if (syn.equals(base)){
194 cce11612 Andreas Müller
					return true;
195
				}
196
			}
197
		}
198
		return false;
199
	}
200
201 9cb92253 Andreas Müller
//	private static final Pattern altitudePattern = Pattern.compile("\\d{1,4}(-\\d{1,4})?");
202 cce11612 Andreas Müller
203
204 8422c0cd Andreas Müller
	private boolean makeAltitude(String altitudeMin, String altitudeMax, Feature altitudeFeature,
205
			Reference sourceRef, TaxonDescription desc, MeasurementUnit meter, int row) {
206
207 8969b71c Andreas Müller
		QuantitativeData data = QuantitativeData.NewInstance(altitudeFeature);
208 8422c0cd Andreas Müller
209 8969b71c Andreas Müller
		//Meikle
210
		if (source != null){
211 ea7deae0 Andreas Müller
			TaxonName nameUsedInSource = null;  //TODO
212 8969b71c Andreas Müller
			data.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, sourceRef, null, nameUsedInSource, null);
213
		}
214
		data.setUnit(meter);
215 8422c0cd Andreas Müller
216 8969b71c Andreas Müller
		Integer min = Integer.valueOf(altitudeMin);
217
		StatisticalMeasurementValue minValue = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MIN(), min);
218
		data.addStatisticalValue(minValue);
219
220
		Integer max = Integer.valueOf(altitudeMax);
221
		StatisticalMeasurementValue maxValue = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MAX(), max);
222
		data.addStatisticalValue(maxValue);
223 8422c0cd Andreas Müller
224 8969b71c Andreas Müller
		desc.addElement(data);
225
		return true;
226
	}
227 8422c0cd Andreas Müller
228
	private TaxonDescription getDescription(Taxon taxon, Reference sourceRef) {
229 cce11612 Andreas Müller
		if (taxon != null){
230
			//TODO Mikle existiert derzeit nicht also Source
231 8422c0cd Andreas Müller
232 cce11612 Andreas Müller
			TaxonDescription desc = TaxonDescription.NewInstance();
233
			desc.setTitleCache("Import from " + getSourceReference().getTitleCache(), true);
234
			desc.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, sourceRef,null);
235
			desc.addSource(OriginalSourceType.Import, null, null, getSourceReference(), null);
236 8422c0cd Andreas Müller
237 cce11612 Andreas Müller
			return desc;
238
		}
239
		return null;
240
	}
241
242 8422c0cd Andreas Müller
	private Reference getSource(String source, Reference m77, Reference m85) {
243 cce11612 Andreas Müller
		if(StringUtils.isNotBlank(source)){
244
			if (source.equals("Meikle 1977")){
245
				return m77;
246
			}else if (source.equals("Meikle 1985")){
247
				return m85;
248
			}else{
249
				logger.warn("Source not recognized: " + source);
250
			}
251
		}
252
		return null;
253
	}
254
255
	/**
256
	 * @param row
257
	 * @return
258
	 */
259 539b7820 Andreas Müller
	private UUID makeUuid(Map<String, String> row, String colName) {
260 cce11612 Andreas Müller
		if (StringUtils.isBlank(row.get(colName))){
261
			return null;
262
		}else{
263
			return UUID.fromString(row.get(colName));
264
		}
265
	}
266
267 8422c0cd Andreas Müller
	Reference sourceReference;
268
	private Reference getSourceReference() {
269 cce11612 Andreas Müller
		if (sourceReference == null){
270
			sourceReference = ReferenceFactory.newGeneric();
271
			sourceReference.setTitleCache(sourceReferenceTitle, true);
272
		}
273
		return sourceReference;
274 8422c0cd Andreas Müller
275 cce11612 Andreas Müller
	}
276
277
	//Cyprus
278
	public static URI cyprus_altitude() {
279
		URI sourceUrl;
280
		try {
281 8969b71c Andreas Müller
			sourceUrl = new URI("file:/F:/data/cyprus/Cyprus-altitude-import-neu.xls");
282
//			sourceUrl = new URI("file:/F:/data/cyprus/Zypern-Altitude.xls");
283 cce11612 Andreas Müller
			return sourceUrl;
284
		} catch (URISyntaxException e) {
285
			e.printStackTrace();
286
			return null;
287
		}
288
	}
289
290
291
	/**
292
	 * @param args
293
	 */
294
	public static void main(String[] args) {
295
		CyprusAltitudeActivator me = new CyprusAltitudeActivator();
296
		me.doImport(cdmDestination);
297
	}
298 8422c0cd Andreas Müller
299 cce11612 Andreas Müller
}