Latest updates to Specimen Excel import
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / excel / in / ExtensionTypeExcelImport.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.specimen.excel.in;
11
12 import java.util.HashMap;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import org.apache.commons.lang.StringUtils;
17 import org.apache.log4j.Logger;
18 import org.springframework.stereotype.Component;
19
20 import eu.etaxonomy.cdm.common.CdmUtils;
21 import eu.etaxonomy.cdm.io.common.ICdmIO;
22 import eu.etaxonomy.cdm.io.excel.common.ExcelImporterBase;
23 import eu.etaxonomy.cdm.model.common.ExtensionType;
24
25 /**
26 * @author a.mueller
27 * @created 18.05.2010
28 */
29 @Component
30 public class ExtensionTypeExcelImport extends ExcelImporterBase<SpecimenCdmExcelImportState> implements ICdmIO<SpecimenCdmExcelImportState> {
31 private static final Logger logger = Logger.getLogger(ExtensionTypeExcelImport.class);
32
33 private static final String WORKSHEET_NAME = "ExtensionTypes";
34
35 private static final String UUID_COLUMN = "UUID";
36 private static final String LABEL_COLUMN = "Label";
37 private static final String ABBREVIATION_COLUMN = "Abbreviation";
38 private static final String DESCRIPTION_COLUMN = "Description";
39 private static final String POSTFIX_COLUMN = "Postfix";
40
41
42 public ExtensionTypeExcelImport() {
43 super();
44 }
45
46 @Override
47 protected void analyzeRecord(HashMap<String, String> record, SpecimenCdmExcelImportState state) {
48 Set<String> keys = record.keySet();
49
50 NamedAreaLevellRow row = new NamedAreaLevellRow();
51 state.setNamedAreaLevelRow(row);
52
53 for (String originalKey: keys) {
54 Integer index = 0;
55 String indexedKey = CdmUtils.removeDuplicateWhitespace(originalKey.trim()).toString();
56 String[] split = indexedKey.split("_");
57 String key = split[0];
58 if (split.length > 1){
59 String indexString = split[split.length - 1];
60 try {
61 index = Integer.valueOf(indexString);
62 } catch (NumberFormatException e) {
63 String message = "Index must be integer";
64 logger.error(message);
65 continue;
66 }
67 }
68
69 String value = (String) record.get(indexedKey);
70 if (! StringUtils.isBlank(value)) {
71 if (logger.isDebugEnabled()) { logger.debug(key + ": " + value); }
72 value = CdmUtils.removeDuplicateWhitespace(value.trim()).toString();
73 }else{
74 continue;
75 }
76
77 if (key.equalsIgnoreCase(UUID_COLUMN)) {
78 row.setUuid(UUID.fromString(value)); //VALIDATE UUID
79 } else if(key.equalsIgnoreCase(LABEL_COLUMN)) {
80 row.setLabel(value);
81 } else if(key.equalsIgnoreCase(ABBREVIATION_COLUMN)) {
82 row.setAbbreviation(value);
83 } else if(key.equalsIgnoreCase(DESCRIPTION_COLUMN)) {
84 row.setDescription(value);
85 } else if(key.equalsIgnoreCase(POSTFIX_COLUMN)) {
86 row.setPostfix(value);
87 }else {
88 state.setUnsuccessfull();
89 logger.error("Unexpected column header " + key);
90 }
91 }
92 return;
93 }
94
95
96 @Override
97 protected void firstPass(SpecimenCdmExcelImportState state) {
98 NamedAreaLevellRow row = state.getNamedAreaLevelRow();
99
100 //level
101 UUID uuid = row.getUuid();
102 String label = row.getAbbreviation();
103 String text = row.getDescription();
104 String labelAbbrev = row.getAbbreviation();
105
106 ExtensionType term = getExtensionType(state, uuid, label, text, labelAbbrev);
107
108 if (StringUtils.isNotBlank(row.getPostfix())){
109 state.putPostfixExtensionType(row.getPostfix(), term);
110 }
111
112 //save
113 getTermService().save(term);
114 return;
115 }
116
117
118
119 @Override
120 protected void secondPass(SpecimenCdmExcelImportState state) {
121 //no second path defined yet
122 return;
123 }
124
125 /* (non-Javadoc)
126 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
127 */
128 @Override
129 protected boolean doCheck(SpecimenCdmExcelImportState state) {
130 logger.warn("Validation not yet implemented for " + this.getClass().getSimpleName());
131 return true;
132 }
133
134 protected String getWorksheetName() {
135 return WORKSHEET_NAME;
136 }
137
138 @Override
139 protected boolean needsNomenclaturalCode() {
140 return false;
141 }
142
143
144 @Override
145 protected boolean isIgnore(SpecimenCdmExcelImportState state) {
146 return !state.getConfig().isDoExtensionTypes();
147 }
148
149
150 }