further refactoring to make Excel imports more generic
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / excel / taxa / NormalExplicitImport.java
index 5d1bb0275e1f9a7d47be4974eb9b0727e2ef7d64..61d2fc50d07b6ed8b4c3ea01f8bacec9346f24af 100644 (file)
@@ -13,11 +13,9 @@ import java.net.MalformedURLException;
 import java.net.URI;\r
 import java.net.URISyntaxException;\r
 import java.util.Arrays;\r
-import java.util.HashMap;\r
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
-import org.apache.commons.lang.StringUtils;\r
 import org.apache.log4j.Logger;\r
 import org.springframework.stereotype.Component;\r
 \r
@@ -73,69 +71,71 @@ public class NormalExplicitImport extends TaxonExcelImporterBase {
                return true;\r
        }\r
 \r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.io.excel.common.ExcelTaxonOrSpecimenImportBase#analyzeSingleValue(eu.etaxonomy.cdm.io.excel.common.ExcelTaxonOrSpecimenImportBase.KeyValue, eu.etaxonomy.cdm.io.excel.common.ExcelImportState)\r
+        */\r
        @Override\r
-    protected boolean analyzeRecord(HashMap<String, String> record, TaxonExcelImportState state) {\r
-               \r
+       protected boolean analyzeSingleValue(KeyValue keyValue, TaxonExcelImportState state) {\r
                boolean success = true;\r
-       Set<String> keys = record.keySet();\r
-       \r
-       NormalExplicitRow normalExplicitRow = new NormalExplicitRow();\r
-       state.setTaxonLight(normalExplicitRow);\r
-       \r
-       for (String originalKey: keys) {\r
-               KeyValue keyValue = makeKeyValue(record, originalKey);\r
-               if (StringUtils.isBlank(keyValue.value)){\r
-                       continue;\r
-               }\r
-               \r
-               String key = keyValue.key;\r
-               String value = keyValue.value;\r
-               Integer index = keyValue.index;\r
-               if (key.equalsIgnoreCase(ID_COLUMN)) {\r
-                       int ivalue = floatString2IntValue(value);\r
-                       normalExplicitRow.setId(ivalue);\r
-                       \r
-                       } else if(key.equalsIgnoreCase(PARENT_ID_COLUMN)) {\r
-                               int ivalue = floatString2IntValue(value);\r
-                               normalExplicitRow.setParentId(ivalue);\r
-                               \r
-                       } else if(key.equalsIgnoreCase(RANK_COLUMN)) {\r
-                               normalExplicitRow.setRank(value);\r
-                       \r
-                       } else if(key.equalsIgnoreCase(SCIENTIFIC_NAME_COLUMN)) {\r
-                               normalExplicitRow.setScientificName(value);\r
-                       \r
-                       } else if(key.equalsIgnoreCase(AUTHOR_COLUMN)) {\r
-                               normalExplicitRow.setAuthor(value);\r
-                       \r
-                       } else if(key.equalsIgnoreCase(NAME_STATUS_COLUMN)) {\r
-                               normalExplicitRow.setNameStatus(value);\r
-                       \r
-                       } else if(key.equalsIgnoreCase(VERNACULAR_NAME_COLUMN)) {\r
-                               normalExplicitRow.setCommonName(value);\r
-                       \r
-                       } else if(key.equalsIgnoreCase(LANGUAGE_COLUMN)) {\r
-                               normalExplicitRow.setLanguage(value);\r
+               \r
+               NormalExplicitRow normalExplicitRow = state.getCurrentRow();\r
+               String key = keyValue.key;\r
+               String value = keyValue.value;\r
+               Integer index = keyValue.index;\r
+               if (isBaseColumn(keyValue)){\r
+                       handleBaseColumn(keyValue, normalExplicitRow);\r
+               }else if (key.equalsIgnoreCase(ID_COLUMN)) {\r
+                       int ivalue = floatString2IntValue(value);\r
+                       normalExplicitRow.setId(ivalue);\r
                        \r
-                       } else if(key.equalsIgnoreCase(TDWG_COLUMN)) {\r
-                               value = value.replace(".0", "");\r
-                               normalExplicitRow.putDistribution(index, value);\r
+               } else if(key.equalsIgnoreCase(PARENT_ID_COLUMN)) {\r
+                       int ivalue = floatString2IntValue(value);\r
+                       normalExplicitRow.setParentId(ivalue);\r
                        \r
-                       } else if(key.equalsIgnoreCase(PROTOLOGUE_COLUMN)) {\r
-                               normalExplicitRow.putProtologue(index, value);\r
-                       \r
-                       } else if(key.equalsIgnoreCase(IMAGE_COLUMN)) {\r
-                               normalExplicitRow.putImage(index, value);\r
-                       \r
-                       } else {\r
-                               success = false;\r
-                               logger.error("Unexpected column header " + key);\r
-                       }\r
-       }\r
-       return success;\r
-    }\r
-       \r
-       \r
+               } else if(key.equalsIgnoreCase(RANK_COLUMN)) {\r
+                       normalExplicitRow.setRank(value);\r
+                       \r
+               } else if(key.equalsIgnoreCase(SCIENTIFIC_NAME_COLUMN)) {\r
+                       normalExplicitRow.setScientificName(value);\r
+                       \r
+               } else if(key.equalsIgnoreCase(AUTHOR_COLUMN)) {\r
+                       normalExplicitRow.setAuthor(value);\r
+                       \r
+               } else if(key.equalsIgnoreCase(NAME_STATUS_COLUMN)) {\r
+                       normalExplicitRow.setNameStatus(value);\r
+                       \r
+               } else if(key.equalsIgnoreCase(VERNACULAR_NAME_COLUMN)) {\r
+                       normalExplicitRow.setCommonName(value);\r
+                       \r
+               } else if(key.equalsIgnoreCase(LANGUAGE_COLUMN)) {\r
+                       normalExplicitRow.setLanguage(value);\r
+               \r
+               } else if(key.equalsIgnoreCase(TDWG_COLUMN)) {\r
+                       value = value.replace(".0", "");\r
+                       normalExplicitRow.putDistribution(index, value);\r
+               \r
+               } else if(key.equalsIgnoreCase(PROTOLOGUE_COLUMN)) {\r
+                       normalExplicitRow.putProtologue(index, value);\r
+                       \r
+               } else if(key.equalsIgnoreCase(IMAGE_COLUMN)) {\r
+                       normalExplicitRow.putImage(index, value);\r
+                       \r
+               } else {\r
+                       success = false;\r
+                       logger.error("Unexpected column header " + key);\r
+               }\r
+               return success;\r
+       }\r
+\r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.io.excel.common.ExcelTaxonOrSpecimenImportBase#createDataHolderRow()\r
+        */\r
+       @Override\r
+       protected NormalExplicitRow createDataHolderRow() {\r
+               return new NormalExplicitRow();\r
+       }\r
+\r
+\r
        /** \r
         *  Stores taxa records in DB\r
         */\r
