Project

General

Profile

Download (16.5 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.berlinModel.in;
11

    
12
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.NAME_FACT_ALSO_PUBLISHED_IN;
13
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.NAME_FACT_BIBLIOGRAPHY;
14
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.NAME_FACT_PROTOLOGUE;
15

    
16
import java.io.File;
17
import java.io.IOException;
18
import java.net.MalformedURLException;
19
import java.net.URI;
20
import java.net.URISyntaxException;
21
import java.net.URL;
22
import java.sql.ResultSet;
23
import java.sql.SQLException;
24
import java.util.HashMap;
25
import java.util.HashSet;
26
import java.util.Map;
27
import java.util.Set;
28

    
29
import org.apache.commons.lang.StringUtils;
30
import org.apache.http.HttpException;
31
import org.apache.log4j.Logger;
32
import org.springframework.stereotype.Component;
33

    
34
import eu.etaxonomy.cdm.common.CdmUtils;
35
import eu.etaxonomy.cdm.common.media.ImageInfo;
36
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelNameFactsImportValidator;
37
import eu.etaxonomy.cdm.io.common.IOValidator;
38
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
39
import eu.etaxonomy.cdm.model.common.CdmBase;
40
import eu.etaxonomy.cdm.model.common.Language;
41
import eu.etaxonomy.cdm.model.description.Feature;
42
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
43
import eu.etaxonomy.cdm.model.description.TextData;
44
import eu.etaxonomy.cdm.model.media.ImageFile;
45
import eu.etaxonomy.cdm.model.media.Media;
46
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
47
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
48
import eu.etaxonomy.cdm.model.name.TaxonName;
49
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
50
import eu.etaxonomy.cdm.model.reference.Reference;
51

    
52

    
53
/**
54
 * @author a.mueller
55
 * @since 20.03.2008
56
 */
57
@Component
58
public class BerlinModelNameFactsImport  extends BerlinModelImportBase  {
59
    private static final long serialVersionUID = 4174085686074314138L;
60

    
61
    private static final Logger logger = Logger.getLogger(BerlinModelNameFactsImport.class);
62

    
63
	public static final String NAMESPACE = "NameFact";
64

    
65
	/**
66
	 * write info message after modCount iterations
67
	 */
68
	private int modCount = 500;
69
	private static final String pluralString = "name facts";
70
	private static final String dbTableName = "NameFact";
71

    
72

    
73
	public BerlinModelNameFactsImport(){
74
		super(dbTableName, pluralString);
75
	}
76

    
77

    
78
	@Override
79
	protected String getIdQuery(BerlinModelImportState state) {
80
		if (isNotBlank(state.getConfig().getNameIdTable())){
81
			String result = super.getIdQuery(state);
82
			result += " WHERE ptNameFk IN (SELECT NameId FROM " + state.getConfig().getNameIdTable() + ")";
83
			if (state.getConfig().isEuroMed()){
84
			    result += " AND NOT (NameFactCategoryFk = 3 AND NameFactRefFk = 8500000) ";  //#7796#note-11
85
			}
86
			return result;
87
		}else{
88
			return super.getIdQuery(state);
89
		}
90
	}
91

    
92
	@Override
93
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
94
		String strQuery =
95
			" SELECT NameFact.*, Name.NameID as nameId, NameFactCategory.NameFactCategory " +
96
			" FROM NameFact INNER JOIN " +
97
              	" Name ON NameFact.PTNameFk = Name.NameId  INNER JOIN "+
98
              	" NameFactCategory ON NameFactCategory.NameFactCategoryID = NameFact.NameFactCategoryFK " +
99
            " WHERE (NameFactId IN ("+ ID_LIST_TOKEN+") )";
100
		return strQuery;
101
	}
102

    
103
	@Override
104
    public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, BerlinModelImportState state) {
105
		boolean success = true ;
106
		BerlinModelImportConfigurator config = state.getConfig();
107
		Set<TaxonName> nameToSave = new HashSet<>();
108
		@SuppressWarnings("unchecked")
109
        Map<String, TaxonName> nameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
110
		@SuppressWarnings("unchecked")
111
        Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
112

    
113
		ResultSet rs = partitioner.getResultSet();
114

    
115
		Reference sourceRef = state.getTransactionalSourceReference();
116
		try {
117
			int i = 0;
118
			//for each reference
119
			while (rs.next() && (config.getMaximumNumberOfNameFacts() == 0 || i < config.getMaximumNumberOfNameFacts())){
120

    
121
				if ((i++ % modCount) == 0  && i!= 1 ){ logger.info("NameFacts handled: " + (i-1));}
122

    
123
				int nameFactId = rs.getInt("nameFactId");
124
				int nameId = rs.getInt("nameId");
125
				Object nameFactRefFkObj = rs.getObject("nameFactRefFk");
126
				String nameFactRefDetail = rs.getString("nameFactRefDetail");
127

    
128
				String category = CdmUtils.Nz(rs.getString("NameFactCategory"));
129
				String nameFact = CdmUtils.Nz(rs.getString("nameFact"));
130

    
131
				TaxonName taxonNameBase = nameMap.get(String.valueOf(nameId));
132
				String nameFactRefFk = String.valueOf(nameFactRefFkObj);
133
				Reference citation = refMap.get(nameFactRefFk);
134

    
135
				if (taxonNameBase != null){
136
					//PROTOLOGUE
137
					if (category.equalsIgnoreCase(NAME_FACT_PROTOLOGUE)){
138
						//Reference ref = (Reference)taxonNameBase.getNomenclaturalReference();
139
						//ref = Book.NewInstance();
140
						try{
141
							Media media = getMedia(nameFact, config.getMediaUrl(), config.getMediaPath());
142
							if (media.getRepresentations().size() > 0){
143
								TaxonNameDescription description = TaxonNameDescription.NewInstance();
144
								TextData protolog = TextData.NewInstance(Feature.PROTOLOGUE());
145
								protolog.addMedia(media);
146
								protolog.addSource(OriginalSourceType.Import, String.valueOf(nameFactId), NAMESPACE, null, null, null, null);
147
								description.addElement(protolog);
148
								taxonNameBase.addDescription(description);
149
								if (citation != null){
150
									description.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, null);
151
									protolog.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, nameFactRefDetail, null, null);
152
								}
153
							}//end NAME_FACT_PROTOLOGUE
154
						}catch(NullPointerException e){
155
							logger.warn("MediaUrl and/or MediaPath not set. Could not get protologue.");
156
							success = false;
157
						}
158
					}else if (category.equalsIgnoreCase(NAME_FACT_ALSO_PUBLISHED_IN)){
159
						if (StringUtils.isNotBlank(nameFact)){
160
							TaxonNameDescription description = TaxonNameDescription.NewInstance();
161
							TextData additionalPublication = TextData.NewInstance(Feature.ADDITIONAL_PUBLICATION());
162
							//TODO language
163
							Language language = Language.DEFAULT();
164
							additionalPublication.putText(language, nameFact);
165
							additionalPublication.addSource(OriginalSourceType.Import, String.valueOf(nameFactId), NAMESPACE, null,null, null, null);
166
							if (citation != null || isNotBlank(nameFactRefDetail)){
167
								additionalPublication.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, nameFactRefDetail, null, null);
168
							}
169
							description.addElement(additionalPublication);
170
							taxonNameBase.addDescription(description);
171
						}
172
					}else if (category.equalsIgnoreCase(NAME_FACT_BIBLIOGRAPHY)){
173
						if (isNotBlank(nameFact)){
174
							TaxonNameDescription description = TaxonNameDescription.NewInstance();
175
							TextData bibliography = TextData.NewInstance(Feature.CITATION());
176
							//TODO language
177
							Language language = Language.DEFAULT();
178
							bibliography.putText(language, nameFact);
179
							bibliography.addSource(OriginalSourceType.Import, String.valueOf(nameFactId), NAMESPACE, null,null, null, null);
180
							if (citation != null || isNotBlank(nameFactRefDetail)){
181
								bibliography.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, nameFactRefDetail, null, null);
182
							}
183
							description.addElement(bibliography);
184
							taxonNameBase.addDescription(description);
185
						}
186
					}else {
187
						//TODO
188
						logger.warn("NameFactCategory '" + category + "' not yet implemented");
189
						success = false;
190
					}
