2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.wp6
;
13 import java
.io
.IOException
;
14 import java
.net
.MalformedURLException
;
17 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.apache
.http
.HttpException
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.joda
.time
.DateTime
;
23 import org
.springframework
.stereotype
.Component
;
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
;
48 public class CichorieaeImageImport
extends AbstractImageImporter
{
49 private static final Logger logger
= Logger
.getLogger(CichorieaeImageImport
.class);
53 * Imports images from a directory.
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();
61 if (source
.isDirectory()){
62 for (File file
: source
.listFiles() ){
64 String fileName
= file
.getName();
65 String taxonName
= getTaxonName(fileName
);
66 if (taxonName
== null){
69 List
<TaxonBase
> taxa
= taxonService
.searchTaxaByName(taxonName
, state
.getConfig().getSourceReference());
71 logger
.warn("no taxon with this name found: " + taxonName
);
73 handleTaxa(tree
, sourceRef
, fileName
, taxonName
, taxa
);
76 logger
.warn("File is not a file (but a directory?): " + file
.getName());
80 logger
.warn("Source is not a directory!" + source
.toString());
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
);
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"
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 '_'");
107 String featureString
= nameParts
[nameParts
.length
-2];
108 logger
.debug("FeatureString: " + featureString
);
109 String detailString
= nameParts
[nameParts
.length
-1];
110 logger
.debug("detailString: " + detailString
);
112 String taxonName
= "";
113 for (int i
= 0; i
< nameParts
.length
-2; i
++){
114 taxonName
+= nameParts
[i
] + " ";
116 taxonName
= taxonName
.trim();
117 logger
.info("Taxon name: " + taxonName
);
120 String subsp
= " subsp. ";
121 if (taxonName
.contains(_s_
)) {
122 taxonName
= taxonName
.replace(_s_
, subsp
);
123 logger
.info("Taxon name: " + taxonName
);
137 private void handleTaxa(Classification tree
, Reference
<?
> sourceRef
, String fileName
, String taxonName
, List
<TaxonBase
> taxa
) {
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
);
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");
160 * @throws MalformedURLException
161 * @throws IOException
162 * @throws HttpException
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);
172 //String uri = url.toString();
174 String mimeType
= imageMetaData
.getMimeType();
175 String suffix
= null;
176 int height
= imageMetaData
.getHeight();
177 int width
= imageMetaData
.getWidth();
179 DateTime mediaCreated
= null;
180 AgentBase
<?
> artist
= null;
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()));
195 private Taxon
getTaxon(Classification tree
, String taxonName
,
196 List
<TaxonBase
> taxa
) {
198 if(taxa
.size() > 1) {
199 if (logger
.isDebugEnabled()) {
200 logger
.debug("multiple taxa with this name found: " + taxonName
);
202 for (TaxonBase taxonBase
: taxa
) {
203 Taxon tax
= (Taxon
)taxonBase
;
204 if (tree
.isTaxonInTree(tax
)) {
210 taxon
= (Taxon
)taxa
.get(0);
211 logger
.warn("Taxon not found in preferred tree. Use " + taxon
.getTitleCache() + " instead.");
215 taxon
= (Taxon
) taxa
.get(0);
218 taxonService
.saveOrUpdate(taxon
);
220 logger
.warn("Taxon was null. Did not save taxon");