Project

General

Profile

Download (3.71 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.ArrayList;
15
import java.util.HashMap;
16
import java.util.HashSet;
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
 * @created 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
		ArrayList<HashMap<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 (HashMap<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 = (TaxonName)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

    
80

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

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

    
92
					Media media = Media.NewInstance();
93
					media.addRepresentation(representation);
94

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

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

    
113

    
114
		getNameService().save(taxonNameStore);
115
		logger.info(count + " protologues imported to CDM store.");
116

    
117
		return;
118
	}
119

    
120
}
(2-2/3)