Project

General

Profile

Download (22.1 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
package eu.etaxonomy.cdm.io.algaterra;
10

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

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

    
26
import eu.etaxonomy.cdm.facade.DerivedUnitFacade;
27
import eu.etaxonomy.cdm.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.IdentifiableSource;
43
import eu.etaxonomy.cdm.model.common.Language;
44
import eu.etaxonomy.cdm.model.name.Rank;
45
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
46
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
47
import eu.etaxonomy.cdm.model.name.TaxonName;
48
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
49
import eu.etaxonomy.cdm.model.occurrence.Collection;
50
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
51
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
52
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
53
import eu.etaxonomy.cdm.model.permission.User;
54
import eu.etaxonomy.cdm.model.reference.Reference;
55
import eu.etaxonomy.cdm.model.term.DefinedTerm;
56

    
57
/**
58
 * @author a.mueller
59
 * @since 20.03.2008
60
 */
61
@Component
62
public class AlgaTerraTypeImport  extends AlgaTerraSpecimenImportBase {
63

    
64
    private static final long serialVersionUID = 32400936433601479L;
65
    private static final Logger logger = LogManager.getLogger();
66

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

    
71
	@Override
72
    protected String getLocalityString(){
73
		return "TypeLocality";
74
	}
75

    
76
	public AlgaTerraTypeImport(){
77
		super(dbTableName, pluralString);
78
	}
79

    
80
	@Override
81
	protected String getIdQuery(BerlinModelImportState state) {
82
		String result = " SELECT TypeDesignationId "
83
				+ " FROM TypeDesignation "
84
				+ " ORDER BY NameFk ";
85
		return result;
86
	}
87

    
88
	@Override
89
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
90
			String strQuery =
91

    
92
			" 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, " +
93
               " ts.RefFk as tsRefFk, ts.RefDetailFk as tsRefDetailFk, ts.MaterialCategoryFK as tsMaterialCategoryFK, td.RefFk as tdRefFk, td.RefDetailFk as tdRefDetailFk, " +
94
               " RefDet.Details as tdRefDetails, " +
95
               " td.created_When as tdCreated_When, tsd.created_When as tsdCreated_When, td.updated_when as tdUpdated_when, " +
96
               " td.created_who as tdCreated_who, tsd.created_who as tsdCreated_who, td.updated_who tdUpdated_who,  " +
97
               " mc.* " +
98
            " FROM TypeSpecimenDesignation tsd  "
99
            	+ " LEFT OUTER JOIN TypeSpecimen AS ts ON tsd.TypeSpecimenFk = ts.TypeSpecimenId "
100
            	+ " FULL OUTER JOIN TypeDesignation td ON  td.TypeDesignationId = tsd.TypeDesignationFk "
101
            	+ " LEFT OUTER JOIN TDWGGazetteer gz ON ts.TDWGGazetteerFk = gz.ID "
102
            	+ " LEFT OUTER JOIN RefDetail refDet ON td.RefDetailFk = refDet.RefDetailId AND td.RefFk = refDet.RefFk "
103
            	+ " LEFT OUTER JOIN MaterialCategory mc ON mc.MaterialCategoryId = ts.MaterialCategoryFK "
104
		+ 	" WHERE (td.TypeDesignationId IN (" + ID_LIST_TOKEN + ")  )"
105
          + " ORDER BY NameFk "
106
            ;
107
		return strQuery;
108
	}
109

    
110
	@Override
111
	public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
112
		boolean success = true;
113

    
114
		AlgaTerraImportState state = (AlgaTerraImportState)bmState;
115

    
116
		//TODO check that no duplicate vocabularies will be created, also remove redundant code here
117
//		and in Specimen importer
118
		try {
119
			makeVocabulariesAndFeatures(state);
120
		} catch (SQLException e1) {
121
			logger.warn("Exception occurred when trying to create Type specimen vocabularies: " + e1.getMessage());
122
			e1.printStackTrace();
123
		}
124

    
125

    
126

    
127
		Set<TaxonName> namesToSave = new HashSet<>();
128

    
129
		@SuppressWarnings("unchecked")
130
        Map<String, TaxonName> taxonNameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
131
		@SuppressWarnings("unchecked")
132
        Map<String, DerivedUnit> ecoFactMap = partitioner.getObjectMap(AlgaTerraSpecimenImportBase.ECO_FACT_FIELD_OBSERVATION_NAMESPACE);
133
		@SuppressWarnings("unchecked")
134
        Map<String, DerivedUnit> typeSpecimenMap = partitioner.getObjectMap(TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE);
135
		@SuppressWarnings("unchecked")
136
        Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
137
		@SuppressWarnings("unchecked")
138
        Map<String, Reference> refDetailMap = partitioner.getObjectMap(BerlinModelRefDetailImport.REFDETAIL_NAMESPACE);
139

    
140

    
141
		ResultSet rs = partitioner.getResultSet();
142

    
143
		try {
144

    
145
			int i = 0;
146

    
147
			//for each reference
148
            while (rs.next()){
149

    
150
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("Type designations handled: " + (i-1));}
151

    
152
				int nameId = rs.getInt("nameFk");
153
				int typeSpecimenId = rs.getInt("TypeSpecimenId");
154
				int typeDesignationId = rs.getInt("TypeDesignationId");
155
				Integer typeStatusFk =  nullSafeInt(rs, "typeStatusFk");
156
				Integer ecoFactId = nullSafeInt(rs, "ecoFactFk");
157
				Integer tdRefFk = nullSafeInt(rs, "tdRefFk");
158
				Integer tdRefDetailFk = nullSafeInt(rs, "tdRefDetailFk");
159
				String tdRefDetails = rs.getString("tdRefDetails");
160
				Boolean restrictedFlag = nullSafeBoolean(rs, "RestrictedFlag");
161

    
162
				String typeSpecimenPhrase = rs.getString("TypeSpecimenPhrase");
163
				Integer tsMaterialCategoryFK = nullSafeInt(rs, "MaterialCategoryFK");
164

    
165
				boolean isIcon = typeSpecimenPhrase != null && typeSpecimenPhrase.toLowerCase().startsWith("\u005bicon");
166

    
167
				try {
168

    
169
					//source ref
170
					Reference sourceRef = state.getTransactionalSourceReference();
171

    
172
					//facade
173
					SpecimenOrObservationType type = SpecimenOrObservationType.PreservedSpecimen;
174
					if (isIcon){
175
						//TODO handle images correctly for these specimen
176
						type = SpecimenOrObservationType.StillImage;
177
					}else if (typeStatusFk != null && typeStatusFk.equals(39)){
178
						type =  SpecimenOrObservationType.LivingSpecimen;
179
					}else if (tsMaterialCategoryFK != null && tsMaterialCategoryFK.equals(4)){
180
						type = SpecimenOrObservationType.Fossil;
181
					}
182

    
183

    
184
					if (tsMaterialCategoryFK != null && typeStatusFk != null &&
185
							( typeStatusFk.equals(39) && !tsMaterialCategoryFK.equals(14) || ! typeStatusFk.equals(39) && tsMaterialCategoryFK.equals(14) )){
186
						logger.warn("Living Specimen type status should be 39 and materialCategoryFk 14 but one of them wasn't");
187
					}
188

    
189

    
190
					DerivedUnitFacade facade = getDerivedUnit(state, typeSpecimenId, typeSpecimenMap, type, ecoFactMap, ecoFactId, sourceRef);
191

    
192
					//field observation
193
					handleFieldObservationSpecimen(rs, facade, state, partitioner);
194

    
195
//					handleTypeSpecimenSpecificFieldObservation(rs,facade, state);
196

    
197
					//TODO divide like in EcoFact (if necessary)
198
					handleTypeSpecimenSpecificSpecimen(rs,facade, state, refMap, typeSpecimenId);
199

    
200
					handleFirstDerivedSpecimen(rs, facade, state, partitioner);
201

    
202

    
203
					//Designation
204
					TaxonName name = getTaxonName(state, taxonNameMap, nameId);
205
					SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
206
					SpecimenTypeDesignationStatus status = getSpecimenTypeDesignationStatusByKey(typeStatusFk);
207
					if (typeStatusFk != null && typeStatusFk.equals(39)){
208
						designation.addAnnotation(Annotation.NewInstance("Type status: Authentic strain", AnnotationType.EDITORIAL(), Language.DEFAULT()));
209
					}
210

    
211
					designation.setTypeSpecimen(facade.innerDerivedUnit());
212
					designation.setTypeStatus(status);
213
					if (tdRefFk != null){
214
						Reference typeDesigRef = getReferenceFromMaps(refDetailMap, refMap, String.valueOf(tdRefDetailFk), String.valueOf(tdRefFk));
215
						if (typeDesigRef == null){
216
							logger.warn("Type designation reference not found in maps: " + tdRefFk);
217
						}else{
218
							designation.setCitation(typeDesigRef);
219
						}
220
					}
221
					if (isNotBlank(tdRefDetails)){
222
						designation.setCitationMicroReference(tdRefDetails);
223
					}
224

    
225
					//ID: Type designations do not allow OriginalSources
226
					designation.addAnnotation(Annotation.NewInstance("Id in BerlinModel-TypeDesignation: " + String.valueOf(typeDesignationId), AnnotationType.TECHNICAL(), Language.UNDETERMINED()));
227

    
228
					if (restrictedFlag != null &&restrictedFlag.equals(true)){
229
						logger.warn("Restricted Flag is expected to be null or 0. TypeDesignationId" + typeDesignationId);
230
					}
231

    
232
					//Created, Updated
233
					this.doCreatedUpdated(state, designation, rs);
234

    
235
					if (name != null){
236
						name.addTypeDesignation(designation, true); //TODO check if true is correct
237
					}else{
238
						logger.warn("Name could not be found for type designation " + typeDesignationId);
239
					}
240
					namesToSave.add(name);
241

    
242

    
243
				} catch (Exception e) {
244
					logger.warn("Exception in TypeDesignation: TypeDesignationId " + typeDesignationId + ". " + e.getMessage());
245
					e.printStackTrace();
246
				}
247

    
248
            }
249

    
250
//            logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
251

    
252
			logger.warn("Names to save: " + namesToSave.size());
253
			getNameService().save(namesToSave);
254

    
255
			return success;
256
		} catch (SQLException e) {
257
			logger.error("SQLException:" +  e);
258
			return false;
259
		}
