Project

General

Profile

Download (22.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.algaterra;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.sql.Timestamp;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.log4j.Logger;
23
import org.joda.time.DateTime;
24
import org.springframework.stereotype.Component;
25

    
26
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
27
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
28
import eu.etaxonomy.cdm.io.algaterra.validation.AlgaTerraTypeImportValidator;
29
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase;
30
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
31
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
32
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelRefDetailImport;
33
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelReferenceImport;
34
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelTaxonNameImport;
35
import eu.etaxonomy.cdm.io.common.IImportConfigurator.EDITOR;
36
import eu.etaxonomy.cdm.io.common.IOValidator;
37
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
38
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
39
import eu.etaxonomy.cdm.model.common.Annotation;
40
import eu.etaxonomy.cdm.model.common.AnnotationType;
41
import eu.etaxonomy.cdm.model.common.CdmBase;
42
import eu.etaxonomy.cdm.model.common.DefinedTerm;
43
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
44
import eu.etaxonomy.cdm.model.common.Language;
45
import eu.etaxonomy.cdm.model.common.User;
46
import eu.etaxonomy.cdm.model.name.BotanicalName;
47
import eu.etaxonomy.cdm.model.name.Rank;
48
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
49
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
50
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
51
import eu.etaxonomy.cdm.model.occurrence.Collection;
52
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
53
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
54
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
55
import eu.etaxonomy.cdm.model.reference.Reference;
56

    
57
/**
58
 * @author a.mueller
59
 * @created 20.03.2008
60
 */
61
@Component
62
public class AlgaTerraTypeImport  extends AlgaTerraSpecimenImportBase {
63
	private static final Logger logger = Logger.getLogger(AlgaTerraTypeImport.class);
64

    
65
	
66
	private static int modCount = 5000;
67
	private static final String pluralString = "types";
68
	private static final String dbTableName = "TypeDesignation";  //??  
69

    
70
	protected String getLocalityString(){
71
		return "TypeLocality";
72
	}
73
	
74
	public AlgaTerraTypeImport(){
75
		super(dbTableName, pluralString);
76
	}
77
	
78
	
79
	@Override
80
	protected String getIdQuery(BerlinModelImportState state) {
81
		String result = " SELECT TypeDesignationId "  
82
				+ " FROM TypeDesignation " 
83
				+ " ORDER BY NameFk ";
84
		return result;
85
	}
86

    
87
	@Override
88
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
89
			String strQuery =    
90
					
91
			" SELECT ts.*, ts.TypeSpecimenId as unitId, td.*, gz.ID as GazetteerId,  gz.L1Code, gz.L2Code, gz.L3Code, gz.L4Code, gz.ISOCountry, gz.Country, gz.subL4, ts.WaterBody, " + 
92
               " ts.RefFk as tsRefFk, ts.RefDetailFk as tsRefDetailFk, ts.MaterialCategoryFK as tsMaterialCategoryFK, td.RefFk as tdRefFk, td.RefDetailFk as tdRefDetailFk, " +
93
               " RefDet.Details as tdRefDetails, " +
94
               " td.created_When as tdCreated_When, tsd.created_When as tsdCreated_When, td.updated_when as tdUpdated_when, " +
95
               " td.created_who as tdCreated_who, tsd.created_who as tsdCreated_who, td.updated_who tdUpdated_who,  " +
96
               " mc.* " +
97
            " FROM TypeSpecimenDesignation tsd  " 
98
            	+ " LEFT OUTER JOIN TypeSpecimen AS ts ON tsd.TypeSpecimenFk = ts.TypeSpecimenId " 
99
            	+ " FULL OUTER JOIN TypeDesignation td ON  td.TypeDesignationId = tsd.TypeDesignationFk "
100
            	+ " LEFT OUTER JOIN TDWGGazetteer gz ON ts.TDWGGazetteerFk = gz.ID "
101
            	+ " LEFT OUTER JOIN RefDetail refDet ON td.RefDetailFk = refDet.RefDetailId AND td.RefFk = refDet.RefFk "
102
            	+ " LEFT OUTER JOIN MaterialCategory mc ON mc.MaterialCategoryId = ts.MaterialCategoryFK "
103
		+ 	" WHERE (td.TypeDesignationId IN (" + ID_LIST_TOKEN + ")  )"  
104
          + " ORDER BY NameFk "
105
            ;
106
		return strQuery;
107
	}
108

    
109
	@Override
110
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
111
		boolean success = true;
112
		
113
		AlgaTerraImportState state = (AlgaTerraImportState)bmState;
114
		
115
		//TODO check that no duplicate vocabularies will be created, also remove redundant code here
116
//		and in Specimen importer
117
		try {
118
			makeVocabulariesAndFeatures(state);
119
		} catch (SQLException e1) {
120
			logger.warn("Exception occurred when trying to create Type specimen vocabularies: " + e1.getMessage());
121
			e1.printStackTrace();
122
		}
123
		
124
		
125
		
126
		Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
127
		
128
		Map<String, TaxonNameBase> taxonNameMap = (Map<String, TaxonNameBase>) partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
129
		Map<String, DerivedUnit> ecoFactMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(AlgaTerraEcoFactImport.ECO_FACT_FIELD_OBSERVATION_NAMESPACE);
130
		Map<String, DerivedUnit> typeSpecimenMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE);
