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