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.io.algaterra;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.HashSet;
|
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.stereotype.Component;
|
24
|
import org.springframework.transaction.TransactionStatus;
|
25
|
|
26
|
import eu.etaxonomy.cdm.io.algaterra.validation.AlgaTerraMorphologyImportValidator;
|
27
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
|
28
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
|
29
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
30
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
31
|
import eu.etaxonomy.cdm.io.common.Source;
|
32
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
33
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
34
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
35
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
36
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
37
|
import eu.etaxonomy.cdm.model.description.Feature;
|
38
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
39
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
40
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
41
|
import eu.etaxonomy.cdm.model.description.State;
|
42
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
|
43
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
|
44
|
import eu.etaxonomy.cdm.model.description.TextData;
|
45
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
46
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
47
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
48
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
49
|
import eu.etaxonomy.cdm.model.term.TermType;
|
50
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
51
|
|
52
|
|
53
|
/**
|
54
|
* @author a.mueller
|
55
|
* @since 01.09.2012
|
56
|
*/
|
57
|
@Component
|
58
|
public class AlgaTerraMorphologyImport extends AlgaTerraSpecimenImportBase {
|
59
|
private static final Logger logger = Logger.getLogger(AlgaTerraMorphologyImport.class);
|
60
|
|
61
|
|
62
|
private static int modCount = 5000;
|
63
|
private static final String pluralString = "morpho facts";
|
64
|
private static final String dbTableName = "MorphoFact";
|
65
|
|
66
|
|
67
|
public AlgaTerraMorphologyImport(){
|
68
|
super(dbTableName, pluralString);
|
69
|
}
|
70
|
|
71
|
|
72
|
@Override
|
73
|
protected String getIdQuery(BerlinModelImportState state) {
|
74
|
String result = " SELECT MorphoFactId " +
|
75
|
" FROM MorphoFact " +
|
76
|
" ORDER BY MorphoFact.MorphoFactId ";
|
77
|
return result;
|
78
|
}
|
79
|
|
80
|
|
81
|
@Override
|
82
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
83
|
String strQuery =
|
84
|
" SELECT mf.*, mf.MorphoFactId as unitId, ecoFact.ecoFactId as ecoFactId, size.* " +
|
85
|
" FROM MorphoFact mf " +
|
86
|
" LEFT OUTER JOIN MorphoSizeRange size ON mf.SizeRangeFk = size.SizeRangeId " +
|
87
|
" LEFT OUTER JOIN MorphoValveDescription valve1Desc ON mf.Valve1DescriptionFk = valve1Desc.ValveDescriptionId " +
|
88
|
" LEFT OUTER JOIN MorphoValveDescription valve2Desc ON mf.Valve2DescriptionFk = valve2Desc.ValveDescriptionId " +
|
89
|
" LEFT OUTER JOIN EcoFact ecoFact ON ecoFact.CultureStrain = mf.CultureStrainNo " +
|
90
|
" WHERE (mf.MorphoFactId IN (" + ID_LIST_TOKEN + ") )"
|
91
|
+ " ORDER BY mf.MorphoFactId "
|
92
|
;
|
93
|
return strQuery;
|
94
|
}
|
95
|
|
96
|
|
97
|
private Map<String, TermVocabulary<State>> vocabularyMap = new HashMap<String, TermVocabulary<State>>();
|
98
|
private Map<String, Feature> featureMap = new HashMap<String, Feature>();
|
99
|
private Map<String, Map<Integer, State>> algaTerraMorphoStates = new HashMap<String, Map<Integer, State>>();
|
100
|
private TermVocabulary<Feature> algaTerraMorphoFeatures = TermVocabulary
|
101
|
.NewInstance(TermType.Feature, "Alga Terra Morphology Features", "AT Morphology Features", null, null);
|
102
|
|
103
|
private void doMorphoListen(AlgaTerraImportState state) throws SQLException{
|
104
|
|
105
|
TransactionStatus txStatus = this.startTransaction();
|
106
|
|
107
|
getVocabularyService().save(algaTerraMorphoFeatures);
|
108
|
|
109
|
//chloroplast position
|
110
|
String baseName = "Chloroplast Position";
|
111
|
UUID uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocChloroplastPosition;
|
112
|
boolean isOrdered = false;
|
113
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
114
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
115
|
|
116
|
//Chloroplast Shape
|
117
|
baseName = "Chloroplast Shape";
|
118
|
uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocChloroplastShape;
|
119
|
isOrdered = false;
|
120
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
121
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
122
|
|
123
|
//Chloroplast Shape
|
124
|
baseName = "Chloroplast Structure";
|
125
|
uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocChloroplastStructure;
|
126
|
isOrdered = false;
|
127
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
128
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
129
|
|
130
|
//Growth Form
|
131
|
baseName = "Growth Form";
|
132
|
uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocGrowthForm;
|
133
|
isOrdered = false;
|
134
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
135
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
136
|
|
137
|
//Organisation Level
|
138
|
baseName = "Organisation Level";
|
139
|
uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocOrganisationLevel;
|
140
|
isOrdered = false;
|
141
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
142
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
143
|
|
144
|
|
145
|
//Raphe
|
146
|
baseName = "Raphe";
|
147
|
uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocRaphe;
|
148
|
isOrdered = false;
|
149
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
150
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
151
|
|
152
|
//Shape
|
153
|
baseName = "Shape";
|
154
|
uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocShape;
|
155
|
isOrdered = false;
|
156
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
157
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
158
|
|
159
|
//Symmetrie
|
160
|
baseName = "Symmetry";
|
161
|
uuidStateVocabulary = AlgaTerraImportTransformer.uuidVocSymmetry;
|
162
|
isOrdered = false;
|
163
|
makeFeatureAndVocabulary(state, vocabularyMap, featureMap, algaTerraMorphoStates,
|
164
|
algaTerraMorphoFeatures, baseName, uuidStateVocabulary, isOrdered);
|
165
|
|
166
|
getVocabularyService().saveOrUpdate(algaTerraMorphoFeatures);
|
167
|
|
168
|
this.commitTransaction(txStatus);
|
169
|
}
|
170
|
|
171
|
|
172
|
private void makeFeatureAndVocabulary(AlgaTerraImportState state,
|
173
|
Map<String, TermVocabulary<State>> vocabularyMap,
|
174
|
Map<String, Feature> featureMap, Map<String, Map<Integer, State>> allMorphoStates,
|
175
|
TermVocabulary<Feature> algaTerraMorphoFeatures, String baseName,
|
176
|
UUID uuidStateVocabulary, boolean isOrdered)
|
177
|
throws SQLException {
|
178
|
|
179
|
Source source = state.getAlgaTerraConfigurator().getSource();
|
180
|
|
181
|
Map<Integer, State> morphoStates = new HashMap<Integer, State>();
|
182
|
allMorphoStates.put(baseName, morphoStates);
|
183
|
|
184
|
String baseNameCamel = baseName.replace(" ", "");
|
185
|
|
186
|
//make feature
|
187
|
handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName);
|
188
|
|
189
|
//make term vocabulary
|
190
|
String vocDescription = "The vocabulary for the " + baseName + " in AlgaTerra";
|
191
|
String vocLabel = baseName;
|
192
|
String vocAbbrevLabel = null;
|
193
|
URI termSourceUri = null;
|
194
|
TermVocabulary<State> voc = getVocabulary(TermType.State, uuidStateVocabulary,vocDescription, vocLabel, vocAbbrevLabel, termSourceUri, isOrdered, null);
|
195
|
vocabularyMap.put(vocLabel, voc);
|
196
|
|
197
|
String idField = baseNameCamel + "Id";
|
198
|
String sql = "SELECT " + idField + "," + baseNameCamel + ", Description FROM Morpho" + baseNameCamel;
|
199
|
ResultSet rs = source.getResultSet(sql);
|
200
|
while (rs.next()){
|
201
|
Integer id = rs.getInt(idField);
|
202
|
String label = rs.getString(baseNameCamel);
|
203
|
String description = rs.getString("Description");
|
204
|
State term = State.NewInstance(description, label, null);
|
205
|
voc.addTerm(term);
|
206
|
term.addSource(OriginalSourceType.Import, String.valueOf(id), baseNameCamel, state.getTransactionalSourceReference() , null);
|
207
|
morphoStates.put(id, term);
|
208
|
}
|
209
|
getVocabularyService().saveOrUpdate(voc);
|
210
|
}
|
211
|
|
212
|
|
213
|
private Feature handleSingleFeature(AlgaTerraImportState state,
|
214
|
Map<String, Feature> featureMap,
|
215
|
TermVocabulary<Feature> algaTerraMorphoFeatures, String baseName) {
|
216
|
|
217
|
String baseNameCamel = baseName.replace(" ", "");
|
218
|
|
219
|
UUID uuidFeature = null;
|
220
|
uuidFeature = AlgaTerraImportTransformer.getFeatureUuid(baseName);
|
221
|
Feature feature = getFeature(state, uuidFeature, baseName, baseName, null, algaTerraMorphoFeatures);
|
222
|
algaTerraMorphoFeatures.addTerm(feature);
|
223
|
featureMap.put(baseNameCamel, feature);
|
224
|
return feature;
|
225
|
}
|
226
|
|
227
|
private void doNonListenFeatures(AlgaTerraImportState state) throws SQLException{
|
228
|
String baseName = "Apices";
|
229
|
handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName);
|
230
|
|
231
|
baseName = "Chloroplast Number";
|
232
|
handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName);
|
233
|
|
234
|
baseName = "Pyrenoid";
|
235
|
handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName);
|
236
|
|
237
|
baseName = "Cell Wall";
|
238
|
handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName);
|
239
|
|
240
|
baseName = "Reproductive Stages";
|
241
|
handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName);
|
242
|
|
243
|
makeValveFeatures(state, featureMap, algaTerraMorphoFeatures);
|
244
|
|
245
|
}
|
246
|
|
247
|
|
248
|
|
249
|
|
250
|
private void makeValveFeatures(AlgaTerraImportState state,
|
251
|
Map<String, Feature> featureMap2,
|
252
|
TermVocabulary<Feature> algaTerraMorphoFeatures2) {
|
253
|
|
254
|
String baseName = "Valve 1";
|
255
|
handleSingleValve(state, featureMap, algaTerraMorphoFeatures, baseName);
|
256
|
|
257
|
baseName = "Valve 2";
|
258
|
handleSingleValve(state, featureMap, algaTerraMorphoFeatures, baseName);
|
259
|
|
260
|
}
|
261
|
|
262
|
|
263
|
private void handleSingleValve(AlgaTerraImportState state,
|
264
|
Map<String, Feature> featureMap2,
|
265
|
TermVocabulary<Feature> algaTerraMorphoFeatures2, String valveStr) {
|
266
|
|
267
|
Feature featureValve = handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, valveStr);
|
268
|
|
269
|
String baseName = "Striae Frequency";
|
270
|
Feature featureSub = handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName + " " + valveStr);
|
271
|
//TODO is partOf correct here? see also below
|
272
|
featureSub.setPartOf(featureValve);
|
273
|
|
274
|
baseName = "Striae Orientation Mid"; //Mid Valve
|
275
|
featureSub = handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName + " " + valveStr);
|
276
|
featureSub.setPartOf(featureValve);
|
277
|
|
278
|
baseName = "Striae Orientation Apices";
|
279
|
featureSub = handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName + " " + valveStr);
|
280
|
featureSub.setPartOf(featureValve);
|
281
|
|
282
|
baseName = "Central Area";
|
283
|
featureSub = handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName + " " + valveStr);
|
284
|
featureSub.setPartOf(featureValve);
|
285
|
|
286
|
baseName = "Axial Area";
|
287
|
featureSub = handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName + " " + valveStr);
|
288
|
featureSub.setPartOf(featureValve);
|
289
|
|
290
|
baseName = "has Raphe";
|
291
|
featureSub = handleSingleFeature(state, featureMap, algaTerraMorphoFeatures, baseName + " " + valveStr);
|
292
|
featureSub.setPartOf(featureValve);
|
293
|
}
|
294
|
|
295
|
|
296
|
@Override
|
297
|
protected void doInvoke(BerlinModelImportState state) {
|
298
|
AlgaTerraImportState atState = (AlgaTerraImportState)state;
|
299
|
try {
|
300
|
doMorphoListen(atState);
|
301
|
doNonListenFeatures(atState);
|
302
|
} catch (SQLException e) {
|
303
|
throw new RuntimeException(e);
|
304
|
}
|
305
|
super.doInvoke(state);
|
306
|
}
|
307
|
|
308
|
|
309
|
@Override
|
310
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
|
311
|
boolean success = true;
|
312
|
|
313
|
AlgaTerraImportState state = (AlgaTerraImportState)bmState;
|
314
|
Set<SpecimenOrObservationBase> objectsToSave = new HashSet<SpecimenOrObservationBase>();
|
315
|
|
316
|
ResultSet rs = partitioner.getResultSet();
|
317
|
|
318
|
try {
|
319
|
|
320
|
int i = 0;
|
321
|
|
322
|
//for each reference
|
323
|
while (rs.next()){
|
324
|
|
325
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
326
|
|
327
|
int morphoFactId = rs.getInt("MorphoFactId");
|
328
|
String cultureStrainNo = rs.getString("CultureStrainNo");
|
329
|
|
330
|
try {
|
331
|
|
332
|
//source ref
|
333
|
Reference sourceRef = state.getTransactionalSourceReference();
|
334
|
|
335
|
//ecoFact
|
336
|
DerivedUnit ecoFact = makeDerivationFromEcoFact(state, rs, morphoFactId);
|
337
|
if (ecoFact != null){
|
338
|
SpecimenDescription desc = SpecimenDescription.NewInstance();
|
339
|
desc.setTitleCache("Morphology for " + cultureStrainNo , true);
|
340
|
|
341
|
ecoFact.addDescription(desc);
|
342
|
|
343
|
String baseLabel = "Organisation Level";
|
344
|
handleStateTerm(state, rs, baseLabel, desc);
|
345
|
|
346
|
baseLabel = "Growth Form";
|
347
|
handleStateTerm(state, rs, baseLabel, desc);
|
348
|
|
349
|
baseLabel = "Shape";
|
350
|
handleStateTerm(state, rs, baseLabel, desc);
|
351
|
|
352
|
baseLabel = "Symmetry";
|
353
|
handleStateTerm(state, rs, baseLabel, desc);
|
354
|
|
355
|
baseLabel = "Raphe";
|
356
|
handleStateTerm(state, rs, baseLabel, desc);
|
357
|
|
358
|
baseLabel = "Chloroplast Shape";
|
359
|
handleStateTerm(state, rs, baseLabel, desc);
|
360
|
|
361
|
baseLabel = "Chloroplast Structure";
|
362
|
handleStateTerm(state, rs, baseLabel, desc);
|
363
|
|
364
|
baseLabel = "Chloroplast Position";
|
365
|
handleStateTerm(state, rs, baseLabel, desc);
|
366
|
|
367
|
baseLabel = "Apices";
|
368
|
handleTextData(state, rs, baseLabel, desc);
|
369
|
|
370
|
baseLabel = "Chloroplast Number";
|
371
|
handleTextData(state, rs, baseLabel, desc);
|
372
|
|
373
|
baseLabel = "Pyrenoid";
|
374
|
handleTextData(state, rs, baseLabel, desc);
|
375
|
|
376
|
baseLabel = "Cell Wall";
|
377
|
handleTextData(state, rs, baseLabel, desc);
|
378
|
|
379
|
baseLabel = "Reproductive Stages";
|
380
|
handleTextData(state, rs, baseLabel, desc);
|
381
|
|
382
|
//TODO to which object to add this information
|
383
|
this.doId(state, desc, morphoFactId, dbTableName);
|
384
|
String notes = rs.getString("Notes");
|
385
|
this.doNotes(desc, notes);
|
386
|
|
387
|
objectsToSave.add(ecoFact);
|
388
|
}else if (cultureStrainNo != null) {
|
389
|
logger.warn("cultureStrainNo (" + cultureStrainNo + ") exists but no ecoFact found for morphoFact " + morphoFactId);
|
390
|
}else{
|
391
|
logger.info("No cultureStrainNo defined for morphoFact " + morphoFactId);
|
392
|
}
|
393
|
} catch (Exception e) {
|
394
|
logger.warn("Exception in morphoFact: morphoFactId " + morphoFactId + ". " + e.getMessage());
|
395
|
e.printStackTrace();
|
396
|
}
|
397
|
|
398
|
}
|
399
|
|
400
|
//TODO DataEntryBy,
|
401
|
//GrowthFormExplanation, ShapeExplanation, RapheExplanation, ChloroplastExplanation,
|
402
|
//SizeRangeFk, SizeRangeExplanation,
|
403
|
//Valve1RapheFlag, Valve1DescriptionFk,
|
404
|
//Valve2RapheFlag, Valve2DescriptionFk,
|
405
|
//StriaeFrequencyExplanation,
|
406
|
//PyrenoidExplanation,
|
407
|
|
408
|
//Not required: OrganisationLevelExplanation, ValveLinearShape, ValveShape, SymmetryExplanation,
|
409
|
//Rimoportula, Fultoportula, Description, CultureCollection
|
410
|
|
411
|
logger.warn("Specimen to save: " + objectsToSave.size());
|
412
|
getOccurrenceService().save(objectsToSave);
|
413
|
|
414
|
return success;
|
415
|
} catch (SQLException e) {
|
416
|
logger.error("SQLException:" + e);
|
417
|
return false;
|
418
|
}
|
419
|
}
|
420
|
|
421
|
private DerivedUnit makeDerivationFromEcoFact(AlgaTerraImportState state, ResultSet rs, Integer morphoFactId) throws SQLException {
|
422
|
Integer ecoFactFk = nullSafeInt(rs, "ecoFactId");
|
423
|
if (ecoFactFk != null){
|
424
|
|
425
|
DerivedUnit ecoFact = (DerivedUnit)state.getRelatedObject(ECO_FACT_DERIVED_UNIT_NAMESPACE, ecoFactFk.toString());
|
426
|
if (ecoFact == null){
|
427
|
logger.warn("EcoFact is null for ecoFactFk: " + ecoFactFk + ", morphoFactId: " + morphoFactId);
|
428
|
return null;
|
429
|
}else{
|
430
|
return ecoFact;
|
431
|
}
|
432
|
}else{
|
433
|
return null;
|
434
|
}
|
435
|
}
|
436
|
|
437
|
private void handleTextData(AlgaTerraImportState state, ResultSet rs,
|
438
|
String baseLabel, SpecimenDescription desc) throws SQLException {
|
439
|
String baseLabelCamel = baseLabel.replace(" ", "");
|
440
|
|
441
|
String value = rs.getString(baseLabelCamel);
|
442
|
if (value != null){
|
443
|
Feature feature = this.featureMap.get(baseLabelCamel);
|
444
|
if (feature == null){
|
445
|
logger.warn("Feature is null");
|
446
|
}
|
447
|
TextData textData = TextData.NewInstance(feature);
|
448
|
desc.addElement(textData);
|
449
|
}
|
450
|
}
|
451
|
|
452
|
|
453
|
private void handleStateTerm(AlgaTerraImportState algaTerraState, ResultSet rs,
|
454
|
String baseLabel, SpecimenDescription desc) throws SQLException {
|
455
|
|
456
|
String baseLabelCamel = baseLabel.replace(" ", "");
|
457
|
Integer id = nullSafeInt(rs, baseLabelCamel + "Fk");
|
458
|
if (id != null){
|
459
|
Feature feature = this.featureMap.get(baseLabelCamel);
|
460
|
State state = getState(baseLabel, id);
|
461
|
if (feature == null || state == null){
|
462
|
logger.warn("Feature or state is null");
|
463
|
}
|
464
|
DescriptionElementBase deb = CategoricalData.NewInstance(state, feature);
|
465
|
desc.addElement(deb);
|
466
|
}
|
467
|
}
|
468
|
|
469
|
|
470
|
private State getState(String baseLabel, Integer id) {
|
471
|
Map<Integer, State> stateMap = this.algaTerraMorphoStates.get(baseLabel);
|
472
|
return stateMap.get(id);
|
473
|
}
|
474
|
|
475
|
|
476
|
@Override
|
477
|
protected String getDerivedUnitNameSpace(){
|
478
|
return ECO_FACT_DERIVED_UNIT_NAMESPACE;
|
479
|
}
|
480
|
|
481
|
@Override
|
482
|
protected String getFieldObservationNameSpace(){
|
483
|
return ECO_FACT_FIELD_OBSERVATION_NAMESPACE;
|
484
|
}
|
485
|
|
486
|
|
487
|
|
488
|
private void makeParameter(AlgaTerraImportState state, ResultSet rs, DescriptionBase<?> descriptionBase) throws SQLException {
|
489
|
for (int i = 1; i <= 10; i++){
|
490
|
String valueStr = rs.getString(String.format("P%dValue", i));
|
491
|
String unitStr = rs.getString(String.format("P%dUnit", i));
|
492
|
String parameter = rs.getString(String.format("P%dParameter", i));
|
493
|
String method = rs.getString(String.format("P%dMethod", i));
|
494
|
|
495
|
//method
|
496
|
if (StringUtils.isNotBlank(method)){
|
497
|
logger.warn("Methods not yet handled: " + method);
|
498
|
}
|
499
|
//parameter
|
500
|
TermVocabulary<Feature> vocParameter = getVocabulary(TermType.Feature, uuidVocParameter, "Feature vocabulary for AlgaTerra measurement parameters", "Parameters", null, null, false, Feature.COMMON_NAME());
|
501
|
if (StringUtils.isNotBlank(parameter)){
|
502
|
UUID featureUuid = getParameterFeatureUuid(state, parameter);
|
503
|
Feature feature = getFeature(state, featureUuid, parameter, parameter, null, vocParameter);
|
504
|
QuantitativeData quantData = QuantitativeData.NewInstance(feature);
|
505
|
|
506
|
//unit
|
507
|
MeasurementUnit unit = getMeasurementUnit(state, unitStr);
|
508
|
quantData.setUnit(unit);
|
509
|
try {
|
510
|
|
511
|
Set<DefinedTerm> valueModifier = new HashSet<DefinedTerm>();
|
512
|
valueStr = normalizeAndModifyValue(state, valueStr, valueModifier);
|
513
|
//value
|
514
|
Float valueFlt = Float.valueOf(valueStr); //TODO maybe change model to Double ??
|
515
|
|
516
|
StatisticalMeasure measureSingleValue = getStatisticalMeasure(state, uuidStatMeasureSingleValue, "Value", "Single measurement value", null, null);
|
517
|
StatisticalMeasurementValue value = StatisticalMeasurementValue.NewInstance(measureSingleValue, valueFlt);
|
518
|
quantData.addStatisticalValue(value);
|
519
|
descriptionBase.addElement(quantData);
|
520
|
|
521
|
} catch (NumberFormatException e) {
|
522
|
logger.warn(String.format("Value '%s' can't be converted to double. Parameter %s not imported.", valueStr, parameter));
|
523
|
}
|
524
|
}else if (isNotBlank(valueStr) || isNotBlank(unitStr) ){
|
525
|
logger.warn("There is value or unit without parameter: " + i);
|
526
|
}
|
527
|
|
528
|
|
529
|
}
|
530
|
|
531
|
}
|
532
|
|
533
|
private String normalizeAndModifyValue(AlgaTerraImportState state, String valueStr, Set<DefinedTerm> valueModifier) {
|
534
|
valueStr = valueStr.replace(",", ".");
|
535
|
if (valueStr.startsWith("<")){
|
536
|
TermVocabulary<DefinedTerm> measurementValueModifierVocabulary = getVocabulary(TermType.Modifier, uuidMeasurementValueModifier, "Measurement value modifier", "Measurement value modifier", null, null, false, DefinedTerm.NewModifierInstance(null, null, null));
|
537
|
DefinedTerm modifier = getModifier(state, uuidModifierLowerThan, "Lower", "Lower than the given measurement value", "<", measurementValueModifierVocabulary);
|
538
|
valueModifier.add(modifier);
|
539
|
valueStr = valueStr.replace("<", "");
|
540
|
}
|
541
|
if (valueStr.startsWith(">")){
|
542
|
TermVocabulary<DefinedTerm> measurementValueModifierVocabulary = getVocabulary(TermType.Modifier, uuidMeasurementValueModifier, "Measurement value modifier", "Measurement value modifier", null, null, false, DefinedTerm.NewModifierInstance(null, null, null));
|
543
|
DefinedTerm modifier = getModifier(state, uuidModifierGreaterThan, "Lower", "Lower than the given measurement value", "<", measurementValueModifierVocabulary);
|
544
|
valueModifier.add(modifier);
|
545
|
valueStr = valueStr.replace(">", "");
|
546
|
}
|
547
|
return valueStr;
|
548
|
}
|
549
|
|
550
|
|
551
|
|
552
|
private UUID getParameterFeatureUuid(AlgaTerraImportState state, String key) {
|
553
|
return AlgaTerraImportTransformer.getFeatureUuid(key);
|
554
|
}
|
555
|
|
556
|
|
557
|
|
558
|
/**
|
559
|
* TODO move to InputTransformerBase
|
560
|
* @param state
|
561
|
* @param unitStr
|
562
|
* @return
|
563
|
*/
|
564
|
private MeasurementUnit getMeasurementUnit(AlgaTerraImportState state, String unitStr) {
|
565
|
if (StringUtils.isNotBlank(unitStr)){
|
566
|
UUID uuid = AlgaTerraImportTransformer.getMeasurementUnitUuid(unitStr);
|
567
|
if (uuid != null){
|
568
|
return getMeasurementUnit(state, uuid, unitStr, unitStr, unitStr, null);
|
569
|
}else{
|
570
|
logger.warn("MeasurementUnit was not recognized");
|
571
|
return null;
|
572
|
}
|
573
|
}else{
|
574
|
return null;
|
575
|
}
|
576
|
}
|
577
|
|
578
|
|
579
|
@Override
|
580
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
581
|
String nameSpace;
|
582
|
Class<?> cdmClass;
|
583
|
Set<String> idSet;
|
584
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
585
|
|
586
|
try{
|
587
|
Set<String> ecoFactFkSet = new HashSet<String>();
|
588
|
|
589
|
while (rs.next()){
|
590
|
handleForeignKey(rs, ecoFactFkSet, "ecoFactId");
|
591
|
}
|
592
|
|
593
|
//eco fact derived unit map
|
594
|
nameSpace = AlgaTerraFactEcologyImport.ECO_FACT_DERIVED_UNIT_NAMESPACE;
|
595
|
cdmClass = DerivedUnit.class;
|
596
|
idSet = ecoFactFkSet;
|
597
|
Map<String, DerivedUnit> derivedUnitMap = (Map<String, DerivedUnit>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
598
|
result.put(nameSpace, derivedUnitMap);
|
599
|
|
600
|
} catch (SQLException e) {
|
601
|
throw new RuntimeException(e);
|
602
|
}
|
603
|
return result;
|
604
|
}
|
605
|
|
606
|
@Override
|
607
|
protected boolean doCheck(BerlinModelImportState state){
|
608
|
IOValidator<BerlinModelImportState> validator = new AlgaTerraMorphologyImportValidator();
|
609
|
return validator.validate(state);
|
610
|
}
|
611
|
|
612
|
@Override
|
613
|
protected boolean isIgnore(BerlinModelImportState state){
|
614
|
return ! ((AlgaTerraImportState)state).getAlgaTerraConfigurator().isDoMorphology();
|
615
|
}
|
616
|
|
617
|
}
|