Project

General

Profile

Download (16.6 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.berlinModel.in;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.sql.Timestamp;
15
import java.util.Map;
16

    
17
import org.apache.log4j.Logger;
18
import org.joda.time.DateTime;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
22
import eu.etaxonomy.cdm.io.common.DbImportBase;
23
import eu.etaxonomy.cdm.io.common.ICdmIO;
24
import eu.etaxonomy.cdm.io.common.IImportConfigurator.EDITOR;
25
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
26
import eu.etaxonomy.cdm.io.common.TdwgAreaProvider;
27
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
28
import eu.etaxonomy.cdm.model.common.Annotation;
29
import eu.etaxonomy.cdm.model.common.AnnotationType;
30
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
31
import eu.etaxonomy.cdm.model.common.Language;
32
import eu.etaxonomy.cdm.model.common.User;
33
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
34
import eu.etaxonomy.cdm.model.location.Country;
35
import eu.etaxonomy.cdm.model.location.NamedArea;
36
import eu.etaxonomy.cdm.model.location.NamedAreaType;
37
import eu.etaxonomy.cdm.model.name.BotanicalName;
38
import eu.etaxonomy.cdm.model.name.Rank;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42

    
43
/**
44
 * @author a.mueller
45
 * @created 20.03.2008
46
 * @version 1.0
47
 */
48
public abstract class BerlinModelImportBase extends DbImportBase<BerlinModelImportState, BerlinModelImportConfigurator>  implements ICdmIO<BerlinModelImportState>, IPartitionedIO<BerlinModelImportState> {
49
	private static final Logger logger = Logger.getLogger(BerlinModelImportBase.class);
50
	
51
	public BerlinModelImportBase(String tableName, String pluralString ) {
52
		super(tableName, pluralString);
53
	}
54
	
55

    
56
	/**
57
	 * @return
58
	 */
59
	protected String getIdQuery(BerlinModelImportState state){
60
		String result = " SELECT " + getTableName() + "id FROM " + getTableName();
61
		return result;
62
	}
63

    
64
	
65
	protected boolean doIdCreatedUpdatedNotes(BerlinModelImportState state, DescriptionElementBase descriptionElement, ResultSet rs, String id, String namespace) throws SQLException{
66
		boolean success = true;
67
		//id
68
		success &= doId(state, descriptionElement, id, namespace);
69
		//createdUpdateNotes
70
		success &= doCreatedUpdatedNotes(state, descriptionElement, rs);
71
		return success;
72
	}
73
	
74
	protected boolean doIdCreatedUpdatedNotes(BerlinModelImportState state, IdentifiableEntity identifiableEntity, ResultSet rs, long id, String namespace, boolean excludeUpdated, boolean excludeNotes)	
75
			throws SQLException{
76
		boolean success = true;
77
		//id
78
		success &= doId(state, identifiableEntity, id, namespace);
79
		//createdUpdateNotes
80
		success &= doCreatedUpdatedNotes(state, identifiableEntity, rs, excludeUpdated, excludeNotes);
81
		return success;
82
	}
83

    
84
	
85
	protected boolean doIdCreatedUpdatedNotes(BerlinModelImportState state, IdentifiableEntity identifiableEntity, ResultSet rs, long id, String namespace)
86
			throws SQLException{
87
		boolean excludeUpdated = false;
88
		return doIdCreatedUpdatedNotes(state, identifiableEntity, rs, id, namespace, excludeUpdated, false);
89
	}
90
	
91
	protected boolean doCreatedUpdatedNotes(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs)
92
			throws SQLException{
93
		boolean excludeUpdated = false;
94
		return doCreatedUpdatedNotes(state, annotatableEntity, rs, excludeUpdated, false);
95
	}
96
	
97
	protected boolean doCreatedUpdatedNotes(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs, boolean excludeUpdated, boolean excludeNotes)
98
			throws SQLException{
99

    
100
		BerlinModelImportConfigurator config = state.getConfig();
101
		Object createdWhen = rs.getObject("Created_When");
102
		String createdWho = rs.getString("Created_Who");
103
		createdWho = handleHieraciumPilosella(createdWho);
104
		Object updatedWhen = null;
105
		String updatedWho = null;
106
		if (excludeUpdated == false){
107
			try {
108
				updatedWhen = rs.getObject("Updated_When");
109
				updatedWho = rs.getString("Updated_who");
110
			} catch (SQLException e) {
111
				//Table "Name" has no updated when/who
112
			}
113
		}
114
		String notes = rs.getString("notes");
115
		
116
		boolean success  = true;
117
		
118
		//Created When, Who, Updated When Who
119
		if (config.getEditor() == null || config.getEditor().equals(EDITOR.NO_EDITORS)){
120
			//do nothing
121
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_ANNOTATION)){
122
			String createdAnnotationString = "Berlin Model record was created By: " + String.valueOf(createdWho) + " (" + String.valueOf(createdWhen) + ") ";
123
			if (updatedWhen != null && updatedWho != null){
124
				createdAnnotationString += " and updated By: " + String.valueOf(updatedWho) + " (" + String.valueOf(updatedWhen) + ")";
125
			}
126
			Annotation annotation = Annotation.NewInstance(createdAnnotationString, Language.DEFAULT());
127
			annotation.setCommentator(config.getCommentator());
128
			annotation.setAnnotationType(AnnotationType.TECHNICAL());
129
			annotatableEntity.addAnnotation(annotation);
130
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_EDITOR)){
131
			User creator = getUser(state, createdWho);
132
			User updator = getUser(state, updatedWho);
133
			DateTime created = getDateTime(createdWhen);
134
			DateTime updated = getDateTime(updatedWhen);
135
			annotatableEntity.setCreatedBy(creator);
136
			annotatableEntity.setUpdatedBy(updator);
137
			annotatableEntity.setCreated(created);
138
			annotatableEntity.setUpdated(updated);
139
		}else {
140
			logger.warn("Editor type not yet implemented: " + config.getEditor());
141
		}
142
		
143
		
144
		//notes
145
		if (! excludeNotes){
146
			doNotes(annotatableEntity, notes);
147
		}
148
		return success;
149
	}
