Project

General

Profile

Download (16.4 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.Rank;
38
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
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 long serialVersionUID = -4982506434258587864L;
50
    private static final Logger logger = Logger.getLogger(BerlinModelImportBase.class);
51

    
52
	public BerlinModelImportBase(String tableName, String pluralString ) {
53
		super(tableName, pluralString);
54
	}
55

    
56

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

    
66

    
67
	protected boolean doIdCreatedUpdatedNotes(BerlinModelImportState state, DescriptionElementBase descriptionElement, ResultSet rs, String id, String namespace) throws SQLException{
68
		boolean success = true;
69
		//id
70
		success &= doId(state, descriptionElement, id, namespace);
71
		//createdUpdateNotes
72
		success &= doCreatedUpdatedNotes(state, descriptionElement, rs);
73
		return success;
74
	}
75

    
76
	protected boolean doIdCreatedUpdatedNotes(BerlinModelImportState state, IdentifiableEntity identifiableEntity, ResultSet rs, long id, String namespace, boolean excludeUpdated, boolean excludeNotes)
77
			throws SQLException{
78
		boolean success = true;
79
		//id
80
		success &= doId(state, identifiableEntity, id, namespace);
81
		//createdUpdateNotes
82
		success &= doCreatedUpdatedNotes(state, identifiableEntity, rs, excludeUpdated, excludeNotes);
83
		return success;
84
	}
85

    
86

    
87
	protected boolean doIdCreatedUpdatedNotes(BerlinModelImportState state, IdentifiableEntity identifiableEntity, ResultSet rs, long id, String namespace)
88
			throws SQLException{
89
		boolean excludeUpdated = false;
90
		return doIdCreatedUpdatedNotes(state, identifiableEntity, rs, id, namespace, excludeUpdated, false);
91
	}
92

    
93
	protected boolean doCreatedUpdatedNotes(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs)
94
			throws SQLException{
95
		boolean excludeUpdated = false;
96
		return doCreatedUpdatedNotes(state, annotatableEntity, rs, excludeUpdated, false);
97
	}
98

    
99
	protected boolean doCreatedUpdatedNotes(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs, boolean excludeUpdated, boolean excludeNotes)
100
			throws SQLException{
101

    
102
		BerlinModelImportConfigurator config = state.getConfig();
103
		Object createdWhen = rs.getObject("Created_When");
104
		String createdWho = rs.getString("Created_Who");
105
		createdWho = handleHieraciumPilosella(createdWho);
106
		Object updatedWhen = null;
107
		String updatedWho = null;
108
		if (excludeUpdated == false){
109
			try {
110
				updatedWhen = rs.getObject("Updated_When");
111
				updatedWho = rs.getString("Updated_who");
112
			} catch (SQLException e) {
113
				//Table "Name" has no updated when/who
114
			}
115
		}
116
		String notes = rs.getString("notes");
117

    
118
		boolean success  = true;
119

    
120
		//Created When, Who, Updated When Who
121
		if (config.getEditor() == null || config.getEditor().equals(EDITOR.NO_EDITORS)){
122
			//do nothing
123
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_ANNOTATION)){
124
			String createdAnnotationString = "Berlin Model record was created By: " + String.valueOf(createdWho) + " (" + String.valueOf(createdWhen) + ") ";
125
			if (updatedWhen != null && updatedWho != null){
126
				createdAnnotationString += " and updated By: " + String.valueOf(updatedWho) + " (" + String.valueOf(updatedWhen) + ")";
127
			}
128
			Annotation annotation = Annotation.NewInstance(createdAnnotationString, Language.DEFAULT());
129
			//TODO make transaction compatible, same as common sec reference
130
			annotation.setCommentator(config.getCommentator());
131
			annotation.setAnnotationType(AnnotationType.TECHNICAL());
132
			annotatableEntity.addAnnotation(annotation);
133
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_EDITOR)){
134
			User creator = getUser(state, createdWho);
135
			User updator = getUser(state, updatedWho);
136
			DateTime created = getDateTime(createdWhen);
137
			DateTime updated = getDateTime(updatedWhen);
138
			annotatableEntity.setCreatedBy(creator);
139
			annotatableEntity.setUpdatedBy(updator);
140
			annotatableEntity.setCreated(created);
141
			annotatableEntity.setUpdated(updated);
142
		}else {
143
			logger.warn("Editor type not yet implemented: " + config.getEditor());
144
		}
