Project

General

Profile

« Previous | Next » 

Revision 8422c0cd

Added by Andreas Müller almost 8 years ago

Remove generics from Reference in cdmlib-app #5830

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/algaterra/AlgaTerraTypeImport.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
......
62 62
public class AlgaTerraTypeImport  extends AlgaTerraSpecimenImportBase {
63 63
	private static final Logger logger = Logger.getLogger(AlgaTerraTypeImport.class);
64 64

  
65
	
65

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

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

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

  
79

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

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

  
113 114
		AlgaTerraImportState state = (AlgaTerraImportState)bmState;
114
		
115

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

  
125

  
126

  
126 127
		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);
128

  
129
		Map<String, TaxonNameBase> taxonNameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
130
		Map<String, DerivedUnit> ecoFactMap = partitioner.getObjectMap(AlgaTerraEcoFactImport.ECO_FACT_FIELD_OBSERVATION_NAMESPACE);
131
		Map<String, DerivedUnit> typeSpecimenMap = partitioner.getObjectMap(TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE);
132
		Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
132 133
		Map<String, Reference> refDetailMap = partitioner.getObjectMap(BerlinModelRefDetailImport.REFDETAIL_NAMESPACE);
133
		
134
		
134

  
135

  
135 136
		ResultSet rs = partitioner.getResultSet();