150
	
151
	/**
152
	 * Special usecase for EDITWP6 import where in the createdWho field the original ID is stored
153
	 * @param createdWho
154
	 * @return
155
	 */
156
	private String handleHieraciumPilosella(String createdWho) {
157
		String result = createdWho;
158
		if (result == null){
159
			return null;
160
		}else if (result.startsWith("Hieracium_Pilosella import from EM")){
161
			return "Hieracium_Pilosella import from EM";
162
		}else{
163
			return result;
164
		}
165
	}
166

    
167
	private DateTime getDateTime(Object timeString){
168
		if (timeString == null){
169
			return null;
170
		}
171
		DateTime dateTime = null;
172
		if (timeString instanceof Timestamp){
173
			Timestamp timestamp = (Timestamp)timeString;
174
			dateTime = new DateTime(timestamp);
175
		}else{
176
			logger.warn("time ("+timeString+") is not a timestamp. Datetime set to current date. ");
177
			dateTime = new DateTime();
178
		}
179
		return dateTime;
180
	}
181
	
182
	/**
183
	 * @param state
184
	 * @param newTaxonId
185
	 * @param taxonMap
186
	 * @param factId
187
	 * @return
188
	 */
189
	protected Taxon getTaxon(BerlinModelImportState state, int taxonId, Map<String, TaxonBase> taxonMap, int factId) {
190
		TaxonBase<?> taxonBase = taxonMap.get(String.valueOf(taxonId));
191
		
192
		//TODO for testing
193
		if (taxonBase == null && ! state.getConfig().isDoTaxa()){
194
			taxonBase = Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
195
		}
196
		
197
		Taxon taxon;
198
		if ( taxonBase instanceof Taxon ) {
199
			taxon = (Taxon) taxonBase;
200
		} else if (taxonBase != null) {
201
			logger.warn("TaxonBase (" + taxonId + ") for Fact(Specimen) with factId " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
202
			return null;
203
		} else {
204
			logger.warn("TaxonBase (" + taxonId + ") for Fact(Specimen) with factId " + factId + " is null.");
205
			return null;
206
		}
207
		return taxon;
208
	}
209

    
210

    
211
	/**
212
	 * 	Searches first in the detail maps then in the ref maps for a reference.
213
	 *  Returns the reference as soon as it finds it in one of the map, according
214
	 *  to the order of the map.
215
	 *  If nomRefDetailFk is <code>null</code> no search on detail maps is performed.
216
	 *  If one of the maps is <code>null</code> no search on the according map is
217
	 *  performed. <BR>
218
	 *  You may define the order of search by the order you pass the maps but
219
	 *  make sure to always pass the detail maps first.
220
	 * @param firstDetailMap
221
	 * @param secondDetailMap
222
	 * @param firstRefMap
223
	 * @param secondRefMap
224
	 * @param nomRefDetailFk
225
	 * @param nomRefFk
226
	 * @return
227
	 */
228
	protected Reference<?> getReferenceFromMaps(
229
			Map<String, Reference> detailMap,
230
			Map<String, Reference> refMap,
231
			String nomRefDetailFk,
232
			String nomRefFk) {
233
		Reference<?> ref = null;
234
		if (detailMap != null){
235
			ref = detailMap.get(nomRefDetailFk);
236
		}
237
		if (ref == null){
238
			ref = refMap.get(nomRefFk);
239
		}
240
		return ref;
241
	}
242
	
243

    
244
	/**
245
	 * Searches for a reference in the first detail map. If it does not exist it 
246
	 * searches in the second detail map. Returns null if it does not exist in any map.
247
	 * A map may be <code>null</code> to avoid search on this map.
248
	 * @param secondDetailMap 
249
	 * @param firstDetailMap 
250
	 * @param nomRefDetailFk 
251
	 * @return
252
	 */
253
	private Reference getReferenceDetailFromMaps(Map<String, Reference> firstDetailMap, Map<String, Reference> secondDetailMap, String nomRefDetailFk) {
254
		Reference<?> result = null;
255
		if (nomRefDetailFk != null){
256
			//get ref
257
			if (firstDetailMap != null){
258
				result = firstDetailMap.get(nomRefDetailFk);
259
			}
260
			if (result == null && secondDetailMap != null){
261
				result = secondDetailMap.get(nomRefDetailFk);
262
			}
263
		}
264
		return result;
265
	}
266
	
267
	protected NamedArea getOtherAreas(BerlinModelImportState state, String emCodeString, String tdwgCodeString) {
268
		String em = CdmUtils.Nz(emCodeString).trim();
269
		String tdwg = CdmUtils.Nz(tdwgCodeString).trim();
270
		//Cichorieae + E+M
271
		if ("EM".equals(em)){
272
			return getNamedArea(state, BerlinModelTransformer.euroMedUuid, "Euro+Med", "Euro+Med area", "EM", null, null);
273
		}else if("Rf".equals(em)){
274
			return Country.RUSSIANFEDERATION();
275
		
276
		}else if("KRY-OO;UKR-UK".equals(tdwg)){
277
			return Country.UKRAINE();
278
		
279
		}else if("TCS-AZ;TCS-NA".equals(tdwg)){
280
			return Country.AZERBAIJANREPUBLICOF();
281
		}else if("TCS-AB;TCS-AD;TCS-GR".equals(tdwg)){
282
			return Country.GEORGIA();
283
		
284
		
285
		}else if("Cc".equals(em)){
286
			return getNamedArea(state, BerlinModelTransformer.uuidCaucasia, "Caucasia (Ab + Ar + Gg + Rf(CS))", "Euro+Med area 'Caucasia (Ab + Ar + Gg + Rf(CS))'", "Cc", null, null);
287
		}
288
		
289
		//E+M
290
		else if("EUR".equals(em)){
291
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("1");
292
		}else if("14".equals(em)){
293
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("14");
294
		}else if("21".equals(em)){
295
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("21");  // Macaronesia
296
		}else if("33".equals(em)){
297
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("33");
298
		
299
		}else if("SM".equals(em)){
300
			return getNamedArea(state, BerlinModelTransformer.uuidSerbiaMontenegro, "Serbia & Montenegro", "Euro+Med area 'Serbia & Montenegro'", "SM", NamedAreaType.ADMINISTRATION_AREA(), null);
301
		}else if("Sr".equals(em)){
302
			return getNamedArea(state, BerlinModelTransformer.uuidSerbia, "Serbia", "Euro+Med area 'Serbia' (including Kosovo and Vojvodina)", "Sr", NamedAreaType.ADMINISTRATION_AREA(), null);
303
		
304
		
305
		//see #2769
306
		}else if("Rs".equals(em)){
307
			return getNamedArea(state, BerlinModelTransformer.uuidUssr, "Former USSR", "Euro+Med area 'Former USSR'", "Rs", NamedAreaType.ADMINISTRATION_AREA(), null);
308
		}else if("Rs(N)".equals(em)){
309
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaNorthern, "Russia Northern", "Euro+Med area 'Russia Northern'", "Rs(N)", null, null);
310
		}else if("Rs(B)".equals(em)){
311
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaBaltic, "Russia Baltic", "Euro+Med area 'Russia Baltic'", "Rs(B)", null, null);
312
		}else if("Rs(C)".equals(em)){
313
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaCentral, "Russia Central", "Euro+Med area 'Russia Central'", "Rs(C)", null, null);
314
		}else if("Rs(W)".equals(em)){
315
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaSouthWest, "Russia Southwest", "Euro+Med area 'Russia Southwest'", "Rs(W)", null, null);
316
		}else if("Rs(E)".equals(em)){
317
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaSouthEast, "Russia Southeast", "Euro+Med area 'Russia Southeast'", "Rs(E)", null, null);
318
			