260
	}
261

    
262
	/**
263
	 * same as {@link BerlinModelImportBase#doCreatedUpdatedNotes}, but handles multiple similar fields
264
	 * @throws SQLException
265
	 */
266
	private void doCreatedUpdated(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs) throws SQLException{
267
		BerlinModelImportConfigurator config = state.getConfig();
268
		Object createdWhen = rs.getObject("tsdCreated_When");
269
		Object tdCreatedWhen = rs.getObject("tdCreated_When");
270
		if (tdCreatedWhen != null){
271
			createdWhen = tdCreatedWhen;
272
		}
273

    
274
		String createdWho = rs.getString("tsdCreated_Who");
275
		String tdCreatedWho = rs.getString("tdCreated_Who");
276
		if (tdCreatedWho != null){
277
			createdWho = tdCreatedWho;
278
		}
279

    
280
		Object updatedWhen = rs.getObject("tdUpdated_When");
281
		String updatedWho = rs.getString("tdUpdated_who");
282

    
283
		//Created When, Who, Updated When Who
284
		if (config.getEditor() == null || config.getEditor().equals(EDITOR.NO_EDITORS)){
285
			//do nothing
286
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_ANNOTATION)){
287
			String createdAnnotationString = "Berlin Model record was created By: " + String.valueOf(createdWho) + " (" + String.valueOf(createdWhen) + ") ";
288
			if (updatedWhen != null && updatedWho != null){
289
				createdAnnotationString += " and updated By: " + String.valueOf(updatedWho) + " (" + String.valueOf(updatedWhen) + ")";
290
			}
291
			Annotation annotation = Annotation.NewInstance(createdAnnotationString, Language.DEFAULT());
292
			annotation.setCommentator(config.getCommentator());
293
			annotation.setAnnotationType(AnnotationType.TECHNICAL());
294
			annotatableEntity.addAnnotation(annotation);
295
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_EDITOR)){
296
			User creator = getUser(state, createdWho);
297
			User updator = getUser(state, updatedWho);
298
			DateTime created = getDateTime(createdWhen);
299
			DateTime updated = getDateTime(updatedWhen);
300
			annotatableEntity.setCreatedBy(creator);
301
			annotatableEntity.setUpdatedBy(updator);
302
			annotatableEntity.setCreated(created);
303
			annotatableEntity.setUpdated(updated);
304
		}else {
305
			logger.warn("Editor type not yet implemented: " + config.getEditor());
306
		}
