Project

General

Profile

Download (16.3 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.reference.Reference;
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
40

    
41
/**
42
 * @author a.mueller
43
 * @since 20.03.2008
44
 */
45
public abstract class BerlinModelImportBase
46
            extends DbImportBase<BerlinModelImportState, BerlinModelImportConfigurator>
47
            implements ICdmIO<BerlinModelImportState>, IPartitionedIO<BerlinModelImportState> {
48
    private static final long serialVersionUID = -4982506434258587864L;
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
	@Override
60
    protected String getIdQuery(BerlinModelImportState state){
61
		String result = " SELECT " + getTableName() + "id FROM " + getTableName();
62
		return result;
63
	}
64

    
65

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

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

    
85

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

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

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

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

    
117
		boolean success  = true;
118

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

    
145

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

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

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

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

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

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

    
212

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

    
245

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

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

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

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

    
286

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

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

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

    
306

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

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

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

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

    
381
		return null;
382
	}
383

    
384

    
385
}
(5-5/21)