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;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.IOException;
|
14
|
import java.net.MalformedURLException;
|
15
|
import java.net.URL;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
|
21
|
import org.apache.commons.imaging.ImageInfo;
|
22
|
import org.apache.commons.imaging.ImageReadException;
|
23
|
import org.apache.commons.imaging.Imaging;
|
24
|
import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem;
|
25
|
import org.apache.commons.imaging.common.ImageMetadata;
|
26
|
import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem;
|
27
|
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
|
28
|
import org.apache.log4j.Logger;
|
29
|
import org.springframework.stereotype.Component;
|
30
|
|
31
|
import eu.etaxonomy.cdm.app.images.AbstractImageImporter;
|
32
|
import eu.etaxonomy.cdm.app.images.ImageImportState;
|
33
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
34
|
import eu.etaxonomy.cdm.model.agent.Person;
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
37
|
import eu.etaxonomy.cdm.model.description.Feature;
|
38
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
39
|
import eu.etaxonomy.cdm.model.description.TextData;
|
40
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
41
|
import eu.etaxonomy.cdm.model.media.Media;
|
42
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
43
|
import eu.etaxonomy.cdm.model.media.Rights;
|
44
|
import eu.etaxonomy.cdm.model.media.RightsType;
|
45
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
46
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
47
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
48
|
import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
|
49
|
import eu.etaxonomy.cdm.strategy.match.IMatchStrategyEqual;
|
50
|
|
51
|
/**
|
52
|
* TODO not working at the moment
|
53
|
*
|
54
|
* @author n.hoffmann
|
55
|
* @since 18.11.2008
|
56
|
*/
|
57
|
@Component
|
58
|
public class PalmaeImageImport extends AbstractImageImporter {
|
59
|
|
60
|
private static final long serialVersionUID = 1226643507245147417L;
|
61
|
|
62
|
private static final Logger logger = Logger.getLogger(PalmaeImageImport.class);
|
63
|
|
64
|
enum MetaData{
|
65
|
NAME,
|
66
|
ARTIST,
|
67
|
COPYRIGHT,
|
68
|
COPYRIGHTNOTICE,
|
69
|
OBJECTNAME
|
70
|
}
|
71
|
|
72
|
private static int modCount = 300;
|
73
|
|
74
|
private static String pluralString = "images";
|
75
|
|
76
|
/**
|
77
|
* Rudimetary implementation using apache sanselan. This implementation depends
|
78
|
* on the metadata standards used in the palmae images. The IPTC field ObjectName
|
79
|
* contains a string like this: "Arecaceae; Eugeissona utilis". The string
|
80
|
* in front of the semicolon is the family name and the one behind, the taxon name.
|
81
|
* So we basically assume, that if the string gets split by ";" the element at
|
82
|
* index 1 should be the taxon name.
|
83
|
* If this format changes this method breaks!
|
84
|
*
|
85
|
* TODO The ImageMetaData class of the commons package should provide
|
86
|
* convenient access to the metadata of an image as well as all the error handling
|
87
|
*
|
88
|
* @param imageFile
|
89
|
* @return the name of the taxon as stored in ObjectName IPTC tag
|
90
|
*/
|
91
|
public String retrieveTaxonNameFromImageMetadata(File imageFile){
|
92
|
String name = null;
|
93
|
|
94
|
ImageMetadata metadata = null;
|
95
|
|
96
|
try {
|
97
|
metadata = Imaging.getMetadata(imageFile);
|
98
|
} catch (ImageReadException e) {
|
99
|
logger.error("Error reading image" + " in " + imageFile.getName(), e);
|
100
|
} catch (IOException e) {
|
101
|
logger.error("Error reading file" + " in " + imageFile.getName(), e);
|
102
|
}
|
103
|
|
104
|
if(metadata instanceof JpegImageMetadata){
|
105
|
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
|
106
|
|
107
|
for (ImageMetadataItem metadataItem : jpegMetadata.getItems()){
|
108
|
if (metadataItem instanceof GenericImageMetadataItem){
|
109
|
GenericImageMetadataItem item = (GenericImageMetadataItem) metadataItem;
|
110
|
if(item.getKeyword().equals("ObjectName")){
|
111
|
logger.debug("File: " + imageFile.getName() + ". ObjectName string is: " + item.getText());
|
112
|
String[] objectNameSplit = item.getText().split(";");
|
113
|
|
114
|
try {
|
115
|
name = objectNameSplit[1].trim();
|
116
|
} catch (ArrayIndexOutOfBoundsException e) {
|
117
|
logger.warn("ObjectNameSplit has no second part: " + item.getText() + " in " + imageFile.getName());
|
118
|
//throw e;
|
119
|
}
|
120
|
}
|
121
|
}else{
|
122
|
throw new IllegalStateException("Unsupported ImageMetadataItem type: " + metadataItem.getClass().getName());
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
|
127
|
|
128
|
return name;
|
129
|
}
|
130
|
|
131
|
private Map<MetaData, String> getMetaData(File imageFile, List<MetaData> metaData){
|
132
|
HashMap<MetaData, String> result = new HashMap<>();
|
133
|
|
134
|
ImageMetadata metadata = null;
|
135
|
List<String> metaDataStrings = new ArrayList<>();
|
136
|
|
137
|
for (MetaData data: metaData){
|
138
|
metaDataStrings.add(data.name().toLowerCase());
|
139
|
}
|
140
|
|
141
|
try {
|
142
|
metadata = Imaging.getMetadata(imageFile);
|
143
|
} catch (ImageReadException e) {
|
144
|
logger.error("Error reading image" + " in " + imageFile.getName(), e);
|
145
|
} catch (IOException e) {
|
146
|
logger.error("Error reading file" + " in " + imageFile.getName(), e);
|
147
|
}
|
148
|
|
149
|
if(metadata instanceof JpegImageMetadata){
|
150
|
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
|
151
|
|
152
|
for (ImageMetadataItem metadataItem : jpegMetadata.getItems()){
|
153
|
if (metadataItem instanceof GenericImageMetadataItem){
|
154
|
GenericImageMetadataItem item = (GenericImageMetadataItem) metadataItem;
|
155
|
if(metaDataStrings.contains(item.getKeyword().toLowerCase())){
|
156
|
logger.debug("File: " + imageFile.getName() + ". "+ item.getKeyword() +"string is: " + item.getText());
|
157
|
result.put(MetaData.valueOf(item.getKeyword().toUpperCase()), item.getText());
|
158
|
}
|
159
|
}else{
|
160
|
throw new IllegalStateException("Unsupported ImageMetadataItem type: " + metadataItem.getClass().getName());
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
|
165
|
return result;
|
166
|
}
|
167
|
|
168
|
@Override
|
169
|
protected void invokeImageImport (ImageImportState state){
|
170
|
|
171
|
logger.info("Importing images from directory: " + state.getConfig().getSourceNameString());
|
172
|
|
173
|
File sourceFolder = new File(state.getConfig().getSource());
|
174
|
String taxonName;
|
175
|
if(sourceFolder.isDirectory()){
|
176
|
int count = 0;
|
177
|
for( File file : sourceFolder.listFiles()){
|
178
|
if(file.isFile()){
|
179
|
doCount(count++, modCount, pluralString);
|
180
|
|
181
|
taxonName= retrieveTaxonNameFromImageMetadata(file);
|
182
|
logger.debug("Looking up taxa with taxon name: " + taxonName);
|
183
|
|
184
|
//TODO:
|
185
|
ArrayList<MetaData> metaDataList = new ArrayList<>();
|
186
|
metaDataList.add (MetaData.ARTIST);
|
187
|
metaDataList.add (MetaData.COPYRIGHT);
|
188
|
metaDataList.add (MetaData.COPYRIGHTNOTICE);
|
189
|
metaDataList.add (MetaData.OBJECTNAME);
|
190
|
//metaDataList.add (MetaData.NAME);
|
191
|
|
192
|
Map<MetaData, String> metaData = getMetaData(file, metaDataList);
|
193
|
|
194
|
|
195
|
|
196
|
Reference sec = referenceService.find(state.getConfig().getSecUuid());
|
197
|
|
198
|
List<TaxonBase> taxa = new ArrayList<>();
|
199
|
if (taxonName != null){
|
200
|
taxa = taxonService.searchByName(taxonName, true, sec);
|
201
|
}else{
|
202
|
logger.error("TaxonName is null " + " in " + file.getName());
|
203
|
}
|
204
|
if(taxa.size() == 0){
|
205
|
logger.warn("no taxon with this name found: " + taxonName + " in " + file.getName());
|
206
|
}else if(taxa.size() > 1){
|
207
|
logger.error(taxa);
|
208
|
logger.error("multiple taxa with this name found: " + taxonName + " in " + file.getName());
|
209
|
}else{
|
210
|
Taxon taxon = (Taxon) taxa.get(0);
|
211
|
|
212
|
taxonService.saveOrUpdate(taxon);
|
213
|
|
214
|
//MetaDataFactory metaDataFactory = MetaDataFactory.getInstance();
|
215
|
//ImageMetaData imageMetaData = (ImageMetaData) metaDataFactory.readMediaData(file.toURI(), MimeType.IMAGE);
|
216
|
try{
|
217
|
ImageInfo imageinfo = Imaging.getImageInfo(file);
|
218
|
|
219
|
String mimeType = imageinfo.getMimeType();
|
220
|
String suffix = "jpg";
|
221
|
|
222
|
// URL for this image
|
223
|
URL url = null;
|
224
|
try {
|
225
|
url = new URL(state.getConfig().getMediaUrlString() + file.getName());
|
226
|
} catch (MalformedURLException e) {
|
227
|
logger.warn("URL is malformed: "+ url);
|
228
|
}
|
229
|
|
230
|
ImageFile imageFile = ImageFile.NewInstance(url.toURI(),null, imageinfo.getHeight(), imageinfo.getWidth());
|
231
|
|
232
|
MediaRepresentation representation = MediaRepresentation.NewInstance(mimeType, suffix);
|
233
|
representation.addRepresentationPart(imageFile);
|
234
|
|
235
|
Media media = Media.NewInstance();
|
236
|
media.addRepresentation(representation);
|
237
|
if (metaData.containsKey(MetaData.OBJECTNAME)){
|
238
|
media.setTitleCache(metaData.get(MetaData.OBJECTNAME).replace("'", ""), true);
|
239
|
}
|
240
|
//TODO: add the rights and the author:
|
241
|
Person artist = null;
|
242
|
if (metaData.containsKey(MetaData.ARTIST)){
|
243
|
//TODO search for the person first and then create the object...
|
244
|
artist = Person.NewTitledInstance(metaData.get(MetaData.ARTIST).replace("'", ""));
|
245
|
artist.setGivenName(getGivenName(metaData.get(MetaData.ARTIST)).replace("'", ""));
|
246
|
artist.setFamilyName(getFamilyName(metaData.get(MetaData.ARTIST)).replace("'", ""));
|
247
|
|
248
|
IMatchStrategyEqual matchStrategy = DefaultMatchStrategy.NewInstance(AgentBase.class);
|
249
|
try{
|
250
|
List<Person> agents = commonService.findMatching(artist, matchStrategy);
|
251
|
|
252
|
if (agents.size()!= 0){
|
253
|
artist = agents.get(0);
|
254
|
}
|
255
|
}catch(eu.etaxonomy.cdm.strategy.match.MatchException e){
|
256
|
logger.warn("MatchException occurred");
|
257
|
}
|
258
|
|
259
|
media.setArtist(artist);
|
260
|
}
|
261
|
|
262
|
if (metaData.containsKey(MetaData.COPYRIGHT)){
|
263
|
//TODO: maybe search for the identic right...
|
264
|
Rights copyright = Rights.NewInstance();
|
265
|
copyright.setType(RightsType.COPYRIGHT());
|
266
|
Person copyrightOwner;
|
267
|
if (artist != null && !artist.getFamilyName().equalsIgnoreCase(getFamilyName(metaData.get(MetaData.COPYRIGHT)))){
|
268
|
copyrightOwner = Person.NewInstance();
|
269
|
|
270
|
copyrightOwner.setGivenName(getGivenName(metaData.get(MetaData.COPYRIGHT)));
|
271
|
copyrightOwner.setFamilyName(getFamilyName(metaData.get(MetaData.COPYRIGHT)));
|
272
|
}else
|
273
|
{
|
274
|
copyrightOwner = artist;
|
275
|
}
|
276
|
copyright.setAgent(copyrightOwner);
|
277
|
//IMatchStrategy matchStrategy = DefaultMatchStrategy.NewInstance(Rights.class);
|
278
|
media.addRights(copyright);
|
279
|
}
|
280
|
|
281
|
Reference sourceRef = state.getConfig().getSourceReference();
|
282
|
TaxonDescription description = taxon.getOrCreateImageGallery(sourceRef == null ? null :sourceRef.getTitleCache());
|
283
|
|
284
|
|
285
|
TextData textData = null;
|
286
|
for (DescriptionElementBase element : description.getElements()){
|
287
|
if (element.isInstanceOf(TextData.class)){
|
288
|
textData = CdmBase.deproxy(element, TextData.class);
|
289
|
}
|
290
|
}
|
291
|
if (textData == null){
|
292
|
textData = TextData.NewInstance();
|
293
|
}
|
294
|
|
295
|
|
296
|
textData.addMedia(media);
|
297
|
|
298
|
textData.setFeature(Feature.IMAGE());
|
299
|
|
300
|
description.addElement(textData);
|
301
|
|
302
|
taxonService.saveOrUpdate(taxon);
|
303
|
}catch(Exception e) {
|
304
|
e.printStackTrace();
|
305
|
}
|
306
|
}
|
307
|
}
|
308
|
}
|
309
|
}else{
|
310
|
logger.error("given source folder is not a directory");
|
311
|
}
|
312
|
return;
|
313
|
}
|
314
|
|
315
|
private String getGivenName(String artist){
|
316
|
if (artist == null){
|
317
|
return "";
|
318
|
}
|
319
|
if (!artist.contains(" ")) {
|
320
|
return "";
|
321
|
}
|
322
|
if (artist.contains(",")){
|
323
|
String [] artistSplits = artist.split(",");
|
324
|
artist = artistSplits[0];
|
325
|
}
|
326
|
|
327
|
try{
|
328
|
return artist.substring(0, artist.lastIndexOf(' ')).replace("'", "");
|
329
|
}catch (Exception e){
|
330
|
return "";
|
331
|
}
|
332
|
}
|
333
|
|
334
|
private String getFamilyName(String artist){
|
335
|
|
336
|
if (artist.contains(",")){
|
337
|
String [] artistSplits = artist.split(",");
|
338
|
artist = artistSplits[0];
|
339
|
|
340
|
}
|
341
|
if (!artist.contains(" ")) {
|
342
|
|
343
|
return artist;
|
344
|
}
|
345
|
try{
|
346
|
return artist.substring(artist.lastIndexOf(' ')).replace(" ", "");
|
347
|
}
|
348
|
catch(Exception e){
|
349
|
return "";
|
350
|
}
|
351
|
}
|
352
|
|
353
|
protected void doCount(int count, int modCount, String pluralString){
|
354
|
if ((count % modCount ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
|
355
|
}
|
356
|
}
|