131
		Map<String, Reference> refMap = (Map<String, Reference>) partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
132
		Map<String, Reference> refDetailMap = partitioner.getObjectMap(BerlinModelRefDetailImport.REFDETAIL_NAMESPACE);
133
		
134
		
135
		ResultSet rs = partitioner.getResultSet();
136

    
137
		try {
138
			
139
			int i = 0;
140

    
141
			//for each reference
142
            while (rs.next()){
143
                
144
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("Type designations handled: " + (i-1));}
145
				
146
				int nameId = rs.getInt("nameFk");
147
				int typeSpecimenId = rs.getInt("TypeSpecimenId");
148
				int typeDesignationId = rs.getInt("TypeDesignationId");
149
				Integer typeStatusFk =  nullSafeInt(rs, "typeStatusFk");
150
				Integer ecoFactId = nullSafeInt(rs, "ecoFactFk");
151
				Integer tdRefFk = nullSafeInt(rs, "tdRefFk");
152
				Integer tdRefDetailFk = nullSafeInt(rs, "tdRefDetailFk");
153
				String tdRefDetails = rs.getString("tdRefDetails");
154
				Boolean restrictedFlag = nullSafeBoolean(rs, "RestrictedFlag");
155
				
156
				String typeSpecimenPhrase = rs.getString("TypeSpecimenPhrase");
157
				Integer tsMaterialCategoryFK = nullSafeInt(rs, "MaterialCategoryFK");
158
				
159
				boolean isIcon = typeSpecimenPhrase != null && typeSpecimenPhrase.toLowerCase().startsWith("\u005bicon");
160
				
161
				try {
162
					
163
					//source ref
164
					Reference<?> sourceRef = state.getTransactionalSourceReference();
165
				
166
					//facade
167
					SpecimenOrObservationType type = SpecimenOrObservationType.PreservedSpecimen;
168
					if (isIcon){
169
						//TODO handle images correctly for these specimen
170
						type = SpecimenOrObservationType.StillImage;
171
					}else if (typeStatusFk != null && typeStatusFk.equals(39)){
172
						type =  SpecimenOrObservationType.LivingSpecimen;
173
					}else if (tsMaterialCategoryFK != null && tsMaterialCategoryFK.equals(4)){
174
						type = SpecimenOrObservationType.Fossil;
175
					}
176
					
177
					
178
					if (tsMaterialCategoryFK != null && typeStatusFk != null && 
179
							( typeStatusFk.equals(39) && !tsMaterialCategoryFK.equals(14) || ! typeStatusFk.equals(39) && tsMaterialCategoryFK.equals(14) )){
180
						logger.warn("Living Specimen type status should be 39 and materialCategoryFk 14 but one of them wasn't");
181
					}
182
					
183
					
184
					DerivedUnitFacade facade = getDerivedUnit(state, typeSpecimenId, typeSpecimenMap, type, ecoFactMap, ecoFactId, sourceRef);
185
					
186
					//field observation
187
					handleFieldObservationSpecimen(rs, facade, state, partitioner);
188
					
189
//					handleTypeSpecimenSpecificFieldObservation(rs,facade, state);
190
					
191
					//TODO divide like in EcoFact (if necessary)
192
					handleTypeSpecimenSpecificSpecimen(rs,facade, state, refMap, typeSpecimenId);
193
					
194
					handleFirstDerivedSpecimen(rs, facade, state, partitioner);
195
					
196
					
197
					//Designation
198
					TaxonNameBase<?,?> name = getTaxonName(state, taxonNameMap, nameId);
199
					SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
200
					SpecimenTypeDesignationStatus status = getSpecimenTypeDesignationStatusByKey(typeStatusFk);
201
					if (typeStatusFk != null && typeStatusFk.equals(39)){
202
						designation.addAnnotation(Annotation.NewInstance("Type status: Authentic strain", AnnotationType.EDITORIAL(), Language.DEFAULT()));
203
					}
204
					
205
					designation.setTypeSpecimen(facade.innerDerivedUnit());
206
					designation.setTypeStatus(status);
207
					if (tdRefFk != null){
208
						Reference<?> typeDesigRef = getReferenceFromMaps(refDetailMap, refMap, String.valueOf(tdRefDetailFk), String.valueOf(tdRefFk));
209
						if (typeDesigRef == null){
210
							logger.warn("Type designation reference not found in maps: " + tdRefFk);
211
						}else{
212
							designation.setCitation(typeDesigRef);
213
						}
214
					}
215
					if (isNotBlank(tdRefDetails)){
216
						designation.setCitationMicroReference(tdRefDetails);
217
					}
218
					
219
					//ID: Type designations do not allow OriginalSources
220
					designation.addAnnotation(Annotation.NewInstance("Id in BerlinModel-TypeDesignation: " + String.valueOf(typeDesignationId), AnnotationType.TECHNICAL(), Language.UNDETERMINED()));
221
					
222
					if (restrictedFlag != null &&restrictedFlag.equals(true)){
223
						logger.warn("Restricted Flag is expected to be null or 0. TypeDesignationId" + typeDesignationId);
224
					}
225
					
226
					//Created, Updated
227
					this.doCreatedUpdated(state, designation, rs);
228
					
229
					if (name != null){
230
						name.addTypeDesignation(designation, true); //TODO check if true is correct
231
					}else{
232
						logger.warn("Name could not be found for type designation " + typeDesignationId);
233
					}
234
					namesToSave.add(name); 
235
					
236

    
237
				} catch (Exception e) {
238
					logger.warn("Exception in TypeDesignation: TypeDesignationId " + typeDesignationId + ". " + e.getMessage());
239
					e.printStackTrace();
240
				} 
241
                
242
            }
