Project

General

Profile

Download (7.63 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.globis;
11

    
12
import java.net.MalformedURLException;
13
import java.net.URI;
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.Map;
19
import java.util.Set;
20

    
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.log4j.Logger;
23
import org.springframework.stereotype.Component;
24

    
25
import eu.etaxonomy.cdm.common.media.ImageInfo;
26
import eu.etaxonomy.cdm.io.common.IOValidator;
27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28
import eu.etaxonomy.cdm.io.globis.validation.GlobisCurrentSpeciesImportValidator;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.media.Media;
32
import eu.etaxonomy.cdm.model.name.Rank;
33
import eu.etaxonomy.cdm.model.name.ZoologicalName;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.taxon.Classification;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
40
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
41

    
42

    
43
/**
44
 * @author a.mueller
45
 * @created 20.02.2010
46
 * @version 1.0
47
 */
48
@Component
49
public class GlobisImageImport  extends GlobisImportBase<Taxon> {
50
	private static final Logger logger = Logger.getLogger(GlobisImageImport.class);
51
	
52
	private int modCount = 10000;
53
	private static final String pluralString = "images";
54
	private static final String dbTableName = "Einzelbilder";
55
	private static final Class cdmTargetClass = Media
56
	.class;  //not needed
57
	
58
	private static final String IMAGE_NAMESPACE = "Einzelbilder";
59
	
60
	public GlobisImageImport(){
61
		super(pluralString, dbTableName, cdmTargetClass);
62
	}
63

    
64

    
65
	
66
	
67
	/* (non-Javadoc)
68
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
69
	 */
70
	@Override
71
	protected String getIdQuery() {
72
		String strRecordQuery = 
73
			" SELECT BildId " + 
74
			" FROM " + dbTableName; 
75
		return strRecordQuery;	
76
	}
77

    
78

    
79

    
80

    
81
	/* (non-Javadoc)
82
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
83
	 */
84
	@Override
85
	protected String getRecordQuery(GlobisImportConfigurator config) {
86
		String strRecordQuery = 
87
			" SELECT i.*, NULL as Created_When, NULL as Created_Who," +
88
				"  NULL as Updated_who, NULL as Updated_When, NULL as Notes " + 
89
			" FROM " + getTableName() + " i " +
90
			" WHERE ( i.BildId IN (" + ID_LIST_TOKEN + ") )";
91
		return strRecordQuery;
92
	}
93
	
94

    
95

    
96
	/* (non-Javadoc)
97
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
98
	 */
99
	@Override
100
	public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
101
		boolean success = true;
102
		
103
		Set<Media> objectsToSave = new HashSet<Media>();
104
		
105
//		Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
106
//		Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
107
		
108
		ResultSet rs = partitioner.getResultSet();
109
		
110
		try {
111
			
112
			int i = 0;
113

    
114
			//for each reference
115
            while (rs.next()){
116
                
117
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
118
				
119
        		Integer bildID = rs.getInt("BildID");
120
        		
121
        		Integer spectaxID = nullSafeInt(rs, "spectaxID");
122
        		
123
        		//ignore: [file lab2], same as Dateiname04 but less data
124
        		
125
        		
126
        		
127
        		try {
128
					
129
        			//source ref
130
					Reference<?> sourceRef = state.getTransactionalSourceReference();
131
					
132
					//make image path
133
					String pathShort = rs.getString("Dateipfad_kurz");
134
					String fileOS = rs.getString("file OS");
135
					pathShort.replace(fileOS, "");
136
					//TODO move to config
137
					String newPath = "http://globis-images.insects-online.de/images/";
138
					String path = pathShort.replace("image:Webversionen", newPath);
139
					
140
					
141
					
142
					Media media1 = makeMedia(state, rs, "file OS", "Legende 1", path );
143
        			Media media2 = makeMedia(state, rs, "Dateiname02", "Legende 2", path );
144
        			Media media3 = makeMedia(state, rs, "Dateiname03", "Legende 3", path );
145
        			Media media4 = makeMedia(state, rs, "Dateiname04", "Legende 4", path );
146
        			
147
					
148
        			//TODO
149
					this.doIdCreatedUpdatedNotes(state, media1, rs, bildID, IMAGE_NAMESPACE);
150
					
151
					save(objectsToSave, media1);
152
					
153
										
154

    
155
				} catch (Exception e) {
156
					logger.warn("Exception in Einzelbilder: bildID " + bildID + ". " + e.getMessage());
157
//					e.printStackTrace();
158
				} 
159
                
160
            }
161
           
162
//            logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
163

    
164
			logger.warn(pluralString + " to save: " + objectsToSave.size());
165
			getMediaService().save(objectsToSave);	
166
			
167
			return success;
168
		} catch (SQLException e) {
169
			logger.error("SQLException:" +  e);
170
			return false;
171
		}
172
	}
173

    
174
	private void save(Set<Media> objectsToSave, Media media) {
175
		if (media != null){
176
			objectsToSave.add(media); 
177
		}
178
	}
179

    
180

    
181

    
182

    
183
	private Media makeMedia(GlobisImportState state, ResultSet rs, String fileNameAttr, String legendAttr, String path) throws SQLException {
184
		Media media = null;
185
		String fileName = rs.getString(fileNameAttr);
186
		String legend = rs.getString(legendAttr);
187
		
188
		URI uri = URI.create(path+fileName); 
189
		
190
//		Media media = ImageInfo.NewInstanceWithMetaData(uri, null);
191
		
192
		try {
193
			media = this.getImageMedia(uri.toString(), true, false);
194
			media.putTitle(Language.ENGLISH(), legend);
195
		} catch (MalformedURLException e) {
196
			e.printStackTrace();
197
		}
198
		
199
		return media;
200
	}
201

    
202

    
203

    
204

    
205
	private Taxon getTaxon(GlobisImportState state, ResultSet rs, String subGenus, Rank rank, String author, Map<String, Taxon> taxonMap) {
206
		String key = subGenus + "@" + "subGenusAuthor" + "@" + rank.getTitleCache();
207
		Taxon taxon = taxonMap.get(key);
208
		if (taxon == null){
209
			ZoologicalName name = ZoologicalName.NewInstance(rank);
210
			taxon = Taxon.NewInstance(name, state.getTransactionalSourceReference());
211
			handleAuthorAndYear(author, name);
212
			getTaxonService().save(taxon);
213
		}
214
		
215
		return taxon;
216
	}
217

    
218

    
219

    
220

    
221

    
222
	/* (non-Javadoc)
223
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
224
	 */
225
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
226
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
227
		return result;  //not needed
228
	}
229
	
230
	/* (non-Javadoc)
231
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
232
	 */
233
	@Override
234
	protected boolean doCheck(GlobisImportState state){
235
		IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
236
		return validator.validate(state);
237
	}
238
	
239
	
240
	/* (non-Javadoc)
241
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
242
	 */
243
	protected boolean isIgnore(GlobisImportState state){
244
		return ! state.getConfig().isDoImages();
245
	}
246

    
247

    
248

    
249

    
250

    
251
}
(2-2/9)