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