243
           
244
//            logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
245

    
246
			logger.warn("Names to save: " + namesToSave.size());
247
			getNameService().save(namesToSave);	
248
			
249
			return success;
250
		} catch (SQLException e) {
251
			logger.error("SQLException:" +  e);
252
			return false;
253
		}
254
	}
255
	
256
	/**
257
	 * same as {@link BerlinModelImportBase#doCreatedUpdatedNotes}, but handles multiple similar fields
258
	 * @throws SQLException 
259
	 */
260
	private void doCreatedUpdated(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs) throws SQLException{
261
		BerlinModelImportConfigurator config = state.getConfig();
262
		Object createdWhen = rs.getObject("tsdCreated_When");
263
		Object tdCreatedWhen = rs.getObject("tdCreated_When");
264
		if (tdCreatedWhen != null){
265
			createdWhen = tdCreatedWhen;
266
		}
267
		
268
		String createdWho = rs.getString("tsdCreated_Who");
269
		String tdCreatedWho = rs.getString("tdCreated_Who");
270
		if (tdCreatedWho != null){
271
			createdWho = tdCreatedWho;
272
		}
273
		
274
		Object updatedWhen = rs.getObject("tdUpdated_When");
275
		String updatedWho = rs.getString("tdUpdated_who");
276
		
277
		//Created When, Who, Updated When Who
278
		if (config.getEditor() == null || config.getEditor().equals(EDITOR.NO_EDITORS)){
279
			//do nothing
280
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_ANNOTATION)){
281
			String createdAnnotationString = "Berlin Model record was created By: " + String.valueOf(createdWho) + " (" + String.valueOf(createdWhen) + ") ";
282
			if (updatedWhen != null && updatedWho != null){
283
				createdAnnotationString += " and updated By: " + String.valueOf(updatedWho) + " (" + String.valueOf(updatedWhen) + ")";
284
			}
285
			Annotation annotation = Annotation.NewInstance(createdAnnotationString, Language.DEFAULT());
286
			annotation.setCommentator(config.getCommentator());
287
			annotation.setAnnotationType(AnnotationType.TECHNICAL());
288
			annotatableEntity.addAnnotation(annotation);
289
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_EDITOR)){
290
			User creator = getUser(state, createdWho);
291
			User updator = getUser(state, updatedWho);
292
			DateTime created = getDateTime(createdWhen);
293
			DateTime updated = getDateTime(updatedWhen);
294
			annotatableEntity.setCreatedBy(creator);
295
			annotatableEntity.setUpdatedBy(updator);
296
			annotatableEntity.setCreated(created);
297
			annotatableEntity.setUpdated(updated);
298
		}else {
299
			logger.warn("Editor type not yet implemented: " + config.getEditor());
300
		}
