minor
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / algaterra / AlgaTerraSpecimenImportBase.java
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.sql.Date;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
23 import org.springframework.transaction.TransactionStatus;
24
25 import eu.etaxonomy.cdm.common.URI;
26 import eu.etaxonomy.cdm.facade.DerivedUnitFacade;
27 import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase;
28 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
29 import eu.etaxonomy.cdm.io.common.Source;
30 import eu.etaxonomy.cdm.io.common.TdwgAreaProvider;
31 import eu.etaxonomy.cdm.model.agent.Team;
32 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33 import eu.etaxonomy.cdm.model.common.Annotation;
34 import eu.etaxonomy.cdm.model.common.AnnotationType;
35 import eu.etaxonomy.cdm.model.common.Language;
36 import eu.etaxonomy.cdm.model.common.TimePeriod;
37 import eu.etaxonomy.cdm.model.description.DescriptionBase;
38 import eu.etaxonomy.cdm.model.description.Feature;
39 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
40 import eu.etaxonomy.cdm.model.description.State;
41 import eu.etaxonomy.cdm.model.description.TaxonDescription;
42 import eu.etaxonomy.cdm.model.location.Country;
43 import eu.etaxonomy.cdm.model.location.NamedArea;
44 import eu.etaxonomy.cdm.model.location.Point;
45 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
46 import eu.etaxonomy.cdm.model.occurrence.Collection;
47 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
48 import eu.etaxonomy.cdm.model.reference.Reference;
49 import eu.etaxonomy.cdm.model.taxon.Taxon;
50 import eu.etaxonomy.cdm.model.term.DefinedTerm;
51 import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
52 import eu.etaxonomy.cdm.model.term.TermType;
53 import eu.etaxonomy.cdm.model.term.TermVocabulary;
54
55 /**
56 * @author a.mueller
57 * @since 12.09.2012
58 */
59 public abstract class AlgaTerraSpecimenImportBase extends BerlinModelImportBase{
60 private static final long serialVersionUID = -1741703900571072861L;
61
62 private static final Logger logger = LogManager.getLogger();
63
64 public static final String ECO_FACT_FIELD_OBSERVATION_NAMESPACE = "EcoFact_FieldObservation";
65 public static final String ECO_FACT_DERIVED_UNIT_NAMESPACE = "EcoFact_DerivedUnit";
66 public static final String TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE = "TypeSpecimen_FieldObservation";
67 public static final String TYPE_SPECIMEN_DERIVED_UNIT_NAMESPACE = "TypeSpecimen_DerivedUnit";
68 public static final String FACT_ECOLOGY_NAMESPACE = "Fact (Ecology)";
69
70
71 public static final String TERMS_NAMESPACE = "ALGA_TERRA_TERMS";
72
73 //TODO move to transformrer
74 final static UUID uuidMarkerAlkalinity = UUID.fromString("e52d0ea2-0c1f-4d95-ae6d-e21ab317c594");
75 final static UUID uuidRefSystemGps = UUID.fromString("c23e4928-c137-4e4a-b6ab-b430da3d0b94");
76 public final static UUID uuidFeatureSpecimenCommunity = UUID.fromString("3ff5b1ab-3999-4b5a-b8f7-01fd2f6c12c7");
77 public final static UUID uuidFeatureAdditionalData = UUID.fromString("0ac82ab8-2c2b-4953-98eb-a9f718eb9c57");
78 public final static UUID uuidFeatureHabitatExplanation = UUID.fromString("6fe32295-61a3-44fc-9fcf-a85790ea888f");
79
80 final static UUID uuidVocAlgaTerraClimate = UUID.fromString("b0a677c6-8bb6-43f4-b1b8-fc377a10feb5");
81 final static UUID uuidVocAlgaTerraHabitat = UUID.fromString("06f30114-e19c-4e7d-a8e5-5488c41fcbc5");
82 final static UUID uuidVocAlgaTerraLifeForm = UUID.fromString("3c0b194e-809c-4b42-9498-6ff034066ed7");
83
84 public final static UUID uuidFeatureAlgaTerraClimate = UUID.fromString("8754674c-9ab9-4f28-95f1-91eeee2314ee");
85 public final static UUID uuidFeatureAlgaTerraHabitat = UUID.fromString("7def3fc2-cdc5-4739-8e13-62edbd053415");
86 public final static UUID uuidFeatureAlgaTerraLifeForm = UUID.fromString("9b657901-1b0d-4a2a-8d21-dd8c1413e2e6");
87
88 final static UUID uuidVocParameter = UUID.fromString("45888b40-5bbb-4293-aa1e-02479796cd7c");
89 final static UUID uuidStatMeasureSingleValue = UUID.fromString("eb4c3d98-4d4b-4c37-8eb4-17315ce79920");
90 final static UUID uuidMeasurementValueModifier = UUID.fromString("0218a7a3-f6c0-4d06-a4f8-6b50b73aef5e");
91
92 final static UUID uuidModifierLowerThan = UUID.fromString("2b500085-6bef-4003-b6ea-e0ad0237d79d");
93 final static UUID uuidModifierGreaterThan = UUID.fromString("828df49d-c745-48f7-b083-0ada43356c34");
94
95 public AlgaTerraSpecimenImportBase(String tableName, String pluralString) {
96 super(tableName, pluralString);
97 }
98
99 /**
100 * Creates the vocabularies and the features for Climate, Habitat and Lifeform
101 * @param state
102 * @throws SQLException
103 */
104 protected void makeVocabulariesAndFeatures(AlgaTerraImportState state) throws SQLException {
105 String abbrevLabel = null;
106 URI uri = null;
107
108 if (! state.isSpecimenVocabulariesCreated()){
109
110 TransactionStatus txStatus = this.startTransaction();
111
112 boolean isOrdered = true;
113 State tmp = State.NewInstance();
114 OrderedTermVocabulary<State> climateVoc = (OrderedTermVocabulary<State>)getVocabulary(state, TermType.State, uuidVocAlgaTerraClimate, "Climate", "Climate", abbrevLabel, uri, isOrdered, tmp);
115 OrderedTermVocabulary<State> habitatVoc = (OrderedTermVocabulary<State>)getVocabulary(state, TermType.State, uuidVocAlgaTerraHabitat, "Habitat", "Habitat", abbrevLabel, uri, isOrdered, tmp);
116 OrderedTermVocabulary<State> lifeformVoc = (OrderedTermVocabulary<State>)getVocabulary(state, TermType.State, uuidVocAlgaTerraLifeForm, "Lifeform", "Lifeform", abbrevLabel, uri, isOrdered, tmp);
117
118 Feature feature = getFeature(state, uuidFeatureAlgaTerraClimate, "Climate","Climate", null, null);
119 feature.setSupportsCategoricalData(true);
120
121 feature = getFeature(state, uuidFeatureAlgaTerraLifeForm, "LifeForm","LifeForm", null, null);
122 feature.setSupportsCategoricalData(true);
123
124 feature = Feature.HABITAT();
125 feature.setSupportsCategoricalData(true);
126 getTermService().saveOrUpdate(feature);
127
128 Source source = state.getAlgaTerraConfigurator().getSource();
129
130 String climateSql = "SELECT * FROM EcoClimate";
131 ResultSet rs = source.getResultSet(climateSql);
132 while (rs.next()){
133 String climate = rs.getString("Climate");
134 String description = rs.getString("Description");
135 Integer id = rs.getInt("ClimateId");
136 UUID uuid = UUID.fromString(rs.getString("UUID"));
137 State stateTerm = getStateTerm(state, uuid, climate, description, null, climateVoc);
138 addOriginalSource(stateTerm, id.toString(), "EcoClimate", state.getTransactionalSourceReference());
139 getTermService().saveOrUpdate(stateTerm);
140 }
141
142 String habitatSql = "SELECT * FROM EcoHabitat";
143 rs = source.getResultSet(habitatSql);
144 while (rs.next()){
145 String habitat = rs.getString("Habitat");
146 String description = rs.getString("Description");
147 Integer id = rs.getInt("HabitatId");
148 UUID uuid = UUID.fromString(rs.getString("UUID"));
149 State stateTerm = getStateTerm(state, uuid, habitat, description, null, habitatVoc);
150 addOriginalSource(stateTerm, id.toString(), "EcoHabitat", state.getTransactionalSourceReference());
151 getTermService().saveOrUpdate(stateTerm);
152 }
153
154 String lifeformSql = "SELECT * FROM EcoLifeForm";
155 rs = source.getResultSet(lifeformSql);
156 while (rs.next()){
157 String lifeform = rs.getString("LifeForm");
158 String description = rs.getString("Description");
159 Integer id = rs.getInt("LifeFormId");
160 UUID uuid = UUID.fromString(rs.getString("UUID"));
161 State stateTerm = getStateTerm(state, uuid, lifeform, description, null, lifeformVoc);
162 addOriginalSource(stateTerm, id.toString(), "EcoLifeForm", state.getTransactionalSourceReference());
163 getTermService().saveOrUpdate(stateTerm);
164 }
165
166 //material category
167 TermVocabulary<DefinedTerm> materialCategoryVoc = getVocabulary(state, TermType.KindOfUnit, AlgaTerraImportTransformer.uuidKindOfUnitVoc, "Alga Terra Material Category", "Alga Terra Material Category", abbrevLabel, uri, false, DefinedTerm.NewKindOfUnitInstance(null, null, null));
168 getVocabularyService().save(materialCategoryVoc);
169
170 String materialSql = "SELECT * FROM MaterialCategory WHERE MaterialCategoryId <> 16 ";
171 rs = source.getResultSet(materialSql);
172 while (rs.next()){
173 Integer id = rs.getInt("MaterialCategoryId");
174 String category = rs.getString("MaterialCategory");
175 String description = rs.getString("Description");
176 UUID uuid = UUID.randomUUID();
177
178 DefinedTerm kindOfUnit = DefinedTerm.NewKindOfUnitInstance(description, category, null);
179 kindOfUnit.setUuid(uuid);
180 addOriginalSource(kindOfUnit, id.toString(), "MaterialCategory", state.getTransactionalSourceReference());
181 materialCategoryVoc.addTerm(kindOfUnit);
182 getTermService().saveOrUpdate(kindOfUnit);
183 materialCategoryMapping.put(id, uuid);
184 }
185
186 //areas
187 OrderedTermVocabulary<NamedArea> informalAreasVoc = (OrderedTermVocabulary<NamedArea>)getVocabulary(state, TermType.NamedArea, AlgaTerraImportTransformer.uuidNamedAreaVocAlgaTerraInformalAreas, "AlgaTerra Specific Areas", "AlgaTerra Specific Areas", abbrevLabel, uri, true, NamedArea.NewInstance());
188 getVocabularyService().save(informalAreasVoc);
189
190 String areaSql = "SELECT * FROM TDWGGazetteer WHERE subL4 = 1 ";
191 rs = source.getResultSet(areaSql);
192 while (rs.next()){
193 String l1Code = rs.getString("L1Code");
194 String l2Code = rs.getString("L2Code");
195 String l3Code = rs.getString("L3Code");
196 String l4Code = rs.getString("L4Code");
197 String gazetteer = rs.getString("Gazetteer");
198 Integer id = rs.getInt("ID");
199 String notes = rs.getString("Notes");
200 //TODO stable uuids
201 // UUID uuid = UUID.fromString(rs.getString("UUID"));
202 UUID uuid = UUID.randomUUID();
203 subL4Mapping.put(id, uuid);
204
205 String tdwgCode = (l4Code != null) ? l4Code : (l3Code != null) ? l3Code : (l2Code != null) ? l2Code : l1Code;
206
207 NamedArea tdwgArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(tdwgCode);
208 NamedArea newArea = getNamedArea(state, uuid ,gazetteer, gazetteer, null, null, null, informalAreasVoc, TermMatchMode.UUID_ONLY, null);
209 if (isNotBlank(notes)){
210 newArea.addAnnotation(Annotation.NewInstance(notes, AnnotationType.EDITORIAL(), Language.DEFAULT()));
211 }
212
213 addOriginalSource(newArea, id.toString(), "TDWGGazetteer", state.getTransactionalSourceReference());
214 getTermService().saveOrUpdate(newArea);
215 newArea.setPartOf(tdwgArea);
216 informalAreasVoc.addTerm(newArea);
217 }
218
219 this.commitTransaction(txStatus);
220
221 state.setSpecimenVocabulariesCreated(true);
222 }
223 }
224
225 //tmp
226 static Map<Integer, UUID> subL4Mapping = new HashMap<>();
227 static Map<Integer, UUID> materialCategoryMapping = new HashMap<>();
228
229 protected String getLocalityString(){
230 return "Locality";
231 }
232
233 protected void handleFieldObservationSpecimen(ResultSet rs, DerivedUnitFacade facade, AlgaTerraImportState state, ResultSetPartitioner partitioner) throws SQLException {
234 //FIXME missing fields #3084, #3085, #3080
235 try {
236
237 Integer unitId = nullSafeInt(rs, "unitId");
238 String locality = rs.getString(getLocalityString());
239 Double latitude = nullSafeDouble(rs, "Latitude");
240 Double longitude = nullSafeDouble(rs, "Longitude");
241 Integer errorRadius = nullSafeInt(rs,"Prec");
242 String geoCodeMethod = rs.getString("GeoCodeMethod");
243
244 Integer altitude = nullSafeInt(rs, "Altitude");
245 Integer lowerAltitude = nullSafeInt(rs, "AltitudeLowerValue");
246 String altitudeUnit = rs.getString("AltitudeUnit");
247 Double depth = nullSafeDouble(rs, "Depth");
248 Double depthLow = nullSafeDouble(rs, "DepthLow");
249
250 String collectorsNumber = rs.getString("CollectorsNumber");
251 Date collectionDateStart = rs.getDate("CollectionDate");
252 Date collectionDateEnd = rs.getDate("CollectionDateEnd");
253
254 //location
255 facade.setLocality(locality);
256
257 //exact location
258 ReferenceSystem referenceSystem = makeRefrenceSystem(geoCodeMethod, state);
259 if (longitude != null || latitude != null || referenceSystem != null || errorRadius != null){
260 Point exactLocation = Point.NewInstance(longitude, latitude, referenceSystem, errorRadius);
261 facade.setExactLocation(exactLocation);
262 }
263
264 //altitude, depth
265 if (StringUtils.isNotBlank(altitudeUnit) && ! altitudeUnit.trim().equalsIgnoreCase("m")){
266 logger.warn("Altitude unit is not [m] but: " + altitudeUnit);
267 }
268 if ( altitude != null){
269 if (lowerAltitude == null){
270 facade.setAbsoluteElevation(altitude);
271 }else{
272 facade.setAbsoluteElevationRange(lowerAltitude,altitude);
273 }
274 }
275 if ( depth != null){
276 if (depthLow == null){
277 facade.setDistanceToWaterSurface(depth);
278 }else{
279 //TODO which direction is correct?
280 facade.setDistanceToWaterSurfaceRange(depth, depthLow);
281 }
282 }
283
284 //field
285 facade.setFieldNumber(collectorsNumber);
286 TimePeriod gatheringPeriod = TimePeriod.NewInstance(collectionDateStart, collectionDateEnd);
287 facade.setGatheringPeriod(gatheringPeriod);
288 handleCollectorTeam(state, facade, rs);
289
290 //areas
291 makeAreas(state, rs, facade);
292
293 //notes
294 //=> not required according to Henning
295
296 //id, created, updated, notes
297 if (unitId != null){
298 this.doIdCreatedUpdatedNotes(state, facade.innerFieldUnit(), rs, unitId, getFieldObservationNameSpace());
299 }else{
300 logger.warn("FieldObservation has no unitId: " + facade.innerFieldUnit() + ": " + getFieldObservationNameSpace());
301 }
302 } catch (Exception e) {
303 throw new RuntimeException(e);
304 }
305
306 }
307
308 protected void handleFirstDerivedSpecimen(ResultSet rs, DerivedUnitFacade facade, AlgaTerraImportState state, ResultSetPartitioner partitioner) throws SQLException {
309 Integer unitId = nullSafeInt(rs, "unitId");
310 Integer collectionFk = nullSafeInt(rs,"CollectionFk");
311 String label = rs.getString("Label");
312
313 //collection
314 if (collectionFk != null){
315 Collection subCollection = state.getRelatedObject(AlgaTerraCollectionImport.NAMESPACE_SUBCOLLECTION, String.valueOf(collectionFk), Collection.class);
316 if (subCollection != null){
317 facade.setCollection(subCollection);
318 }else{
319 Collection collection = state.getRelatedObject(AlgaTerraCollectionImport.NAMESPACE_COLLECTION, String.valueOf(collectionFk), Collection.class);
320 if (collection == null){
321 logger.warn("Collection for collectionFK " + collectionFk + " can not be found.");
322 }
323 facade.setCollection(collection);
324 }
325 }
326
327 //Label
328 if (isNotBlank(label)){
329 //TODO implement label #4218, #3090, #3084
330 logger.warn("Label not yet implemented for specimen, #4218, #3090, #3084");
331 }
332
333 //TODO id, created for fact + ecoFact
334 // this.doIdCreatedUpdatedNotes(state, descriptionElement, rs, id, namespace);
335 if (unitId != null){
336 this.doIdCreatedUpdatedNotes(state, facade.innerDerivedUnit(), rs, unitId, getDerivedUnitNameSpace());
337 }else{
338 logger.warn("Specimen has no unitId: " + facade.innerDerivedUnit() + ": " + getDerivedUnitNameSpace());
339 }
340 }
341
342
343
344 protected abstract String getDerivedUnitNameSpace();
345
346 protected abstract String getFieldObservationNameSpace();
347
348
349 protected DescriptionBase<?> getFieldObservationDescription(DerivedUnitFacade facade) {
350 Set<DescriptionBase<?>> descriptions = (Set)facade.innerFieldUnit().getDescriptions();
351 for (DescriptionBase<?> desc : descriptions){
352 if (desc.isImageGallery() == false){
353 return desc;
354 }
355 }
356 SpecimenDescription specDesc = SpecimenDescription.NewInstance(facade.innerFieldUnit());
357 descriptions.add(specDesc);
358 return specDesc;
359 }
360
361
362 private void makeAreas(AlgaTerraImportState state, ResultSet rs, DerivedUnitFacade facade) throws SQLException {
363 Integer gazetteerId = nullSafeInt(rs, "GazetteerId");
364 if (gazetteerId != null){
365 //TDWG
366 NamedArea tdwgArea;
367 String tdwg4 = rs.getString("L4Code");
368 if (isNotBlank(tdwg4)){
369 tdwgArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(tdwg4);
370 }else{
371 String tdwg3 = rs.getString("L3Code");
372 if (isNotBlank(tdwg3)){
373 tdwgArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(tdwg3);
374 }else{
375 Number tdwg2D = nullSafeDouble(rs, "L2Code");
376 if (tdwg2D != null){
377 Integer tdwg2 = tdwg2D.intValue();
378 tdwgArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(String.valueOf(tdwg2));
379 }else{
380 Number tdwg1D = nullSafeDouble(rs, "L1Code");
381 if (tdwg1D != null){
382 Integer tdwg1 = tdwg1D.intValue();
383 tdwgArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(String.valueOf(tdwg1));
384 }else{
385 tdwgArea = null;
386 }
387 }
388 }
389 }
390 if (tdwgArea == null){
391 logger.warn("TDWG area could not be defined for gazetterId: " + gazetteerId);
392 }else{
393 facade.addCollectingArea(tdwgArea);
394 }
395
396 //Countries
397 Country country = null;
398 String isoCountry = rs.getString("ISOCountry");
399 String countryStr = rs.getString("Country");
400 if (isNotBlank(isoCountry)){
401 country = Country.getCountryByIso3166A2(isoCountry);
402 }else if (isNotBlank(countryStr)){
403 logger.warn("Country exists but no ISO code");
404 }else{
405
406 }
407
408 NamedArea subL4Area = null;
409 Boolean subL4 = nullSafeBoolean(rs, "subL4");
410 if (subL4 != null && subL4.booleanValue() == true){
411 subL4Area = makeSubL4Area(state, gazetteerId);
412 if (subL4Area != null){
413 facade.addCollectingArea(subL4Area);
414 }else{
415 logger.warn("SubL4 area not found for gazetteerId: " + gazetteerId);
416 }
417 }
418
419 if (country == null ){
420 if (! gazetteerId.equals(40)){//special handling for Borneo, TDWG area is enough here as it matches exactly
421 if (subL4Area == null ){
422 logger.warn("Country does not exist and SubL4 could not be found for GazetteerID " + gazetteerId);
423 }else {
424 logger.info("Country could not be defined but subL4 area was added");
425 }
426 }
427 }else{
428 facade.setCountry(country);
429 }
430
431 }
432
433 //Waterbody
434 NamedArea waterbody = null;
435 String waterbodyStr = rs.getString("WaterBody");
436 if (isNotBlank(waterbodyStr)){
437 if (waterbodyStr.equals("Atlantic Ocean")){
438 waterbody = NamedArea.ATLANTICOCEAN();
439 }else if (waterbodyStr.equals("Pacific Ocean")){
440 waterbody = NamedArea.PACIFICOCEAN();
441 }else if (waterbodyStr.equals("Indian Ocean")){
442 waterbody = NamedArea.INDIANOCEAN();
443 }else if (waterbodyStr.equals("Arctic Ocean")){
444 waterbody = NamedArea.ARCTICOCEAN();
445 }else{
446 logger.warn("Waterbody not recognized: " + waterbody);
447 }
448 if (waterbody != null){
449 facade.addCollectingArea(waterbody);
450 }
451 }
452
453
454 //countries sub
455 //TODO -> SpecimenImport (not existing in TypeSpecimen)
456 }
457
458
459 private NamedArea makeSubL4Area(AlgaTerraImportState state, Integer gazetteerId) {
460 UUID uuid = subL4Mapping.get(gazetteerId);
461 NamedArea area = (NamedArea)getTermService().find(uuid);
462 if (area == null){
463 logger.warn("SubL4 area could not be found in repository");
464 }
465 return area;
466 }
467
468 private boolean handleMissingCountry(AlgaTerraImportState state, DerivedUnitFacade facade, Integer gazetteerId) {
469 NamedArea area = null;
470 if (gazetteerId != null){
471 if (gazetteerId.equals(42)){
472 area = getNamedArea(state, AlgaTerraImportTransformer.uuidNamedAreaBorneo, null, null, null, null, null);
473 }else if (gazetteerId.equals(1684)){
474 area = getNamedArea(state, AlgaTerraImportTransformer.uuidNamedAreaPatagonia, null, null, null, null, null);
475 }else if (gazetteerId.equals(2167)){
476 area = getNamedArea(state, AlgaTerraImportTransformer.uuidNamedAreaTierraDelFuego, null, null, null, null, null);
477 }
478 }
479 if (area != null){
480 facade.addCollectingArea(area);
481 return true;
482 }
483 return false;
484
485 }
486
487 protected SpecimenOrObservationType makeDerivedUnitType(String recordBasis) {
488 SpecimenOrObservationType result = null;
489 if (StringUtils.isBlank(recordBasis)){
490 result = SpecimenOrObservationType.DerivedUnit;
491 } else if (recordBasis.equalsIgnoreCase("FossileSpecimen")){
492 result = SpecimenOrObservationType.Fossil;
493 }else if (recordBasis.equalsIgnoreCase("HumanObservation")){
494 result = SpecimenOrObservationType.HumanObservation;
495 }else if (recordBasis.equalsIgnoreCase("Literature")){
496 //FIXME
497 logger.warn("Literature record basis not yet supported");
498 result = SpecimenOrObservationType.DerivedUnit;
499 }else if (recordBasis.equalsIgnoreCase("LivingSpecimen")){
500 result = SpecimenOrObservationType.LivingSpecimen;
501 }else if (recordBasis.equalsIgnoreCase("MachineObservation")){
502 result = SpecimenOrObservationType.MachineObservation;
503 }else if (recordBasis.equalsIgnoreCase("Observation")){
504 result = SpecimenOrObservationType.Observation;
505 }else if (recordBasis.equalsIgnoreCase("LivingCulture")){
506 //FIXME
507 logger.warn("LivingCulture record basis not yet supported");
508 result = SpecimenOrObservationType.DerivedUnit;
509 }else if (recordBasis.equalsIgnoreCase("PreservedSpecimen")){
510 result = SpecimenOrObservationType.PreservedSpecimen;
511 }
512 return result;
513 }
514
515
516 protected Feature makeFeature(SpecimenOrObservationType type, AlgaTerraImportState state) {
517 if (type.equals(SpecimenOrObservationType.DerivedUnit)){
518 return Feature.INDIVIDUALS_ASSOCIATION();
519 }else if (type.isFeatureObservation()){
520 return Feature.OBSERVATION();
521 }else if (type.isPreservedSpecimen()){
522 return Feature.SPECIMEN();
523 }else if (type.equals(SpecimenOrObservationType.LivingSpecimen)){
524 UUID uuid = AlgaTerraImportTransformer.uuidFeatureLivingSpecimen;
525 Feature feature = getFeature(state, uuid, "Living Specimen", "Living Specimen", null, Feature.SPECIMEN().getVocabulary());
526 if (feature == null){
527 logger.warn("Living Specimen Feature could not be created");
528 }
529 return feature;
530 }
531 logger.warn("No feature defined for derived unit type: " + type);
532 return null;
533 }
534
535 private ReferenceSystem makeRefrenceSystem(String geoCodeMethod, AlgaTerraImportState state) {
536 if (StringUtils.isBlank(geoCodeMethod)){
537 return null;
538 }else if(geoCodeMethod.startsWith("GPS")){
539 getReferenceSystem(state, uuidRefSystemGps, "GPS", "GPS", "GPS", ReferenceSystem.GOOGLE_EARTH().getVocabulary());
540 return ReferenceSystem.WGS84();
541 }else if(geoCodeMethod.startsWith("Google")){
542 return ReferenceSystem.GOOGLE_EARTH();
543 }else if(geoCodeMethod.startsWith("Map")){
544 return ReferenceSystem.MAP();
545 }else if(geoCodeMethod.startsWith("WikiProjekt Georeferenzierung") || geoCodeMethod.startsWith("http://toolserver.org/~geohack/geohack.php") ){
546 return ReferenceSystem.WGS84();
547 }else {
548 logger.warn("Reference system " + geoCodeMethod + " not yet supported.");
549 return null;
550 }
551 }
552
553
554
555
556 private void handleCollectorTeam(AlgaTerraImportState state, DerivedUnitFacade facade, ResultSet rs) throws SQLException {
557 String collector = rs.getString("Collector");
558 TeamOrPersonBase<?> author = getAuthor(collector);
559 facade.setCollector(author);
560 }
561
562 /**
563 * @param facade
564 * @param collector
565 */
566 protected TeamOrPersonBase<?> getAuthor(String author) {
567 // FIXME TODO parsen und deduplizieren
568 Team team = Team.NewTitledInstance(author, author);
569 return team;
570 }
571
572
573 /**
574 * Use same TaxonDescription if two records belong to the same taxon
575 * @param state
576 * @param newTaxonId
577 * @param oldTaxonId
578 * @param oldDescription
579 * @param taxonMap
580 * @return
581 */
582 protected TaxonDescription getTaxonDescription(AlgaTerraImportState state, Taxon taxon, Reference sourceSec){
583 TaxonDescription result = null;
584 Set<TaxonDescription> descriptionSet= taxon.getDescriptions();
585 if (descriptionSet.size() > 0) {
586 result = descriptionSet.iterator().next();
587 }else{
588 result = TaxonDescription.NewInstance();
589 result.setTitleCache(sourceSec.getTitleCache(), true);
590 taxon.addDescription(result);
591 }
592 return result;
593 }
594
595
596
597
598 }