Project

General

Profile

Download (13.2 KB) Statistics
| Branch: | Revision:
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.Date;
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.springframework.transaction.TransactionStatus;
22

    
23
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
24
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase;
25
import eu.etaxonomy.cdm.io.common.Source;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
28
import eu.etaxonomy.cdm.model.common.TimePeriod;
29
import eu.etaxonomy.cdm.model.description.DescriptionBase;
30
import eu.etaxonomy.cdm.model.description.Feature;
31
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
32
import eu.etaxonomy.cdm.model.description.State;
33
import eu.etaxonomy.cdm.model.location.NamedArea;
34
import eu.etaxonomy.cdm.model.location.Point;
35
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
36
import eu.etaxonomy.cdm.model.location.TdwgArea;
37
import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
38

    
39
/**
40
 * @author a.mueller
41
 * @created 12.09.2012
42
 */
43
public abstract class AlgaTerraSpecimenImportBase extends BerlinModelImportBase{
44
	private static final Logger logger = Logger.getLogger(AlgaTerraSpecimenImportBase.class);
45

    
46
	public static final String ECO_FACT_NAMESPACE = "EcoFact";
47
	public static final String TYPE_SPECIMEN_NAMESPACE = "TypeSpecimen";
48
	public static final String TERMS_NAMESPACE = "ALGA_TERRA_TERMS";
49
	
50
	//TODO move to transformrer
51
	final static UUID uuidMarkerAlkalinity = UUID.fromString("e52d0ea2-0c1f-4d95-ae6d-e21ab317c594");  
52
	final static UUID uuidRefSystemGps = UUID.fromString("c23e4928-c137-4e4a-b6ab-b430da3d0b94");  
53
	final static UUID uuidFeatureSpecimenCommunity = UUID.fromString("3ff5b1ab-3999-4b5a-b8f7-01fd2f6c12c7");
54
	final static UUID uuidFeatureAdditionalData = UUID.fromString("0ac82ab8-2c2b-4953-98eb-a9f718eb9c57");
55
	final static UUID uuidFeatureHabitatExplanation = UUID.fromString("6fe32295-61a3-44fc-9fcf-a85790ea888f");
56
	
57
	final static UUID uuidVocAlgaTerraClimate = UUID.fromString("b0a677c6-8bb6-43f4-b1b8-fc377a10feb5");
58
	final static UUID uuidVocAlgaTerraHabitat = UUID.fromString("06f30114-e19c-4e7d-a8e5-5488c41fcbc5");
59
	final static UUID uuidVocAlgaTerraLifeForm = UUID.fromString("3c0b194e-809c-4b42-9498-6ff034066ed7");
60
	
61
	final static UUID uuidFeatureAlgaTerraClimate = UUID.fromString("8754674c-9ab9-4f28-95f1-91eeee2314ee");
62
	final static UUID uuidFeatureAlgaTerraHabitat = UUID.fromString("7def3fc2-cdc5-4739-8e13-62edbd053415");
63
	final static UUID uuidFeatureAlgaTerraLifeForm = UUID.fromString("9b657901-1b0d-4a2a-8d21-dd8c1413e2e6");
64
	
65
	final static UUID uuidVocParameter = UUID.fromString("45888b40-5bbb-4293-aa1e-02479796cd7c");
66
	final static UUID uuidStatMeasureSingleValue = UUID.fromString("eb4c3d98-4d4b-4c37-8eb4-17315ce79920");
67
	final static UUID uuidMeasurementValueModifier = UUID.fromString("0218a7a3-f6c0-4d06-a4f8-6b50b73aef5e");
68
	
69
	final static UUID uuidModifierLowerThan = UUID.fromString("2b500085-6bef-4003-b6ea-e0ad0237d79d");
70
	final static UUID uuidModifierGreaterThan = UUID.fromString("828df49d-c745-48f7-b083-0ada43356c34");
71

    
72

    
73
	/**
74
	 * Creates the vocabularies and the features for Climate, Habitat and Lifeform
75
	 * @param state
76
	 * @throws SQLException
77
	 */
78
	protected void makeVocabulariesAndFeatures(AlgaTerraImportState state) throws SQLException {
79
		String abbrevLabel = null;
80
		URI uri = null;
81
		
82
		if (! state.isSpecimenVocabulariesCreated()){
83
			
84
			TransactionStatus txStatus = this.startTransaction();
85
		
86
			boolean isOrdered = true;
87
			OrderedTermVocabulary<State> climateVoc = (OrderedTermVocabulary)getVocabulary(uuidVocAlgaTerraClimate, "Climate", "Climate", abbrevLabel, uri, isOrdered, null);
88
			OrderedTermVocabulary<State> habitatVoc = (OrderedTermVocabulary)getVocabulary(uuidVocAlgaTerraHabitat, "Habitat", "Habitat", abbrevLabel, uri, isOrdered, null);
89
			OrderedTermVocabulary<State> lifeformVoc = (OrderedTermVocabulary)getVocabulary(uuidVocAlgaTerraLifeForm, "Lifeform", "Lifeform", abbrevLabel, uri, isOrdered, null);
90
			
91
			
92
			Feature feature = getFeature(state, uuidFeatureAlgaTerraClimate, "Climate","Climate", null, null);
93
			feature.setSupportsCategoricalData(true);
94
			
95
			feature = getFeature(state, uuidFeatureAlgaTerraLifeForm, "LifeForm","LifeForm", null, null);
96
			feature.setSupportsCategoricalData(true);
97
			
98
			feature = Feature.HABITAT();
99
			feature.setSupportsCategoricalData(true);
100
			getTermService().saveOrUpdate(feature);
101
			
102
			Source source = state.getAlgaTerraConfigurator().getSource();
103
			
104
			String climateSql = "SELECT * FROM EcoClimate";
105
			ResultSet rs = source.getResultSet(climateSql);
106
			while (rs.next()){
107
				String climate = rs.getString("Climate");
108
				String description = rs.getString("Description");
109
				Integer id = rs.getInt("ClimateId");
110
				UUID uuid = UUID.fromString(rs.getString("UUID"));
111
				State stateTerm = getStateTerm(state, uuid, climate, description, null, climateVoc);
112
				addOriginalSource(stateTerm, id.toString(), "EcoClimate", state.getTransactionalSourceReference());
113
				getTermService().saveOrUpdate(stateTerm);
114
			}
115
			
116
			String habitatSql = "SELECT * FROM EcoHabitat";
117
			rs = source.getResultSet(habitatSql);
118
			while (rs.next()){
119
				String habitat = rs.getString("Habitat");
120
				String description = rs.getString("Description");
121
				Integer id = rs.getInt("HabitatId");
122
				UUID uuid = UUID.fromString(rs.getString("UUID"));
123
				State stateTerm = getStateTerm(state, uuid, habitat, description, null, habitatVoc);
124
				addOriginalSource(stateTerm, id.toString(), "EcoHabitat", state.getTransactionalSourceReference());
125
				getTermService().saveOrUpdate(stateTerm);
126
			}
127
			
128
			String lifeformSql = "SELECT * FROM EcoLifeForm";
129
			rs = source.getResultSet(lifeformSql);
130
			while (rs.next()){
131
				String lifeform = rs.getString("LifeForm");
132
				String description = rs.getString("Description");
133
				Integer id = rs.getInt("LifeFormId");
134
				UUID uuid = UUID.fromString(rs.getString("UUID"));
135
				State stateTerm = getStateTerm(state, uuid, lifeform, description, null, lifeformVoc);
136
				addOriginalSource(stateTerm, id.toString(), "EcoLifeForm", state.getTransactionalSourceReference());
137
				getTermService().saveOrUpdate(stateTerm);
138
			}
139
			
140
			this.commitTransaction(txStatus);
141
			
142
			state.setSpecimenVocabulariesCreated(true);
143
		}
144
		
145
	}
146
	
147
	protected String getLocalityString(){
148
		return "Locality";
149
	}
150
	
151
	protected void handleSingleSpecimen(ResultSet rs, DerivedUnitFacade facade, AlgaTerraImportState state) throws SQLException {
152
		//FIXME missing fields #3084, #3085, #3080
153
		try {
154
			
155
			String locality = rs.getString(getLocalityString());
156
			Double latitude = nullSafeDouble(rs, "Latitude");
157
			Double longitude = nullSafeDouble(rs, "Longitude");
158
			Integer errorRadius = nullSafeInt(rs,"Prec");
159
			String geoCodeMethod = rs.getString("GeoCodeMethod");
160
			
161
			Integer altitude = nullSafeInt(rs, "Altitude");
162
			Integer lowerAltitude = nullSafeInt(rs,"AltitudeLowerValue");
163
			String altitudeUnit = rs.getString("AltitudeUnit");
164
			Double depth = nullSafeDouble(rs, "Depth");
165
			Double depthLow = nullSafeDouble(rs, "DepthLow");
166
			   	
167
			String collectorsNumber = rs.getString("CollectorsNumber");
168
			Date collectionDateStart = rs.getDate("CollectionDate");
169
			Date collectionDateEnd = rs.getDate("CollectionDateEnd");
170

    
171
			//location
172
			facade.setLocality(locality);
173
			    	
174
			//exact location
175
			ReferenceSystem referenceSystem = makeRefrenceSystem(geoCodeMethod, state);
176
			if (longitude != null || latitude != null || referenceSystem != null || errorRadius != null){
177
				Point exactLocation = Point.NewInstance(longitude, latitude, referenceSystem, errorRadius);
178
				facade.setExactLocation(exactLocation);
179
			}
180
			
181
			//altitude, depth
182
			if (StringUtils.isNotBlank(altitudeUnit) && ! altitudeUnit.trim().equalsIgnoreCase("m")){
183
				logger.warn("Altitude unit is not [m] but: " +  altitudeUnit);
184
			}
185
			if ( altitude != null){
186
				if (lowerAltitude == null){
187
					facade.setAbsoluteElevation(altitude);
188
				}else{
189
			   		if (! facade.isEvenDistance(lowerAltitude, altitude)){
190
			   			//FIXME there is a ticket for this
191
			   			altitude = altitude + 1;
192
			   			logger.info("Current implementation of altitude does not allow uneven distances");
193
			   		}
194
					facade.setAbsoluteElevationRange(lowerAltitude,altitude);
195
			   	}
196
			}
197
			if ( depth != null){
198
				//FIXME needs model change to accept double #3072
199
				Integer intDepth = depth.intValue();
200
				if (depthLow == null){
201
					facade.setDistanceToWaterSurface(intDepth);
202
				}else{
203
					//FIXME range not yet in model #3074
204
			   		facade.setDistanceToWaterSurface(intDepth);
205
			   	}
206
			}
207

    
208
			
209
			//field
210
			facade.setFieldNumber(collectorsNumber);
211
			TimePeriod gatheringPeriod = TimePeriod.NewInstance(collectionDateStart, collectionDateEnd);
212
			facade.setGatheringPeriod(gatheringPeriod);
213
			handleCollectorTeam(state, facade, rs);
214
			
215
			//areas
216
			makeAreas(state, rs, facade);
217
			
218
			
219
			//notes
220
			//TODO is this an annotation on field observation or on the derived unit?
221
			
222
			//TODO id, created for fact +  ecoFact
223
			//    	this.doIdCreatedUpdatedNotes(state, descriptionElement, rs, id, namespace);
224
		
225
		} catch (Exception e) {
226
			throw new RuntimeException(e);
227
		}
228
    	
229
	}
230
	
231
	protected DescriptionBase getFieldObservationDescription(DerivedUnitFacade facade) {
232
		Set<DescriptionBase> descriptions = facade.innerFieldObservation().getDescriptions();
233
		for (DescriptionBase desc : descriptions){
234
			if (desc.isImageGallery() == false){
235
				return desc;
236
			}
237
		}
238
		SpecimenDescription specDesc = SpecimenDescription.NewInstance(facade.innerFieldObservation());
239
		descriptions.add(specDesc);
240
		return specDesc;
241
	}
242
	
243

    
244
	private void makeAreas(AlgaTerraImportState state, ResultSet rs, DerivedUnitFacade facade) throws SQLException {
245
	   	Object gazetteerId = rs.getObject("GazetteerId");
246
	   	if (gazetteerId != null){
247
	   		//TDWG
248
	   		NamedArea tdwgArea;
249
	   		String tdwg4 = rs.getString("L4Code");
250
	   		if (isNotBlank(tdwg4)){
251
	   			tdwgArea = TdwgArea.getAreaByTdwgAbbreviation(tdwg4);
252
	   		}else{
253
	   			String tdwg3 = rs.getString("L3Code");
254
	   			if (isNotBlank(tdwg3)){
255
	   				tdwgArea = TdwgArea.getAreaByTdwgAbbreviation(tdwg3);
256
	   			}else{
257
	   				Integer tdwg2 = rs.getInt("L2Code");   				
258
	   				tdwgArea = TdwgArea.getAreaByTdwgAbbreviation(String.valueOf(tdwg2));
259
		   		}
260
	   		}
261
	   		if (tdwgArea == null){
262
	   			logger.warn("TDWG area could not be defined for gazetterId: " + gazetteerId);
263
	   		}else{
264
	   			facade.addCollectingArea(tdwgArea);
265
	   		}
266
	   		
267
	   		//Countries
268
	   		WaterbodyOrCountry country = null;
269
	   		String isoCountry = rs.getString("ISOCountry");
270
	   		String countryStr = rs.getString("Country");
271
	   		if (isNotBlank(isoCountry)){
272
		   		country = WaterbodyOrCountry.getWaterbodyOrCountryByIso3166A2(isoCountry);
273
	   		}else if (isNotBlank(countryStr)){
274
	   			logger.warn("Country exists but no ISO code");
275
	   		}
276
	   		if (country == null){
277
	   			logger.warn("Country does not exist for GazetteerID " + gazetteerId);
278
	   		}else{
279
	   			facade.setCountry(country);
280
	   		}
281
	   		
282
	   	}
283
	    
284
	   	//Waterbody
285
	   	WaterbodyOrCountry waterbody = null;
286
	   	String waterbodyStr = rs.getString("WaterBody");
287
	   	if (isNotBlank(waterbodyStr)){
288
	   		if (waterbodyStr.equals("Atlantic Ocean")){
289
	   			waterbody = WaterbodyOrCountry.ATLANTICOCEAN();
290
	   		}else{
291
	   			logger.warn("Waterbody not recognized: " + waterbody);
292
	   		}
293
	   		if (waterbody != null){
294
	   			facade.addCollectingArea(waterbody);
295
	   		}
296
	   	}
297

    
298
		
299
	   	//countries sub
300
	   	//TODO -> SpecimenImport (not existing in TypeSpecimen)
301
	}
302

    
303

    
304
	
305

    
306
	private ReferenceSystem makeRefrenceSystem(String geoCodeMethod, AlgaTerraImportState state) {
307
		if (StringUtils.isBlank(geoCodeMethod)){
308
			return null;
309
		}else if(geoCodeMethod.startsWith("GPS")){
310
			getReferenceSystem(state, uuidRefSystemGps, "GPS", "GPS", "GPS", ReferenceSystem.GOOGLE_EARTH().getVocabulary());
311
			return ReferenceSystem.WGS84(); 
312
		}else if(geoCodeMethod.startsWith("Google")){
313
			return ReferenceSystem.GOOGLE_EARTH();
314
		}else if(geoCodeMethod.startsWith("Map")){
315
			logger.warn("Reference system " +  geoCodeMethod +  " not yet supported.");
316
			return null;
317
		}else if(geoCodeMethod.startsWith("WikiProjekt Georeferenzierung") || geoCodeMethod.startsWith("http://toolserver.org/~geohack/geohack.php") ){
318
			return ReferenceSystem.WGS84();
319
		}else {
320
			logger.warn("Reference system " +  geoCodeMethod +  " not yet supported.");
321
			return null;
322
		}
323
	}
324
	
325

    
326
	
327

    
328
	private void handleCollectorTeam(AlgaTerraImportState state, DerivedUnitFacade facade, ResultSet rs) throws SQLException {
329
		// FIXME parsen
330
		String collector = rs.getString("Collector");
331
		Team team = Team.NewTitledInstance(collector, collector);
332
		facade.setCollector(team);
333
	}
334

    
335
	
336

    
337

    
338
}
(4-4/5)