301
	}
302
	
303

    
304
	private DateTime getDateTime(Object timeString){
305
		if (timeString == null){
306
			return null;
307
		}
308
		DateTime dateTime = null;
309
		if (timeString instanceof Timestamp){
310
			Timestamp timestamp = (Timestamp)timeString;
311
			dateTime = new DateTime(timestamp);
312
		}else{
313
			logger.warn("time ("+timeString+") is not a timestamp. Datetime set to current date. ");
314
			dateTime = new DateTime();
315
		}
316
		return dateTime;
317
	}
318
	
319
	
320
	protected String getDerivedUnitNameSpace(){
321
		return TYPE_SPECIMEN_DERIVED_UNIT_NAMESPACE;
322
	}
323
	
324
	protected String getFieldObservationNameSpace(){
325
		return TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE;
326
	}
327

    
328
	/**
329
	 * @param state 
330
	 * @param taxonNameMap
331
	 * @param nameId
332
	 * @return
333
	 */
334
	private TaxonNameBase<?,?> getTaxonName(AlgaTerraImportState state, Map<String, TaxonNameBase> taxonNameMap, int nameId) {
335
		TaxonNameBase<?,?> result;
336
		if (state.getConfig().isDoTaxonNames()){
337
			result = taxonNameMap.get(String.valueOf(nameId));
338
		}else{
339
			//for testing
340
			result = BotanicalName.NewInstance(Rank.SPECIES());
341
		}
342
		return result;
343
	}
344

    
345
	private void handleTypeSpecimenSpecificSpecimen(ResultSet rs, DerivedUnitFacade facade, AlgaTerraImportState state, Map<String, Reference> refMap, int typeSpecimenId) throws SQLException {
346
		
347
		
348
		//TODO
349
		
350
		
351
		
352
		DerivedUnit derivedUnit = facade.innerDerivedUnit();
353
		
354
		Integer tsMaterialCategoryFK = nullSafeInt(rs, "tsMaterialCategoryFK");
355
		String matCat = rs.getString("MaterialCategory");
356
		if (tsMaterialCategoryFK != null){
357
			if (tsMaterialCategoryFK.equals(16)){
358
				tsMaterialCategoryFK = 9;
359
			}
360
			UUID uuid = materialCategoryMapping.get(tsMaterialCategoryFK);
361
			if (uuid == null){
362
				logger.warn("Uuid was null. This should not happen.");
363
			}
364
			DefinedTerm kindOfUnit = getKindOfUnit(state, uuid, matCat, null, null, null);  //all terms should exist already
365
			facade.setKindOfUnit(kindOfUnit);
366
		}else{
367
			logger.warn("Material Category was null. This is not expected");
368
		}
369
		
370
		
371
		//collection
372
		String barcode = rs.getString("Barcode");
373
		if (StringUtils.isNotBlank(barcode)){
374
			facade.setBarcode(barcode);
375
		}
376
		
377
		//RefFk + RefDetailFk
378
		Integer  refFk = nullSafeInt(rs, "tsRefFk");
379
		if (refFk != null){
380
			
381
			Reference<?> ref = refMap.get(String.valueOf(refFk));
382
			if (ref == null){
383
				logger.warn("TypeSpecimen reference (" + refFk + ")not found in biblioRef. TypeSpecimenId: " + typeSpecimenId);
384
			}else{
385
				IdentifiableSource source = IdentifiableSource.NewPrimarySourceInstance(ref, null);
386
				derivedUnit.addSource(source);
387
			}
388
		}
389
		
390
		Integer refDetailFk = nullSafeInt(rs, "tsRefDetailFk");
391
		if (refDetailFk != null){
392
			logger.warn("TypeSpecimen.RefDetailFk should always be NULL but wasn't: " + typeSpecimenId);
393
		}
394
		
395
	}
