Project

General

Profile

Download (6.56 KB) Statistics
| Branch: | Tag: | Revision:
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.URI;
16
import java.net.URL;
17
import java.util.List;
18
import java.util.UUID;
19

    
20
import org.apache.log4j.Logger;
21
import org.joda.time.DateTime;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.app.images.AbstractImageImporter;
25
import eu.etaxonomy.cdm.app.images.ImageImportConfigurator;
26
import eu.etaxonomy.cdm.common.CdmUtils;
27
import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData;
28
import eu.etaxonomy.cdm.model.agent.AgentBase;
29
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.common.LanguageString;
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.ReferenceBase;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38
import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
39

    
40
/**
41
 * @author n.hoffmann
42
 * @created 18.11.2008
43
 * @version 1.0
44
 */
45
@Component
46
public class CichorieaeImageImport extends AbstractImageImporter {
47
	private static final Logger logger = Logger.getLogger(CichorieaeImageImport.class);
48
	
49
	
50
	/** 
51
	 * Imports images from a directory.
52
	 */
53
	protected boolean invokeImageImport (ImageImportConfigurator config){
54
		File source = (File)config.getSource();
55
		UUID treeUuid = config.getTaxonomicTreeUuid();
56
		TaxonomicTree tree = taxonTreeService.getTaxonomicTreeByUuid(treeUuid);
57
		ReferenceBase sourceRef = config.getSourceReference();
58
		
59
		if (source.isDirectory()){
60
			for (File file : source.listFiles() ){
61
				if (file.isFile()){
62
					String fileName = file.getName();
63
					String taxonName = getTaxonName(fileName);
64
					if (taxonName == null){
65
						continue;
66
					}
67
					List<TaxonBase> taxa = taxonService.searchTaxaByName(taxonName, config.getSourceReference());			
68
					if(taxa.size() == 0){
69
						logger.warn("no taxon with this name found: " + taxonName);
70
					} else {
71
						handleTaxa(tree, sourceRef, fileName, taxonName, taxa);
72
					}
73
				}else{
74
					logger.warn("File is not a file (but a directory?): " + file.getName());
75
				}
76
			}	
77
		}else{
78
			logger.warn("Source is not a directory!" + source.toString());
79
		}
80
	
81
		return true;
82
		
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
	}
222
}
(1-1/4)