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.ImageImportConfigurator;
|
24
|
import eu.etaxonomy.cdm.app.images.ImageImportState;
|
25
|
import eu.etaxonomy.cdm.common.ExcelUtils;
|
26
|
import eu.etaxonomy.cdm.model.description.Feature;
|
27
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
28
|
import eu.etaxonomy.cdm.model.description.TextData;
|
29
|
import eu.etaxonomy.cdm.model.media.Media;
|
30
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
31
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
32
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
33
|
|
34
|
/**
|
35
|
* @author n.hoffmann
|
36
|
* @created 19.11.2008
|
37
|
* @version 1.0
|
38
|
*/
|
39
|
@Component
|
40
|
public class PalmaeProtologueImport extends AbstractImageImporter {
|
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
|
/* (non-Javadoc)
|
48
|
* @see eu.etaxonomy.cdm.app.images.AbstractImageImporter#invokeImageImport(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
49
|
*/
|
50
|
@Override
|
51
|
protected void invokeImageImport(ImageImportState state) {
|
52
|
|
53
|
ArrayList<HashMap<String, String>> contents;
|
54
|
try {
|
55
|
contents = ExcelUtils.parseXLS(state.getConfig().getSource());
|
56
|
} catch (/*FileNotFound*/Exception e) {
|
57
|
logger.error("FileNotFound: " + state.getConfig().getSource().toString());
|
58
|
state.setUnsuccessfull();
|
59
|
return;
|
60
|
}
|
61
|
|
62
|
Set<TaxonNameBase> taxonNameStore = new HashSet<TaxonNameBase>();
|
63
|
|
64
|
int count = 0;
|
65
|
|
66
|
for (HashMap<String, String> row : contents){
|
67
|
count++;
|
68
|
|
69
|
TaxonNameBase taxonNameBase = null;
|
70
|
String species = null;
|
71
|
String taxonId = null;
|
72
|
String linkProto = null;
|
73
|
try{
|
74
|
species = row.get(PalmaeProtologueImport.SPECIES).trim();
|
75
|
taxonId = row.get(PalmaeProtologueImport.TAXONID);
|
76
|
linkProto= row.get(PalmaeProtologueImport.LINK_PROTO).trim();
|
77
|
taxonNameBase = (TaxonNameBase)getCommonService().getSourcedObjectByIdInSource(TaxonNameBase.class, "palm_tn_" + taxonId.replace(".0", ""), "TaxonName");
|
78
|
}catch (Exception e){
|
79
|
logger.error("The row has errors: rowNumber: " +count + ", content: " + row, e);
|
80
|
}
|
81
|
|
82
|
|
83
|
|
84
|
if(taxonNameBase == null){
|
85
|
logger.warn("no taxon with this name found: " + species + ", idInSource: " + taxonId);
|
86
|
}else{
|
87
|
|
88
|
URI uri;
|
89
|
try {
|
90
|
uri = new URI(linkProto);
|
91
|
MediaRepresentationPart representationPart = MediaRepresentationPart.NewInstance(uri, 0);
|
92
|
MediaRepresentation representation = MediaRepresentation.NewInstance("text/html", null);
|
93
|
representation.addRepresentationPart(representationPart);
|
94
|
|
95
|
Media media = Media.NewInstance();
|
96
|
media.addRepresentation(representation);
|
97
|
|
98
|
TaxonNameDescription description = TaxonNameDescription.NewInstance();
|
99
|
TextData protolog = TextData.NewInstance(Feature.PROTOLOGUE());
|
100
|
protolog.addMedia(media);
|
101
|
description.addElement(protolog);
|
102
|
taxonNameBase.addDescription(description);
|
103
|
} catch (URISyntaxException e) {
|
104
|
String message= "URISyntaxException when trying to convert: " + linkProto;
|
105
|
logger.error(message);
|
106
|
e.printStackTrace();
|
107
|
}
|
108
|
|
109
|
taxonNameStore.add(taxonNameBase);
|
110
|
if(count % 50 == 0){
|
111
|
logger.info(count + " protologues processed.");
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
|
116
|
|
117
|
getNameService().save(taxonNameStore);
|
118
|
logger.info(count + " protologues imported to CDM store.");
|
119
|
|
120
|
return;
|
121
|
}
|
122
|
|
123
|
}
|