@@ -143,7 +143,7 @@ public class NormalExplicitImport extends TaxonExcelImporterBase {
     protected boolean firstPass(TaxonExcelImportState state) {\r
                boolean success = true;\r
                Rank rank = null;\r
-               NormalExplicitRow taxonLight = state.getTaxonLight();\r
+               NormalExplicitRow taxonLight = state.getCurrentRow();\r
                \r
                String rankStr = taxonLight.getRank();\r
                String taxonNameStr = taxonLight.getScientificName();\r
@@ -261,11 +261,11 @@ public class NormalExplicitImport extends TaxonExcelImporterBase {
     protected boolean secondPass(TaxonExcelImportState state) {\r
                boolean success = true;\r
                try {\r
-                       String taxonNameStr = state.getTaxonLight().getScientificName();\r
-                       String nameStatus = state.getTaxonLight().getNameStatus();\r
-                       String commonNameStr = state.getTaxonLight().getCommonName();\r
-                       Integer parentId = state.getTaxonLight().getParentId();\r
-                       Integer childId = state.getTaxonLight().getId();\r
+                       String taxonNameStr = state.getCurrentRow().getScientificName();\r
+                       String nameStatus = state.getCurrentRow().getNameStatus();\r
+                       String commonNameStr = state.getCurrentRow().getCommonName();\r
+                       Integer parentId = state.getCurrentRow().getParentId();\r
+                       Integer childId = state.getCurrentRow().getId();\r
                        \r
                        Taxon parentTaxon = (Taxon)state.getTaxonBase(parentId);\r
                        if (CdmUtils.isNotEmpty(taxonNameStr)) {\r
@@ -273,7 +273,7 @@ public class NormalExplicitImport extends TaxonExcelImporterBase {
                                if (validMarkers.contains(nameStatus)){\r
                                        Taxon taxon = (Taxon)state.getTaxonBase(childId);\r
                                        // Add the parent relationship\r
-                                       if (state.getTaxonLight().getParentId() != 0) {\r
+                                       if (state.getCurrentRow().getParentId() != 0) {\r
                                                if (parentTaxon != null) {\r
                                                        //Taxon taxon = (Taxon)state.getTaxonBase(childId);\r
                                                        \r
@@ -320,8 +320,8 @@ public class NormalExplicitImport extends TaxonExcelImporterBase {
         */\r
        private void handleCommonName(TaxonExcelImportState state,\r
                        String taxonNameStr, String commonNameStr, Integer parentId) {\r
-               Language language = getTermService().getLanguageByIso(state.getTaxonLight().getLanguage());\r
-               if (language == null && CdmUtils.isNotEmpty(state.getTaxonLight().getLanguage())  ){\r
+               Language language = getTermService().getLanguageByIso(state.getCurrentRow().getLanguage());\r
+               if (language == null && CdmUtils.isNotEmpty(state.getCurrentRow().getLanguage())  ){\r
                        String error ="Language is null but shouldn't"; \r
                        logger.error(error);\r
                        throw new IllegalArgumentException(error);\r