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