Project

General

Profile

Download (17.6 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.io.IOException;
13
import java.net.MalformedURLException;
14
import java.net.URI;
15
import java.sql.ResultSet;
16
import java.sql.SQLException;
17
import java.util.HashMap;
18
import java.util.HashSet;
19
import java.util.Map;
20
import java.util.Set;
21
import java.util.UUID;
22
import java.util.regex.Matcher;
23
import java.util.regex.Pattern;
24

    
25
import org.apache.http.client.ClientProtocolException;
26
import org.apache.log4j.Logger;
27
import org.springframework.stereotype.Component;
28

    
29
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
30
import eu.etaxonomy.cdm.common.UriUtils;
31
import eu.etaxonomy.cdm.io.common.IOValidator;
32
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
33
import eu.etaxonomy.cdm.io.globis.validation.GlobisImageImportValidator;
34
import eu.etaxonomy.cdm.model.common.Annotation;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.Language;
37
import eu.etaxonomy.cdm.model.common.Marker;
38
import eu.etaxonomy.cdm.model.common.MarkerType;
39
import eu.etaxonomy.cdm.model.media.Media;
40
import eu.etaxonomy.cdm.model.name.ZoologicalName;
41
import eu.etaxonomy.cdm.model.occurrence.Collection;
42
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
43
import eu.etaxonomy.cdm.model.occurrence.Specimen;
44
import eu.etaxonomy.cdm.model.reference.Reference;
45
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47

    
48

    
49
/**
50
 * @author a.mueller
51
 * @created 20.02.2010
52
 * @version 1.0
53
 */
54
@Component
55
public class GlobisImageImport  extends GlobisImportBase<Taxon> {
56
	private static final Logger logger = Logger.getLogger(GlobisImageImport.class);
57
	
58
	private int modCount = 1000;
59

    
60
	private UUID uuidArtNonSpecTaxMarkerType = UUID.fromString("be362085-0f5b-4314-96d1-78b9b129ef6d") ;
61
	private static final String pluralString = "images";
62
	private static final String dbTableName = "Einzelbilder";
63
	private static final Class cdmTargetClass = Media.class;  //not needed
64
	
65
	private static UUID uuidGartRef = UUID.fromString("af85470f-6e54-4304-9d29-fd117cd56161"); 
66
	
67
	public GlobisImageImport(){
68
		super(pluralString, dbTableName, cdmTargetClass);
69
	}
70

    
71

    
72
	
73
	
74
	/* (non-Javadoc)
75
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
76
	 */
77
	@Override
78
	protected String getIdQuery() {
79
		String strRecordQuery = 
80
			" SELECT BildId " + 
81
			" FROM " + dbTableName; 
82
		return strRecordQuery;	
83
	}
84

    
85

    
86

    
87

    
88
	/* (non-Javadoc)
89
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
90
	 */
91
	@Override
92
	protected String getRecordQuery(GlobisImportConfigurator config) {
93
		String strRecordQuery = 
94
			" SELECT i.*, NULL as Created_When, NULL as Created_Who," +
95
				"  NULL as Updated_who, NULL as Updated_When, NULL as Notes, st.SpecCurrspecID " + 
96
			" FROM " + getTableName() + " i " +
97
				" LEFT JOIN specTax st ON i.spectaxID = st.SpecTaxID " +
98
			" WHERE ( i.BildId IN (" + ID_LIST_TOKEN + ") )";
99
		return strRecordQuery;
100
	}
101
	
102

    
103

    
104
	/* (non-Javadoc)
105
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
106
	 */
107
	@Override
108
	public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
109
		boolean success = true;
110
		
111
		Set<Media> objectsToSave = new HashSet<Media>();
112
		
113
		Map<String, Specimen> typeMap = (Map<String, Specimen>) partitioner.getObjectMap(TYPE_NAMESPACE);
114
		
115
		Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
116
		Map<String, ZoologicalName> specTaxNameMap = (Map<String, ZoologicalName>) partitioner.getObjectMap(SPEC_TAX_NAMESPACE);
117
		
118
		ResultSet rs = partitioner.getResultSet();
119
		
120
		Reference<?> refGart = getReferenceService().find(uuidGartRef);
121
		
122
		
123
		try {
124
			
125
			int i = 0;
126

    
127
			//for each record
128
            while (rs.next()){
129
                
130
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
131
				
132
        		Integer bildID = rs.getInt("BildID");
133
        		Integer spectaxID = nullSafeInt(rs, "spectaxID");
134
        		Integer taxonID = nullSafeInt(rs, "SpecCurrspecID");
135
        		String copyright = rs.getString("copyright");
136
        		String specimenId = rs.getString("specimenID");
137
        		String bemerkungen = rs.getString("Bemerkungen");
138
        		String artNotSpecTax = rs.getString("Art non spectax");
139
        		String motiv = rs.getString("Motiv");
140
        		
141
        		//ignore: 
142
        		//	[file lab2], same as Dateiname04 but less data
143
        		//	Dateipfad
144

    
145
        		Set<Media> recordMedia = new HashSet<Media>();
146
        		
147
        		try {
148
					
149
        			makeAllMedia(state, rs, recordMedia, objectsToSave);
150
        			
151
        			String title = null;
152
        			
153
        			Specimen specimen = null;
154
        			if (spectaxID != null){
155
        				//try to find type specimen
156
        				if (isNotBlank(motiv) && (motiv.startsWith("type specimen"))){
157
	        				String collectionCode = transformCopyright2CollectionCode(copyright);
158
	    					String id = GlobisSpecTaxImport.getTypeId(spectaxID, collectionCode);
159
	    					specimen = typeMap.get(id);
160
        				}
161
        				
162
    					//try to find specTaxName
163
        				ZoologicalName specTaxTaxonName = specTaxNameMap.get(String.valueOf(spectaxID));
164
            			if (specTaxTaxonName != null){
165
            				title = " taxon name " + specTaxTaxonName.getTitleCache();
166
            			}else{
167
            				title = " spectaxID " + spectaxID;
168
            			}
169
    				}else{
170
    					title = " name " + getNameFromFileOs(rs) + (isBlank(specimenId)? "" : " (specimenId: " + specimenId + ")");
171
    				}
172
        			
173
        			//not type specimen
174
        			if (specimen == null){
175
						specimen = Specimen.NewInstance();
176
						specimen.setTitleCache("Specimen for " + title );
177
						String collectionCode = transformCopyright2CollectionCode(copyright);
178
						//TODO
179
						Collection collection = getCollection(collectionCode);
180
						specimen.setCollection(collection);
181
					}
182
					
183
					
184
					//source
185
					specimen.addSource(String.valueOf(bildID), IMAGE_NAMESPACE, state.getTransactionalSourceReference(), null);
186
					
187
					//GART id (specimenID)
188
					if (isNotBlank(specimenId)){
189
						specimen.addSource(specimenId, "specimenId", refGart, null);
190
					}
191
					//bemerkungen
192
					if (isNotBlank(bemerkungen)){
193
						Annotation annotation = Annotation.NewInstance(bemerkungen, null, null);
194
						specimen.addAnnotation(annotation);
195
					}
196
					//media
197
					DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
198
					for (Media media: recordMedia){
199
						facade.addDerivedUnitMedia(media);
200
					}
201
					//art non spectax
202
					if (isNotBlank(artNotSpecTax)){
203
						if (artNotSpecTax.equalsIgnoreCase("ja")){
204
							MarkerType artNotSpecTaxMarker = getMarkerType(state, uuidArtNonSpecTaxMarkerType  , "Art non spectax", "This marker is true if in the orginal data the 'Art non spectax' was 'ja'", null) ;
205
							specimen.addMarker(Marker.NewInstance(artNotSpecTaxMarker, true));
206
						}else{
207
							logger.warn(artNotSpecTax + " is not a valid value for 'Art non spectax' (BildID: " + bildID + ")" );
208
						}
209
					}
210
        			
211
					if (spectaxID != null){
212
						
213
						//add to image gallery (discuss if this is also needed if taxon is already added to type specimen
214
//						Taxon taxon = taxonMap.get(String.valueOf(taxonID));
215
						ZoologicalName specTaxTaxonName = specTaxNameMap.get(String.valueOf(spectaxID));
216
						
217
//						
218
//						if (taxon == null){
219
////							taxon = specTaxMap.get(String.valueOf(spectaxID));
220
////							specTaxName = specTaxMap.g
221
//						}
222
//						if (taxon == null){
223
//							logger.warn("No taxon available for specTaxID: " +  spectaxID);
224
//						}else{
225
//							name = CdmBase.deproxy(taxon.getName(), ZoologicalName.class);
226
//						}
227
						
228
						//TODO FIXME
229
						
230
						if (specTaxTaxonName == null){
231
							logger.warn("Name could not be found for spectaxID: " + spectaxID +  " in BildID: " + bildID);
232
						}else{
233
							Taxon taxon = null;
234
							for (Taxon specTaxTaxon: specTaxTaxonName.getTaxa()){
235
								taxon = specTaxTaxon;
236
							}
237
							if (taxon == null){
238
								//FIXME
239
								Reference<?> undefinedSec = null;
240
								taxon = Taxon.NewInstance(specTaxTaxonName, undefinedSec);
241
							}
242
							
243
							DeterminationEvent.NewInstance(taxon, specimen);
244

    
245
						}
246
						
247
						
248

    
249
						
250
//						if (taxon != null){
251
//							TaxonDescription taxonDescription = getTaxonDescription(taxon, true, true);
252
//							if (taxonDescription.getElements().size() == 0){
253
//								TextData textData = TextData.NewInstance(Feature.IMAGE());
254
//								taxonDescription.addElement(textData);
255
//							}
256
//							Set<DescriptionElementBase> elements = taxonDescription.getElements();
257
//							TextData textData = CdmBase.deproxy(elements.iterator().next(), TextData.class);
258
//							for (Media media: recordMedia){
259
//								textData.addMedia(media);
260
//							}
261
//						}
262
					}
263
					
264
				} catch (Exception e) {
265
					logger.warn("Exception in Einzelbilder: bildID " + bildID + ". " + e.getMessage());
266
					e.printStackTrace();
267
				} 
268
                
269
            }
270
           
271
			logger.info(pluralString + " to save: " + objectsToSave.size());
272
			getMediaService().save(objectsToSave);	
273
			
274
			return success;
275
		} catch (SQLException e) {
276
			logger.error("SQLException:" +  e);
277
			return false;
278
		}
279
	}
280
	
281
	private Collection getCollection(String collectionCode) {
282
		//TODO
283
		return null;
284
	}
285

    
286

    
287

    
288

    
289
	private String getNameFromFileOs(ResultSet rs) throws SQLException {
290
		String fileOS = rs.getString("file OS");
291
		Pattern pattern = Pattern.compile("(.+)(_.{4}(-.{1,3})?(_Nr\\d{3,4})?_.{2,3}\\.jpg)");
292
		Matcher matcher = pattern.matcher(fileOS);
293
		if (matcher.matches()){
294
			String match = matcher.group(1);
295
			return match;
296
		}else{
297
			logger.warn("FileOS does not match: " +  fileOS);
298
			return fileOS;
299
		}
300
	}
301

    
302

    
303

    
304

    
305
	private void makeAllMedia(GlobisImportState state, ResultSet rs, Set<Media> recordMedia, Set<Media> objectsToSave) throws SQLException{
306
			//make image path
307
		String pathShort = rs.getString("Dateipfad_kurz");
308
		String fileOS = rs.getString("file OS");
309
		pathShort= pathShort.replace(fileOS, "");
310
		String newPath = state.getConfig().getImageBaseUrl();
311
		String path = pathShort.replace("image:Webversionen/", newPath);
312
		
313
		Media singleMedia = makeMedia(state, rs, "file OS", "Legende 1", path, objectsToSave );
314
		recordMedia.add(singleMedia);
315
		singleMedia = makeMedia(state, rs, "Dateinamen02", "Legende 2", path, objectsToSave );
316
		recordMedia.add(singleMedia);
317
		singleMedia = makeMedia(state, rs, "Dateinamen03", "Legende 3", path, objectsToSave );
318
		recordMedia.add(singleMedia);
319
		singleMedia = makeMedia(state, rs, "Dateinamen04", "Legende 4", path, objectsToSave );
320
		recordMedia.add(singleMedia);
321

    
322
	}
323

    
324
	private Media makeMedia(GlobisImportState state, ResultSet rs, String fileNameAttr, String legendAttr, String path, Set<Media> objectsToSave) throws SQLException {
325
		Media media = null;
326
		String fileName = rs.getString(fileNameAttr);
327
		String legend = rs.getString(legendAttr);
328
		Integer bildID = rs.getInt("BildID");
329
		
330
		String uriStr = path+fileName;
331
		uriStr = uriStr.replace(" ", "%20");
332
		
333
		URI uri = URI.create(uriStr); 
334
		
335
//		Media media = ImageInfo.NewInstanceWithMetaData(uri, null);
336
		
337
		try {
338
			boolean readMediaData = state.getConfig().isDoReadMediaData();
339
			if (isBlank(legend) && readMediaData){
340
				if (UriUtils.isOk(UriUtils.getResponse(uri, null))){
341
					logger.warn("Image exists but legend is null " + uri + ", bildID" + bildID );
342
				}else{
343
					return null;
344
				}
345
			}
346
			
347
			media = this.getImageMedia(uri.toString(), readMediaData, false);
348
			media.putTitle(Language.ENGLISH(), legend);
349
			this.doIdCreatedUpdatedNotes(state, media, rs, bildID, IMAGE_NAMESPACE);
350
			
351
			objectsToSave.add(media);
352
			
353
			
354
		} catch (MalformedURLException e) {
355
			e.printStackTrace();
356
		} catch (ClientProtocolException e) {
357
			e.printStackTrace();
358
		} catch (IOException e) {
359
			e.printStackTrace();
360
		}
361
		
362
		return media;
363
	}
364
	
365
	private String transformCopyright2CollectionCode(String copyright){
366
		
367
		if (isBlank(copyright)){
368
			return "";
369
		}else if(copyright.matches("Museum f.?r Naturkunde der Humboldt-Universit.?t, Berlin")){
370
			return "MFNB";
371
		}else if(copyright.matches("Staatliches Museum f.?r Tierkunde Dresden")){
372
			return "SMTD";
373
		}else if(copyright.equals("Natural History Museum, London")){
374
			return "BMNH";
375
		}else if(copyright.matches("Zoologische Staatssammlung M.?nchen")){
376
			return "ZSSM";
377
		}else if(copyright.matches("Staatliches Museum f.?r Naturkunde Karlsruhe")){
378
			return "SMNK";
379
		}else if(copyright.matches("Deutsches Entomologisches Institut M.?ncheberg")){
380
			return "DEIE";
381
		}else if(copyright.equals("Forschungsinstitut und Naturmuseum Senckenberg")){
382
			return "SMFM";
383
		}else if(copyright.matches("Mus.?um National d.?Histoire Naturelle, Paris")){
384
			return "MNHN";
385
		}else if(copyright.equals("Naturhistorisches Museum Wien")){
386
			return "NHMW";
387
		}else if(copyright.equals("Naturhistoriska Riksmuseet Stockholm")){
388
			return "NRMS";
389
		}else if(copyright.matches("Staatliches Museum f.?r Naturkunde Stuttgart")){
390
			return "SMNS";
391
		}else if(copyright.equals("United States National Museum of Natural History, Washington")){
392
			return "USNM";
393
		}else if(copyright.matches("Zentrum f.?r Biodokumentation des Saarlandes")){
394
			return "ZFBS";
395
		}else if(copyright.equals("Zoological Museum, University of Copenhagen")){
396
			return "ZMUC";
397
		}else if(copyright.equals("Zoologisches Forschungsinstitut und Museum \"Alexander Koenig\", Bonn")){
398
			return "ZFMK";
399
		}else if(copyright.equals("Zoologisches Forschungsmuseum \"Alexander Koenig\", Bonn")){
400
			return "ZFMK";
401
		}else if(copyright.matches("Zoologisches Institut der Martin-Luther-Universit.?t Halle-Wittenberg")){
402
			return "ZIUH";
403
		}else if(copyright.matches("Zoologisches Institut Universit.?t T.?bingen")){
404
			return "ZIUT";
405
		}else{
406
			logger.warn("Unknown copyright entry: " + copyright);
407
			return "";
408
		}
409

    
410
	
411
	}
412

    
413

    
414

    
415
	/* (non-Javadoc)
416
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
417
	 */
418
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
419
		String nameSpace;
420
		Class cdmClass;
421
		Set<String> idSet;
422
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
423
		try{
424
			Set<String> currSpecIdSet = new HashSet<String>();
425
			Set<String> specTaxIdSet = new HashSet<String>();
426
			Set<String> typeIdSet = new HashSet<String>();
427
			
428
			while (rs.next()){
429
				handleForeignKey(rs, currSpecIdSet, "SpecCurrspecID");
430
				handleForeignKey(rs, specTaxIdSet, "spectaxID");
431
				handleTypeKey(rs, typeIdSet, "spectaxID", "copyright");
432
			}
433
			
434
			//specTax map
435
			nameSpace = SPEC_TAX_NAMESPACE;
436
			cdmClass = ZoologicalName.class;
437
			idSet = specTaxIdSet;
438
			Map<String, ZoologicalName> specTaxNameMap = (Map<String, ZoologicalName>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
439
			result.put(nameSpace, specTaxNameMap);
440

    
441
//			//taxon map
442
//			nameSpace = TAXON_NAMESPACE;
443
//			cdmClass = Taxon.class;
444
//			idSet = currSpecIdSet;
445
//			Map<String, Taxon> taxonMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
446
//			result.put(nameSpace, taxonMap);
447

    
448
			
449
			//type map
450
			nameSpace = GlobisSpecTaxImport.TYPE_NAMESPACE;
451
			cdmClass = Specimen.class;
452
			idSet = typeIdSet;
453
			Map<String, Specimen> typeMap = (Map<String, Specimen>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
454
			result.put(nameSpace, typeMap);
455
			
456
			
457
		} catch (SQLException e) {
458
			throw new RuntimeException(e);
459
		}
460
		return result;
461
	}
462
	
463
	private void handleTypeKey(ResultSet rs, Set<String> idSet, String specTaxIdAttr, String copyrightAttr) throws SQLException {
464
		Integer specTaxId = nullSafeInt(rs, specTaxIdAttr);
465
		if (specTaxId != null){
466
			String copyright = rs.getString(copyrightAttr);
467
			if (isNotBlank(copyright)){
468
				String id  = GlobisSpecTaxImport.getTypeId(specTaxId, transformCopyright2CollectionCode(copyright));
469
				idSet.add(id);
470
			}
471
		}
472
	}
473
	
474
	/* (non-Javadoc)
475
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
476
	 */
477
	@Override
478
	protected boolean doCheck(GlobisImportState state){
479
		IOValidator<GlobisImportState> validator = new GlobisImageImportValidator();
480
		return validator.validate(state);
481
	}
482
	
483
	
484
	/* (non-Javadoc)
485
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
486
	 */
487
	protected boolean isIgnore(GlobisImportState state){
488
		return ! state.getConfig().isDoImages();
489
	}
490

    
491

    
492

    
493

    
494
	/* (non-Javadoc)
495
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doInvoke(eu.etaxonomy.cdm.io.globis.GlobisImportState)
496
	 */
497
	@Override
498
	protected void doInvoke(GlobisImportState state) {
499
		Reference refGart = ReferenceFactory.newGeneric();
500
		refGart.setTitleCache("GART");
501
		refGart.setUuid(uuidGartRef);
502
		getReferenceService().saveOrUpdate(refGart);
503
		super.doInvoke(state);
504
	}
505

    
506

    
507

    
508

    
509

    
510
}
(3-3/9)