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.io.IOException;
|
13
|
import java.net.URI;
|
14
|
import java.net.URISyntaxException;
|
15
|
import java.sql.ResultSet;
|
16
|
import java.sql.SQLException;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.HashSet;
|
19
|
import java.util.Map;
|
20
|
import java.util.Set;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import org.apache.commons.lang.StringUtils;
|
24
|
import org.apache.http.HttpException;
|
25
|
import org.apache.log4j.Logger;
|
26
|
import org.springframework.stereotype.Component;
|
27
|
|
28
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
29
|
import eu.etaxonomy.cdm.common.media.ImageInfo;
|
30
|
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
|
31
|
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
|
32
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelFactsImportValidator;
|
33
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
34
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
35
|
import eu.etaxonomy.cdm.io.common.Source;
|
36
|
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
|
37
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.common.Language;
|
40
|
import eu.etaxonomy.cdm.model.common.Marker;
|
41
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
42
|
import eu.etaxonomy.cdm.model.common.TermType;
|
43
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
44
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
45
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
46
|
import eu.etaxonomy.cdm.model.description.Feature;
|
47
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
48
|
import eu.etaxonomy.cdm.model.description.TextData;
|
49
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
50
|
import eu.etaxonomy.cdm.model.media.Media;
|
51
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
52
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
53
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
54
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
55
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
56
|
|
57
|
/**
|
58
|
* @author a.mueller
|
59
|
* @created 20.03.2008
|
60
|
* @version 1.0
|
61
|
*/
|
62
|
@Component
|
63
|
public class BerlinModelFactsImport extends BerlinModelImportBase {
|
64
|
private static final Logger logger = Logger.getLogger(BerlinModelFactsImport.class);
|
65
|
|
66
|
public static final String NAMESPACE = "Fact";
|
67
|
|
68
|
public static final String SEQUENCE_PREFIX = "ORDER: ";
|
69
|
|
70
|
private int modCount = 10000;
|
71
|
private static final String pluralString = "facts";
|
72
|
private static final String dbTableName = "Fact";
|
73
|
|
74
|
//FIXME don't use as class variable
|
75
|
private Map<Integer, Feature> featureMap;
|
76
|
|
77
|
public BerlinModelFactsImport(){
|
78
|
super(dbTableName, pluralString);
|
79
|
}
|
80
|
|
81
|
|
82
|
private TermVocabulary<Feature> getFeatureVocabulary(){
|
83
|
try {
|
84
|
//TODO work around until service method works
|
85
|
TermVocabulary<Feature> featureVocabulary = BerlinModelTransformer.factCategory2Feature(1).getVocabulary();
|
86
|
//TermVocabulary<Feature> vocabulary = getTermService().getVocabulary(vocabularyUuid);
|
87
|
return featureVocabulary;
|
88
|
} catch (UnknownCdmTypeException e) {
|
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);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
private Map<Integer, Feature> invokeFactCategories(BerlinModelImportState state){
|
95
|
|
96
|
Map<Integer, Feature> result = state.getConfig().getFeatureMap();
|
97
|
Source source = state.getConfig().getSource();
|
98
|
|
99
|
try {
|
100
|
//get data from database
|
101
|
String strQuery =
|
102
|
" SELECT FactCategory.* " +
|
103
|
" FROM FactCategory "+
|
104
|
" WHERE (1=1)";
|
105
|
ResultSet rs = source.getResultSet(strQuery) ;
|
106
|
|
107
|
|
108
|
TermVocabulary<Feature> featureVocabulary = getFeatureVocabulary();
|
109
|
int i = 0;
|
110
|
//for each reference
|
111
|
while (rs.next()){
|
112
|
|
113
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("FactCategories handled: " + (i-1));}
|
114
|
|
115
|
int factCategoryId = rs.getInt("factCategoryId");
|
116
|
String factCategory = rs.getString("factCategory");
|
117
|
|
118
|
Feature feature;
|
119
|
try {
|
120
|
feature = BerlinModelTransformer.factCategory2Feature(factCategoryId);
|
121
|
} catch (UnknownCdmTypeException e) {
|
122
|
UUID featureUuid = null;
|
123
|
featureUuid = BerlinModelTransformer.getFeatureUuid(String.valueOf(factCategoryId+"-"+factCategory));
|
124
|
if (featureUuid == null){
|
125
|
logger.warn("New Feature (FactCategoryId: " + factCategoryId + ")");
|
126
|
featureUuid = UUID.randomUUID();
|
127
|
}
|
128
|
feature = getFeature(state, featureUuid, factCategory, factCategory, null, featureVocabulary);
|
129
|
|
130
|
//TODO
|
131
|
// MaxFactNumber int Checked
|
132
|
// ExtensionTableName varchar(100) Checked
|
133
|
// Description nvarchar(1000) Checked
|
134
|
// locExtensionFormName nvarchar(80) Checked
|
135
|
// RankRestrictionFk int Checked
|
136
|
}
|
137
|
|
138
|
result.put(factCategoryId, feature);
|
139
|
}
|
140
|
return result;
|
141
|
} catch (SQLException e) {
|
142
|
logger.error("SQLException:" + e);
|
143
|
return null;
|
144
|
} catch (UndefinedTransformerMethodException e1) {
|
145
|
logger.error("UndefinedTransformerMethodException:" + e1);
|
146
|
e1.printStackTrace();
|
147
|
return null;
|
148
|
}
|
149
|
|
150
|
}
|
151
|
|
152
|
/* (non-Javadoc)
|
153
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#doInvoke(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
154
|
*/
|
155
|
@Override
|
156
|
protected void doInvoke(BerlinModelImportState state) {
|
157
|
featureMap = invokeFactCategories(state);
|
158
|
super.doInvoke(state);
|
159
|
return;
|
160
|
}
|
161
|
|
162
|
|
163
|
|
164
|
/* (non-Javadoc)
|
165
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
166
|
*/
|
167
|
@Override
|
168
|
protected String getIdQuery(BerlinModelImportState state) {
|
169
|
String result = super.getIdQuery(state);
|
170
|
if (StringUtils.isNotBlank(state.getConfig().getFactFilter())){
|
171
|
result += " WHERE " + state.getConfig().getFactFilter();
|
172
|
}else{
|
173
|
result = super.getIdQuery(state);
|
174
|
}
|
175
|
result += getOrderBy(state.getConfig());
|
176
|
return result;
|
177
|
}
|
178
|
|
179
|
|
180
|
|
181
|
/* (non-Javadoc)
|
182
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
183
|
*/
|
184
|
@Override
|
185
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
186
|
String strQuery =
|
187
|
" SELECT Fact.*, PTaxon.RIdentifier as taxonId, RefDetail.Details " +
|
188
|
" FROM Fact " +
|
189
|
" INNER JOIN PTaxon ON Fact.PTNameFk = PTaxon.PTNameFk AND Fact.PTRefFk = PTaxon.PTRefFk " +
|
190
|
" LEFT OUTER JOIN RefDetail ON Fact.FactRefDetailFk = RefDetail.RefDetailId AND Fact.FactRefFk = RefDetail.RefFk " +
|
191
|
" WHERE (FactId IN (" + ID_LIST_TOKEN + "))";
|
192
|
strQuery += getOrderBy(config);
|
193
|
|
194
|
return strQuery;
|
195
|
}
|
196
|
|
197
|
|
198
|
private String getOrderBy(BerlinModelImportConfigurator config) {
|
199
|
String result;
|
200
|
try{
|
201
|
if (config.getSource().checkColumnExists("Fact", "Sequence")){
|
202
|
result = " ORDER By Fact.Sequence, Fact.FactId";
|
203
|
}else{
|
204
|
result = " ORDER By Fact.FactId";
|
205
|
}
|
206
|
} catch (DatabaseTypeNotSupportedException e) {
|
207
|
logger.info("checkColumnExists not supported");
|
208
|
result = " ORDER By Fact.FactId";
|
209
|
}
|
210
|
return result;
|
211
|
}
|
212
|
|
213
|
|
214
|
/* (non-Javadoc)
|
215
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
216
|
*/
|
217
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
218
|
boolean success = true ;
|
219
|
BerlinModelImportConfigurator config = state.getConfig();
|
220
|
Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
|
221
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
|
222
|
Map<String, Reference> refMap = (Map<String, Reference>) partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
223
|
|
224
|
ResultSet rs = partitioner.getResultSet();
|
225
|
|
226
|
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
227
|
|
228
|
try{
|
229
|
int i = 0;
|
230
|
//for each fact
|
231
|
while (rs.next()){
|
232
|
try{
|
233
|
if ((i++ % modCount) == 0){ logger.info("Facts handled: " + (i-1));}
|
234
|
|
235
|
int factId = rs.getInt("factId");
|
236
|
Object taxonIdObj = rs.getObject("taxonId");
|
237
|
long taxonId = rs.getLong("taxonId");
|
238
|
Object factRefFkObj = rs.getObject("factRefFk");
|
239
|
Object categoryFkObj = rs.getObject("factCategoryFk");
|
240
|
Integer categoryFk = rs.getInt("factCategoryFk");
|
241
|
String details = rs.getString("Details");
|
242
|
String fact = CdmUtils.Nz(rs.getString("Fact"));
|
243
|
String notes = CdmUtils.Nz(rs.getString("notes"));
|
244
|
Boolean doubtfulFlag = rs.getBoolean("DoubtfulFlag");
|
245
|
|
246
|
TaxonBase<?> taxonBase = getTaxon(taxonMap, taxonIdObj, taxonId);
|
247
|
Feature feature = getFeature(featureMap, categoryFkObj, categoryFk) ;
|
248
|
|
249
|
if (taxonBase == null){
|
250
|
logger.warn("Taxon for Fact " + factId + " does not exist in store");
|
251
|
success = false;
|
252
|
}else{
|
253
|
TaxonDescription taxonDescription;
|
254
|
if ( (taxonDescription = getMyTaxonDescripion(taxonBase, state, categoryFk, taxonIdObj, taxonId, factId, fact, sourceRef)) == null){
|
255
|
success = false;
|
256
|
continue;
|
257
|
}
|
258
|
|
259
|
//textData
|
260
|
TextData textData = null;
|
261
|
boolean newTextData = true;
|
262
|
|
263
|
// For Cichorieae DB: If fact category is 31 (Systematics) and there is already a Systematics TextData
|
264
|
// description element append the fact text to the existing TextData
|
265
|
if(categoryFk == 31) {
|
266
|
Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
|
267
|
for (DescriptionElementBase descriptionElement : descriptionElements) {
|
268
|
String featureString = descriptionElement.getFeature().getRepresentation(Language.DEFAULT()).getLabel();
|
269
|
if (descriptionElement instanceof TextData && featureString.equals("Systematics")) { // TODO: test
|
270
|
textData = (TextData)descriptionElement;
|
271
|
String factTextStr = textData.getText(Language.DEFAULT());
|
272
|
// FIXME: Removing newlines doesn't work
|
273
|
if (factTextStr.contains("\\r\\n")) {
|
274
|
factTextStr = factTextStr.replaceAll("\\r\\n","");
|
275
|
}
|
276
|
StringBuilder factText = new StringBuilder(factTextStr);
|
277
|
factText.append(fact);
|
278
|
fact = factText.toString();
|
279
|
newTextData = false;
|
280
|
break;
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
|
285
|
if(newTextData == true) {
|
286
|
textData = TextData.NewInstance();
|
287
|
}
|
288
|
|
289
|
//for diptera database
|
290
|
if (categoryFk == 99 && notes.contains("<OriginalName>")){
|
291
|
// notes = notes.replaceAll("<OriginalName>", "");
|
292
|
// notes = notes.replaceAll("</OriginalName>", "");
|
293
|
fact = notes + ": " + fact ;
|
294
|
}
|
295
|
//for E+M maps
|
296
|
if (categoryFk == 14 && state.getConfig().isRemoveHttpMapsAnchor() && fact.contains("<a href")){
|
297
|
//example <a href="http://euromed.luomus.fi/euromed_map.php?taxon=280629&size=medium">distribution</a>
|
298
|
fact = fact.replace("<a href=\"", "").replace("\">distribution</a>", "");
|
299
|
}
|
300
|
|
301
|
//TODO textData.putText(fact, bmiConfig.getFactLanguage()); //doesn't work because bmiConfig.getFactLanguage() is not not a persistent Language Object
|
302
|
//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
|
303
|
if (! taxonDescription.isImageGallery()){
|
304
|
textData.putText(Language.DEFAULT(), fact);
|
305
|
textData.setFeature(feature);
|
306
|
}
|
307
|
|
308
|
//reference
|
309
|
Reference<?> citation = null;
|
310
|
String factRefFk = String.valueOf(factRefFkObj);
|
311
|
if (factRefFkObj != null){
|
312
|
citation = refMap.get(factRefFk);
|
313
|
}
|
314
|
if (citation == null && (factRefFkObj != null)){
|
315
|
logger.warn("Citation not found in referenceMap: " + factRefFk);
|
316
|
success = false;
|
317
|
}
|
318
|
if (citation != null || StringUtils.isNotBlank(details)){
|
319
|
DescriptionElementSource originalSource = DescriptionElementSource.NewPrimarySourceInstance(citation, details);
|
320
|
textData.addSource(originalSource);
|
321
|
}
|
322
|
taxonDescription.addElement(textData);
|
323
|
//doubtfulFlag
|
324
|
if (doubtfulFlag){
|
325
|
textData.addMarker(Marker.NewInstance(MarkerType.IS_DOUBTFUL(), true));
|
326
|
}
|
327
|
//publisheFlag
|
328
|
String strPublishFlag = "publishFlag";
|
329
|
boolean publishFlagExists = state.getConfig().getSource().checkColumnExists(dbTableName, strPublishFlag);
|
330
|
if (publishFlagExists){
|
331
|
Boolean publishFlag = rs.getBoolean(strPublishFlag);
|
332
|
textData.addMarker(Marker.NewInstance(MarkerType.PUBLISH(), publishFlag));
|
333
|
}
|
334
|
|
335
|
//Sequence
|
336
|
Integer sequence = rs.getInt("Sequence");
|
337
|
if (sequence != null && sequence != 999){
|
338
|
String strSequence = String.valueOf(sequence);
|
339
|
strSequence = SEQUENCE_PREFIX + strSequence;
|
340
|
//TODO make it an Extension when possible
|
341
|
//Extension datesExtension = Extension.NewInstance(textData, strSequence, ExtensionType.ORDER());
|
342
|
Annotation annotation = Annotation.NewInstance(strSequence, Language.DEFAULT());
|
343
|
textData.addAnnotation(annotation);
|
344
|
}
|
345
|
|
346
|
// if (categoryFkObj == FACT_DESCRIPTION){
|
347
|
// //;
|
348
|
// }else if (categoryFkObj == FACT_OBSERVATION){
|
349
|
// //;
|
350
|
// }else if (categoryFkObj == FACT_DISTRIBUTION_EM){
|
351
|
// //
|
352
|
// }else {
|
353
|
// //TODO
|
354
|
// //logger.warn("FactCategory " + categoryFk + " not yet implemented");
|
355
|
// }
|
356
|
|
357
|
//notes
|
358
|
doCreatedUpdatedNotes(state, textData, rs);
|
359
|
|
360
|
//TODO
|
361
|
//Designation References -> unclear how to map to CDM
|
362
|
//factId -> OriginalSource for descriptionElements not yet implemented
|
363
|
|
364
|
//sequence -> textData is not an identifiable entity therefore extensions are not possible
|
365
|
//fact category better
|
366
|
|
367
|
taxaToSave.add(taxonBase);
|
368
|
}
|
369
|
} catch (Exception re){
|
370
|
logger.error("An exception occurred during the facts import");
|
371
|
re.printStackTrace();
|
372
|
success = false;
|
373
|
}
|
374
|
//put
|
375
|
}
|
376
|
logger.info("Facts handled: " + (i-1));
|
377
|
logger.info("Taxa to save: " + taxaToSave.size());
|
378
|
getTaxonService().save(taxaToSave);
|
379
|
}catch(SQLException e){
|
380
|
throw new RuntimeException(e);
|
381
|
}
|
382
|
return success;
|
383
|
}
|
384
|
|
385
|
private TaxonDescription getMyTaxonDescripion(TaxonBase taxonBase, BerlinModelImportState state, Integer categoryFk, Object taxonIdObj, long taxonId, int factId, String fact, Reference<?> sourceRef) {
|
386
|
Taxon taxon = null;
|
387
|
if ( taxonBase instanceof Taxon ) {
|
388
|
taxon = (Taxon) taxonBase;
|
389
|
}else{
|
390
|
logger.warn("TaxonBase " + (taxonIdObj==null?"(null)":taxonIdObj) + " for Fact " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
|
391
|
return null;
|
392
|
}
|
393
|
|
394
|
TaxonDescription taxonDescription = null;
|
395
|
Set<TaxonDescription> descriptionSet= taxon.getDescriptions();
|
396
|
|
397
|
boolean isImage = false;
|
398
|
Media media = null;
|
399
|
//for diptera images
|
400
|
if (categoryFk == 51){ //TODO check also FactCategory string
|
401
|
isImage = true;
|
402
|
media = Media.NewInstance();
|
403
|
taxonDescription = makeImage(state, fact, media, descriptionSet, taxon);
|
404
|
|
405
|
|
406
|
|
407
|
if (taxonDescription == null){
|
408
|
return null;
|
409
|
}
|
410
|
|
411
|
TextData textData = null;
|
412
|
for (DescriptionElementBase el: taxonDescription.getElements()){
|
413
|
if (el.isInstanceOf(TextData.class)){
|
414
|
textData = CdmBase.deproxy(el, TextData.class);
|
415
|
}
|
416
|
}
|
417
|
if (textData == null){
|
418
|
textData = TextData.NewInstance(Feature.IMAGE());
|
419
|
taxonDescription.addElement(textData);
|
420
|
}
|
421
|
textData.addMedia(media);
|
422
|
}
|
423
|
//all others (no image) -> getDescription
|
424
|
else{
|
425
|
for (TaxonDescription desc: descriptionSet){
|
426
|
if (! desc.isImageGallery()){
|
427
|
taxonDescription = desc;
|
428
|
}
|
429
|
}
|
430
|
if (taxonDescription == null){
|
431
|
taxonDescription = TaxonDescription.NewInstance();
|
432
|
taxonDescription.setTitleCache(sourceRef == null ? null : sourceRef.getTitleCache(), true);
|
433
|
taxon.addDescription(taxonDescription);
|
434
|
}
|
435
|
}
|
436
|
return taxonDescription;
|
437
|
}
|
438
|
|
439
|
|
440
|
/* (non-Javadoc)
|
441
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
442
|
*/
|
443
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
444
|
String nameSpace;
|
445
|
Class cdmClass;
|
446
|
Set<String> idSet;
|
447
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
448
|
|
449
|
try{
|
450
|
Set<String> taxonIdSet = new HashSet<String>();
|
451
|
Set<String> referenceIdSet = new HashSet<String>();
|
452
|
Set<String> refDetailIdSet = new HashSet<String>();
|
453
|
while (rs.next()){
|
454
|
handleForeignKey(rs, taxonIdSet, "taxonId");
|
455
|
handleForeignKey(rs, referenceIdSet, "FactRefFk");
|
456
|
handleForeignKey(rs, referenceIdSet, "PTDesignationRefFk");
|
457
|
handleForeignKey(rs, refDetailIdSet, "FactRefDetailFk");
|
458
|
handleForeignKey(rs, refDetailIdSet, "PTDesignationRefDetailFk");
|
459
|
}
|
460
|
|
461
|
//taxon map
|
462
|
nameSpace = BerlinModelTaxonImport.NAMESPACE;
|
463
|
cdmClass = TaxonBase.class;
|
464
|
idSet = taxonIdSet;
|
465
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
466
|
result.put(nameSpace, taxonMap);
|
467
|
|
468
|
//reference map
|
469
|
nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
|
470
|
cdmClass = Reference.class;
|
471
|
idSet = referenceIdSet;
|
472
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
473
|
result.put(nameSpace, referenceMap);
|
474
|
|
475
|
//refDetail map
|
476
|
nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE;
|
477
|
cdmClass = Reference.class;
|
478
|
idSet = refDetailIdSet;
|
479
|
Map<String, Reference> refDetailMap= (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
480
|
result.put(nameSpace, refDetailMap);
|
481
|
|
482
|
} catch (SQLException e) {
|
483
|
throw new RuntimeException(e);
|
484
|
}
|
485
|
return result;
|
486
|
}
|
487
|
|
488
|
|
489
|
/**
|
490
|
* @param state
|
491
|
* @param media
|
492
|
* @param media
|
493
|
* @param descriptionSet
|
494
|
*
|
495
|
*/
|
496
|
private TaxonDescription makeImage(BerlinModelImportState state, String fact, Media media, Set<TaxonDescription> descriptionSet, Taxon taxon) {
|
497
|
TaxonDescription taxonDescription = null;
|
498
|
Reference sourceRef = state.getTransactionalSourceReference();
|
499
|
Integer size = null;
|
500
|
ImageInfo imageInfo = null;
|
501
|
URI uri;
|
502
|
try {
|
503
|
uri = new URI(fact.trim());
|
504
|
} catch (URISyntaxException e) {
|
505
|
logger.warn("URISyntaxException. Image could not be imported: " + fact);
|
506
|
return null;
|
507
|
}
|
508
|
try {
|
509
|
imageInfo = ImageInfo.NewInstance(uri, 0);
|
510
|
} catch (IOException e) {
|
511
|
logger.error("IOError reading image metadata." , e);
|
512
|
} catch (HttpException e) {
|
513
|
logger.error("HttpException reading image metadata." , e);
|
514
|
}
|
515
|
MediaRepresentation mediaRepresentation = MediaRepresentation.NewInstance(imageInfo.getMimeType(), null);
|
516
|
media.addRepresentation(mediaRepresentation);
|
517
|
ImageFile image = ImageFile.NewInstance(uri, size, imageInfo);
|
518
|
mediaRepresentation.addRepresentationPart(image);
|
519
|
|
520
|
taxonDescription = taxon.getOrCreateImageGallery(sourceRef == null ? null :sourceRef.getTitleCache());
|
521
|
|
522
|
return taxonDescription;
|
523
|
}
|
524
|
|
525
|
private TaxonBase getTaxon(Map<String, TaxonBase> taxonMap, Object taxonIdObj, Long taxonId){
|
526
|
if (taxonIdObj != null){
|
527
|
return taxonMap.get(String.valueOf(taxonId));
|
528
|
}else{
|
529
|
return null;
|
530
|
}
|
531
|
|
532
|
}
|
533
|
|
534
|
private Feature getFeature(Map<Integer, Feature> featureMap, Object categoryFkObj, Integer categoryFk){
|
535
|
if (categoryFkObj != null){
|
536
|
return featureMap.get(categoryFk);
|
537
|
}else{
|
538
|
return null;
|
539
|
}
|
540
|
|
541
|
}
|
542
|
|
543
|
|
544
|
/* (non-Javadoc)
|
545
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
546
|
*/
|
547
|
@Override
|
548
|
protected boolean doCheck(BerlinModelImportState state){
|
549
|
IOValidator<BerlinModelImportState> validator = new BerlinModelFactsImportValidator();
|
550
|
return validator.validate(state);
|
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().isDoFacts();
|
558
|
}
|
559
|
|
560
|
|
561
|
|
562
|
}
|