319
		//see #2770
320
		}else if("AE".equals(em)){
321
			return getNamedArea(state, BerlinModelTransformer.uuidEastAegeanIslands, "East Aegean Islands", "Euro+Med area 'East Aegean Islands'", "AE", null, null);
322
		}else if("AE(T)".equals(em)){
323
			return getNamedArea(state, BerlinModelTransformer.uuidTurkishEastAegeanIslands, "Turkish East Aegean Islands", "Euro+Med area 'Turkish East Aegean Islands'", "AE(T)", null, null);
324
		}else if("Tu".equals(em)){
325
			return getNamedArea(state, BerlinModelTransformer.uuidTurkey, "Turkey", "Euro+Med area 'Turkey' (without AE(T))", "Tu", null, null);
326
		
327
		//TODO Azores, Canary Is. 
328
		}else if("Md(D)".equals(em)){
329
			return getNamedArea(state, BerlinModelTransformer.uuidDesertas, "Desertas", "Euro+Med area 'Desertas'", "Md(D)", null, null);
330
		}else if("Md(M)".equals(em)){
331
			return getNamedArea(state, BerlinModelTransformer.uuidMadeira, "Madeira", "Euro+Med area 'Madeira'", "Md(M)", null, null);
332
		}else if("Md(P)".equals(em)){
333
			return getNamedArea(state, BerlinModelTransformer.uuidPortoSanto, "Porto Santo", "Euro+Med area 'Porto Santo'", "Md(P)", null, null);
334
		//Azores
335
		}else if("Az(L)".equals(em)){
336
			return getNamedArea(state, BerlinModelTransformer.uuidFlores, "Flores", "Euro+Med area 'Flores'", "Az(L)", null, null);
337
		}else if("Az(C)".equals(em)){
338
			return getNamedArea(state, BerlinModelTransformer.uuidCorvo, "Corvo", "Euro+Med area 'Corvo'", "Az(C)", null, null);
339
		}else if("Az(F)".equals(em)){
340
			return getNamedArea(state, BerlinModelTransformer.uuidFaial, "Faial", "Euro+Med area 'Faial'", "Az(F)", null, null);
341
		}else if("Az(G)".equals(em)){
342
			return getNamedArea(state, BerlinModelTransformer.uuidGraciosa, "Graciosa", "Euro+Med area 'Graciosa'", "Az(G)", null, null);
343
		}else if("Az(J)".equals(em)){
344
			return getNamedArea(state, BerlinModelTransformer.uuidSaoJorge, "S\u00E3o Jorge", "Euro+Med area 'S\u00E3o Jorge'", "Az(J)", null, null);
345
		}else if("Az(M)".equals(em)){
346
			return getNamedArea(state, BerlinModelTransformer.uuidSaoMiguel, "S\u00E3o Miguel", "Euro+Med area 'S\u00E3o Miguel'", "Az(M)", null, null);
347
		}else if("Az(P)".equals(em)){
348
			return getNamedArea(state, BerlinModelTransformer.uuidPico, "Pico", "Euro+Med area 'Pico'", "Az(P)", null, null);
349
		}else if("Az(S)".equals(em)){
350
			return getNamedArea(state, BerlinModelTransformer.uuidSantaMaria, "Santa Maria", "Euro+Med area 'Santa Maria'", "Az(S)", null, null);
351
		}else if("Az(T)".equals(em)){
352
			return getNamedArea(state, BerlinModelTransformer.uuidTerceira, "Terceira", "Euro+Med area 'Terceira'", "Az(T)", null, null);
353
		//Canary Islands
354
		}else if("Ca(C)".equals(em)){
355
			return getNamedArea(state, BerlinModelTransformer.uuidGranCanaria, "Gran Canaria", "Euro+Med area 'Gran Canaria'", "Ca(C)", null, null);
356
		}else if("Ca(F)".equals(em)){
357
			return getNamedArea(state, BerlinModelTransformer.uuidFuerteventura, "Fuerteventura with Lobos", "Euro+Med area 'Fuerteventura with Lobos'", "Ca(F)", null, null);
358
		}else if("Ca(G)".equals(em)){
359
			return getNamedArea(state, BerlinModelTransformer.uuidGomera, "Gomera", "Euro+Med area 'Gomera'", "Ca(G)", null, null);
360
		}else if("Ca(H)".equals(em)){
361
			return getNamedArea(state, BerlinModelTransformer.uuidHierro, "Hierro", "Euro+Med area 'Hierro'", "Ca(H)", null, null);
362
		}else if("Ca(L)".equals(em)){
363
			return getNamedArea(state, BerlinModelTransformer.uuidLanzaroteWithGraciosa, "Lanzarote with Graciosa", "Euro+Med area 'Lanzarote with Graciosa'", "Ca(L)", null, null);
364
		}else if("Ca(P)".equals(em)){
365
			return getNamedArea(state, BerlinModelTransformer.uuidLaPalma, "La Palma", "Euro+Med area 'La Palma'", "Ca(P)", null, null);
366
		}else if("Ca(T)".equals(em)){
367
			return getNamedArea(state, BerlinModelTransformer.uuidTenerife, "Tenerife", "Euro+Med area 'Tenerife'", "Ca(T)", null, null);
368
			//Baleares
369
		}else if("Bl(I)".equals(em)){
370
			return getNamedArea(state, BerlinModelTransformer.uuidIbizaWithFormentera, "Ibiza with Formentera", "Euro+Med area 'Ibiza with Formentera'", "Bl(I)", null, null);
371
		}else if("Bl(M)".equals(em)){
372
			return getNamedArea(state, BerlinModelTransformer.uuidTerceira, "Mallorca", "Euro+Med area 'Mallorca'", "Bl(M)", null, null);
373
		}else if("Bl(N)".equals(em)){
374
			return getNamedArea(state, BerlinModelTransformer.uuidTerceira, "Menorca", "Euro+Med area 'Menorca'", "Bl(N)", null, null);
375
		}
376
		
377
		logger.warn("Area(em: '" + em + "', tdwg: '" + tdwg +"') could not be found for occurrence import");
378
		
379
		return null;
380
	}
381

    
382
	
383
}
(5-5/21)