Project

General

Profile

Download (6.64 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.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.io.common.IOValidator;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.io.globis.validation.GlobisImageImportValidator;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.media.Media;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32

    
33

    
34
/**
35
 * @author a.mueller
36
 * @created 20.02.2010
37
 * @version 1.0
38
 */
39
@Component
40
public class GlobisImageImport  extends GlobisImportBase<Taxon> {
41
	private static final Logger logger = Logger.getLogger(GlobisImageImport.class);
42
	
43
	private int modCount = 10000;
44
	private static final String pluralString = "images";
45
	private static final String dbTableName = "Einzelbilder";
46
	private static final Class cdmTargetClass = Media
47
	.class;  //not needed
48
	
49
	private static final String IMAGE_NAMESPACE = "Einzelbilder";
50
	
51
	public GlobisImageImport(){
52
		super(pluralString, dbTableName, cdmTargetClass);
53
	}
54

    
55

    
56
	
57
	
58
	/* (non-Javadoc)
59
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
60
	 */
61
	@Override
62
	protected String getIdQuery() {
63
		String strRecordQuery = 
64
			" SELECT BildId " + 
65
			" FROM " + dbTableName; 
66
		return strRecordQuery;	
67
	}
68

    
69

    
70

    
71

    
72
	/* (non-Javadoc)
73
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
74
	 */
75
	@Override
76
	protected String getRecordQuery(GlobisImportConfigurator config) {
77
		String strRecordQuery = 
78
			" SELECT i.*, NULL as Created_When, NULL as Created_Who," +
79
				"  NULL as Updated_who, NULL as Updated_When, NULL as Notes " + 
80
			" FROM " + getTableName() + " i " +
81
			" WHERE ( i.BildId IN (" + ID_LIST_TOKEN + ") )";
82
		return strRecordQuery;
83
	}
84
	
85

    
86

    
87
	/* (non-Javadoc)
88
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
89
	 */
90
	@Override
91
	public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
92
		boolean success = true;
93
		
94
		Set<Media> objectsToSave = new HashSet<Media>();
95
		
96
//		Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
97
//		Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
98
		
99
		ResultSet rs = partitioner.getResultSet();
100
		
101
		try {
102
			
103
			int i = 0;
104

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

    
146
				} catch (Exception e) {
147
					logger.warn("Exception in Einzelbilder: bildID " + bildID + ". " + e.getMessage());
148
//					e.printStackTrace();
149
				} 
150
                
151
            }
152
           
153
//            logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
154

    
155
			logger.warn(pluralString + " to save: " + objectsToSave.size());
156
			getMediaService().save(objectsToSave);	
157
			
158
			return success;
159
		} catch (SQLException e) {
160
			logger.error("SQLException:" +  e);
161
			return false;
162
		}
163
	}
164

    
165
	private void save(Set<Media> objectsToSave, Media media) {
166
		if (media != null){
167
			objectsToSave.add(media); 
168
		}
169
	}
170

    
171

    
172

    
173

    
174
	private Media makeMedia(GlobisImportState state, ResultSet rs, String fileNameAttr, String legendAttr, String path) throws SQLException {
175
		Media media = null;
176
		String fileName = rs.getString(fileNameAttr);
177
		String legend = rs.getString(legendAttr);
178
		
179
		URI uri = URI.create(path+fileName); 
180
		
181
//		Media media = ImageInfo.NewInstanceWithMetaData(uri, null);
182
		
183
		try {
184
			media = this.getImageMedia(uri.toString(), true, false);
185
			media.putTitle(Language.ENGLISH(), legend);
186
		} catch (MalformedURLException e) {
187
			e.printStackTrace();
188
		}
189
		
190
		return media;
191
	}
192

    
193

    
194

    
195
	/* (non-Javadoc)
196
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
197
	 */
198
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
199
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
200
		return result;  //not needed
201
	}
202
	
203
	/* (non-Javadoc)
204
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
205
	 */
206
	@Override
207
	protected boolean doCheck(GlobisImportState state){
208
		IOValidator<GlobisImportState> validator = new GlobisImageImportValidator();
209
		return validator.validate(state);
210
	}
211
	
212
	
213
	/* (non-Javadoc)
214
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
215
	 */
216
	protected boolean isIgnore(GlobisImportState state){
217
		return ! state.getConfig().isDoImages();
218
	}
219

    
220

    
221

    
222

    
223

    
224
}
(2-2/9)