ref #8162 move OriginalSourceXXX to reference package
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / PartialUserType.java
index 66c321730d1456d75139423146177cd607854534..ddf6bffea2959ff6e4626a202ea9044de38e070b 100644 (file)
@@ -1,8 +1,8 @@
 /**\r
 * Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
+* European Distributed Institute of Taxonomy\r
 * http://www.e-taxonomy.eu\r
-* \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
@@ -19,19 +19,17 @@ import org.hibernate.HibernateException;
 import org.hibernate.engine.spi.SessionImplementor;\r
 import org.hibernate.type.StandardBasicTypes;\r
 import org.hibernate.usertype.UserType;\r
-import org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType;\r
 import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;\r
-import org.jadira.usertype.dateandtime.shared.spi.ColumnMapper;\r
 import org.joda.time.DateTimeFieldType;\r
 import org.joda.time.Partial;\r
 \r
 /**\r
  * Persist {@link org.joda.time.Partial} via hibernate.\r
  * This is a preliminary implementation that fulfills the needs of CDM but does not fully store a Partial.\r
- * Only year, month and day is stored\r
+ * Only year, month and day is stored. Since 5.0 also hour and minute is supported.\r
+ *\r
  * @author a.mueller\r
- * @created 11.11.2008\r
- * @version 2.0\r
+ * @since 11.11.2008\r
  */\r
 public class PartialUserType extends AbstractUserType implements UserType /* extends AbstractSingleColumnUserType<Partial, String, ColumnMapper<Partial,String>> implements UserType */ {\r
        private static final long serialVersionUID = -5323104403077597869L;\r
@@ -47,17 +45,25 @@ public class PartialUserType extends AbstractUserType implements UserType /* ext
 \r
 \r
        @Override\r
-       public Partial nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) \r
+       public Partial nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)\r
                        throws HibernateException, SQLException {\r
                String partial = (String)StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);\r
-               Partial result = new Partial(); \r
-               if (partial == null || partial.length() != 8) {\r
+               Partial result = new Partial();\r
+               if (partial == null) {\r
                        return null;\r
+               }else if (partial.length() != 8 &&  partial.length() != 13){\r
+                   throw new HibernateException("Format for Partial not supported. Length mus be 8 or 13: " + partial);\r
                }\r
                Integer year = Integer.valueOf(partial.substring(0,4));\r
                Integer month = Integer.valueOf(partial.substring(4,6));\r
                Integer day = Integer.valueOf(partial.substring(6,8));\r
-               \r
+               Integer hour = null;\r
+               Integer minute = null;\r
+               if (partial.length() == 13){\r
+               hour = Integer.valueOf(partial.substring(9,11));\r
+            minute = Integer.valueOf(partial.substring(11,13));\r
+               }\r
+\r
                if (year != 0){\r
                        result = result.with(DateTimeFieldType.year(), year);\r
                }\r
@@ -67,6 +73,12 @@ public class PartialUserType extends AbstractUserType implements UserType /* ext
                if (day != 0){\r
                        result = result.with(DateTimeFieldType.dayOfMonth(), day);\r
                }\r
+           if (hour != null){\r
+               result = result.with(DateTimeFieldType.hourOfDay(), hour);\r
+           }\r
+        if (minute != null){\r
+            result = result.with(DateTimeFieldType.minuteOfHour(), minute);\r
+        }\r
                return result;\r
        }\r
 \r
@@ -87,16 +99,31 @@ public class PartialUserType extends AbstractUserType implements UserType /* ext
         */\r
        public static String partialToString(Partial p) {\r
                //FIXME reduce code by use org.joda.time.format.ISODateTimeFormat.basicDate() instead ?\r
-               //      for a date with unknown day this will produce e.g. 195712?? \r
-               //              \r
+               //      for a date with unknown day this will produce e.g. 195712??\r
+               //\r
                String strYear = getNullFilledString(p, DateTimeFieldType.year(),4);\r
                String strMonth = getNullFilledString(p, DateTimeFieldType.monthOfYear(),2);\r
                String strDay = getNullFilledString(p, DateTimeFieldType.dayOfMonth(),2);\r
-               String result = strYear + strMonth + strDay;\r
-               return result;\r
+               String strHour = getNullFilledString(p, DateTimeFieldType.hourOfDay(),2);\r
+               String strMinute = getNullFilledString(p, DateTimeFieldType.minuteOfHour(),2);\r
+               boolean timeExists = timeExists(p);\r
+        String result = strYear + strMonth + strDay;\r
+        if (timeExists) {\r
+            result = result + "_" + strHour + strMinute;\r
+        }\r
+        return result;\r
        }\r
-       \r
-       private static String getNullFilledString(Partial partial, DateTimeFieldType type, int count){\r
+\r
+       /**\r
+     * @param p\r
+     * @return\r
+     */\r
+    private static boolean timeExists(Partial partial) {\r
+        return partial.isSupported(DateTimeFieldType.hourOfDay()) ||\r
+                partial.isSupported(DateTimeFieldType.minuteOfHour());\r
+    }\r
+\r
+    private static String getNullFilledString(Partial partial, DateTimeFieldType type, int count){\r
                String nul = "0000000000";\r
                if (! partial.isSupported(type)){\r
                        return nul.substring(0, count);\r
@@ -113,25 +140,24 @@ public class PartialUserType extends AbstractUserType implements UserType /* ext
                }\r
        }\r
 \r
+    @Override\r
     public Object deepCopy(Object value) throws HibernateException {\r
         if (value == null) {\r
             return null;\r
         }\r
 \r
-        return new Partial((Partial)value);\r
+        return value;\r
     }\r
 \r
        @Override\r
        public int[] sqlTypes() {\r
-               // TODO Auto-generated method stub\r
                return SQL_TYPES;\r
        }\r
 \r
        @Override\r
        public Class returnedClass() {\r
-               // TODO Auto-generated method stub\r
-               return null;\r
-       }       \r
+               return Partial.class;\r
+       }\r
 \r
 }\r
 \r