2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.excel
.taxa
;
12 import java
.util
.HashMap
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.springframework
.stereotype
.Component
;
19 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.io
.excel
.common
.ExcelImportState
;
22 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
24 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
29 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
30 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
31 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
34 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
37 * @author a.babadshanjan
43 public class NormalExplicitImport
extends TaxonExcelImporterBase
{
44 private static final Logger logger
= Logger
.getLogger(NormalExplicitImport
.class);
47 protected boolean isIgnore(ExcelImportState state
) {
51 private int floatString2IntValue(String value
) {
55 Float fobj
= new Float(Float
.parseFloat(value
));
56 intValue
= fobj
.intValue();
57 if (logger
.isDebugEnabled()) { logger
.debug("Value formatted: " + intValue
); }
58 } catch (NumberFormatException ex
) {
59 logger
.error(value
+ " is not an integer");
67 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
70 protected boolean doCheck(ExcelImportState state
) {
71 logger
.warn("DoCheck not yet implemented for NormalExplicitImport");
76 protected boolean analyzeRecord(HashMap
<String
, String
> record
) {
78 boolean success
= true;
79 Set
<String
> keys
= record
.keySet();
81 NormalExplicitRow normalExplicitRow
= new NormalExplicitRow();
82 setTaxonLight(normalExplicitRow
);
84 for (String key
: keys
) {
86 key
= CdmUtils
.removeDuplicateWhitespace(key
.trim()).toString();
88 String value
= (String
) record
.get(key
);
89 if (!value
.equals("")) {
90 if (logger
.isDebugEnabled()) { logger
.debug(key
+ ": " + value
); }
91 value
= CdmUtils
.removeDuplicateWhitespace(value
.trim()).toString();
94 if (key
.equalsIgnoreCase(ID_COLUMN
)) {
95 int ivalue
= floatString2IntValue(value
);
96 getTaxonLight().setId(ivalue
);
98 } else if(key
.equalsIgnoreCase(PARENT_ID_COLUMN
)) {
99 int ivalue
= floatString2IntValue(value
);
100 getTaxonLight().setParentId(ivalue
);
102 } else if(key
.equalsIgnoreCase(RANK_COLUMN
)) {
103 getTaxonLight().setRank(value
);
105 } else if(key
.equalsIgnoreCase(SCIENTIFIC_NAME_COLUMN
)) {
106 getTaxonLight().setScientificName(value
);
108 } else if(key
.equalsIgnoreCase(AUTHOR_COLUMN
)) {
109 getTaxonLight().setAuthor(value
);
111 } else if(key
.equalsIgnoreCase(NAME_STATUS_COLUMN
)) {
112 getTaxonLight().setNameStatus(value
);
114 } else if(key
.equalsIgnoreCase(VERNACULAR_NAME_COLUMN
)) {
115 getTaxonLight().setCommonName(value
);
117 } else if(key
.equalsIgnoreCase(LANGUAGE_COLUMN
)) {
118 getTaxonLight().setLanguage(value
);
122 logger
.error("Unexpected column header " + key
);
130 * Stores taxa records in DB
133 protected boolean saveRecord() {
135 boolean success
= true;
138 CdmApplicationController appCtr
= getApplicationController();
140 String rankStr
= getTaxonLight().getRank();
141 String taxonNameStr
= getTaxonLight().getScientificName();
142 String authorStr
= getTaxonLight().getAuthor();
143 String nameStatus
= getTaxonLight().getNameStatus();
144 String commonNameStr
= getTaxonLight().getCommonName();
146 if (!taxonNameStr
.equals("")) {
148 // Determine the rank
150 rank
= Rank
.getRankByName(rankStr
);
151 } catch (UnknownCdmTypeException ex
) {
153 logger
.error(rankStr
+ " is not a valid rank.");
156 // Create the taxon name object depending on the setting of the nomenclatural code
157 // in the configurator (botanical code, zoological code, etc.)
158 NomenclaturalCode nc
= getConfigurator().getNomenclaturalCode();
159 TaxonNameBase
<?
,?
> taxonNameBase
= nc
.getNewTaxonNameInstance(rank
);
160 taxonNameBase
.setTitleCache(taxonNameStr
);
161 taxonNameBase
.setFullTitleCache(taxonNameStr
);
164 if (!authorStr
.equals("")) {
165 if (getAuthors().contains(authorStr
)) {
166 if (logger
.isDebugEnabled()) { logger
.debug("Author " + authorStr
+ " is already loaded"); }
168 getAuthors().add(authorStr
);
169 Person author
= Person
.NewTitledInstance(authorStr
);
171 NonViralName nonViralName
= (NonViralName
)taxonNameBase
;
172 nonViralName
.setCombinationAuthorTeam(author
);
173 } catch (ClassCastException ex
) {
174 logger
.error(taxonNameBase
.getTitleCache() + " is not a non-viral name." +
175 "Author " + authorStr
+ " ignored");
180 // Create the nomenclatural status
182 NomenclaturalStatusType statusType
=
183 NomenclaturalStatusType
.getNomenclaturalStatusTypeByLabel(nameStatus
);
184 taxonNameBase
.addStatus(NomenclaturalStatus
.NewInstance(statusType
));
185 } catch (UnknownCdmTypeException ex
) {
186 logger
.warn(nameStatus
+ " is not a valid nomenclatural status label");
189 Taxon taxon
= Taxon
.NewInstance(taxonNameBase
, null);
191 // Add the parent relationship
192 if (getTaxonLight().getParentId() != 0) {
193 Taxon parentTaxon
= findParentTaxon(getTaxonLight());
194 if (parentTaxon
!= null) {
195 parentTaxon
.addTaxonomicChild(taxon
, null, null);
196 UUID parentUuid
= appCtr
.getTaxonService().saveTaxon(parentTaxon
);
197 if (logger
.isDebugEnabled()) {
198 logger
.debug("Child " + getTaxonLight().getScientificName() + " added to parent "
199 + parentTaxon
.getTitleCache() + " (" + parentUuid
+ ")");
202 logger
.warn("Taxonomic parent not found for " + taxonNameStr
);
207 UUID taxonUuid
= appCtr
.getTaxonService().saveTaxon(taxon
);
208 // if (logger.isDebugEnabled()) { logger.debug("taxonUuid = " + taxonUuid); }
210 // Add the taxon representation to the processed taxa map
211 if (getTaxaMap().containsKey(getTaxonLight())) {
212 logger
.info("Taxon name " + taxonNameStr
+ " is already loaded");
215 getTaxaMap().put(getTaxonLight(), taxonUuid
);
217 //Set the previous taxon
218 setPreviousTaxonUuid(taxonUuid
);
221 // add common name to previous taxon
223 UUID taxonUuid
= getPreviousTaxonUuid();
224 Language language
= appCtr
.getTermService().getLanguageByIso(getTaxonLight().getLanguage());
225 CommonTaxonName commonTaxonName
= CommonTaxonName
.NewInstance(commonNameStr
, language
);
226 TaxonBase taxonBase
= appCtr
.getTaxonService().findByUuid(taxonUuid
);
228 TaxonDescription description
= TaxonDescription
.NewInstance((Taxon
)taxonBase
);
229 description
.addElement(commonTaxonName
);
230 logger
.info("Common name " + commonNameStr
+ " added to " + taxonBase
.getTitleCache());
231 } catch (ClassCastException ex
) {
232 logger
.error(taxonNameStr
+ " is not a taxon instance.");
240 private Taxon
findParentTaxon(NormalExplicitRow normalExplicitRow
) {
242 UUID parentTaxonUuid
= null;
243 Taxon parentTaxon
= null;
245 for (NormalExplicitRow tLight
: getTaxaMap().keySet()) {
246 // logger.debug("tLight.getId() = " + tLight.getId());
247 // logger.debug("taxonLight.getParentId() = " + taxonLight.getParentId());
248 if (tLight
.getId() == normalExplicitRow
.getParentId()) {
249 parentTaxonUuid
= getTaxaMap().get(tLight
);
254 if (parentTaxonUuid
== null) {
255 logger
.warn("Parent taxon of " + normalExplicitRow
.getScientificName() + " unknown." +
256 " Ignoring parent-child relationship.");
260 TaxonBase parentTaxonBase
= getApplicationController().getTaxonService().findByUuid(parentTaxonUuid
);
262 parentTaxon
= (Taxon
)parentTaxonBase
;
263 } catch (ClassCastException ex
) {
264 logger
.error(normalExplicitRow
.getScientificName() + " is not a taxon instance.");