add generic config to ExcelImportState
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / ImportHelper.java
index fdb530b8f50bd8b56b10e2d0bff52e943f936365..ba1c1bf8cf1e279580da2e0692958247e7989e5b 100644 (file)
@@ -1,29 +1,38 @@
 /**\r
- * \r
- */\r
+* Copyright (C) 2009 EDIT\r
+* European Distributed Institute of Taxonomy\r
+* http://www.e-taxonomy.eu\r
+*\r
+* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* See LICENSE.TXT at the top of this package for the full license terms.\r
+*/ \r
+\r
 package eu.etaxonomy.cdm.io.common;\r
 \r
 import java.lang.reflect.InvocationTargetException;\r
 import java.lang.reflect.Method;\r
 import java.sql.ResultSet;\r
 import java.sql.SQLException;\r
-import java.util.Calendar;\r
 import java.util.List;\r
 \r
 import org.apache.log4j.Logger;\r
 import org.jdom.Element;\r
 import org.jdom.Namespace;\r
-import org.joda.time.DateTime;\r
 \r
 import eu.etaxonomy.cdm.common.CdmUtils;\r
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;\r
 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;\r
 import eu.etaxonomy.cdm.model.common.Annotation;\r
 import eu.etaxonomy.cdm.model.common.CdmBase;\r
+import eu.etaxonomy.cdm.model.common.DescriptionElementSource;\r
+import eu.etaxonomy.cdm.model.common.IOriginalSource;\r
+import eu.etaxonomy.cdm.model.common.ISourceable;\r
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;\r
+import eu.etaxonomy.cdm.model.common.IdentifiableSource;\r
 import eu.etaxonomy.cdm.model.common.Language;\r
-import eu.etaxonomy.cdm.model.common.OriginalSource;\r
 import eu.etaxonomy.cdm.model.common.TimePeriod;\r
-import eu.etaxonomy.cdm.model.reference.ReferenceBase;\r
+import eu.etaxonomy.cdm.model.description.DescriptionElementBase;\r
+import eu.etaxonomy.cdm.model.reference.Reference;\r
 /**\r
  * @author a.mueller\r
  *\r
@@ -39,7 +48,7 @@ public class ImportHelper {
        \r
        \r
 \r
-       public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, long sourceId, String namespace){\r
+       public static boolean setOriginalSource(IdentifiableEntity idEntity, Reference sourceReference, long sourceId, String namespace){\r
                return setOriginalSource(idEntity, sourceReference, String.valueOf(sourceId), namespace);\r
        }\r
        \r
@@ -50,16 +59,19 @@ public class ImportHelper {
         * @param sourceId\r
         * @return\r
         */\r
-       public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, String sourceId, String namespace){\r
-               OriginalSource originalSource = new OriginalSource();\r
-               originalSource.setIdInSource(sourceId);\r
-               originalSource.setCitation(sourceReference);\r
-               originalSource.setIdNamespace(namespace);\r
-               idEntity.addSource(originalSource);\r
+       public static boolean setOriginalSource(ISourceable sourceable, Reference sourceReference, String sourceId, String namespace){\r
+               IOriginalSource originalSource;\r
+               if (HibernateProxyHelper.isInstanceOf(sourceable, IdentifiableEntity.class)){\r
+                       originalSource = IdentifiableSource.NewInstance(sourceId, namespace, sourceReference, null);\r
+               }else if (HibernateProxyHelper.isInstanceOf(sourceable, DescriptionElementBase.class)){\r
+                       originalSource = DescriptionElementSource.NewInstance(sourceId, namespace, sourceReference, null);\r
+               }else{\r
+                       throw new ClassCastException("Unknown implementing class for ISourceable "+ sourceable.getClass() + " . Not supported bei ImportHelper.");\r
+               }\r
+               sourceable.addSource(originalSource);\r
                return true;\r
        }\r
        \r
-       \r
        public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){\r
                return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, OVERWRITE);\r
        }\r
@@ -117,20 +129,7 @@ public class ImportHelper {
                                return true;\r
                        }\r
                        if (logger.isDebugEnabled()) { logger.debug("addValue: " + sourceValue);}\r
-                       if (clazz == boolean.class || clazz == Boolean.class){\r
-                               if (cdmAttrName == null || cdmAttrName.length() < 1 ){\r
-                                       throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");\r
-                               }\r
-                               methodName = "set" + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;\r
-                       }else if(clazz == String.class) {\r
-                               if (cdmAttrName == null || cdmAttrName.length() < 1 ){\r
-                                       throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");\r
-                               }\r
-                               methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;\r
-                       }else{\r
-                               logger.error("Class not supported: " + clazz.toString());\r
-                               return false;\r
-                       }\r
+                       methodName = getSetterMethodName(clazz, cdmAttrName);\r
                        Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);\r
                        cdmMethod.invoke(cdmBase, sourceValue);\r
                        return true;\r
@@ -161,6 +160,32 @@ public class ImportHelper {
                \r
        }\r
 \r