145

    
146

    
147
		//notes
148
		if (! excludeNotes){
149
			doNotes(annotatableEntity, notes);
150
		}
151
		return success;
152
	}
153

    
154
	/**
155
	 * Special usecase for EDITWP6 import where in the createdWho field the original ID is stored
156
	 * @param createdWho
157
	 * @return
158
	 */
159
	private String handleHieraciumPilosella(String createdWho) {
160
		String result = createdWho;
161
		if (result == null){
162
			return null;
163
		}else if (result.startsWith("Hieracium_Pilosella import from EM")){
164
			return "Hieracium_Pilosella import from EM";
165
		}else{
166
			return result;
167
		}
168
	}
169

    
170
	private DateTime getDateTime(Object timeString){
171
		if (timeString == null){
172
			return null;
173
		}
174
		DateTime dateTime = null;
175
		if (timeString instanceof Timestamp){
176
			Timestamp timestamp = (Timestamp)timeString;
177
			dateTime = new DateTime(timestamp);
178
		}else{
179
			logger.warn("time ("+timeString+") is not a timestamp. Datetime set to current date. ");
180
			dateTime = new DateTime();
181
		}
182
		return dateTime;
183
	}
184

    
185
	/**
186
	 * @param state
187
	 * @param newTaxonId
188
	 * @param taxonMap
189
	 * @param factId
190
	 * @return
191
	 */
