Revision 892efc69
Added by Andreas Kohlbecker almost 14 years ago
app-import/src/main/java/eu/etaxonomy/cdm/io/CichorieaeImageImport.java | ||
---|---|---|
10 | 10 |
package eu.etaxonomy.cdm.io; |
11 | 11 |
|
12 | 12 |
import java.io.File; |
13 |
import java.io.FileNotFoundException; |
|
14 | 13 |
import java.io.IOException; |
15 | 14 |
import java.net.MalformedURLException; |
16 | 15 |
import java.net.URI; |
17 | 16 |
import java.net.URL; |
18 |
import java.util.ArrayList; |
|
19 |
import java.util.HashMap; |
|
20 | 17 |
import java.util.List; |
21 |
import java.util.Set; |
|
22 | 18 |
import java.util.UUID; |
23 | 19 |
|
24 | 20 |
import org.apache.log4j.Logger; |
25 |
import org.apache.sanselan.ImageInfo; |
|
26 |
import org.apache.sanselan.ImageReadException; |
|
27 |
import org.apache.sanselan.Sanselan; |
|
21 |
import org.joda.time.DateTime; |
|
28 | 22 |
import org.springframework.stereotype.Component; |
29 | 23 |
|
30 |
import eu.etaxonomy.cdm.api.service.INameService; |
|
31 | 24 |
import eu.etaxonomy.cdm.app.images.AbstractImageImporter; |
32 | 25 |
import eu.etaxonomy.cdm.app.images.ImageImportConfigurator; |
33 | 26 |
import eu.etaxonomy.cdm.common.CdmUtils; |
34 |
import eu.etaxonomy.cdm.common.ExcelUtils; |
|
35 | 27 |
import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData; |
36 |
import eu.etaxonomy.cdm.common.mediaMetaData.MetaDataFactory; |
|
37 |
import eu.etaxonomy.cdm.common.mediaMetaData.MimeType; |
|
28 |
import eu.etaxonomy.cdm.model.agent.AgentBase; |
|
38 | 29 |
import eu.etaxonomy.cdm.model.common.Language; |
39 |
import eu.etaxonomy.cdm.model.description.Feature;
|
|
30 |
import eu.etaxonomy.cdm.model.common.LanguageString;
|
|
40 | 31 |
import eu.etaxonomy.cdm.model.description.TaxonDescription; |
41 | 32 |
import eu.etaxonomy.cdm.model.description.TextData; |
42 | 33 |
import eu.etaxonomy.cdm.model.media.ImageFile; |
43 | 34 |
import eu.etaxonomy.cdm.model.media.Media; |
44 |
import eu.etaxonomy.cdm.model.media.MediaRepresentation; |
|
45 |
import eu.etaxonomy.cdm.model.name.TaxonNameBase; |
|
46 | 35 |
import eu.etaxonomy.cdm.model.reference.ReferenceBase; |
47 | 36 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
48 | 37 |
import eu.etaxonomy.cdm.model.taxon.TaxonBase; |
... | ... | |
57 | 46 |
public class CichorieaeImageImport extends AbstractImageImporter { |
58 | 47 |
private static final Logger logger = Logger.getLogger(CichorieaeImageImport.class); |
59 | 48 |
|
60 |
private static final String URL = "URL"; |
|
61 |
private static final String URL_APP = "URL_APP"; |
|
62 |
private static final String NAME = "NAME"; |
|
63 |
//private static final String CODE = "CODE"; |
|
64 |
|
|
65 |
// /** |
|
66 |
// * Imports images from an Excel file. |
|
67 |
// */ |
|
68 |
// protected boolean invokeImageImport_ (ImageImportConfigurator config){ |
|
69 |
// |
|
70 |
// ArrayList<HashMap<String, String>> contents; |
|
71 |
// try { |
|
72 |
// contents = ExcelUtils.parseXLS(config.getSource().toString()); |
|
73 |
// } catch (FileNotFoundException e1) { |
|
74 |
// logger.error("FileNotFound: " + config.getSource().toString()); |
|
75 |
// return false; |
|
76 |
// } |
|
77 |
// |
|
78 |
// for (HashMap<String, String> row : contents){ |
|
79 |
// |
|
80 |
// String taxonName = row.get(CichorieaeImageImport.NAME).trim(); |
|
81 |
// |
|
82 |
// INameService nameService = getNameService(); |
|
83 |
// List<TaxonBase> taxa = taxonService.searchTaxaByName(taxonName, config.getSourceReference()); |
|
84 |
// |
|
85 |
// if(taxa.size() == 0){ |
|
86 |
// logger.warn("no taxon with this name found: " + taxonName); |
|
87 |
// }else if(taxa.size() > 1){ |
|
88 |
// logger.warn("multiple taxa with this name found: " + taxonName); |
|
89 |
// }else{ |
|
90 |
// Taxon taxon = (Taxon) taxa.get(0); |
|
91 |
// |
|
92 |
// taxonService.saveTaxon(taxon); |
|
93 |
// |
|
94 |
// TextData feature = TextData.NewInstance(); |
|
95 |
// |
|
96 |
// logger.info("Importing image for taxon: " + taxa); |
|
97 |
// |
|
98 |
// |
|
99 |
// ImageMetaData imageMetaData = new ImageMetaData(); |
|
100 |
// |
|
101 |
// |
|
102 |
// try { |
|
103 |
// URL url = new URL(row.get(CichorieaeImageImport.URL).trim()); |
|
104 |
// |
|
105 |
// imageMetaData.readFrom(url); |
|
106 |
// |
|
107 |
// ImageFile image = ImageFile.NewInstance(url.toString(), null, imageMetaData); |
|
108 |
// |
|
109 |
// MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null); |
|
110 |
// representation.addRepresentationPart(image); |
|
111 |
// |
|
112 |
// Media media = Media.NewInstance(); |
|
113 |
// media.addRepresentation(representation); |
|
114 |
// |
|
115 |
// feature.addMedia(media); |
|
116 |
// |
|
117 |
// feature.setType(Feature.IMAGE()); |
|
118 |
// |
|
119 |
// TaxonDescription description = TaxonDescription.NewInstance(taxon); |
|
120 |
// |
|
121 |
// description.addElement(feature); |
|
122 |
// |
|
123 |
// } catch (MalformedURLException e) { |
|
124 |
// logger.error("Malformed URL", e); |
|
125 |
// } |
|
126 |
// |
|
127 |
// } |
|
128 |
// } |
|
129 |
// return true; |
|
130 |
// |
|
131 |
// } |
|
132 | 49 |
|
133 | 50 |
/** |
134 | 51 |
* Imports images from a directory. |
... | ... | |
142 | 59 |
if (source.isDirectory()){ |
143 | 60 |
for (File file : source.listFiles() ){ |
144 | 61 |
if (file.isFile()){ |
145 |
String name = file.getName(); |
|
146 |
String[] fileNameParts = name.split("\\."); |
|
147 |
if (fileNameParts.length < 2){ |
|
148 |
logger.warn("No file extension found for: " + name); |
|
149 |
continue; |
|
150 |
} |
|
151 |
String extension = fileNameParts[fileNameParts.length - 1]; |
|
152 |
if (! "jpg".equalsIgnoreCase(extension)) { |
|
153 |
logger.warn("Extension not recognized: " + extension); |
|
154 |
// Sometimes occurs here "Thumbs.db" |
|
62 |
String fileName = file.getName(); |
|
63 |
String taxonName = getTaxonName(fileName); |
|
64 |
if (taxonName == null){ |
|
155 | 65 |
continue; |
156 | 66 |
} |
157 |
String firstPart = name.substring(0, name.length() - extension.length() - 1); |
|
158 |
logger.info(firstPart); |
|
159 |
String[] nameParts = firstPart.split("_"); |
|
160 |
if (nameParts.length < 3){ |
|
161 |
logger.warn("name string has less than 2 '_'"); |
|
162 |
continue; |
|
163 |
} |
|
164 |
String featureString = nameParts[nameParts.length-2]; |
|
165 |
logger.debug("FeatureString: " + featureString); |
|
166 |
String detailString = nameParts[nameParts.length-1]; |
|
167 |
logger.debug("detailString: " + detailString); |
|
168 |
|
|
169 |
String taxonName = ""; |
|
170 |
for (int i= 0; i < nameParts.length-2; i++){ |
|
171 |
taxonName += nameParts[i] + " "; |
|
172 |
} |
|
173 |
taxonName = taxonName.trim(); |
|
174 |
logger.info("Taxon name: " + taxonName); |
|
175 |
|
|
176 |
String _s_ = " s "; |
|
177 |
String subsp = " subsp. "; |
|
178 |
if (taxonName.contains(_s_)) { |
|
179 |
taxonName = taxonName.replace(_s_, subsp); |
|
180 |
logger.info("Taxon name: " + taxonName); |
|
181 |
} |
|
182 |
|
|
183 | 67 |
List<TaxonBase> taxa = taxonService.searchTaxaByName(taxonName, config.getSourceReference()); |
184 |
|
|
185 |
Taxon taxon = null; |
|
186 |
|
|
187 | 68 |
if(taxa.size() == 0){ |
188 | 69 |
logger.warn("no taxon with this name found: " + taxonName); |
189 |
} |
|
190 |
|
|
191 |
else { |
|
192 |
if(taxa.size() > 1) { |
|
193 |
if (logger.isDebugEnabled()) { |
|
194 |
logger.debug("multiple taxa with this name found: " + taxonName); |
|
195 |
} |
|
196 |
for (TaxonBase taxonBase : taxa) { |
|
197 |
Taxon tax = (Taxon)taxonBase; |
|
198 |
if (tree.isTaxonInTree(tax)) { |
|
199 |
taxon = tax; |
|
200 |
break; |
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
} else { |
|
205 |
taxon = (Taxon) taxa.get(0); |
|
206 |
} |
|
207 |
taxonService.saveOrUpdate(taxon); |
|
208 |
|
|
209 |
TextData textData = TextData.NewInstance(); |
|
210 |
logger.info("Importing image for taxon: " + taxa); |
|
211 |
//ImageMetaData imageMetaData =ImageMetaData.newInstance(); |
|
212 |
MetaDataFactory metaDataFactory = MetaDataFactory.getInstance(); |
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
try { |
|
217 |
String urlPrefix = "http://media.bgbm.org/erez/erez?src=EditWP6/photos/"; |
|
218 |
String urlString = urlPrefix + name; |
|
219 |
logger.info(urlString); |
|
220 |
URL url = new URL(urlString); |
|
221 |
|
|
222 |
URI uri = CdmUtils.string2Uri(urlString); |
|
223 |
|
|
224 |
//imageinfo = Sanselan.getImageInfo(file); |
|
225 |
ImageMetaData imageMetaData =ImageMetaData.newInstance(); |
|
226 |
imageMetaData.readImageInfo(uri, 0); |
|
227 |
//ImageMetaData imageMetaData = (ImageMetaData) metaDataFactory.readMediaData(uri, MimeType.IMAGE); |
|
228 |
ImageFile image = ImageFile.NewInstance(url.toString(), null, imageMetaData.getHeight(), imageMetaData.getWidth()); |
|
229 |
|
|
230 |
MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null); |
|
231 |
representation.addRepresentationPart(image); |
|
232 |
Media media = Media.NewInstance(); |
|
233 |
media.addRepresentation(representation); |
|
234 |
|
|
235 |
textData.addMedia(media); |
|
236 |
textData.putText(taxonName, Language.ENGLISH()); |
|
237 |
|
|
238 |
textData.setType(Feature.IMAGE()); |
|
239 |
TaxonDescription taxonDescription = taxon.getOrCreateImageGallery(sourceRef == null ? null :sourceRef.getTitleCache()); |
|
240 |
|
|
241 |
taxonDescription.addElement(textData); |
|
242 |
|
|
243 |
} catch (MalformedURLException e) { |
|
244 |
logger.error("Malformed URL", e); |
|
245 |
} catch (IOException e) { |
|
246 |
// TODO Auto-generated catch block |
|
247 |
e.printStackTrace(); |
|
248 |
} |
|
70 |
} else { |
|
71 |
handleTaxa(tree, sourceRef, fileName, taxonName, taxa); |
|
249 | 72 |
} |
250 | 73 |
}else{ |
251 | 74 |
logger.warn("File is not a file (but a directory?): " + file.getName()); |
... | ... | |
258 | 81 |
return true; |
259 | 82 |
|
260 | 83 |
} |
84 |
|
|
85 |
private String getTaxonName(String fileName){ |
|
86 |
String[] fileNameParts = fileName.split("\\."); |
|
87 |
if (fileNameParts.length < 2){ |
|
88 |
logger.warn("No file extension found for: " + fileName); |
|
89 |
return null; |
|
90 |
} |
|
91 |
String extension = fileNameParts[fileNameParts.length - 1]; |
|
92 |
if (! "jpg".equalsIgnoreCase(extension)) { |
|
93 |
logger.warn("Extension not recognized: " + extension); |
|
94 |
// Sometimes occurs here "Thumbs.db" |
|
95 |
return null; |
|
96 |
} |
|
97 |
String firstPart = fileName.substring(0, fileName.length() - extension.length() - 1); |
|
98 |
logger.info(firstPart); |
|
99 |
String[] nameParts = firstPart.split("_"); |
|
100 |
if (nameParts.length < 3){ |
|
101 |
logger.warn("name string has less than 2 '_'"); |
|
102 |
return null; |
|
103 |
} |
|
104 |
|
|
105 |
String featureString = nameParts[nameParts.length-2]; |
|
106 |
logger.debug("FeatureString: " + featureString); |
|
107 |
String detailString = nameParts[nameParts.length-1]; |
|
108 |
logger.debug("detailString: " + detailString); |
|
109 |
|
|
110 |
String taxonName = ""; |
|
111 |
for (int i= 0; i < nameParts.length-2; i++){ |
|
112 |
taxonName += nameParts[i] + " "; |
|
113 |
} |
|
114 |
taxonName = taxonName.trim(); |
|
115 |
logger.info("Taxon name: " + taxonName); |
|
116 |
|
|
117 |
String _s_ = " s "; |
|
118 |
String subsp = " subsp. "; |
|
119 |
if (taxonName.contains(_s_)) { |
|
120 |
taxonName = taxonName.replace(_s_, subsp); |
|
121 |
logger.info("Taxon name: " + taxonName); |
|
122 |
} |
|
123 |
return taxonName; |
|
124 |
} |
|
125 |
|
|
126 |
|
|
127 |
/** |
|
128 |
* @param tree |
|
129 |
* @param sourceRef |
|
130 |
* @param name |
|
131 |
* @param taxonName |
|
132 |
* @param taxa |
|
133 |
* @param taxon |
|
134 |
*/ |
|
135 |
private void handleTaxa(TaxonomicTree tree, ReferenceBase sourceRef, String fileName, String taxonName, List<TaxonBase> taxa) { |
|
136 |
|
|
137 |
Taxon taxon = getTaxon(tree, taxonName, taxa); |
|
138 |
TaxonDescription imageGallery = taxon.getOrCreateImageGallery(sourceRef == null ? null :sourceRef.getTitleCache()); |
|
139 |
TextData textData = imageGallery.getOrCreateImageTextData(); |
|
140 |
logger.info("Importing image for taxon: " + taxa); |
|
141 |
try { |
|
142 |
Media media = getMedia(fileName, taxonName); |
|
143 |
textData.addMedia(media); |
|
144 |
} catch (MalformedURLException e) { |
|
145 |
logger.error("Malformed URL", e); |
|
146 |
} catch (IOException e) { |
|
147 |
logger.error("IOException when handling image url"); |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
|
|
152 |
/** |
|
153 |
* @param fileName |
|
154 |
* @param taxonName |
|
155 |
* @return |
|
156 |
* @throws MalformedURLException |
|
157 |
* @throws IOException |
|
158 |
*/ |
|
159 |
private Media getMedia(String fileName, String taxonName) throws MalformedURLException, IOException { |
|
160 |
String urlPrefix = "http://media.bgbm.org/erez/erez?src=EditWP6/photos/"; |
|
161 |
String urlString = urlPrefix + fileName; |
|
162 |
logger.info(urlString); |
|
163 |
URL url = new URL(urlString); |
|
164 |
URI uri = CdmUtils.string2Uri(urlString); |
|
165 |
ImageMetaData imageMetaData =ImageMetaData.newInstance(); |
|
166 |
imageMetaData.readImageInfo(uri, 0); |
|
167 |
|
|
168 |
//String uri = url.toString(); |
|
169 |
|
|
170 |
String uriString = url.toString(); |
|
171 |
String mimeType = imageMetaData.getMimeType(); |
|
172 |
String suffix = null; |
|
173 |
int height = imageMetaData.getHeight(); |
|
174 |
int width = imageMetaData.getWidth(); |
|
175 |
Integer size = null; |
|
176 |
DateTime mediaCreated = null; |
|
177 |
AgentBase artist = null; |
|
178 |
|
|
179 |
|
|
180 |
ImageFile image = ImageFile.NewInstance(uriString, size, height, width); |
|
181 |
Media media = ImageFile.NewMediaInstance(mediaCreated, artist, uriString, mimeType, suffix, size, height, width); |
|
182 |
media.addTitle(LanguageString.NewInstance(taxonName, Language.LATIN())); |
|
183 |
|
|
184 |
return media; |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* @param tree |
|
189 |
* @param taxonName |
|
190 |
* @param taxa |
|
191 |
* @return |
|
192 |
*/ |
|
193 |
private Taxon getTaxon(TaxonomicTree tree, String taxonName, |
|
194 |
List<TaxonBase> taxa) { |
|
195 |
Taxon taxon = null; |
|
196 |
if(taxa.size() > 1) { |
|
197 |
if (logger.isDebugEnabled()) { |
|
198 |
logger.debug("multiple taxa with this name found: " + taxonName); |
|
199 |
} |
|
200 |
for (TaxonBase taxonBase : taxa) { |
|
201 |
Taxon tax = (Taxon)taxonBase; |
|
202 |
if (tree.isTaxonInTree(tax)) { |
|
203 |
taxon = tax; |
|
204 |
break; |
|
205 |
} |
|
206 |
} |
|
207 |
if (taxon == null){ |
|
208 |
taxon = (Taxon)taxa.get(0); |
|
209 |
logger.warn("Taxon not found in preferred tree. Use " + taxon.getTitleCache() + " instead."); |
|
210 |
} |
|
211 |
|
|
212 |
} else { |
|
213 |
taxon = (Taxon) taxa.get(0); |
|
214 |
} |
|
215 |
if (taxon != null){ |
|
216 |
taxonService.saveOrUpdate(taxon); |
|
217 |
}else{ |
|
218 |
logger.warn("Taxon was null. Did not save taxon"); |
|
219 |
} |
|
220 |
return taxon; |
|
221 |
} |
|
261 | 222 |
} |
Also available in: Unified diff
merging /branches/cdmlib/SPRINT-Chichorieae1/ to trunk