+       /**\r
+        * @param clazz either boolean or other class (for boolean the naming strategy is different !)\r
+        * @param cdmAttrName \r
+        * @return\r
+//      * @throws IllegalArgumentException if a clazz is not yet supported\r
+        */\r
+       public static String getSetterMethodName(Class clazz, String cdmAttrName) {\r
+               String methodName; \r
+               if (clazz == boolean.class || clazz == Boolean.class){\r
+                       if (cdmAttrName == null || cdmAttrName.length() < 1 ){\r
+                               throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");\r
+                       }\r
+                       methodName = "set" + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;\r
+               }else  {\r
+                       if (cdmAttrName == null || cdmAttrName.length() < 1 ){\r
+                               throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");\r
+                       }\r
+                       methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;\r
+               }\r
+//             else{\r
+//                     logger.error("Class not supported: " + clazz.toString());\r
+//                     throw new IllegalArgumentException("Classes other than boolean or String are not yet supported");\r
+//             };\r
+               return methodName;\r
+       }\r
+\r
        private static boolean valuesAreNull(List<Object> values){\r
                for (Object sourceValue : values.toArray()){\r
                        if (sourceValue != null){\r
@@ -248,63 +273,73 @@ public class ImportHelper {
        \r
        \r
        public static TimePeriod getDatePublished(String refYear){\r
-               //FIXME until now only quick and dirty and wrong\r
-               if (refYear == null){\r
-                       return null;\r
-               }\r
-               String[] years = refYear.split("-");\r
-               DateTime dtStart = null;\r
-               DateTime dtEnd = null;\r
-               \r
-               if (years.length > 2 || years.length <= 0){\r
-                       logger.warn("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX getDatePublished");\r
-               }else {\r
-                       dtStart = parseSingleDate(years[0]);\r
-                       \r
-                       if (years.length >= 2){\r
-                               dtEnd = parseSingleDate(years[1]);\r
-                       }\r
+               TimePeriod resultNew;\r
+               try {\r
+                       resultNew = TimePeriod.parseString(refYear);\r
+               } catch (IllegalArgumentException e) {\r
+                       logger.warn("RefYear could not be parsed: " + refYear);\r
+                       resultNew = null;\r
                }\r
-               TimePeriod result = TimePeriod.NewInstance(dtStart, dtEnd);\r
-               return result;\r
+               return resultNew;\r
        }\r
+\r
+       //************** EXPORT *******************/\r
+       \r
        \r
-       private static DateTime parseSingleDate(String singleDateString){\r
-               //FIXME until now only quick and dirty and wrong\r
-               DateTime dt = new DateTime(9999, 12, 30, 0,0,0,0);\r
-               if (CdmUtils.isNumeric(singleDateString)){\r
-                       try {\r
-                               Integer year = Integer.valueOf(singleDateString.trim());\r
-                               if (year > 1750 && year < 2050){\r
-                                       dt = dt.withYear(year);\r
-                               }\r
-                       } catch (NumberFormatException e) {\r
-                               logger.debug("Not a Integer format in getCalendar()");\r
+       public static<T extends Object> T getValue(CdmBase cdmBase, String cdmAttrName, boolean isBoolean, boolean obligatory){\r
+               String methodName;\r
+               T result;\r
+               try {\r
+                       methodName = getGetterMethodName(cdmAttrName, isBoolean);\r
+                       Method cdmMethod = cdmBase.getClass().getMethod(methodName);\r
+                       result = (T)cdmMethod.invoke(cdmBase);\r
+                       return result;\r
+               } catch (NullPointerException e) {\r
+                       logger.error("NullPointerException: " + e.getMessage());\r
+                       return null;\r
+               } catch (IllegalArgumentException e) {\r
+                       logger.error("IllegalArgumentException: " + e.getMessage());\r
+                       return null;\r
+               } catch (IllegalAccessException e) {\r
+                       logger.error("IllegalAccessException: " + e.getMessage());\r
+                       return null;\r
+               } catch (InvocationTargetException e) {\r
+                       logger.error("InvocationTargetException: " + e.getMessage());\r
+                       return null;\r
+               }catch (SecurityException e) {\r
+                       logger.error("SecurityException: " + e.getMessage());\r
+                       return null;\r
+               } catch (NoSuchMethodException e) {\r
+                       if (obligatory){\r
+                               logger.error("NoSuchMethod: " + e.getMessage());\r
+                               return null;\r
+                       }else{\r
+                               if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}\r
+                               return null;\r
                        }\r
                }\r
-               return dt;\r
-\r
+               \r
        }\r
 \r
-       \r
-       //******* old *****************\r
-       \r
-//     private static Calendar getCalendar(String strYear){\r
-//             //FIXME until now only quick and dirty and wrong\r
-//             Calendar cal = Calendar.getInstance();\r
-//             cal.set(9999, Calendar.DECEMBER, 30, 0, 0, 0);\r
-//             if (CdmUtils.isNumeric(strYear)){\r
-//                     try {\r
-//                             Integer year = Integer.valueOf(strYear.trim());\r
-//                             if (year > 1750 && year < 2030){\r
-//                                     cal.set(year, Calendar.JANUARY, 1, 0, 0, 0);\r
-//                             }\r
-//                     } catch (NumberFormatException e) {\r
-//                             logger.debug("Not a Integer format in getCalendar()");\r
-//                     }\r
-//             }\r
-//             return cal;\r
-//     }\r
-\r
+       /**\r
+        * @param cdmAttrName\r
+        * @param isBoolean\r
+        * @return\r
+        */\r
+       public static String getGetterMethodName(String cdmAttrName, boolean isBoolean) {\r
+               String methodName;\r
+               if (isBoolean){\r
+                       if (cdmAttrName == null || cdmAttrName.length() < 3 ||  !( cdmAttrName.startsWith("is") || cdmAttrName.startsWith("use"))     ){\r
+                               throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters and start with 'is' or 'use': " + cdmAttrName);\r
+                       }\r
+                       methodName = cdmAttrName ;\r
+               }else {\r
+                       if (cdmAttrName == null || cdmAttrName.length() < 1 ){\r
+                               throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");\r
+                       }\r
+                       methodName = "get" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;\r
+               }\r
+               return methodName;\r
+       }\r
 \r
 }\r