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.wp6;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.IOException;
|
14
|
import java.net.MalformedURLException;
|
15
|
import java.net.URI;
|
16
|
import java.net.URL;
|
17
|
import java.util.List;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.http.HttpException;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.joda.time.DateTime;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
|
25
|
import eu.etaxonomy.cdm.app.images.AbstractImageImporter;
|
26
|
import eu.etaxonomy.cdm.app.images.ImageImportConfigurator;
|
27
|
import eu.etaxonomy.cdm.app.images.ImageImportState;
|
28
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
29
|
import eu.etaxonomy.cdm.common.media.ImageInfo;
|
30
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
31
|
import eu.etaxonomy.cdm.model.common.Language;
|
32
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
33
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
34
|
import eu.etaxonomy.cdm.model.description.TextData;
|
35
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
36
|
import eu.etaxonomy.cdm.model.media.Media;
|
37
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
41
|
|
42
|
/**
|
43
|
* @author n.hoffmann
|
44
|
* @created 18.11.2008
|
45
|
* @version 1.0
|
46
|
*/
|
47
|
@Component
|
48
|
public class CichorieaeImageImport extends AbstractImageImporter {
|
49
|
private static final Logger logger = Logger.getLogger(CichorieaeImageImport.class);
|
50
|
|
51
|
|
52
|
/**
|
53
|
* Imports images from a directory.
|
54
|
*/
|
55
|
protected void invokeImageImport (ImageImportState state){
|
56
|
File source = new File(state.getConfig().getSource());
|
57
|
UUID treeUuid = state.getConfig().getClassificationUuid();
|
58
|
Classification tree = classificationService.find(treeUuid);
|
59
|
Reference<?> sourceRef = state.getConfig().getSourceReference();
|
60
|
|
61
|
if (source.isDirectory()){
|
62
|
for (File file : source.listFiles() ){
|
63
|
if (file.isFile()){
|
64
|
String fileName = file.getName();
|
65
|
String taxonName = getTaxonName(fileName);
|
66
|
if (taxonName == null){
|
67
|
continue;
|
68
|
}
|
69
|
List<TaxonBase> taxa = taxonService.searchTaxaByName(taxonName, state.getConfig().getSourceReference());
|
70
|
if(taxa.size() == 0){
|
71
|
logger.warn("no taxon with this name found: " + taxonName);
|
72
|
} else {
|
73
|
handleTaxa(tree, sourceRef, fileName, taxonName, taxa);
|
74
|
}
|
75
|
}else{
|
76
|
logger.warn("File is not a file (but a directory?): " + file.getName());
|
77
|
}
|
78
|
}
|
79
|
}else{
|
80
|
logger.warn("Source is not a directory!" + source.toString());
|
81
|
}
|
82
|
|
83
|
return;
|
84
|
|
85
|
}
|
86
|
|
87
|
private String getTaxonName(String fileName){
|
88
|
String[] fileNameParts = fileName.split("\\.");
|
89
|
if (fileNameParts.length < 2){
|
90
|
logger.warn("No file extension found for: " + fileName);
|
91
|
return null;
|
92
|
}
|
93
|
String extension = fileNameParts[fileNameParts.length - 1];
|
94
|
if (! "jpg".equalsIgnoreCase(extension)) {
|
95
|
logger.warn("Extension not recognized: " + extension);
|
96
|
// Sometimes occurs here "Thumbs.db"
|
97
|
return null;
|
98
|
}
|
99
|
String firstPart = fileName.substring(0, fileName.length() - extension.length() - 1);
|
100
|
logger.info(firstPart);
|
101
|
String[] nameParts = firstPart.split("_");
|
102
|
if (nameParts.length < 3){
|
103
|
logger.warn("name string has less than 2 '_'");
|
104
|
return null;
|
105
|
}
|
106
|
|
107
|
String featureString = nameParts[nameParts.length-2];
|
108
|
logger.debug("FeatureString: " + featureString);
|
109
|
String detailString = nameParts[nameParts.length-1];
|
110
|
logger.debug("detailString: " + detailString);
|
111
|
|
112
|
String taxonName = "";
|
113
|
for (int i= 0; i < nameParts.length-2; i++){
|
114
|
taxonName += nameParts[i] + " ";
|
115
|
}
|
116
|
taxonName = taxonName.trim();
|
117
|
logger.info("Taxon name: " + taxonName);
|
118
|
|
119
|
String _s_ = " s ";
|
120
|
String subsp = " subsp. ";
|
121
|
if (taxonName.contains(_s_)) {
|
122
|
taxonName = taxonName.replace(_s_, subsp);
|
123
|
logger.info("Taxon name: " + taxonName);
|
124
|
}
|
125
|
return taxonName;
|
126
|
}
|
127
|
|
128
|
|
129
|
/**
|
130
|
* @param tree
|
131
|
* @param sourceRef
|
132
|
* @param name
|
133
|
* @param taxonName
|
134
|
* @param taxa
|
135
|
* @param taxon
|
136
|
*/
|
137
|
private void handleTaxa(Classification tree, Reference<?> sourceRef, String fileName, String taxonName, List<TaxonBase> taxa) {
|
138
|
|
139
|
Taxon taxon = getTaxon(tree, taxonName, taxa);
|
140
|
TaxonDescription imageGallery = taxon.getOrCreateImageGallery(sourceRef == null ? null :sourceRef.getTitleCache());
|
141
|
TextData textData = imageGallery.getOrCreateImageTextData();
|
142
|
logger.info("Importing image for taxon: " + taxa);
|
143
|
try {
|
144
|
Media media = getMedia(fileName, taxonName);
|
145
|
textData.addMedia(media);
|
146
|
} catch (MalformedURLException e) {
|
147
|
logger.error("Malformed URL", e);
|
148
|
} catch (IOException e) {
|
149
|
logger.error("IOException when handling image url");
|
150
|
} catch (HttpException e) {
|
151
|
logger.error("HttpException when handling image url");
|
152
|
}
|
153
|
}
|
154
|
|
155
|
|
156
|
/**
|
157
|
* @param fileName
|
158
|
* @param taxonName
|
159
|
* @return
|
160
|
* @throws MalformedURLException
|
161
|
* @throws IOException
|
162
|
* @throws HttpException
|
163
|
*/
|
164
|
private Media getMedia(String fileName, String taxonName) throws MalformedURLException, IOException, HttpException {
|
165
|
String urlPrefix = "http://media.bgbm.org/erez/erez?src=EditWP6/photos/";
|
166
|
String urlString = urlPrefix + fileName;
|
167
|
logger.info(urlString);
|
168
|
URL url = new URL(urlString);
|
169
|
URI uri = CdmUtils.string2Uri(urlString);
|
170
|
ImageInfo imageMetaData =ImageInfo.NewInstance(uri, 0);
|
171
|
|
172
|
//String uri = url.toString();
|
173
|
|
174
|
String mimeType = imageMetaData.getMimeType();
|
175
|
String suffix = null;
|
176
|
int height = imageMetaData.getHeight();
|
177
|
int width = imageMetaData.getWidth();
|
178
|
Integer size = null;
|
179
|
DateTime mediaCreated = null;
|
180
|
AgentBase<?> artist = null;
|
181
|
|
182
|
// ImageFile image = ImageFile.NewInstance(uri, size, height, width);
|
183
|
Media media = ImageFile.NewMediaInstance(mediaCreated, artist, uri, mimeType, suffix, size, height, width);
|
184
|
media.putTitle(LanguageString.NewInstance(taxonName, Language.LATIN()));
|
185
|
|
186
|
return media;
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* @param tree
|
191
|
* @param taxonName
|
192
|
* @param taxa
|
193
|
* @return
|
194
|
*/
|
195
|
private Taxon getTaxon(Classification tree, String taxonName,
|
196
|
List<TaxonBase> taxa) {
|
197
|
Taxon taxon = null;
|
198
|
if(taxa.size() > 1) {
|
199
|
if (logger.isDebugEnabled()) {
|
200
|
logger.debug("multiple taxa with this name found: " + taxonName);
|
201
|
}
|
202
|
for (TaxonBase taxonBase : taxa) {
|
203
|
Taxon tax = (Taxon)taxonBase;
|
204
|
if (tree.isTaxonInTree(tax)) {
|
205
|
taxon = tax;
|
206
|
break;
|
207
|
}
|
208
|
}
|
209
|
if (taxon == null){
|
210
|
taxon = (Taxon)taxa.get(0);
|
211
|
logger.warn("Taxon not found in preferred tree. Use " + taxon.getTitleCache() + " instead.");
|
212
|
}
|
213
|
|
214
|
} else {
|
215
|
taxon = (Taxon) taxa.get(0);
|
216
|
}
|
217
|
if (taxon != null){
|
218
|
taxonService.saveOrUpdate(taxon);
|
219
|
}else{
|
220
|
logger.warn("Taxon was null. Did not save taxon");
|
221
|
}
|
222
|
return taxon;
|
223
|
}
|
224
|
}
|