191

    
192
					//TODO
193
//					DoubtfulFlag    bit        Checked
194
//					PublishFlag      bit        Checked
195
//					Created_When  datetime           Checked
196
//					Updated_When datetime           Checked
197
//					Created_Who    nvarchar(255)    Checked
198
//					Updated_Who  nvarchar(255)    Checked
199
//					Notes      nvarchar(1000)           Checked
200

    
201
					nameToSave.add(taxonNameBase);
202
				}else{
203
					//TODO
204
					logger.warn("TaxonName for NameFact " + nameFactId + " does not exist in store");
205
					success = false;
206
				}
207
				//put
208
			}
209
			if (config.getMaximumNumberOfNameFacts() != 0 && i >= config.getMaximumNumberOfNameFacts() - 1){
210
				logger.warn("ONLY " + config.getMaximumNumberOfNameFacts() + " NAMEFACTS imported !!!" )
211
			;}
212
			logger.info("Names to save: " + nameToSave.size());
213
			getNameService().save(nameToSave);
214
			return success;
215
		} catch (SQLException e) {
216
			logger.error("SQLException:" +  e);
217
			return false;
218
		}
219
	}
220

    
221
	@Override
222
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
223

    
224
	    String nameSpace;
225
		Set<String> idSet;
226
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
227

    
228
		try{
229
			Set<String> nameIdSet = new HashSet<>();
230
			Set<String> referenceIdSet = new HashSet<>();
231
			while (rs.next()){
232
				handleForeignKey(rs, nameIdSet, "PTnameFk");
233
				handleForeignKey(rs, referenceIdSet, "nameFactRefFk");
234
			}
235

    
236
			//name map
237
			nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
238
			idSet = nameIdSet;
239
			Map<String, TaxonName> objectMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonName.class, idSet, nameSpace);