307
	}
308

    
309

    
310
	private DateTime getDateTime(Object timeString){
311
		if (timeString == null){
312
			return null;
313
		}
314
		DateTime dateTime = null;
315
		if (timeString instanceof Timestamp){
316
			Timestamp timestamp = (Timestamp)timeString;
317
			dateTime = new DateTime(timestamp);
318
		}else{
319
			logger.warn("time ("+timeString+") is not a timestamp. Datetime set to current date. ");
320
			dateTime = new DateTime();
321
		}
322
		return dateTime;
323
	}
324

    
325

    
326
	@Override
327
    protected String getDerivedUnitNameSpace(){
328
		return TYPE_SPECIMEN_DERIVED_UNIT_NAMESPACE;
329
	}
330

    
331
	@Override
332
    protected String getFieldObservationNameSpace(){
333
		return TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE;
334
	}
335

    
336
	private TaxonName getTaxonName(AlgaTerraImportState state, Map<String, TaxonName> taxonNameMap, int nameId) {
337
		TaxonName result;
338
		if (state.getConfig().isDoTaxonNames()){
339
			result = taxonNameMap.get(String.valueOf(nameId));
340
		}else{
341
			//for testing
342
			result = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
343
		}
344
		return result;
345
	}
346

    
347
	private void handleTypeSpecimenSpecificSpecimen(ResultSet rs, DerivedUnitFacade facade, AlgaTerraImportState state, Map<String, Reference> refMap, int typeSpecimenId) throws SQLException {
348

    
349

    
350
		//TODO
351

    
352

    
353

    
354
		DerivedUnit derivedUnit = facade.innerDerivedUnit();
355

    
356
		Integer tsMaterialCategoryFK = nullSafeInt(rs, "tsMaterialCategoryFK");
357
		String matCat = rs.getString("MaterialCategory");
358
		if (tsMaterialCategoryFK != null){
359
			if (tsMaterialCategoryFK.equals(16)){
360
				tsMaterialCategoryFK = 9;
361
			}
362
			UUID uuid = materialCategoryMapping.get(tsMaterialCategoryFK);
363
			if (uuid == null){
364
				logger.warn("Uuid was null. This should not happen.");
365
			}
366
			DefinedTerm kindOfUnit = getKindOfUnit(state, uuid, matCat, null, null, null);  //all terms should exist already
367
			facade.setFieldUnitKindOfUnit(kindOfUnit);
368
		}else{
369
			logger.warn("Material Category was null. This is not expected");
370
		}
371

    
372

    
373
		//collection
374
		String barcode = rs.getString("Barcode");
375
		if (StringUtils.isNotBlank(barcode)){
376
			facade.setBarcode(barcode);
377
		}
378

    
379
		//RefFk + RefDetailFk
380
		Integer  refFk = nullSafeInt(rs, "tsRefFk");
381
		if (refFk != null){
382

    
383
			Reference ref = refMap.get(String.valueOf(refFk));
384
			if (ref == null){
385
				logger.warn("TypeSpecimen reference (" + refFk + ")not found in biblioRef. TypeSpecimenId: " + typeSpecimenId);
386
			}else{
387
				IdentifiableSource source = IdentifiableSource.NewPrimarySourceInstance(ref, null);
388
				derivedUnit.addSource(source);
389
			}
390
		}
391

    
392
		Integer refDetailFk = nullSafeInt(rs, "tsRefDetailFk");
393
		if (refDetailFk != null){
394
			logger.warn("TypeSpecimen.RefDetailFk should always be NULL but wasn't: " + typeSpecimenId);
395
		}
396

    
397
	}
