Latest updates to Specimen Excel import
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / excel / in / NamedAreaLevelExcelImport.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.location.NamedAreaLevel;
24
25 /**
26 * @author a.mueller
27 * @created 15.05.2010
28 */
29 @Component
30 public class NamedAreaLevelExcelImport extends ExcelImporterBase<SpecimenCdmExcelImportState> implements ICdmIO<SpecimenCdmExcelImportState> {
31 private static final Logger logger = Logger.getLogger(NamedAreaLevelExcelImport.class);
32
33 private static final String WORKSHEET_NAME = "AreaLevels";
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 private static final String GEOSERVER_LABEL_COLUMN = "GeoserverLabel";
41 private static final String GEOSERVER_ATTRIBUTE_COLUMN = "GeoserverAttribute";
42 private static final String ORDER_INDEX_COLUMN = "OrderIndex";
43
44
45 public NamedAreaLevelExcelImport() {
46 super();
47 }
48
49 @Override
50 protected void analyzeRecord(HashMap<String, String> record, SpecimenCdmExcelImportState state) {
51 Set<String> keys = record.keySet();
52
53 NamedAreaLevellRow row = new NamedAreaLevellRow();
54 state.setNamedAreaLevelRow(row);
55
56 for (String originalKey: keys) {
57 Integer index = 0;
58 String indexedKey = CdmUtils.removeDuplicateWhitespace(originalKey.trim()).toString();
59 String[] split = indexedKey.split("_");
60 String key = split[0];
61 if (split.length > 1){
62 String indexString = split[split.length - 1];
63 try {
64 index = Integer.valueOf(indexString);
65 } catch (NumberFormatException e) {
66 String message = "Index must be integer";
67 logger.error(message);
68 continue;
69 }
70 }
71
72 String value = (String) record.get(indexedKey);
73 if (! StringUtils.isBlank(value)) {
74 if (logger.isDebugEnabled()) { logger.debug(key + ": " + value); }
75 value = CdmUtils.removeDuplicateWhitespace(value.trim()).toString();
76 }else{
77 continue;
78 }
79
80 if (key.equalsIgnoreCase(UUID_COLUMN)) {
81 row.setUuid(UUID.fromString(value)); //VALIDATE UUID
82 } else if(key.equalsIgnoreCase(LABEL_COLUMN)) {
83 row.setLabel(value);
84 } else if(key.equalsIgnoreCase(ABBREVIATION_COLUMN)) {
85 row.setAbbreviation(value);
86 } else if(key.equalsIgnoreCase(DESCRIPTION_COLUMN)) {
87 row.setDescription(value);
88 } else if(key.equalsIgnoreCase(POSTFIX_COLUMN)) {
89 row.setPostfix(value);
90 } else if(key.equalsIgnoreCase(GEOSERVER_LABEL_COLUMN)) {
91 row.setGeoserverLabel(value);
92 } else if(key.equalsIgnoreCase(GEOSERVER_ATTRIBUTE_COLUMN)) {
93 row.setGeoServerAttribute(value);
94 } else if(key.equalsIgnoreCase(ORDER_INDEX_COLUMN)) {
95 row.setOrderIndex(value);
96 }else {
97 state.setUnsuccessfull();
98 logger.error("Unexpected column header " + key);
99 }
100 }
101 return;
102 }
103
104
105 @Override
106 protected void firstPass(SpecimenCdmExcelImportState state) {
107 NamedAreaLevellRow row = state.getNamedAreaLevelRow();
108
109 //level
110 UUID uuid = row.getUuid();
111 String label = row.getAbbreviation();
112 String text = row.getDescription();
113 String labelAbbrev = row.getAbbreviation();
114
115 NamedAreaLevel level = getNamedAreaLevel(state, uuid, label, text, labelAbbrev, null);
116
117 //TODO orderIndex
118
119 //TODO geoserverLabel
120
121 //TODO geoserverAttribute
122
123 if (StringUtils.isNotBlank(row.getPostfix())){
124 state.putPostfixLevel(row.getPostfix(), level);
125 }
126
127 //save
128 getTermService().save(level);
129 return;
130 }
131
132
133
134 @Override
135 protected void secondPass(SpecimenCdmExcelImportState state) {
136 //no second path defined yet
137 return;
138 }
139
140 /* (non-Javadoc)
141 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
142 */
143 @Override
144 protected boolean doCheck(SpecimenCdmExcelImportState state) {
145 logger.warn("Validation not yet implemented for " + this.getClass().getSimpleName());
146 return true;
147 }
148
149 protected String getWorksheetName() {
150 return WORKSHEET_NAME;
151 }
152
153 @Override
154 protected boolean needsNomenclaturalCode() {
155 return false;
156 }
157
158
159 @Override
160 protected boolean isIgnore(SpecimenCdmExcelImportState state) {
161 return !state.getConfig().isDoAreaLevels();
162 }
163
164
165 }