Revision 8422c0cd
Added by Andreas Müller almost 8 years ago
app-import/src/main/java/eu/etaxonomy/cdm/io/berlinModel/in/BerlinModelFactsImport.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 |
*/ |
... | ... | |
64 | 64 |
private static final Logger logger = Logger.getLogger(BerlinModelFactsImport.class); |
65 | 65 |
|
66 | 66 |
public static final String NAMESPACE = "Fact"; |
67 |
|
|
67 |
|
|
68 | 68 |
public static final String SEQUENCE_PREFIX = "ORDER: "; |
69 |
|
|
69 |
|
|
70 | 70 |
private int modCount = 10000; |
71 | 71 |
private static final String pluralString = "facts"; |
72 | 72 |
private static final String dbTableName = "Fact"; |
73 | 73 |
|
74 | 74 |
//FIXME don't use as class variable |
75 | 75 |
private Map<Integer, Feature> featureMap; |
76 |
|
|
76 |
|
|
77 | 77 |
public BerlinModelFactsImport(){ |
78 | 78 |
super(dbTableName, pluralString); |
79 | 79 |
} |
... | ... | |
87 | 87 |
return featureVocabulary; |
88 | 88 |
} catch (UnknownCdmTypeException e) { |
89 | 89 |
logger.error("Feature vocabulary not available. New vocabulary created"); |
90 |
return TermVocabulary.NewInstance(TermType.Feature, "User Defined Feature Vocabulary", "User Defined Feature Vocabulary", null, null);
|
|
90 |
return TermVocabulary.NewInstance(TermType.Feature, "User Defined Feature Vocabulary", "User Defined Feature Vocabulary", null, null); |
|
91 | 91 |
} |
92 | 92 |
} |
93 |
|
|
93 |
|
|
94 | 94 |
private Map<Integer, Feature> invokeFactCategories(BerlinModelImportState state){ |
95 |
|
|
95 |
|
|
96 | 96 |
Map<Integer, Feature> result = state.getConfig().getFeatureMap(); |
97 | 97 |
Source source = state.getConfig().getSource(); |
98 |
|
|
98 |
|
|
99 | 99 |
try { |
100 | 100 |
//get data from database |
101 |
String strQuery =
|
|
102 |
" SELECT FactCategory.* " +
|
|
101 |
String strQuery = |
|
102 |
" SELECT FactCategory.* " + |
|
103 | 103 |
" FROM FactCategory "+ |
104 | 104 |
" WHERE (1=1)"; |
105 | 105 |
ResultSet rs = source.getResultSet(strQuery) ; |
106 | 106 |
|
107 |
|
|
107 |
|
|
108 | 108 |
TermVocabulary<Feature> featureVocabulary = getFeatureVocabulary(); |
109 | 109 |
int i = 0; |
110 | 110 |
//for each reference |
111 | 111 |
while (rs.next()){ |
112 |
|
|
112 |
|
|
113 | 113 |
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("FactCategories handled: " + (i-1));} |
114 |
|
|
114 |
|
|
115 | 115 |
int factCategoryId = rs.getInt("factCategoryId"); |
116 | 116 |
String factCategory = rs.getString("factCategory"); |
117 |
|
|
117 |
|
|
118 | 118 |
Feature feature; |
119 | 119 |
try { |
120 | 120 |
feature = BerlinModelTransformer.factCategory2Feature(factCategoryId); |
... | ... | |
134 | 134 |
// locExtensionFormName nvarchar(80) Checked |
135 | 135 |
// RankRestrictionFk int Checked |
136 | 136 |
} |
137 |
|
|
137 |
|
|
138 | 138 |
result.put(factCategoryId, feature); |
139 | 139 |
} |
140 | 140 |
return result; |
... | ... | |
155 | 155 |
super.doInvoke(state); |
156 | 156 |
return; |
157 | 157 |
} |
158 |
|
|
158 |
|
|
159 | 159 |
|
160 | 160 |
@Override |
161 | 161 |
protected String getIdQuery(BerlinModelImportState state) { |
... | ... | |
171 | 171 |
|
172 | 172 |
@Override |
173 | 173 |
protected String getRecordQuery(BerlinModelImportConfigurator config) { |
174 |
String strQuery =
|
|
175 |
" SELECT Fact.*, PTaxon.RIdentifier as taxonId, RefDetail.Details " +
|
|
174 |
String strQuery = |
|
175 |
" SELECT Fact.*, PTaxon.RIdentifier as taxonId, RefDetail.Details " + |
|
176 | 176 |
" FROM Fact " + |
177 | 177 |
" INNER JOIN PTaxon ON Fact.PTNameFk = PTaxon.PTNameFk AND Fact.PTRefFk = PTaxon.PTRefFk " + |
178 | 178 |
" LEFT OUTER JOIN RefDetail ON Fact.FactRefDetailFk = RefDetail.RefDetailId AND Fact.FactRefFk = RefDetail.RefFk " + |
179 | 179 |
" WHERE (FactId IN (" + ID_LIST_TOKEN + "))"; |
180 | 180 |
strQuery += getOrderBy(config); |
181 |
|
|
181 |
|
|
182 | 182 |
return strQuery; |
183 | 183 |
} |
184 | 184 |
|
... | ... | |
197 | 197 |
} |
198 | 198 |
return result; |
199 | 199 |
} |
200 |
|
|
200 |
|
|
201 | 201 |
@Override |
202 | 202 |
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) { |
203 | 203 |
boolean success = true ; |
204 | 204 |
BerlinModelImportConfigurator config = state.getConfig(); |
205 | 205 |
Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>(); |
206 |
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
|
|
207 |
Map<String, Reference> refMap = (Map<String, Reference>) partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
|
208 |
|
|
206 |
Map<String, TaxonBase> taxonMap = partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE); |
|
207 |
Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE); |
|
208 |
|
|
209 | 209 |
ResultSet rs = partitioner.getResultSet(); |
210 |
|
|
211 |
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
|
212 |
|
|
210 |
|
|
211 |
Reference sourceRef = state.getTransactionalSourceReference(); |
|
212 |
|
|
213 | 213 |
try{ |
214 | 214 |
int i = 0; |
215 | 215 |
//for each fact |
216 | 216 |
while (rs.next()){ |
217 | 217 |
try{ |
218 | 218 |
if ((i++ % modCount) == 0){ logger.info("Facts handled: " + (i-1));} |
219 |
|
|
219 |
|
|
220 | 220 |
int factId = rs.getInt("factId"); |
221 | 221 |
Integer taxonId = nullSafeInt(rs, "taxonId"); |
222 | 222 |
Integer factRefFkInt = nullSafeInt(rs, "factRefFk"); |
... | ... | |
225 | 225 |
String fact = CdmUtils.Nz(rs.getString("Fact")); |
226 | 226 |
String notes = CdmUtils.Nz(rs.getString("notes")); |
227 | 227 |
Boolean doubtfulFlag = rs.getBoolean("DoubtfulFlag"); |
228 |
|
|
228 |
|
|
229 | 229 |
TaxonBase<?> taxonBase = getTaxon(taxonMap, taxonId, taxonId); |
230 | 230 |
Feature feature = getFeature(featureMap, categoryFkInt) ; |
231 |
|
|
231 |
|
|
232 | 232 |
if (taxonBase == null){ |
233 | 233 |
logger.warn("Taxon for Fact " + factId + " does not exist in store"); |
234 | 234 |
success = false; |
... | ... | |
238 | 238 |
success = false; |
239 | 239 |
continue; |
240 | 240 |
} |
241 |
|
|
241 |
|
|
242 | 242 |
//textData |
243 | 243 |
TextData textData = null; |
244 | 244 |
boolean newTextData = true; |
245 |
|
|
246 |
// For Cichorieae DB: If fact category is 31 (Systematics) and there is already a Systematics TextData
|
|
245 |
|
|
246 |
// For Cichorieae DB: If fact category is 31 (Systematics) and there is already a Systematics TextData |
|
247 | 247 |
// description element append the fact text to the existing TextData |
248 | 248 |
if(categoryFkInt.equals(31)) { |
249 | 249 |
Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements(); |
... | ... | |
264 | 264 |
} |
265 | 265 |
} |
266 | 266 |
} |
267 |
|
|
268 |
if(newTextData == true) {
|
|
269 |
textData = TextData.NewInstance();
|
|
267 |
|
|
268 |
if(newTextData == true) { |
|
269 |
textData = TextData.NewInstance(); |
|
270 | 270 |
} |
271 |
|
|
271 |
|
|
272 | 272 |
//for diptera database |
273 | 273 |
if (categoryFkInt.equals(99) && notes.contains("<OriginalName>")){ |
274 | 274 |
// notes = notes.replaceAll("<OriginalName>", ""); |
... | ... | |
280 | 280 |
//example <a href="http://euromed.luomus.fi/euromed_map.php?taxon=280629&size=medium">distribution</a> |
281 | 281 |
fact = fact.replace("<a href=\"", "").replace("\">distribution</a>", ""); |
282 | 282 |
} |
283 |
|
|
283 |
|
|
284 | 284 |
//TODO textData.putText(fact, bmiConfig.getFactLanguage()); //doesn't work because bmiConfig.getFactLanguage() is not not a persistent Language Object |
285 | 285 |
//throws in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language |
286 | 286 |
if (! taxonDescription.isImageGallery()){ |
287 | 287 |
textData.putText(Language.DEFAULT(), fact); |
288 | 288 |
textData.setFeature(feature); |
289 | 289 |
} |
290 |
|
|
290 |
|
|
291 | 291 |
//reference |
292 |
Reference<?> citation = null;
|
|
292 |
Reference citation = null; |
|
293 | 293 |
String factRefFk = String.valueOf(factRefFkInt); |
294 | 294 |
if (factRefFkInt != null){ |
295 |
citation = refMap.get(factRefFk);
|
|
295 |
citation = refMap.get(factRefFk); |
|
296 | 296 |
} |
297 | 297 |
if (citation == null && (factRefFkInt != null)){ |
298 | 298 |
logger.warn("Citation not found in referenceMap: " + factRefFk); |
... | ... | |
314 | 314 |
Boolean publishFlag = rs.getBoolean(strPublishFlag); |
315 | 315 |
textData.addMarker(Marker.NewInstance(MarkerType.PUBLISH(), publishFlag)); |
316 | 316 |
} |
317 |
|
|
317 |
|
|
318 | 318 |
//Sequence |
319 | 319 |
Integer sequence = rs.getInt("Sequence"); |
320 | 320 |
if (sequence != null && sequence != 999){ |
... | ... | |
325 | 325 |
Annotation annotation = Annotation.NewInstance(strSequence, Language.DEFAULT()); |
326 | 326 |
textData.addAnnotation(annotation); |
327 | 327 |
} |
328 |
|
|
328 |
|
|
329 | 329 |
// if (categoryFkObj == FACT_DESCRIPTION){ |
330 | 330 |
// //; |
331 | 331 |
// }else if (categoryFkObj == FACT_OBSERVATION){ |
... | ... | |
336 | 336 |
// //TODO |
337 | 337 |
// //logger.warn("FactCategory " + categoryFk + " not yet implemented"); |
338 | 338 |
// } |
339 |
|
|
339 |
|
|
340 | 340 |
//notes |
341 | 341 |
doCreatedUpdatedNotes(state, textData, rs); |
342 | 342 |
doId(state, textData, factId, "Fact"); |
343 |
|
|
343 |
|
|
344 | 344 |
//TODO |
345 | 345 |
//Designation References -> unclear how to map to CDM |
346 |
|
|
347 |
|
|
346 |
|
|
347 |
|
|
348 | 348 |
//sequence -> textData is not an identifiable entity therefore extensions are not possible |
349 | 349 |
//fact category better |
350 |
|
|
350 |
|
|
351 | 351 |
taxaToSave.add(taxonBase); |
352 | 352 |
} |
353 | 353 |
} catch (Exception re){ |
... | ... | |
359 | 359 |
} |
360 | 360 |
logger.info("Facts handled: " + (i-1)); |
361 | 361 |
logger.info("Taxa to save: " + taxaToSave.size()); |
362 |
getTaxonService().save(taxaToSave);
|
|
362 |
getTaxonService().save(taxaToSave); |
|
363 | 363 |
}catch(SQLException e){ |
364 | 364 |
throw new RuntimeException(e); |
365 | 365 |
} |
366 | 366 |
return success; |
367 | 367 |
} |
368 | 368 |
|
369 |
private TaxonDescription getMyTaxonDescripion(TaxonBase taxonBase, BerlinModelImportState state, Integer categoryFk, Integer taxonId, int factId, String fact, Reference<?> sourceRef) {
|
|
369 |
private TaxonDescription getMyTaxonDescripion(TaxonBase taxonBase, BerlinModelImportState state, Integer categoryFk, Integer taxonId, int factId, String fact, Reference sourceRef) { |
|
370 | 370 |
Taxon taxon = null; |
371 | 371 |
if ( taxonBase instanceof Taxon ) { |
372 | 372 |
taxon = (Taxon) taxonBase; |
... | ... | |
374 | 374 |
logger.warn("TaxonBase " + (taxonId==null?"(null)":taxonId) + " for Fact " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName()); |
375 | 375 |
return null; |
376 | 376 |
} |
377 |
|
|
377 |
|
|
378 | 378 |
TaxonDescription taxonDescription = null; |
379 | 379 |
Set<TaxonDescription> descriptionSet= taxon.getDescriptions(); |
380 |
|
|
380 |
|
|
381 | 381 |
boolean isImage = false; |
382 | 382 |
Media media = null; |
383 | 383 |
//for diptera images |
... | ... | |
385 | 385 |
isImage = true; |
386 | 386 |
media = Media.NewInstance(); |
387 | 387 |
taxonDescription = makeImage(state, fact, media, descriptionSet, taxon); |
388 |
|
|
389 |
|
|
390 |
|
|
388 |
|
|
389 |
|
|
390 |
|
|
391 | 391 |
if (taxonDescription == null){ |
392 | 392 |
return null; |
393 | 393 |
} |
394 |
|
|
394 |
|
|
395 | 395 |
TextData textData = null; |
396 | 396 |
for (DescriptionElementBase el: taxonDescription.getElements()){ |
397 | 397 |
if (el.isInstanceOf(TextData.class)){ |
... | ... | |
405 | 405 |
textData.addMedia(media); |
406 | 406 |
} |
407 | 407 |
//all others (no image) -> getDescription |
408 |
else{
|
|
408 |
else{ |
|
409 | 409 |
for (TaxonDescription desc: descriptionSet){ |
410 | 410 |
if (! desc.isImageGallery()){ |
411 | 411 |
taxonDescription = desc; |
... | ... | |
427 | 427 |
Class<?> cdmClass; |
428 | 428 |
Set<String> idSet; |
429 | 429 |
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>(); |
430 |
|
|
430 |
|
|
431 | 431 |
try{ |
432 | 432 |
Set<String> taxonIdSet = new HashSet<String>(); |
433 | 433 |
Set<String> referenceIdSet = new HashSet<String>(); |
... | ... | |
439 | 439 |
handleForeignKey(rs, refDetailIdSet, "FactRefDetailFk"); |
440 | 440 |
handleForeignKey(rs, refDetailIdSet, "PTDesignationRefDetailFk"); |
441 | 441 |
} |
442 |
|
|
442 |
|
|
443 | 443 |
//taxon map |
444 | 444 |
nameSpace = BerlinModelTaxonImport.NAMESPACE; |
445 | 445 |
cdmClass = TaxonBase.class; |
... | ... | |
453 | 453 |
idSet = referenceIdSet; |
454 | 454 |
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace); |
455 | 455 |
result.put(nameSpace, referenceMap); |
456 |
|
|
456 |
|
|
457 | 457 |
//refDetail map |
458 | 458 |
nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE; |
459 | 459 |
cdmClass = Reference.class; |
460 | 460 |
idSet = refDetailIdSet; |
461 | 461 |
Map<String, Reference> refDetailMap= (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace); |
462 | 462 |
result.put(nameSpace, refDetailMap); |
463 |
|
|
463 |
|
|
464 | 464 |
} catch (SQLException e) { |
465 | 465 |
throw new RuntimeException(e); |
466 | 466 |
} |
467 | 467 |
return result; |
468 | 468 |
} |
469 |
|
|
470 |
|
|
469 |
|
|
470 |
|
|
471 | 471 |
/** |
472 |
* @param state
|
|
473 |
* @param media
|
|
474 |
* @param media
|
|
475 |
* @param descriptionSet
|
|
476 |
*
|
|
472 |
* @param state |
|
473 |
* @param media |
|
474 |
* @param media |
|
475 |
* @param descriptionSet |
|
476 |
* |
|
477 | 477 |
*/ |
478 | 478 |
private TaxonDescription makeImage(BerlinModelImportState state, String fact, Media media, Set<TaxonDescription> descriptionSet, Taxon taxon) { |
479 | 479 |
TaxonDescription taxonDescription = null; |
480 |
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
|
481 |
Integer size = null;
|
|
480 |
Reference sourceRef = state.getTransactionalSourceReference(); |
|
481 |
Integer size = null; |
|
482 | 482 |
ImageInfo imageInfo = null; |
483 | 483 |
URI uri; |
484 | 484 |
try { |
... | ... | |
498 | 498 |
media.addRepresentation(mediaRepresentation); |
499 | 499 |
ImageFile image = ImageFile.NewInstance(uri, size, imageInfo); |
500 | 500 |
mediaRepresentation.addRepresentationPart(image); |
501 |
|
|
501 |
|
|
502 | 502 |
taxonDescription = taxon.getOrCreateImageGallery(sourceRef == null ? null :sourceRef.getTitleCache()); |
503 |
|
|
503 |
|
|
504 | 504 |
return taxonDescription; |
505 | 505 |
} |
506 | 506 |
|
... | ... | |
510 | 510 |
}else{ |
511 | 511 |
return null; |
512 | 512 |
} |
513 |
|
|
513 |
|
|
514 | 514 |
} |
515 |
|
|
515 |
|
|
516 | 516 |
private Feature getFeature(Map<Integer, Feature> featureMap, Integer categoryFkInt){ |
517 | 517 |
if (categoryFkInt != null){ |
518 |
return featureMap.get(categoryFkInt);
|
|
518 |
return featureMap.get(categoryFkInt); |
|
519 | 519 |
}else{ |
520 | 520 |
return null; |
521 | 521 |
} |
522 |
|
|
522 |
|
|
523 | 523 |
} |
524 |
|
|
524 |
|
|
525 | 525 |
@Override |
526 | 526 |
protected boolean doCheck(BerlinModelImportState state){ |
527 | 527 |
IOValidator<BerlinModelImportState> validator = new BerlinModelFactsImportValidator(); |
528 | 528 |
return validator.validate(state); |
529 | 529 |
} |
530 |
|
|
530 |
|
|
531 | 531 |
@Override |
532 | 532 |
protected boolean isIgnore(BerlinModelImportState state){ |
533 | 533 |
return ! state.getConfig().isDoFacts(); |
Also available in: Unified diff
Remove generics from Reference in cdmlib-app #5830