Project

General

Profile

Download (16.5 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.NamedArea;
35
import eu.etaxonomy.cdm.model.location.NamedAreaType;
36
import eu.etaxonomy.cdm.model.location.Country;
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)	
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);
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);
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);
95
	}
96
	
97
	protected boolean doCreatedUpdatedNotes(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs, boolean excludeUpdated)
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
		doNotes(annotatableEntity, notes);
146
		return success;
147
	}
148
	
149
	/**
150
	 * Special usecase for EDITWP6 import where in the createdWho field the original ID is stored
151
	 * @param createdWho
152
	 * @return
153
	 */
154
	private String handleHieraciumPilosella(String createdWho) {
155
		String result = createdWho;
156
		if (result == null){
157
			return null;
158
		}else if (result.startsWith("Hieracium_Pilosella import from EM")){
159
			return "Hieracium_Pilosella import from EM";
160
		}else{
161
			return result;
162
		}
163
	}
164

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

    
208

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

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

    
380
	
381
}
(5-5/21)