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