240
			result.put(nameSpace, objectMap);
241

    
242
			//reference map
243
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
244
			idSet = referenceIdSet;
245
			Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
246
			result.put(nameSpace, referenceMap);
247

    
248

    
249
		} catch (SQLException e) {
250
			throw new RuntimeException(e);
251
		}
252
		return result;
253
	}
254

    
255

    
256
	//FIXME gibt es da keine allgemeine Methode in common?
257
	public Media getMedia(String nameFact, URL mediaUrl, File mediaPath){
258
		if (mediaUrl == null){
259
			logger.warn("Media Url should not be null");
260
			return null;
261
		}
262
		String mimeTypeTif = "image/tiff";
263
		String mimeTypeJpg = "image/jpeg";
264
		String mimeTypePng = "image/png";
265
		String mimeTypePdf = "application/pdf";
266
		String suffixTif = "tif";
267
		String suffixJpg = "jpg";
268
		String suffixPng = "png";
269
		String suffixPdf = "pdf";
270

    
271
		String sep = File.separator;
272
		Integer size = null;
273

    
274
		logger.debug("Getting media for NameFact: " + nameFact);
275

    
276
		Media media = Media.NewInstance();
277

    
278
		String mediaUrlString = mediaUrl.toString();
279

    
280
		//tiff
281
		String urlStringTif = mediaUrlString + "tif/" + nameFact + "." + suffixTif;
282
		File file = new File(mediaPath, "tif" + sep + nameFact + "." + suffixTif);
283
		MediaRepresentation representationTif = MediaRepresentation.NewInstance(mimeTypeTif, suffixTif);
284
		if (file.exists()){
285
			representationTif.addRepresentationPart(makeImage(urlStringTif, size, file));
286
		}
287
		if(representationTif.getParts().size() > 0){
288
			media.addRepresentation(representationTif);
289
		}
290
		// end tif
291
		// jpg
292
		boolean fileExists = true;
293
		int jpgCount = 0;
294
		MediaRepresentation representationJpg = MediaRepresentation.NewInstance(mimeTypeJpg, suffixJpg);
295
		while(fileExists){
296
			String urlStringJpeg = mediaUrlString + "cmd_jpg/" + nameFact + "_page_000" + jpgCount + "." + suffixJpg;
297
			file = new File(mediaPath, "cmd_jpg" + sep + nameFact + "_page_000" + jpgCount + "." + suffixJpg);
298
			jpgCount++;
299
			if (file.exists()){
300
				representationJpg.addRepresentationPart(makeImage(urlStringJpeg, size, file));
301
			}else{
302
				fileExists = false;
303
			}
304
		}
305
		if(representationJpg.getParts().size() > 0){
306
			media.addRepresentation(representationJpg);
307
		}
308
		// end jpg
309
		//png
310
		String urlStringPng = mediaUrlString + "png/" + nameFact + "." + suffixPng;
311
		file = new File(mediaPath, "png" + sep + nameFact + "." + suffixPng);
312
		MediaRepresentation representationPng = MediaRepresentation.NewInstance(mimeTypePng, suffixPng);
313
		if (file.exists()){
314
			representationPng.addRepresentationPart(makeImage(urlStringPng, size, file));
315
		}else{
316
			fileExists = true;
317
			int pngCount = 0;
318
			while (fileExists){
319
				pngCount++;
320
				urlStringPng = mediaUrlString + "png/" + nameFact + "00" + pngCount + "." + suffixPng;
321
				file = new File(mediaPath, "png" + sep + nameFact + "00" + pngCount + "." + suffixPng);
322

    
323
				if (file.exists()){
324
					representationPng.addRepresentationPart(makeImage(urlStringPng, size, file));
325
				}else{
326
					fileExists = false;
327
				}
328
			}
329
		}
330
		if(representationPng.getParts().size() > 0){
331
			media.addRepresentation(representationPng);
332
		}
333
		//end png
334
        //pdf
335
        String urlStringPdf = mediaUrlString + "pdf/" + nameFact + "." + suffixPdf;
336
        URI uriPdf;
337
		try {
338
			uriPdf = new URI(urlStringPdf);
339
			file = new File(mediaPath, "pdf" + sep + nameFact + "." + suffixPdf);
340
	        MediaRepresentation representationPdf = MediaRepresentation.NewInstance(mimeTypePdf, suffixPdf);
341
	        if (file.exists()){
342
	                representationPdf.addRepresentationPart(MediaRepresentationPart.NewInstance(uriPdf, size));
343
	        }else{
344
	                fileExists = true;
345
	                int pdfCount = 0;
346
	                while (fileExists){
347
	                        pdfCount++;
348
	                        urlStringPdf = mediaUrlString + "pdf/" + nameFact + "00" + pdfCount + "." + suffixPdf;
349
	                        file = new File(mediaPath, "pdf/" + sep + nameFact + "00" + pdfCount + "." + suffixPdf);
350

    
351
	                        if (file.exists()){
352
	                                representationPdf.addRepresentationPart(MediaRepresentationPart.NewInstance(uriPdf, size));
353
	                        }else{
354
	                                fileExists = false;
355
	                        }
356
	                }
357
	        }
358
			if(representationPdf.getParts().size() > 0){
359
	        	media.addRepresentation(representationPdf);
360
	        }
361
		} catch (URISyntaxException e) {
362
			e.printStackTrace();
363
			logger.error("URISyntaxException" + urlStringPdf);
364
		}
365
        //end pdf
366

    
367
		if(logger.isDebugEnabled()){
368
			for (MediaRepresentation rep : media.getRepresentations()){
369
				for (MediaRepresentationPart part : rep.getParts()){
370
					logger.debug("in representation: " + part.getUri());
371
				}
372
			}
373
		}
374

    
375
		return media;
376
	}