398

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

    
427
		return facade;
428
	}
429

    
430

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

    
458
	}
459

    
460

    
461
	@Override
462
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
463

    
464
	    String nameSpace;
465
		Set<String> idSet;
466
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
467

    
468
		try{
469
			Set<String> nameIdSet = new HashSet<>();
470
			Set<String> ecoFieldObservationIdSet = new HashSet<>();
471
			Set<String> typeSpecimenIdSet = new HashSet<>();
472
//			Set<String> termsIdSet = new HashSet<>();
473
			Set<String> collectionIdSet = new HashSet<>();
474
			Set<String> referenceIdSet = new HashSet<>();
475
			Set<String> refDetailIdSet = new HashSet<>();
476

    
477
			while (rs.next()){
478
				handleForeignKey(rs, nameIdSet, "nameFk");
479
				handleForeignKey(rs, ecoFieldObservationIdSet, "ecoFactFk");
480
				handleForeignKey(rs, typeSpecimenIdSet, "TypeSpecimenId");
481
				handleForeignKey(rs, collectionIdSet, "CollectionFk");
482
				handleForeignKey(rs, referenceIdSet, "tsRefFk");
483
				handleForeignKey(rs, referenceIdSet, "tdRefFk");
484
				handleForeignKey(rs, refDetailIdSet, "tdRefDetailFk");
485
			}
486

    
487
			//name map
488
			nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
489
			idSet = nameIdSet;
490
			Map<String, TaxonName> objectMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonName.class, idSet, nameSpace);
