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 static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.NAME_FACT_ALSO_PUBLISHED_IN;
|
13
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.NAME_FACT_PROTOLOGUE;
|
14
|
|
15
|
import java.io.File;
|
16
|
import java.io.IOException;
|
17
|
import java.net.MalformedURLException;
|
18
|
import java.net.URL;
|
19
|
import java.sql.ResultSet;
|
20
|
import java.sql.SQLException;
|
21
|
import java.util.HashMap;
|
22
|
import java.util.HashSet;
|
23
|
import java.util.Map;
|
24
|
import java.util.Set;
|
25
|
|
26
|
import org.apache.http.HttpException;
|
27
|
import org.apache.log4j.Logger;
|
28
|
import org.springframework.stereotype.Component;
|
29
|
|
30
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
31
|
import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData;
|
32
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelNameFactsImportValidator;
|
33
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
34
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
35
|
import eu.etaxonomy.cdm.model.agent.Person;
|
36
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
37
|
import eu.etaxonomy.cdm.model.common.Language;
|
38
|
import eu.etaxonomy.cdm.model.description.Feature;
|
39
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
40
|
import eu.etaxonomy.cdm.model.description.TextData;
|
41
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
42
|
import eu.etaxonomy.cdm.model.media.Media;
|
43
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
44
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
45
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
46
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
47
|
|
48
|
|
49
|
/**
|
50
|
* @author a.mueller
|
51
|
* @created 20.03.2008
|
52
|
* @version 1.0
|
53
|
*/
|
54
|
@Component
|
55
|
public class BerlinModelNameFactsImport extends BerlinModelImportBase {
|
56
|
private static final Logger logger = Logger.getLogger(BerlinModelNameFactsImport.class);
|
57
|
|
58
|
public static final String NAMESPACE = "NameFact";
|
59
|
|
60
|
/**
|
61
|
* write info message after modCount iterations
|
62
|
*/
|
63
|
private int modCount = 50;
|
64
|
private static final String pluralString = "name facts";
|
65
|
private static final String dbTableName = "NameFact";
|
66
|
|
67
|
|
68
|
public BerlinModelNameFactsImport(){
|
69
|
super();
|
70
|
}
|
71
|
|
72
|
/* (non-Javadoc)
|
73
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
74
|
*/
|
75
|
@Override
|
76
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
77
|
String strQuery =
|
78
|
" SELECT NameFact.*, Name.NameID as nameId, NameFactCategory.NameFactCategory " +
|
79
|
" FROM NameFact INNER JOIN " +
|
80
|
" Name ON NameFact.PTNameFk = Name.NameId INNER JOIN "+
|
81
|
" NameFactCategory ON NameFactCategory.NameFactCategoryID = NameFact.NameFactCategoryFK " +
|
82
|
" WHERE (NameFactId IN ("+ ID_LIST_TOKEN+") )";
|
83
|
return strQuery;
|
84
|
}
|
85
|
|
86
|
/* (non-Javadoc)
|
87
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
88
|
*/
|
89
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
90
|
boolean success = true ;
|
91
|
BerlinModelImportConfigurator config = state.getConfig();
|
92
|
Set<TaxonNameBase> nameToSave = new HashSet<TaxonNameBase>();
|
93
|
Map<String, TaxonNameBase> nameMap = (Map<String, TaxonNameBase>) partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
|
94
|
Map<String, ReferenceBase> biblioRefMap = partitioner.getObjectMap(BerlinModelReferenceImport.BIBLIO_REFERENCE_NAMESPACE);
|
95
|
Map<String, ReferenceBase> nomRefMap = partitioner.getObjectMap(BerlinModelReferenceImport.NOM_REFERENCE_NAMESPACE);
|
96
|
|
97
|
ResultSet rs = partitioner.getResultSet();
|
98
|
|
99
|
ReferenceBase<?> sourceRef = state.getConfig().getSourceReference();
|
100
|
try {
|
101
|
int i = 0;
|
102
|
//for each reference
|
103
|
while (rs.next() && (config.getMaximumNumberOfNameFacts() == 0 || i < config.getMaximumNumberOfNameFacts())){
|
104
|
|
105
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("NameFacts handled: " + (i-1));}
|
106
|
|
107
|
int nameFactId = rs.getInt("nameFactId");
|
108
|
int nameId = rs.getInt("nameId");
|
109
|
Object nameFactRefFkObj = rs.getObject("nameFactRefFk");
|
110
|
String nameFactRefDetail = rs.getString("nameFactRefDetail");
|
111
|
|
112
|
String category = CdmUtils.Nz(rs.getString("NameFactCategory"));
|
113
|
String nameFact = CdmUtils.Nz(rs.getString("nameFact"));
|
114
|
|
115
|
TaxonNameBase taxonNameBase = nameMap.get(String.valueOf(nameId));
|
116
|
String nameFactRefFk = String.valueOf(nameFactRefFkObj);
|
117
|
ReferenceBase citation = getReferenceOnlyFromMaps(biblioRefMap,
|
118
|
nomRefMap, nameFactRefFk);
|
119
|
|
120
|
if (taxonNameBase != null){
|
121
|
//PROTOLOGUE
|
122
|
if (category.equalsIgnoreCase(NAME_FACT_PROTOLOGUE)){
|
123
|
//ReferenceBase ref = (ReferenceBase)taxonNameBase.getNomenclaturalReference();
|
124
|
//ref = Book.NewInstance();
|
125
|
try{
|
126
|
Media media = getMedia(nameFact, config.getMediaUrl(), config.getMediaPath());
|
127
|
if (media.getRepresentations().size() > 0){
|
128
|
TaxonNameDescription description = TaxonNameDescription.NewInstance();
|
129
|
TextData protolog = TextData.NewInstance(Feature.PROTOLOGUE());
|
130
|
protolog.addMedia(media);
|
131
|
protolog.addSource(String.valueOf(nameFactId), NAMESPACE, null, null, null, null);
|
132
|
description.addElement(protolog);
|
133
|
taxonNameBase.addDescription(description);
|
134
|
if (citation != null){
|
135
|
description.addDescriptionSource(citation);
|
136
|
protolog.addSource(null, null, citation, nameFactRefDetail, null, null);
|
137
|
}
|
138
|
}//end NAME_FACT_PROTOLOGUE
|
139
|
}catch(NullPointerException e){
|
140
|
logger.warn("MediaUrl and/or MediaPath not set. Could not get protologue.");
|
141
|
success = false;
|
142
|
}
|
143
|
}else if (category.equalsIgnoreCase(NAME_FACT_ALSO_PUBLISHED_IN)){
|
144
|
if (! nameFact.equals("")){
|
145
|
TaxonNameDescription description = TaxonNameDescription.NewInstance();
|
146
|
TextData additionalPublication = TextData.NewInstance(Feature.ADDITIONAL_PUBLICATION());
|
147
|
//TODO language
|
148
|
Language language = Language.DEFAULT();
|
149
|
additionalPublication.putText(nameFact, language);
|
150
|
additionalPublication.addSource(String.valueOf(nameFactId), NAMESPACE, citation,
|
151
|
nameFactRefDetail, null, null);
|
152
|
description.addElement(additionalPublication);
|
153
|
taxonNameBase.addDescription(description);
|
154
|
}
|
155
|
}else {
|
156
|
//TODO
|
157
|
logger.warn("NameFactCategory '" + category + "' not yet implemented");
|
158
|
success = false;
|
159
|
}
|
160
|
|
161
|
//TODO
|
162
|
// DoubtfulFlag bit Checked
|
163
|
// PublishFlag bit Checked
|
164
|
// Created_When datetime Checked
|
165
|
// Updated_When datetime Checked
|
166
|
// Created_Who nvarchar(255) Checked
|
167
|
// Updated_Who nvarchar(255) Checked
|
168
|
// Notes nvarchar(1000) Checked
|
169
|
|
170
|
nameToSave.add(taxonNameBase);
|
171
|
}else{
|
172
|
//TODO
|
173
|
logger.warn("TaxonName for NameFact " + nameFactId + " does not exist in store");
|
174
|
success = false;
|
175
|
}
|
176
|
//put
|
177
|
}
|
178
|
if (config.getMaximumNumberOfNameFacts() != 0 && i >= config.getMaximumNumberOfNameFacts() - 1){
|
179
|
logger.warn("ONLY " + config.getMaximumNumberOfNameFacts() + " NAMEFACTS imported !!!" )
|
180
|
;};
|
181
|
logger.info("Names to save: " + nameToSave.size());
|
182
|
getNameService().save(nameToSave);
|
183
|
return success;
|
184
|
} catch (SQLException e) {
|
185
|
logger.error("SQLException:" + e);
|
186
|
return false;
|
187
|
}
|
188
|
|
189
|
}
|
190
|
|
191
|
/* (non-Javadoc)
|
192
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
193
|
*/
|
194
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
195
|
String nameSpace;
|
196
|
Class cdmClass;
|
197
|
Set<String> idSet;
|
198
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
199
|
|
200
|
try{
|
201
|
Set<String> nameIdSet = new HashSet<String>();
|
202
|
Set<String> referenceIdSet = new HashSet<String>();
|
203
|
while (rs.next()){
|
204
|
handleForeignKey(rs, nameIdSet, "PTnameFk");
|
205
|
handleForeignKey(rs, referenceIdSet, "nameFactRefFk");
|
206
|
}
|
207
|
|
208
|
//name map
|
209
|
nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
|
210
|
cdmClass = TaxonNameBase.class;
|
211
|
idSet = nameIdSet;
|
212
|
Map<String, Person> objectMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
213
|
result.put(nameSpace, objectMap);
|
214
|
|
215
|
//nom reference map
|
216
|
nameSpace = BerlinModelReferenceImport.NOM_REFERENCE_NAMESPACE;
|
217
|
cdmClass = ReferenceBase.class;
|
218
|
idSet = referenceIdSet;
|
219
|
Map<String, ReferenceBase> nomReferenceMap = (Map<String, ReferenceBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
220
|
result.put(nameSpace, nomReferenceMap);
|
221
|
|
222
|
//biblio reference map
|
223
|
nameSpace = BerlinModelReferenceImport.BIBLIO_REFERENCE_NAMESPACE;
|
224
|
cdmClass = ReferenceBase.class;
|
225
|
idSet = referenceIdSet;
|
226
|
Map<String, ReferenceBase> biblioReferenceMap = (Map<String, ReferenceBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
227
|
result.put(nameSpace, biblioReferenceMap);
|
228
|
|
229
|
|
230
|
} catch (SQLException e) {
|
231
|
throw new RuntimeException(e);
|
232
|
}
|
233
|
return result;
|
234
|
}
|
235
|
|
236
|
|
237
|
//FIXME gibt es da keine allgemeine Methode in common?
|
238
|
public Media getMedia(String nameFact, URL mediaUrl, File mediaPath){
|
239
|
if (mediaUrl == null){
|
240
|
logger.warn("Media Url should not be null");
|
241
|
return null;
|
242
|
}
|
243
|
String mimeTypeTif = "image/tiff";
|
244
|
String mimeTypeJpg = "image/jpeg";
|
245
|
String mimeTypePng = "image/png";
|
246
|
String mimeTypePdf = "application/pdf";
|
247
|
String suffixTif = "tif";
|
248
|
String suffixJpg = "jpg";
|
249
|
String suffixPng = "png";
|
250
|
String suffixPdf = "pdf";
|
251
|
|
252
|
String sep = File.separator;
|
253
|
Integer size = null;
|
254
|
|
255
|
logger.debug("Getting media for NameFact: " + nameFact);
|
256
|
|
257
|
Media media = Media.NewInstance();
|
258
|
|
259
|
String mediaUrlString = mediaUrl.toString();
|
260
|
|
261
|
//tiff
|
262
|
String urlStringTif = mediaUrlString + "tif/" + nameFact + "." + suffixTif;
|
263
|
File file = new File(mediaPath, "tif" + sep + nameFact + "." + suffixTif);
|
264
|
MediaRepresentation representationTif = MediaRepresentation.NewInstance(mimeTypeTif, suffixTif);
|
265
|
if (file.exists()){
|
266
|
representationTif.addRepresentationPart(makeImage(urlStringTif, size, file));
|
267
|
}
|
268
|
if(representationTif.getParts().size() > 0){
|
269
|
media.addRepresentation(representationTif);
|
270
|
}
|
271
|
// end tif
|
272
|
// jpg
|
273
|
boolean fileExists = true;
|
274
|
int jpgCount = 0;
|
275
|
MediaRepresentation representationJpg = MediaRepresentation.NewInstance(mimeTypeJpg, suffixJpg);
|
276
|
while(fileExists){
|
277
|
String urlStringJpeg = mediaUrlString + "cmd_jpg/" + nameFact + "_page_000" + jpgCount + "." + suffixJpg;
|
278
|
file = new File(mediaPath, "cmd_jpg" + sep + nameFact + "_page_000" + jpgCount + "." + suffixJpg);
|
279
|
jpgCount++;
|
280
|
if (file.exists()){
|
281
|
representationJpg.addRepresentationPart(makeImage(urlStringJpeg, size, file));
|
282
|
}else{
|
283
|
fileExists = false;
|
284
|
}
|
285
|
}
|
286
|
if(representationJpg.getParts().size() > 0){
|
287
|
media.addRepresentation(representationJpg);
|
288
|
}
|
289
|
// end jpg
|
290
|
//png
|
291
|
String urlStringPng = mediaUrlString + "png/" + nameFact + "." + suffixPng;
|
292
|
file = new File(mediaPath, "png" + sep + nameFact + "." + suffixPng);
|
293
|
MediaRepresentation representationPng = MediaRepresentation.NewInstance(mimeTypePng, suffixPng);
|
294
|
if (file.exists()){
|
295
|
representationPng.addRepresentationPart(makeImage(urlStringPng, size, file));
|
296
|
}else{
|
297
|
fileExists = true;
|
298
|
int pngCount = 0;
|
299
|
while (fileExists){
|
300
|
pngCount++;
|
301
|
urlStringPng = mediaUrlString + "png/" + nameFact + "00" + pngCount + "." + suffixPng;
|
302
|
file = new File(mediaPath, "png" + sep + nameFact + "00" + pngCount + "." + suffixPng);
|
303
|
|
304
|
if (file.exists()){
|
305
|
representationPng.addRepresentationPart(makeImage(urlStringPng, size, file));
|
306
|
}else{
|
307
|
fileExists = false;
|
308
|
}
|
309
|
}
|
310
|
}
|
311
|
if(representationPng.getParts().size() > 0){
|
312
|
media.addRepresentation(representationPng);
|
313
|
}
|
314
|
//end png
|
315
|
//pdf
|
316
|
String urlStringPdf = mediaUrlString + "pdf/" + nameFact + "." + suffixPdf;
|
317
|
file = new File(mediaPath, "pdf" + sep + nameFact + "." + suffixPdf);
|
318
|
MediaRepresentation representationPdf = MediaRepresentation.NewInstance(mimeTypePdf, suffixPdf);
|
319
|
if (file.exists()){
|
320
|
representationPdf.addRepresentationPart(MediaRepresentationPart.NewInstance(urlStringPdf, size));
|
321
|
}else{
|
322
|
fileExists = true;
|
323
|
int pdfCount = 0;
|
324
|
while (fileExists){
|
325
|
pdfCount++;
|
326
|
urlStringPdf = mediaUrlString + "pdf/" + nameFact + "00" + pdfCount + "." + suffixPdf;
|
327
|
file = new File(mediaPath, "pdf/" + sep + nameFact + "00" + pdfCount + "." + suffixPdf);
|
328
|
|
329
|
if (file.exists()){
|
330
|
representationPdf.addRepresentationPart(MediaRepresentationPart.NewInstance(urlStringPdf, size));
|
331
|
}else{
|
332
|
fileExists = false;
|
333
|
}
|
334
|
}
|
335
|
}
|
336
|
if(representationPdf.getParts().size() > 0){
|
337
|
media.addRepresentation(representationPdf);
|
338
|
}
|
339
|
//end pdf
|
340
|
|
341
|
if(logger.isDebugEnabled()){
|
342
|
for (MediaRepresentation rep : media.getRepresentations()){
|
343
|
for (MediaRepresentationPart part : rep.getParts()){
|
344
|
logger.debug("in representation: " + part.getUri());
|
345
|
}
|
346
|
}
|
347
|
}
|
348
|
|
349
|
return media;
|
350
|
}
|
351
|
|
352
|
|
353
|
private ImageFile makeImage(String imageUri, Integer size, File file){
|
354
|
ImageMetaData imageMetaData = ImageMetaData.newInstance();
|
355
|
try {
|
356
|
imageMetaData.readMetaData(file.toURI(), 0);
|
357
|
} catch (IOException e) {
|
358
|
logger.error("IOError reading image metadata." , e);
|
359
|
} catch (HttpException e) {
|
360
|
logger.error("HttpException reading image metadata." , e);
|
361
|
}
|
362
|
ImageFile image = ImageFile.NewInstance(imageUri, size, imageMetaData);
|
363
|
return image;
|
364
|
}
|
365
|
|
366
|
|
367
|
/* (non-Javadoc)
|
368
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
369
|
*/
|
370
|
@Override
|
371
|
protected boolean doCheck(BerlinModelImportState state){
|
372
|
IOValidator<BerlinModelImportState> validator = new BerlinModelNameFactsImportValidator();
|
373
|
return validator.validate(state);
|
374
|
}
|
375
|
|
376
|
|
377
|
/* (non-Javadoc)
|
378
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getTableName()
|
379
|
*/
|
380
|
@Override
|
381
|
protected String getTableName() {
|
382
|
return dbTableName;
|
383
|
}
|
384
|
|
385
|
/* (non-Javadoc)
|
386
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getPluralString()
|
387
|
*/
|
388
|
@Override
|
389
|
public String getPluralString() {
|
390
|
return pluralString;
|
391
|
}
|
392
|
|
393
|
|
394
|
/* (non-Javadoc)
|
395
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
396
|
*/
|
397
|
protected boolean isIgnore(BerlinModelImportState state){
|
398
|
return ! state.getConfig().isDoNameFacts();
|
399
|
}
|
400
|
|
401
|
|
402
|
|
403
|
//for testing only
|
404
|
public static void main(String[] args) {
|
405
|
|
406
|
BerlinModelNameFactsImport nf = new BerlinModelNameFactsImport();
|
407
|
|
408
|
URL url;
|
409
|
try {
|
410
|
url = new URL("http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/");
|
411
|
File path = new File("/Volumes/protolog/protolog/");
|
412
|
if(path.exists()){
|
413
|
String fact = "Acanthocephalus_amplexifolius";
|
414
|
// make getMedia public for this to work
|
415
|
Media media = nf.getMedia(fact, url, path);
|
416
|
logger.info(media);
|
417
|
for (MediaRepresentation rep : media.getRepresentations()){
|
418
|
logger.info(rep.getMimeType());
|
419
|
for (MediaRepresentationPart part : rep.getParts()){
|
420
|
logger.info(part.getUri());
|
421
|
}
|
422
|
}
|
423
|
}
|
424
|
} catch (MalformedURLException e) {
|
425
|
e.printStackTrace();
|
426
|
}
|
427
|
}
|
428
|
}
|