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