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
.sql
.PreparedStatement
;
13 import java
.sql
.ResultSet
;
14 import java
.sql
.SQLException
;
15 import java
.sql
.Types
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.hibernate
.HibernateException
;
19 import org
.hibernate
.engine
.spi
.SessionImplementor
;
20 import org
.hibernate
.type
.StandardBasicTypes
;
21 import org
.hibernate
.usertype
.UserType
;
22 import org
.jadira
.usertype
.dateandtime
.shared
.spi
.AbstractSingleColumnUserType
;
23 import org
.jadira
.usertype
.dateandtime
.shared
.spi
.AbstractUserType
;
24 import org
.jadira
.usertype
.dateandtime
.shared
.spi
.ColumnMapper
;
25 import org
.joda
.time
.DateTimeFieldType
;
26 import org
.joda
.time
.Partial
;
29 * Persist {@link org.joda.time.Partial} via hibernate.
30 * This is a preliminary implementation that fulfills the needs of CDM but does not fully store a Partial.
31 * Only year, month and day is stored
36 public class PartialUserType
extends AbstractUserType
implements UserType
/* extends AbstractSingleColumnUserType<Partial, String, ColumnMapper<Partial,String>> implements UserType */ {
37 private static final long serialVersionUID
= -5323104403077597869L;
39 private static final Logger logger
= Logger
.getLogger(PartialUserType
.class);
42 public final static PartialUserType INSTANCE
= new PartialUserType();
44 private static final int[] SQL_TYPES
= new int[]{
50 public Partial
nullSafeGet(ResultSet rs
, String
[] names
, SessionImplementor session
, Object owner
)
51 throws HibernateException
, SQLException
{
52 String partial
= (String
)StandardBasicTypes
.STRING
.nullSafeGet(rs
, names
, session
, owner
);
53 Partial result
= new Partial();
54 if (partial
== null || partial
.length() != 8) {
57 Integer year
= Integer
.valueOf(partial
.substring(0,4));
58 Integer month
= Integer
.valueOf(partial
.substring(4,6));
59 Integer day
= Integer
.valueOf(partial
.substring(6,8));
62 result
= result
.with(DateTimeFieldType
.year(), year
);
65 result
= result
.with(DateTimeFieldType
.monthOfYear(), month
);
68 result
= result
.with(DateTimeFieldType
.dayOfMonth(), day
);
74 public void nullSafeSet(PreparedStatement preparedStatement
, Object value
, int index
,
75 SessionImplementor session
) throws HibernateException
, SQLException
{
77 StandardBasicTypes
.STRING
.nullSafeSet(preparedStatement
, null, index
, session
);
79 Partial p
= ((Partial
) value
);
80 StandardBasicTypes
.STRING
.nullSafeSet(preparedStatement
, partialToString(p
), index
, session
);
86 * @return an ISO 8601 like time representations of the form yyyyMMdd
88 public static String
partialToString(Partial p
) {
89 //FIXME reduce code by use org.joda.time.format.ISODateTimeFormat.basicDate() instead ?
90 // for a date with unknown day this will produce e.g. 195712??
92 String strYear
= getNullFilledString(p
, DateTimeFieldType
.year(),4);
93 String strMonth
= getNullFilledString(p
, DateTimeFieldType
.monthOfYear(),2);
94 String strDay
= getNullFilledString(p
, DateTimeFieldType
.dayOfMonth(),2);
95 String result
= strYear
+ strMonth
+ strDay
;
99 private static String
getNullFilledString(Partial partial
, DateTimeFieldType type
, int count
){
100 String nul
= "0000000000";
101 if (! partial
.isSupported(type
)){
102 return nul
.substring(0, count
);
104 int value
= partial
.get(type
);
105 String result
= String
.valueOf(value
);
106 if (result
.length() > count
){
107 logger
.error("value to long");
108 result
= result
.substring(0, count
);
109 }else if (result
.length() < count
){
110 result
= nul
.substring(0, count
- result
.length()) + result
;
116 public Object
deepCopy(Object value
) throws HibernateException
{
121 return new Partial((Partial
)value
);
125 public int[] sqlTypes() {
126 // TODO Auto-generated method stub
131 public Class
returnedClass() {
132 // TODO Auto-generated method stub