Project

General

Profile

Download (17.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.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.MarkerType;
33
import eu.etaxonomy.cdm.model.common.TermType;
34
import eu.etaxonomy.cdm.model.common.TermVocabulary;
35
import eu.etaxonomy.cdm.model.common.User;
36
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37
import eu.etaxonomy.cdm.model.location.Country;
38
import eu.etaxonomy.cdm.model.location.NamedArea;
39
import eu.etaxonomy.cdm.model.location.NamedAreaType;
40
import eu.etaxonomy.cdm.model.reference.Reference;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43

    
44
/**
45
 * @author a.mueller
46
 * @since 20.03.2008
47
 */
48
public abstract class BerlinModelImportBase
49
            extends DbImportBase<BerlinModelImportState, BerlinModelImportConfigurator>
50
            implements ICdmIO<BerlinModelImportState>, IPartitionedIO<BerlinModelImportState> {
51
    private static final long serialVersionUID = -4982506434258587864L;
52
    private static final Logger logger = Logger.getLogger(BerlinModelImportBase.class);
53

    
54
	public BerlinModelImportBase(String tableName, String pluralString ) {
55
		super(tableName, pluralString);
56
	}
57

    
58

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

    
68

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

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

    
88

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

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

    
101
	protected boolean doCreatedUpdatedNotes(BerlinModelImportState state, AnnotatableEntity annotatableEntity,
102
	        ResultSet rs, boolean excludeUpdated, boolean excludeNotes)
103
			throws SQLException{
104

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

    
121
		boolean success  = true;
122

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

    
149

    
150
		//notes
151
		if (! excludeNotes){
152
			doNotes(annotatableEntity, notes);
153
		}
154
		return success;
155
	}
156

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

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

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

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

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

    
216

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

    
249

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

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

    
282
		}else if("KRY-OO;UKR-UK".equals(tdwg)){
283
			return Country.UKRAINE();
284

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

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

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

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

    
309

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

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

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

    
382
		logger.warn("Area(em: '" + em + "', tdwg: '" + tdwg +"') could not be found");
383

    
384
		return null;
385
	}
386

    
387
    /**
388
     * @return
389
     */
390
    protected TermVocabulary<MarkerType> getEuroMedMarkerTypeVoc() {
391
        TermVocabulary<MarkerType> markerTypeVoc = getVocabulary(TermType.MarkerType, BerlinModelTransformer.uuidVocEMMarkerType,
392
                "Euro+Med marker type vocabulary", "E+M marker types", null, null, false, MarkerType.COMPLETE());
393
        return markerTypeVoc;
394
    }
395

    
396

    
397
    /**
398
     * @param sourceReference
399
     * @return
400
     */
401
    protected Reference getSourceReference(Reference sourceReference) {
402
        Reference persistentSourceReference = getReferenceService().find(sourceReference.getUuid());  //just to be sure
403
        if (persistentSourceReference != null){
404
            sourceReference = persistentSourceReference;
405
        }
406
        return sourceReference;
407
    }
408

    
409
}
(6-6/22)