491
			result.put(nameSpace, objectMap);
492

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

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

    
505

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

    
512
			//sub-collections
513
			nameSpace = AlgaTerraCollectionImport.NAMESPACE_SUBCOLLECTION;
514
			idSet = collectionIdSet;
515
			Map<String, Collection> subCollectionMap = getCommonService().getSourcedObjectsByIdInSourceC(Collection.class, idSet, nameSpace);
516
			result.put(nameSpace, subCollectionMap);
517

    
518
			//reference map
519
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
520
			idSet = referenceIdSet;
521
			Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
522
			result.put(nameSpace, referenceMap);
523

    
524
			//refDetail map
525
			nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE;
526
			idSet = refDetailIdSet;
527
			Map<String, Reference> refDetailMap= getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
528
			result.put(nameSpace, refDetailMap);
529

    
530
		} catch (SQLException e) {
531
			throw new RuntimeException(e);
532
		}
533
		return result;
534
	}
535

    
536
	@Override
537
	protected boolean doCheck(BerlinModelImportState state){
538
		IOValidator<BerlinModelImportState> validator = new AlgaTerraTypeImportValidator();
539
		return validator.validate(state);
540
	}
541

    
542
	@Override
543
    protected boolean isIgnore(BerlinModelImportState state){
544
		return ! state.getConfig().isDoTypes();
545
	}
546
}
(14-14/15)