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