377

    
378

    
379
	private ImageFile makeImage(String imageUri, Integer size, File file){
380
		ImageInfo imageMetaData = null;
381
		URI uri;
382
		try {
383
			uri = new URI(imageUri);
384
			try {
385
				imageMetaData = ImageInfo.NewInstance(uri, 0);
386
			} catch (IOException e) {
387
				logger.error("IOError reading image metadata." , e);
388
			} catch (HttpException e) {
389
				logger.error("HttpException reading image metadata." , e);
390
			}
391
			ImageFile image = ImageFile.NewInstance(uri, size, imageMetaData);
392
			return image;
393
		} catch (URISyntaxException e1) {
394
			logger.warn("URISyntaxException: " + imageUri);
395
			return null;
396
		}
397

    
398
	}
399

    
400

    
401
	/* (non-Javadoc)
402
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
403
	 */
404
	@Override
405
	protected boolean doCheck(BerlinModelImportState state){
406
		IOValidator<BerlinModelImportState> validator = new BerlinModelNameFactsImportValidator();
407
		return validator.validate(state);
408
	}
409

    
410

    
411
	/* (non-Javadoc)
412
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
413
	 */
414
	@Override
415
    protected boolean isIgnore(BerlinModelImportState state){
416
		return ! state.getConfig().isDoNameFacts();
417
	}
418

    
419

    
420

    
421
	//for testing only
422
	public static void main(String[] args) {
423

    
424
		BerlinModelNameFactsImport nf = new BerlinModelNameFactsImport();
425

    
426
		URL url;
427
		try {
428
			url = new URL("http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/");
429
			File path = new File("/Volumes/protolog/protolog/");
430
			if(path.exists()){
431
				String fact = "Acanthocephalus_amplexifolius";
432
				// make getMedia public for this to work
433
				Media media = nf.getMedia(fact, url, path);
434
				logger.info(media);
435
				for (MediaRepresentation rep : media.getRepresentations()){
436
					logger.info(rep.getMimeType());
437
					for (MediaRepresentationPart part : rep.getParts()){
438
						logger.info(part.getUri());
439
					}
440
				}
441
			}
442
		} catch (MalformedURLException e) {
443
			e.printStackTrace();
444
		}
445
	}
446
}
(9-9/22)