minor
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / excel / taxa / NormalExplicitImport.java
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.excel.taxa;
11
12 import java.util.HashMap;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.stereotype.Component;
18
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;
35
36 /**
37 * @author a.babadshanjan
38 * @created 08.01.2009
39 * @version 1.0
40 */
41
42 @Component
43 public class NormalExplicitImport extends TaxonExcelImporterBase {
44 private static final Logger logger = Logger.getLogger(NormalExplicitImport.class);
45
46 @Override
47 protected boolean isIgnore(ExcelImportState state) {
48 return false;
49 }
50
51 private int floatString2IntValue(String value) {
52
53 int intValue = 0;
54 try {
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");
60 }
61 return intValue;
62 }
63
64
65
66 /* (non-Javadoc)
67 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
68 */
69 @Override
70 protected boolean doCheck(ExcelImportState state) {
71 logger.warn("DoCheck not yet implemented for NormalExplicitImport");
72 return true;
73 }
74
75 @Override
76 protected boolean analyzeRecord(HashMap<String, String> record) {
77
78 boolean success = true;
79 Set<String> keys = record.keySet();
80
81 NormalExplicitRow normalExplicitRow = new NormalExplicitRow();
82 setTaxonLight(normalExplicitRow);
83
84 for (String key: keys) {
85
86 key = CdmUtils.removeDuplicateWhitespace(key.trim()).toString();
87
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();
92 }
93
94 if (key.equalsIgnoreCase(ID_COLUMN)) {
95 int ivalue = floatString2IntValue(value);
96 getTaxonLight().setId(ivalue);
97
98 } else if(key.equalsIgnoreCase(PARENT_ID_COLUMN)) {
99 int ivalue = floatString2IntValue(value);
100 getTaxonLight().setParentId(ivalue);
101
102 } else if(key.equalsIgnoreCase(RANK_COLUMN)) {
103 getTaxonLight().setRank(value);
104
105 } else if(key.equalsIgnoreCase(SCIENTIFIC_NAME_COLUMN)) {
106 getTaxonLight().setScientificName(value);
107
108 } else if(key.equalsIgnoreCase(AUTHOR_COLUMN)) {
109 getTaxonLight().setAuthor(value);
110
111 } else if(key.equalsIgnoreCase(NAME_STATUS_COLUMN)) {
112 getTaxonLight().setNameStatus(value);
113
114 } else if(key.equalsIgnoreCase(VERNACULAR_NAME_COLUMN)) {
115 getTaxonLight().setCommonName(value);
116
117 } else if(key.equalsIgnoreCase(LANGUAGE_COLUMN)) {
118 getTaxonLight().setLanguage(value);
119
120 } else {
121 success = false;
122 logger.error("Unexpected column header " + key);
123 }
124 }
125 return success;
126 }
127
128
129 /**
130 * Stores taxa records in DB
131 */
132 @Override
133 protected boolean saveRecord() {
134
135 boolean success = true;
136 Rank rank = null;
137
138 CdmApplicationController appCtr = getApplicationController();
139
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();
145
146 if (!taxonNameStr.equals("")) {
147
148 // Determine the rank
149 try {
150 rank = Rank.getRankByName(rankStr);
151 } catch (UnknownCdmTypeException ex) {
152 success = false;
153 logger.error(rankStr + " is not a valid rank.");
154 }
155
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);
162
163 // Create the author
164 if (!authorStr.equals("")) {
165 if (getAuthors().contains(authorStr)) {
166 if (logger.isDebugEnabled()) { logger.debug("Author " + authorStr + " is already loaded"); }
167 } else {
168 getAuthors().add(authorStr);
169 Person author = Person.NewTitledInstance(authorStr);
170 try {
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");
176 }
177 }
178 }
179
180 // Create the nomenclatural status
181 try {
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");
187 }
188 // Create the taxon
189 Taxon taxon = Taxon.NewInstance(taxonNameBase, null);
190
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 + ")");
200 }
201 } else {
202 logger.warn("Taxonomic parent not found for " + taxonNameStr);
203 }
204 }
205
206 // Save the taxon
207 UUID taxonUuid = appCtr.getTaxonService().saveTaxon(taxon);
208 // if (logger.isDebugEnabled()) { logger.debug("taxonUuid = " + taxonUuid); }
209
210 // Add the taxon representation to the processed taxa map
211 if (getTaxaMap().containsKey(getTaxonLight())) {
212 logger.info("Taxon name " + taxonNameStr + " is already loaded");
213 return true;
214 } else {
215 getTaxaMap().put(getTaxonLight(), taxonUuid);
216 }
217 //Set the previous taxon
218 setPreviousTaxonUuid(taxonUuid);
219
220 } else {
221 // add common name to previous taxon
222
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);
227 try {
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.");
233 }
234 }
235
236 return success;
237 }
238
239
240 private Taxon findParentTaxon(NormalExplicitRow normalExplicitRow) {
241
242 UUID parentTaxonUuid = null;
243 Taxon parentTaxon = null;
244
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);
250 break;
251 }
252 }
253
254 if (parentTaxonUuid == null) {
255 logger.warn("Parent taxon of " + normalExplicitRow.getScientificName() + " unknown." +
256 " Ignoring parent-child relationship.");
257 return null;
258 }
259
260 TaxonBase parentTaxonBase = getApplicationController().getTaxonService().findByUuid(parentTaxonUuid);
261 try {
262 parentTaxon = (Taxon)parentTaxonBase;
263 } catch (ClassCastException ex) {
264 logger.error(normalExplicitRow.getScientificName() + " is not a taxon instance.");
265 }
266 return parentTaxon;
267 }
268
269 }