396

    
397
	/**
398
	 * @param state
399
	 * @param ecoFactId
400
	 * @param derivedUnitMap
401
	 * @param type 
402
	 * @param ecoFactId2 
403
	 * @param ecoFactMap 
404
	 * @param sourceRef 
405
	 * @return
406
	 */
407
	private DerivedUnitFacade getDerivedUnit(AlgaTerraImportState state, int typeSpecimenId, Map<String, DerivedUnit> typeSpecimenMap, SpecimenOrObservationType type, Map<String, DerivedUnit> ecoFactMap, Integer ecoFactId2, Reference<?> sourceRef) {
408
		//TODO implement ecoFact map - if not all null anymore
409
		String typeKey = String.valueOf(typeSpecimenId);
410
		DerivedUnit derivedUnit = typeSpecimenMap.get(typeKey);
411
		DerivedUnitFacade facade;
412
		if (derivedUnit == null){
413
			facade = DerivedUnitFacade.NewInstance(type);
414
			typeSpecimenMap.put(typeKey, derivedUnit);
415
		}else{
416
			try {
417
				facade = DerivedUnitFacade.NewInstance(derivedUnit);
418
				facade.addSource(IdentifiableSource.NewDataImportInstance(typeKey, "TypeSpecimen", sourceRef));
419
			} catch (DerivedUnitFacadeNotSupportedException e) {
420
				logger.error(e.getMessage());
421
				facade = DerivedUnitFacade.NewInstance(type);
422
			}
423
		}
424
		
425
		return facade;
426
	}
427

    
428
	
429
	private SpecimenTypeDesignationStatus getSpecimenTypeDesignationStatusByKey(Integer typeStatusFk) {
430
		if (typeStatusFk == null){ return SpecimenTypeDesignationStatus.TYPE();
431
		}else if (typeStatusFk == 1) { return SpecimenTypeDesignationStatus.HOLOTYPE();
432
		}else if (typeStatusFk == 2) { return SpecimenTypeDesignationStatus.LECTOTYPE();
433
		}else if (typeStatusFk == 3) { return SpecimenTypeDesignationStatus.NEOTYPE();
434
		}else if (typeStatusFk == 4) { return SpecimenTypeDesignationStatus.EPITYPE();
435
		}else if (typeStatusFk == 5) { return SpecimenTypeDesignationStatus.ISOLECTOTYPE();
436
		}else if (typeStatusFk == 6) { return SpecimenTypeDesignationStatus.ISONEOTYPE();
437
		}else if (typeStatusFk == 7) { return SpecimenTypeDesignationStatus.ISOTYPE();
438
		}else if (typeStatusFk == 8) { return SpecimenTypeDesignationStatus.PARANEOTYPE();
439
		}else if (typeStatusFk == 9) { return SpecimenTypeDesignationStatus.PARATYPE();
440
		}else if (typeStatusFk == 10) { return SpecimenTypeDesignationStatus.SECOND_STEP_LECTOTYPE();
441
		}else if (typeStatusFk == 11) { return SpecimenTypeDesignationStatus.SECOND_STEP_NEOTYPE();
442
		}else if (typeStatusFk == 12) { return SpecimenTypeDesignationStatus.SYNTYPE();
443
		}else if (typeStatusFk == 13) { return SpecimenTypeDesignationStatus.PARALECTOTYPE();
444
		}else if (typeStatusFk == 14) { return SpecimenTypeDesignationStatus.ISOEPITYPE();
445
		}else if (typeStatusFk == 21) { return SpecimenTypeDesignationStatus.ICONOTYPE();
446
		}else if (typeStatusFk == 22) { return SpecimenTypeDesignationStatus.PHOTOTYPE();
447
		}else if (typeStatusFk == 30) { return SpecimenTypeDesignationStatus.TYPE();
448
		}else if (typeStatusFk == 38) { return SpecimenTypeDesignationStatus.ISOEPITYPE();
449
		}else if (typeStatusFk == 39) { return SpecimenTypeDesignationStatus.ORIGINAL_MATERIAL();  //but add annotation
450
		}else if (typeStatusFk == 40) { return SpecimenTypeDesignationStatus.ORIGINAL_MATERIAL();
451
		}else{
452
			logger.warn("typeStatusFk undefined for " +  typeStatusFk);
453
			return SpecimenTypeDesignationStatus.TYPE();
454
		}
455
		
456
	}