192
	protected Taxon getTaxon(BerlinModelImportState state, int taxonId, Map<String, TaxonBase> taxonMap, int factId) {
193
		TaxonBase<?> taxonBase = taxonMap.get(String.valueOf(taxonId));
194

    
195
		//TODO for testing
196
		if (taxonBase == null && ! state.getConfig().isDoTaxa()){
197
			taxonBase = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
198
		}
199

    
200
		Taxon taxon;
201
		if ( taxonBase instanceof Taxon ) {
202
			taxon = (Taxon) taxonBase;
203
		} else if (taxonBase != null) {
204
			logger.warn("TaxonBase (" + taxonId + ") for Fact(Specimen) with factId " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
205
			return null;
206
		} else {
207
			logger.warn("TaxonBase (" + taxonId + ") for Fact(Specimen) with factId " + factId + " is null.");
208
			return null;
209
		}
210
		return taxon;
211
	}
212

    
213

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

    
246

    
247
	/**
248
	 * Searches for a reference in the first detail map. If it does not exist it
249
	 * searches in the second detail map. Returns null if it does not exist in any map.
250
	 * A map may be <code>null</code> to avoid search on this map.
251
	 * @param secondDetailMap
252
	 * @param firstDetailMap
253
	 * @param nomRefDetailFk
254
	 * @return
255
	 */
256
	private Reference getReferenceDetailFromMaps(Map<String, Reference> firstDetailMap, Map<String, Reference> secondDetailMap, String nomRefDetailFk) {
257
		Reference result = null;
258
		if (nomRefDetailFk != null){
259
			//get ref
260
			if (firstDetailMap != null){
261
				result = firstDetailMap.get(nomRefDetailFk);
262
			}
263
			if (result == null && secondDetailMap != null){
264
				result = secondDetailMap.get(nomRefDetailFk);
265
			}
266
		}
267
		return result;
268
	}
269

    
270
	protected NamedArea getOtherAreas(BerlinModelImportState state, String emCodeString, String tdwgCodeString) {
271
		String em = CdmUtils.Nz(emCodeString).trim();
272
		String tdwg = CdmUtils.Nz(tdwgCodeString).trim();
273
		//Cichorieae + E+M
274
		if ("EM".equals(em)){
275
			return getNamedArea(state, BerlinModelTransformer.euroMedUuid, "Euro+Med", "Euro+Med area", "EM", null, null);
276
		}else if("Rf".equals(em)){
277
			return Country.RUSSIANFEDERATION();
278

    
279
		}else if("KRY-OO;UKR-UK".equals(tdwg)){
280
			return Country.UKRAINE();
281

    
282
		}else if("TCS-AZ;TCS-NA".equals(tdwg)){
283
			return Country.AZERBAIJANREPUBLICOF();
284
		}else if("TCS-AB;TCS-AD;TCS-GR".equals(tdwg)){
285
			return Country.GEORGIA();
286

    
287

    
288
		}else if("Cc".equals(em)){
289
			return getNamedArea(state, BerlinModelTransformer.uuidCaucasia, "Caucasia (Ab + Ar + Gg + Rf(CS))", "Euro+Med area 'Caucasia (Ab + Ar + Gg + Rf(CS))'", "Cc", null, null);
290
		}
291

    
292
		//E+M
293
		else if("EUR".equals(em)){
294
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("1");
295
		}else if("14".equals(em)){
296
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("14");
297
		}else if("21".equals(em)){
298
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("21");  // Macaronesia
299
		}else if("33".equals(em)){
300
			return TdwgAreaProvider.getAreaByTdwgAbbreviation("33");
301

    
302
		}else if("SM".equals(em)){
303
			return getNamedArea(state, BerlinModelTransformer.uuidSerbiaMontenegro, "Serbia & Montenegro", "Euro+Med area 'Serbia & Montenegro'", "SM", NamedAreaType.ADMINISTRATION_AREA(), null);
304
		}else if("Sr".equals(em)){
305
			return getNamedArea(state, BerlinModelTransformer.uuidSerbia, "Serbia", "Euro+Med area 'Serbia' (including Kosovo and Vojvodina)", "Sr", NamedAreaType.ADMINISTRATION_AREA(), null);
306

    
307

    
308
		//see #2769
309
		}else if("Rs".equals(em)){
310
			return getNamedArea(state, BerlinModelTransformer.uuidUssr, "Former USSR", "Euro+Med area 'Former USSR'", "Rs", NamedAreaType.ADMINISTRATION_AREA(), null);
311
		}else if("Rs(N)".equals(em)){
312
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaNorthern, "Russia Northern", "Euro+Med area 'Russia Northern'", "Rs(N)", null, null);
313
		}else if("Rs(B)".equals(em)){
314
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaBaltic, "Russia Baltic", "Euro+Med area 'Russia Baltic'", "Rs(B)", null, null);
315
		}else if("Rs(C)".equals(em)){
316
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaCentral, "Russia Central", "Euro+Med area 'Russia Central'", "Rs(C)", null, null);
317
		}else if("Rs(W)".equals(em)){
318
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaSouthWest, "Russia Southwest", "Euro+Med area 'Russia Southwest'", "Rs(W)", null, null);
319
		}else if("Rs(E)".equals(em)){
320
			return getNamedArea(state, BerlinModelTransformer.uuidRussiaSouthEast, "Russia Southeast", "Euro+Med area 'Russia Southeast'", "Rs(E)", null, null);
321

    
322
		//see #2770
323
		}else if("AE".equals(em)){
324
			return getNamedArea(state, BerlinModelTransformer.uuidEastAegeanIslands, "East Aegean Islands", "Euro+Med area 'East Aegean Islands'", "AE", null, null);
325
		}else if("AE(T)".equals(em)){
326
			return getNamedArea(state, BerlinModelTransformer.uuidTurkishEastAegeanIslands, "Turkish East Aegean Islands", "Euro+Med area 'Turkish East Aegean Islands'", "AE(T)", null, null);
327
		}else if("Tu".equals(em)){
328
			return getNamedArea(state, BerlinModelTransformer.uuidTurkey, "Turkey", "Euro+Med area 'Turkey' (without AE(T))", "Tu", null, null);
329

    
330
		//TODO Azores, Canary Is.
331
		}else if("Md(D)".equals(em)){
332
			return getNamedArea(state, BerlinModelTransformer.uuidDesertas, "Desertas", "Euro+Med area 'Desertas'", "Md(D)", null, null);
333
		}else if("Md(M)".equals(em)){
334
			return getNamedArea(state, BerlinModelTransformer.uuidMadeira, "Madeira", "Euro+Med area 'Madeira'", "Md(M)", null, null);
335
		}else if("Md(P)".equals(em)){
336
			return getNamedArea(state, BerlinModelTransformer.uuidPortoSanto, "Porto Santo", "Euro+Med area 'Porto Santo'", "Md(P)", null, null);
337
		//Azores
338
		}else if("Az(L)".equals(em)){
339
			return getNamedArea(state, BerlinModelTransformer.uuidFlores, "Flores", "Euro+Med area 'Flores'", "Az(L)", null, null);
340
		}else if("Az(C)".equals(em)){
341
			return getNamedArea(state, BerlinModelTransformer.uuidCorvo, "Corvo", "Euro+Med area 'Corvo'", "Az(C)", null, null);
342
		}else if("Az(F)".equals(em)){
343
			return getNamedArea(state, BerlinModelTransformer.uuidFaial, "Faial", "Euro+Med area 'Faial'", "Az(F)", null, null);
344
		}else if("Az(G)".equals(em)){
345
			return getNamedArea(state, BerlinModelTransformer.uuidGraciosa, "Graciosa", "Euro+Med area 'Graciosa'", "Az(G)", null, null);
346
		}else if("Az(J)".equals(em)){
347
			return getNamedArea(state, BerlinModelTransformer.uuidSaoJorge, "S\u00E3o Jorge", "Euro+Med area 'S\u00E3o Jorge'", "Az(J)", null, null);
348
		}else if("Az(M)".equals(em)){
349
			return getNamedArea(state, BerlinModelTransformer.uuidSaoMiguel, "S\u00E3o Miguel", "Euro+Med area 'S\u00E3o Miguel'", "Az(M)", null, null);
350
		}else if("Az(P)".equals(em)){
351
			return getNamedArea(state, BerlinModelTransformer.uuidPico, "Pico", "Euro+Med area 'Pico'", "Az(P)", null, null);
352
		}else if("Az(S)".equals(em)){
353
			return getNamedArea(state, BerlinModelTransformer.uuidSantaMaria, "Santa Maria", "Euro+Med area 'Santa Maria'", "Az(S)", null, null);
354
		}else if("Az(T)".equals(em)){
355
			return getNamedArea(state, BerlinModelTransformer.uuidTerceira, "Terceira", "Euro+Med area 'Terceira'", "Az(T)", null, null);
356
		//Canary Islands
357
		}else if("Ca(C)".equals(em)){
358
			return getNamedArea(state, BerlinModelTransformer.uuidGranCanaria, "Gran Canaria", "Euro+Med area 'Gran Canaria'", "Ca(C)", null, null);
359
		}else if("Ca(F)".equals(em)){
360
			return getNamedArea(state, BerlinModelTransformer.uuidFuerteventura, "Fuerteventura with Lobos", "Euro+Med area 'Fuerteventura with Lobos'", "Ca(F)", null, null);
361
		}else if("Ca(G)".equals(em)){
362
			return getNamedArea(state, BerlinModelTransformer.uuidGomera, "Gomera", "Euro+Med area 'Gomera'", "Ca(G)", null, null);
363
		}else if("Ca(H)".equals(em)){
364
			return getNamedArea(state, BerlinModelTransformer.uuidHierro, "Hierro", "Euro+Med area 'Hierro'", "Ca(H)", null, null);
365
		}else if("Ca(L)".equals(em)){
366
			return getNamedArea(state, BerlinModelTransformer.uuidLanzaroteWithGraciosa, "Lanzarote with Graciosa", "Euro+Med area 'Lanzarote with Graciosa'", "Ca(L)", null, null);
367
		}else if("Ca(P)".equals(em)){
368
			return getNamedArea(state, BerlinModelTransformer.uuidLaPalma, "La Palma", "Euro+Med area 'La Palma'", "Ca(P)", null, null);
369
		}else if("Ca(T)".equals(em)){
370
			return getNamedArea(state, BerlinModelTransformer.uuidTenerife, "Tenerife", "Euro+Med area 'Tenerife'", "Ca(T)", null, null);
371
			//Baleares
372
		}else if("Bl(I)".equals(em)){
373
			return getNamedArea(state, BerlinModelTransformer.uuidIbizaWithFormentera, "Ibiza with Formentera", "Euro+Med area 'Ibiza with Formentera'", "Bl(I)", null, null);
374
		}else if("Bl(M)".equals(em)){
375
			return getNamedArea(state, BerlinModelTransformer.uuidTerceira, "Mallorca", "Euro+Med area 'Mallorca'", "Bl(M)", null, null);
376
		}else if("Bl(N)".equals(em)){
377
			return getNamedArea(state, BerlinModelTransformer.uuidTerceira, "Menorca", "Euro+Med area 'Menorca'", "Bl(N)", null, null);
378
		}
379

    
380
		logger.warn("Area(em: '" + em + "', tdwg: '" + tdwg +"') could not be found for occurrence import");
381

    
382
		return null;
383
	}
384

    
385

    
386
}
(5-5/21)