Project

General

Profile

Download (3.67 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;
11

    
12
import java.net.URI;
13
import java.net.URISyntaxException;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.app.images.AbstractImageImporter;
23
import eu.etaxonomy.cdm.app.images.ImageImportState;
24
import eu.etaxonomy.cdm.common.ExcelUtils;
25
import eu.etaxonomy.cdm.model.description.Feature;
26
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
27
import eu.etaxonomy.cdm.model.description.TextData;
28
import eu.etaxonomy.cdm.model.media.Media;
29
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
30
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32

    
33
/**
34
 * @author n.hoffmann
35
 * @since 19.11.2008
36
 */
37
@Component
38
public class PalmaeProtologueImport extends AbstractImageImporter {
39
    private static final long serialVersionUID = -7178567387220714414L;
40

    
41
    private static final Logger logger = Logger.getLogger(PalmaeProtologueImport.class);
42

    
43
	public static final String SPECIES = "Species";
44
	public static final String TAXONID = "Taxon ID";
45
	public static final String LINK_PROTO = "Link proto";
46

    
47
	@Override
48
	protected void invokeImageImport(ImageImportState state) {
49

    
50
		List<Map<String, String>> contents;
51
		try {
52
			contents = ExcelUtils.parseXLS(state.getConfig().getSource());
53
		} catch (/*FileNotFound*/Exception e) {
54
			logger.error("FileNotFound: " + state.getConfig().getSource().toString());
55
			state.setUnsuccessfull();
56
			return;
57
		}
58

    
59
		Set<TaxonName> taxonNameStore = new HashSet<>();
60

    
61
		int count = 0;
62

    
63
		for (Map<String, String> row : contents){
64
			count++;
65

    
66
			TaxonName taxonNameBase = null;
67
			String species = null;
68
			String taxonId = null;
69
			String linkProto = null;
70
			try{
71
				species = row.get(PalmaeProtologueImport.SPECIES).trim();
72
				taxonId = row.get(PalmaeProtologueImport.TAXONID);
73
				linkProto= row.get(PalmaeProtologueImport.LINK_PROTO).trim();
74
				taxonNameBase = getCommonService().getSourcedObjectByIdInSource(TaxonName.class, "palm_tn_" + taxonId.replace(".0", ""), "TaxonName");
75
			}catch (Exception e){
76
				logger.error("The row has errors: rowNumber: " +count + ", content: "  + row, e);
77
			}
78

    
79
			if(taxonNameBase == null){
80
				logger.warn("no taxon with this name found: " + species + ", idInSource: " + taxonId);
81
			}else{
82

    
83
				URI uri;
84
				try {
85
					uri = new URI(linkProto);
86
					MediaRepresentationPart representationPart = MediaRepresentationPart.NewInstance(uri, 0);
87
					MediaRepresentation representation = MediaRepresentation.NewInstance("text/html", null);
88
					representation.addRepresentationPart(representationPart);
89

    
90
					Media media = Media.NewInstance();
91
					media.addRepresentation(representation);
92

    
93
					TaxonNameDescription description = TaxonNameDescription.NewInstance();
94
					TextData protolog = TextData.NewInstance(Feature.PROTOLOGUE());
95
					protolog.addMedia(media);
96
					description.addElement(protolog);
97
					taxonNameBase.addDescription(description);
98
				} catch (URISyntaxException e) {
99
					String message= "URISyntaxException when trying to convert: " + linkProto;
100
					logger.error(message);
101
					e.printStackTrace();
102
				}
103

    
104
				taxonNameStore.add(taxonNameBase);
105
				if(count % 50 == 0){
106
					logger.info(count + " protologues processed.");
107
				}
108
			}
109
		}
110

    
111
		getNameService().save(taxonNameStore);
112
		logger.info(count + " protologues imported to CDM store.");
113

    
114
		return;
115
	}
116

    
117
}
(2-2/3)