Project

General

Profile

Download (6.53 KB) Statistics
| Branch: | 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.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.util.List;
17
import java.util.UUID;
18

    
19
import org.apache.http.HttpException;
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.ImageImportState;
26
import eu.etaxonomy.cdm.common.CdmUtils;
27
import eu.etaxonomy.cdm.common.media.ImageInfo;
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.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
 * @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
	@Override
54
    protected void invokeImageImport (ImageImportState state){
55
		File source = new File(state.getConfig().getSource());
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.searchTaxaByName(taxonName, 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

    
82
		return;
83

    
84
	}
85

    
86
	private String getTaxonName(String fileName){
87
		String[] fileNameParts = fileName.split("\\.");
88
		if (fileNameParts.length < 2){
89
			logger.warn("No file extension found for: " +  fileName);
90
			return null;
91
		}
92
		String extension = fileNameParts[fileNameParts.length - 1];
93
		if (! "jpg".equalsIgnoreCase(extension)) {
94
			logger.warn("Extension not recognized: " + extension);
95
			// Sometimes occurs here "Thumbs.db"
96
			return null;
97
		}
98
		String firstPart = fileName.substring(0, fileName.length() - extension.length() - 1);
99
		logger.info(firstPart);
100
		String[] nameParts = firstPart.split("_");
101
		if (nameParts.length < 3){
102
			logger.warn("name string has less than 2 '_'");
103
			return null;
104
		}
105

    
106
		String featureString = nameParts[nameParts.length-2];
107
		logger.debug("FeatureString: " +  featureString);
108
		String detailString = nameParts[nameParts.length-1];
109
		logger.debug("detailString: " +  detailString);
110

    
111
		String taxonName = "";
112
		for (int i= 0; i < nameParts.length-2; i++){
113
			taxonName += nameParts[i] + " ";
114
		}
115
		taxonName = taxonName.trim();
116
		logger.info("Taxon name: " +  taxonName);
117

    
118
		String _s_ = " s ";
119
		String subsp = " subsp. ";
120
		if (taxonName.contains(_s_)) {
121
			taxonName = taxonName.replace(_s_, subsp);
122
			logger.info("Taxon name: " +  taxonName);
123
		}
124
		return taxonName;
125
	}
126

    
127

    
128
	/**
129
	 * @param tree
130
	 * @param sourceRef
131
	 * @param name
132
	 * @param taxonName
133
	 * @param taxa
134
	 * @param taxon
135
	 */
136
	private void handleTaxa(Classification tree, Reference sourceRef, String fileName, String taxonName, List<TaxonBase> taxa) {
137

    
138
		Taxon taxon = getTaxon(tree, taxonName, taxa);
139
		TaxonDescription imageGallery = taxon.getOrCreateImageGallery(sourceRef == null ? null :sourceRef.getTitleCache());
140
		TextData textData = imageGallery.getOrCreateImageTextData();
141
		logger.info("Importing image for taxon: " + taxa);
142
		try {
143
			Media media = getMedia(fileName, taxonName);
144
			textData.addMedia(media);
145
		} catch (MalformedURLException e) {
146
			logger.error("Malformed URL", e);
147
		} catch (IOException e) {
148
			logger.error("IOException when handling image url");
149
		} catch (HttpException e) {
150
			logger.error("HttpException when handling image url");
151
		}
152
	}
153

    
154

    
155
	/**
156
	 * @param fileName
157
	 * @param taxonName
158
	 * @return
159
	 * @throws MalformedURLException
160
	 * @throws IOException
161
	 * @throws HttpException
162
	 */
163
	private Media getMedia(String fileName, String taxonName) throws MalformedURLException, IOException, HttpException {
164
		String urlPrefix = "http://media.bgbm.org/erez/erez?src=EditWP6/photos/";
165
		String urlString = urlPrefix + fileName;
166
		logger.info(urlString);
167
		URI uri = CdmUtils.string2Uri(urlString);
168
		ImageInfo imageMetaData =ImageInfo.NewInstance(uri, 0);
169

    
170
		String mimeType = imageMetaData.getMimeType();
171
		String suffix = null;
172
		int height = imageMetaData.getHeight();
173
		int width = imageMetaData.getWidth();
174
		Integer size = null;
175
		DateTime mediaCreated = null;
176
		AgentBase<?> artist = null;
177

    
178
//		ImageFile image = ImageFile.NewInstance(uri, size, height, width);
179
		Media media = ImageFile.NewMediaInstance(mediaCreated, artist, uri, mimeType, suffix, size, height, width);
180
		media.putTitle(LanguageString.NewInstance(taxonName, Language.LATIN()));
181

    
182
		return media;
183
	}
184

    
185
	/**
186
	 * @param tree
187
	 * @param taxonName
188
	 * @param taxa
189
	 * @return
190
	 */
191
	private Taxon getTaxon(Classification tree, String taxonName,
192
			List<TaxonBase> taxa) {
193
		Taxon taxon = null;
194
		if(taxa.size() > 1) {
195
			if (logger.isDebugEnabled()) {
196
				logger.debug("multiple taxa with this name found: " + taxonName);
197
			}
198
			for (TaxonBase taxonBase : taxa) {
199
				Taxon tax = (Taxon)taxonBase;
200
				if (tree.isTaxonInTree(tax)) {
201
					taxon = tax;
202
					break;
203
				}
204
			}
205
			if (taxon == null){
206
				taxon = (Taxon)taxa.get(0);
207
				logger.warn("Taxon not found in preferred tree. Use " + taxon.getTitleCache() + " instead.");
208
			}
209

    
210
		} else {
211
			taxon = (Taxon) taxa.get(0);
212
		}
213
		if (taxon != null){
214
			taxonService.saveOrUpdate(taxon);
215
		}else{
216
			logger.warn("Taxon was null. Did not save taxon");
217
		}
218
		return taxon;
219
	}
220
}
(2-2/6)