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