Project

General

Profile

Download (17.8 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.ISourceable;
31
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
32
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
33
import eu.etaxonomy.cdm.model.common.Language;
34
import eu.etaxonomy.cdm.model.common.MarkerType;
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
import eu.etaxonomy.cdm.model.term.TermType;
44
import eu.etaxonomy.cdm.model.term.TermVocabulary;
45

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

    
56
	public BerlinModelImportBase(String tableName, String pluralString ) {
57
		super(tableName, pluralString);
58
	}
59

    
60

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

    
70

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

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

    
90

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

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

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

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

    
123
		boolean success  = true;
124

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

    
151

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

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

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

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

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

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

    
218

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

    
251

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

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

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

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

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

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

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

    
311

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

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

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

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

    
386
		return null;
387
	}
388

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

    
398

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

    
411
    protected static <T extends IdentifiableSource> boolean importSourceExists(ISourceable<T> sourceable, String idInSource,
412
            String namespace, Reference ref) {
413
        for (T source : sourceable.getSources()){
414
            if (CdmUtils.nullSafeEqual(namespace, source.getIdNamespace()) &&
415
                CdmUtils.nullSafeEqual(idInSource, source.getIdInSource()) &&
416
                CdmUtils.nullSafeEqual(ref, source.getCitation())){
417
                    return true;
418
            }
419
        }
420
        return false;
421
    }
422

    
423
}
(6-6/22)