136 137

  
137 138
		try {
138
			
139

  
139 140
			int i = 0;
140 141

  
141 142
			//for each reference
142 143
            while (rs.next()){
143
                
144

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

  
146 147
				int nameId = rs.getInt("nameFk");
147 148
				int typeSpecimenId = rs.getInt("TypeSpecimenId");
148 149
				int typeDesignationId = rs.getInt("TypeDesignationId");
......
152 153
				Integer tdRefDetailFk = nullSafeInt(rs, "tdRefDetailFk");
153 154
				String tdRefDetails = rs.getString("tdRefDetails");
154 155
				Boolean restrictedFlag = nullSafeBoolean(rs, "RestrictedFlag");
155
				
156

  
156 157
				String typeSpecimenPhrase = rs.getString("TypeSpecimenPhrase");
157 158
				Integer tsMaterialCategoryFK = nullSafeInt(rs, "MaterialCategoryFK");
158
				
159

  
159 160
				boolean isIcon = typeSpecimenPhrase != null && typeSpecimenPhrase.toLowerCase().startsWith("\u005bicon");
160
				
161

  
161 162
				try {
162
					
163

  
163 164
					//source ref
164
					Reference<?> sourceRef = state.getTransactionalSourceReference();
165
				
165
					Reference sourceRef = state.getTransactionalSourceReference();
166

  
166 167
					//facade
167 168
					SpecimenOrObservationType type = SpecimenOrObservationType.PreservedSpecimen;
168 169
					if (isIcon){
......
173 174
					}else if (tsMaterialCategoryFK != null && tsMaterialCategoryFK.equals(4)){
174 175
						type = SpecimenOrObservationType.Fossil;
175 176
					}
176
					
177
					
178
					if (tsMaterialCategoryFK != null && typeStatusFk != null && 
177

  
178

  
179
					if (tsMaterialCategoryFK != null && typeStatusFk != null &&
179 180
							( typeStatusFk.equals(39) && !tsMaterialCategoryFK.equals(14) || ! typeStatusFk.equals(39) && tsMaterialCategoryFK.equals(14) )){
180 181
						logger.warn("Living Specimen type status should be 39 and materialCategoryFk 14 but one of them wasn't");
181 182
					}
182
					
183
					
183

  
184

  
184 185
					DerivedUnitFacade facade = getDerivedUnit(state, typeSpecimenId, typeSpecimenMap, type, ecoFactMap, ecoFactId, sourceRef);
185
					
186

  
186 187
					//field observation
187 188
					handleFieldObservationSpecimen(rs, facade, state, partitioner);
188
					
189

  
189 190
//					handleTypeSpecimenSpecificFieldObservation(rs,facade, state);
190
					
191

  
191 192
					//TODO divide like in EcoFact (if necessary)
192 193
					handleTypeSpecimenSpecificSpecimen(rs,facade, state, refMap, typeSpecimenId);
193
					
194

  
194 195
					handleFirstDerivedSpecimen(rs, facade, state, partitioner);
195
					
196
					
196

  
197

  
197 198
					//Designation
198 199
					TaxonNameBase<?,?> name = getTaxonName(state, taxonNameMap, nameId);
199 200
					SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
......
201 202
					if (typeStatusFk != null && typeStatusFk.equals(39)){
202 203
						designation.addAnnotation(Annotation.NewInstance("Type status: Authentic strain", AnnotationType.EDITORIAL(), Language.DEFAULT()));
203 204
					}
204
					
205

  
205 206
					designation.setTypeSpecimen(facade.innerDerivedUnit());
206 207
					designation.setTypeStatus(status);
207 208
					if (tdRefFk != null){
208
						Reference<?> typeDesigRef = getReferenceFromMaps(refDetailMap, refMap, String.valueOf(tdRefDetailFk), String.valueOf(tdRefFk));
209
						Reference typeDesigRef = getReferenceFromMaps(refDetailMap, refMap, String.valueOf(tdRefDetailFk), String.valueOf(tdRefFk));
209 210
						if (typeDesigRef == null){
210 211
							logger.warn("Type designation reference not found in maps: " + tdRefFk);
211 212
						}else{
......
215 216
					if (isNotBlank(tdRefDetails)){
216 217
						designation.setCitationMicroReference(tdRefDetails);
217 218
					}
218
					
219

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

  
222 223
					if (restrictedFlag != null &&restrictedFlag.equals(true)){
223 224
						logger.warn("Restricted Flag is expected to be null or 0. TypeDesignationId" + typeDesignationId);
224 225
					}
225
					
226

  
226 227
					//Created, Updated
227 228
					this.doCreatedUpdated(state, designation, rs);
228
					
229

  
229 230
					if (name != null){
230 231
						name.addTypeDesignation(designation, true); //TODO check if true is correct
231 232
					}else{
232 233
						logger.warn("Name could not be found for type designation " + typeDesignationId);
233 234
					}
234
					namesToSave.add(name); 
235
					
235
					namesToSave.add(name);
236

  
236 237

  
237 238
				} catch (Exception e) {
238 239
					logger.warn("Exception in TypeDesignation: TypeDesignationId " + typeDesignationId + ". " + e.getMessage());
239 240
					e.printStackTrace();
240
				} 
241
                
241
				}
242

  
242 243
            }
243
           
244

  
244 245
//            logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
245 246

  
246 247
			logger.warn("Names to save: " + namesToSave.size());
247
			getNameService().save(namesToSave);	
248
			
248
			getNameService().save(namesToSave);
249

  
249 250
			return success;
250 251
		} catch (SQLException e) {
251 252
			logger.error("SQLException:" +  e);
252 253
			return false;
253 254
		}
254 255
	}
255
	
256

  
256 257
	/**
257 258
	 * same as {@link BerlinModelImportBase#doCreatedUpdatedNotes}, but handles multiple similar fields
258
	 * @throws SQLException 
259
	 * @throws SQLException
259 260
	 */
260 261
	private void doCreatedUpdated(BerlinModelImportState state, AnnotatableEntity annotatableEntity, ResultSet rs) throws SQLException{
261 262
		BerlinModelImportConfigurator config = state.getConfig();
......
264 265
		if (tdCreatedWhen != null){
265 266
			createdWhen = tdCreatedWhen;
266 267
		}
267
		
268

  
268 269
		String createdWho = rs.getString("tsdCreated_Who");
269 270
		String tdCreatedWho = rs.getString("tdCreated_Who");
270 271
		if (tdCreatedWho != null){
271 272
			createdWho = tdCreatedWho;
272 273
		}
273
		
274

  
274 275
		Object updatedWhen = rs.getObject("tdUpdated_When");
275 276
		String updatedWho = rs.getString("tdUpdated_who");
276
		
277

  
277 278
		//Created When, Who, Updated When Who
278 279
		if (config.getEditor() == null || config.getEditor().equals(EDITOR.NO_EDITORS)){
279 280
			//do nothing
......
299 300
			logger.warn("Editor type not yet implemented: " + config.getEditor());
300 301
		}
301 302
	}
302
	
303

  
303 304

  
304 305
	private DateTime getDateTime(Object timeString){
305 306
		if (timeString == null){
......
315 316
		}
316 317
		return dateTime;
317 318
	}
318
	
319
	
320
	protected String getDerivedUnitNameSpace(){
319

  
320

  
321
	@Override
322
    protected String getDerivedUnitNameSpace(){
321 323
		return TYPE_SPECIMEN_DERIVED_UNIT_NAMESPACE;
322 324
	}
323
	
324
	protected String getFieldObservationNameSpace(){
325

  
326
	@Override
327
    protected String getFieldObservationNameSpace(){
325 328
		return TYPE_SPECIMEN_FIELD_OBSERVATION_NAMESPACE;
326 329
	}
327 330

  
328 331
	/**
329
	 * @param state 
332
	 * @param state
330 333
	 * @param taxonNameMap
331 334
	 * @param nameId
332 335
	 * @return
......
343 346
	}
344 347

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

  
350

  
348 351
		//TODO
349
		
350
		
351
		
352

  
353

  
354

  
352 355
		DerivedUnit derivedUnit = facade.innerDerivedUnit();
353
		
356

  
354 357
		Integer tsMaterialCategoryFK = nullSafeInt(rs, "tsMaterialCategoryFK");
355 358
		String matCat = rs.getString("MaterialCategory");
356 359
		if (tsMaterialCategoryFK != null){
......
366 369
		}else{
367 370
			logger.warn("Material Category was null. This is not expected");
368 371
		}
369
		
370
		
372

  
373

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

  
377 380
		//RefFk + RefDetailFk
378 381
		Integer  refFk = nullSafeInt(rs, "tsRefFk");
379 382
		if (refFk != null){
380
			
381
			Reference<?> ref = refMap.get(String.valueOf(refFk));
383

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

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

  
395 398
	}
396 399

  
397 400
	/**
398 401
	 * @param state
399 402
	 * @param ecoFactId
400 403
	 * @param derivedUnitMap
401
	 * @param type 
402
	 * @param ecoFactId2 
403
	 * @param ecoFactMap 
404
	 * @param sourceRef 
404
	 * @param type
405
	 * @param ecoFactId2
406
	 * @param ecoFactMap
407
	 * @param sourceRef
405 408
	 * @return
406 409
	 */
407
	private DerivedUnitFacade getDerivedUnit(AlgaTerraImportState state, int typeSpecimenId, Map<String, DerivedUnit> typeSpecimenMap, SpecimenOrObservationType type, Map<String, DerivedUnit> ecoFactMap, Integer ecoFactId2, Reference<?> sourceRef) {
410
	private DerivedUnitFacade getDerivedUnit(AlgaTerraImportState state, int typeSpecimenId, Map<String, DerivedUnit> typeSpecimenMap, SpecimenOrObservationType type, Map<String, DerivedUnit> ecoFactMap, Integer ecoFactId2, Reference sourceRef) {
408 411
		//TODO implement ecoFact map - if not all null anymore
409 412
		String typeKey = String.valueOf(typeSpecimenId);
410 413
		DerivedUnit derivedUnit = typeSpecimenMap.get(typeKey);
......
421 424
				facade = DerivedUnitFacade.NewInstance(type);
422 425
			}
423 426
		}
424
		
427

  
425 428
		return facade;
426 429
	}
427 430

  
428
	
431

  
429 432
	private SpecimenTypeDesignationStatus getSpecimenTypeDesignationStatusByKey(Integer typeStatusFk) {
430 433
		if (typeStatusFk == null){ return SpecimenTypeDesignationStatus.TYPE();
431 434
		}else if (typeStatusFk == 1) { return SpecimenTypeDesignationStatus.HOLOTYPE();
......
452 455
			logger.warn("typeStatusFk undefined for " +  typeStatusFk);
453 456
			return SpecimenTypeDesignationStatus.TYPE();
454 457
		}
455
		
458

  
456 459
	}
457 460

  
458
	
461

  
459 462
	@Override
460 463
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
461 464
		String nameSpace;
462 465
		Class<?> cdmClass;
463 466
		Set<String> idSet;
464 467
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
465
		
468

  
466 469
		try{
467 470
			Set<String> nameIdSet = new HashSet<String>();
468 471
			Set<String> ecoFieldObservationIdSet = new HashSet<String>();
......
471 474
			Set<String> collectionIdSet = new HashSet<String>();
472 475
			Set<String> referenceIdSet = new HashSet<String>();
473 476
			Set<String> refDetailIdSet = new HashSet<String>();
474
			
477

  
475 478
			while (rs.next()){
476 479
				handleForeignKey(rs, nameIdSet, "nameFk");
477 480
				handleForeignKey(rs, ecoFieldObservationIdSet, "ecoFactFk");
......
481 484
				handleForeignKey(rs, referenceIdSet, "tdRefFk");
482 485
				handleForeignKey(rs, refDetailIdSet, "tdRefDetailFk");
483 486
			}
484
			
487

  
485 488
			//name map
486 489
			nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
487 490
			cdmClass = TaxonNameBase.class;
......
524 527
			idSet = referenceIdSet;
525 528
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
526 529
			result.put(nameSpace, referenceMap);
527
			
530

  
528 531
			//refDetail map
529 532
			nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE;
530 533
			cdmClass = Reference.class;
531 534
			idSet = refDetailIdSet;
532 535
			Map<String, Reference> refDetailMap= (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
533 536
			result.put(nameSpace, refDetailMap);
534
	
535
			
536
			
537

  
538

  
539

  
537 540
		} catch (SQLException e) {
538 541
			throw new RuntimeException(e);
539 542
		}
......
553 556
	/* (non-Javadoc)
554 557
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
555 558
	 */
556
	protected boolean isIgnore(BerlinModelImportState state){
559
	@Override
560
    protected boolean isIgnore(BerlinModelImportState state){
557 561
		return ! state.getConfig().isDoTypes();
558 562
	}
559
	
563

  
560 564
}

Also available in: Unified diff