457

    
458
	
459
	@Override
460
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
461
		String nameSpace;
462
		Class<?> cdmClass;
463
		Set<String> idSet;
464
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
465
		
466
		try{
467
			Set<String> nameIdSet = new HashSet<String>();
468
			Set<String> ecoFieldObservationIdSet = new HashSet<String>();
469
			Set<String> typeSpecimenIdSet = new HashSet<String>();
470
//			Set<String> termsIdSet = new HashSet<String>();
471
			Set<String> collectionIdSet = new HashSet<String>();
472
			Set<String> referenceIdSet = new HashSet<String>();
473
			Set<String> refDetailIdSet = new HashSet<String>();
474
			
475
			while (rs.next()){
476
				handleForeignKey(rs, nameIdSet, "nameFk");
477
				handleForeignKey(rs, ecoFieldObservationIdSet, "ecoFactFk");
478
				handleForeignKey(rs, typeSpecimenIdSet, "TypeSpecimenId");
479
				handleForeignKey(rs, collectionIdSet, "CollectionFk");
480
				handleForeignKey(rs, referenceIdSet, "tsRefFk");
481
				handleForeignKey(rs, referenceIdSet, "tdRefFk");
482
				handleForeignKey(rs, refDetailIdSet, "tdRefDetailFk");
483
			}
484
			
485
			//name map
486
			nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
487
			cdmClass = TaxonNameBase.class;
488
			idSet = nameIdSet;
489
			Map<String, TaxonNameBase> objectMap = (Map<String, TaxonNameBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
490
			result.put(nameSpace, objectMap);
491

    
492
			//eco fact field observation map
493
			nameSpace = AlgaTerraTypeImport.ECO_FACT_FIELD_OBSERVATION_NAMESPACE;
494
			cdmClass = FieldUnit.class;
495
			idSet = ecoFieldObservationIdSet;
496
			Map<String, FieldUnit> fieldObservationMap = (Map<String, FieldUnit>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
497
			result.put(nameSpace, fieldObservationMap);
498

    
499
			//type specimen map
500
			nameSpace = AlgaTerraTypeImport.TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE;
501
			cdmClass = FieldUnit.class;
502
			idSet = typeSpecimenIdSet;
503
			Map<String, FieldUnit> typeSpecimenMap = (Map<String, FieldUnit>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
504
			result.put(nameSpace, typeSpecimenMap);
505

    
506

    
507
			//collections
508
			nameSpace = AlgaTerraCollectionImport.NAMESPACE_COLLECTION;
509
			cdmClass = Collection.class;
510
			idSet = collectionIdSet;
511
			Map<String, Collection> collectionMap = (Map<String, Collection>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
512
			result.put(nameSpace, collectionMap);
513

    
514
			//sub-collections
515
			nameSpace = AlgaTerraCollectionImport.NAMESPACE_SUBCOLLECTION;
516
			cdmClass = Collection.class;
517
			idSet = collectionIdSet;
518
			Map<String, Collection> subCollectionMap = (Map<String, Collection>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
519
			result.put(nameSpace, subCollectionMap);
520

    
521
			//reference map
522
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
523
			cdmClass = Reference.class;
524
			idSet = referenceIdSet;
525
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
526
			result.put(nameSpace, referenceMap);
527
			
528
			//refDetail map
529
			nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE;
530
			cdmClass = Reference.class;
531
			idSet = refDetailIdSet;
532
			Map<String, Reference> refDetailMap= (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
533
			result.put(nameSpace, refDetailMap);
534
	
535
			
536
			
537
		} catch (SQLException e) {
538
			throw new RuntimeException(e);
539
		}
540
		return result;
541
	}
542

    
543
	/* (non-Javadoc)
544
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
545
	 */
546
	@Override
547
	protected boolean doCheck(BerlinModelImportState state){
548
		IOValidator<BerlinModelImportState> validator = new AlgaTerraTypeImportValidator();
549
		return validator.validate(state);
550
	}
551

    
552

    
553
	/* (non-Javadoc)
554
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
555
	 */
556
	protected boolean isIgnore(BerlinModelImportState state){
557
		return ! state.getConfig().isDoTypes();
558
	}
559
	
560
}
(14-14/15)