2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.hibernate
;
12 import java
.io
.Serializable
;
13 import java
.sql
.PreparedStatement
;
14 import java
.sql
.ResultSet
;
15 import java
.sql
.SQLException
;
16 import java
.sql
.Types
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.hibernate
.Hibernate
;
20 import org
.hibernate
.HibernateException
;
21 import org
.hibernate
.usertype
.UserType
;
22 import org
.joda
.time
.DateTimeFieldType
;
23 import org
.joda
.time
.Partial
;
26 * Persist {@link org.joda.time.Partial} via hibernate.
27 * This is a preliminary implementation that fulfills the needs of CDM but does not fully store a Partial.
28 * Only year, month and day is stored
33 public class PartialUserType
implements UserType
{
34 private static final Logger logger
= Logger
.getLogger(PartialUserType
.class);
36 public final static PartialUserType INSTANCE
= new PartialUserType();
38 private static final int[] SQL_TYPES
= new int[]
43 public int[] sqlTypes()
48 public Class
returnedClass()
53 public boolean equals(Object x
, Object y
) throws HibernateException
59 if (x
== null || y
== null)
63 Partial dtx
= (Partial
) x
;
64 Partial dty
= (Partial
) y
;
66 return dtx
.equals(dty
);
69 public int hashCode(Object object
) throws HibernateException
71 return object
.hashCode();
74 public Object
nullSafeGet(ResultSet resultSet
, String
[] strings
, Object object
) throws HibernateException
, SQLException
76 return nullSafeGet(resultSet
, strings
[0]);
80 public Object
nullSafeGet(ResultSet resultSet
, String string
) throws SQLException
82 String partial
= (String
)Hibernate
.STRING
.nullSafeGet(resultSet
, string
);
83 Partial result
= new Partial();
84 if (partial
== null || partial
.length() != 8)
88 Integer year
= Integer
.valueOf(partial
.substring(0,4));
89 Integer month
= Integer
.valueOf(partial
.substring(4,6));
90 Integer day
= Integer
.valueOf(partial
.substring(6,8));
93 result
= result
.with(DateTimeFieldType
.year(), year
);
96 result
= result
.with(DateTimeFieldType
.monthOfYear(), month
);
99 result
= result
.with(DateTimeFieldType
.dayOfMonth(), day
);
105 public void nullSafeSet(PreparedStatement preparedStatement
, Object value
, int index
) throws HibernateException
, SQLException
109 Hibernate
.STRING
.nullSafeSet(preparedStatement
, null, index
);
113 Partial p
= ((Partial
) value
);
114 Hibernate
.STRING
.nullSafeSet(preparedStatement
, partialToString(p
), index
);
120 * @return an ISO 8601 like time representations of the form yyyyMMdd
122 public static String
partialToString(Partial p
) {
123 //FIXME reduce code by use org.joda.time.format.ISODateTimeFormat.basicDate() instead ?
124 // for a date with unknown day this will produce e.g. 195712??
126 String strYear
= getNullFilledString(p
, DateTimeFieldType
.year(),4);
127 String strMonth
= getNullFilledString(p
, DateTimeFieldType
.monthOfYear(),2);
128 String strDay
= getNullFilledString(p
, DateTimeFieldType
.dayOfMonth(),2);
129 String result
= strYear
+ strMonth
+ strDay
;
133 private static String
getNullFilledString(Partial partial
, DateTimeFieldType type
, int count
){
134 String nul
= "0000000000";
135 if (! partial
.isSupported(type
)){
136 return nul
.substring(0, count
);
138 int value
= partial
.get(type
);
139 String result
= String
.valueOf(value
);
140 if (result
.length() > count
){
141 logger
.error("value to long");
142 result
= result
.substring(0, count
);
143 }else if (result
.length() < count
){
144 result
= nul
.substring(0, count
- result
.length()) + result
;
150 public Object
deepCopy(Object value
) throws HibernateException
157 return new Partial((Partial
)value
);
160 public boolean isMutable()
165 public Serializable
disassemble(Object value
) throws HibernateException
167 return (Serializable
) value
;
170 public Object
assemble(Serializable cached
, Object value
) throws HibernateException
175 public Object
replace(Object original
, Object target
, Object owner
) throws HibernateException