Refactoring Excel Import
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / excel / common / ExcelTaxonOrSpecimenImportBase.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.excel.common;
11
12 import java.util.HashMap;
13
14 import org.apache.commons.lang.StringUtils;
15 import org.apache.log4j.Logger;
16
17 import eu.etaxonomy.cdm.common.CdmUtils;
18
19 /**
20 * @author a.mueller
21 * @date 12.07.2011
22 */
23 public abstract class ExcelTaxonOrSpecimenImportBase<STATE extends ExcelImportState<? extends ExcelImportConfiguratorBase>> extends ExcelImporterBase<STATE> {
24 private static final Logger logger = Logger.getLogger(ExcelTaxonOrSpecimenImportBase.class);
25
26
27 protected static final String UUID_COLUMN = "(?i)(UUID)";
28
29
30 protected static final String RANK_COLUMN = "(?i)(Rank)";
31 protected static final String FULL_NAME_COLUMN = "(?i)(FullName)";
32 protected static final String FAMILY_COLUMN = "(?i)(Family)";
33 protected static final String GENUS_COLUMN = "(?i)(Genus)";
34 protected static final String SPECIFIC_EPITHET_COLUMN = "(?i)(SpecificEpi(thet)?)";
35 protected static final String INFRASPECIFIC_EPITHET_COLUMN = "(?i)(InfraSpecificEpi(thet)?)";
36
37
38 protected class KeyValue{
39 public KeyValue() {}
40
41 public String key;
42 public String value;
43 public String postfix;
44 public Integer index;
45 public String ref;
46 public String refAuthor;
47 public String refIndex;
48 }
49
50
51 /**
52 * @param record
53 * @param originalKey
54 * @param keyValue
55 * @return
56 */
57 protected KeyValue makeKeyValue(HashMap<String, String> record, String originalKey) {
58 KeyValue keyValue = new KeyValue();
59 String indexedKey = CdmUtils.removeDuplicateWhitespace(originalKey.trim()).toString();
60 String[] split = indexedKey.split("_");
61 keyValue.key = split[0];
62 if (split.length > 1){
63 for (int i = 1 ; i < split.length ; i++ ){
64 String indexString = split[i];
65 if (isInteger(indexString)){
66 keyValue.index = Integer.valueOf(indexString);
67 }else{
68 keyValue.postfix = split[i];
69 }
70 }
71 }
72
73 //TODO shouldn't we use originalKey here??
74 String value = (String) record.get(indexedKey);
75 if (! StringUtils.isBlank(value)) {
76 if (logger.isDebugEnabled()) { logger.debug(keyValue.key + ": " + value); }
77 value = CdmUtils.removeDuplicateWhitespace(value.trim()).toString();
78 keyValue.value = value;
79 }else{
80 keyValue.value = null;
81 }
82 return keyValue;
83 }
84
85
86 protected boolean isInteger(String value){
87 try {
88 Integer.valueOf(value);
89 return true;
90 } catch (NumberFormatException e) {
91 return false;
92 }
93 }
94 }