minor
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / PartialUserType.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.model.common;
11
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;
17
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;
24
25 /**
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
29 * @author a.mueller
30 * @created 11.11.2008
31 * @version 1.0
32 */
33 public class PartialUserType implements UserType {
34 private static final Logger logger = Logger.getLogger(PartialUserType.class);
35
36 public final static PartialUserType INSTANCE = new PartialUserType();
37
38 private static final int[] SQL_TYPES = new int[]
39 {
40 Types.VARCHAR,
41 };
42
43 public int[] sqlTypes()
44 {
45 return SQL_TYPES;
46 }
47
48 public Class returnedClass()
49 {
50 return Partial.class;
51 }
52
53 public boolean equals(Object x, Object y) throws HibernateException
54 {
55 if (x == y)
56 {
57 return true;
58 }
59 if (x == null || y == null)
60 {
61 return false;
62 }
63 Partial dtx = (Partial) x;
64 Partial dty = (Partial) y;
65
66 return dtx.equals(dty);
67 }
68
69 public int hashCode(Object object) throws HibernateException
70 {
71 return object.hashCode();
72 }
73
74 public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException
75 {
76 return nullSafeGet(resultSet, strings[0]);
77
78 }
79
80 public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException
81 {
82 String partial = (String)Hibernate.STRING.nullSafeGet(resultSet, string);
83 Partial result = new Partial();
84 if (partial == null || partial.length() != 8)
85 {
86 return null;
87 }
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));
91
92 if (year != 0){
93 result = result.with(DateTimeFieldType.year(), year);
94 }
95 if (month != 0){
96 result = result.with(DateTimeFieldType.monthOfYear(), month);
97 }
98 if (day != 0){
99 result = result.with(DateTimeFieldType.dayOfMonth(), day);
100 }
101 return result;
102 }
103
104
105 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException
106 {
107 if (value == null)
108 {
109 Hibernate.STRING.nullSafeSet(preparedStatement, null, index);
110 }
111 else
112 {
113 Partial p = ((Partial) value);
114 Hibernate.STRING.nullSafeSet(preparedStatement, partialToString(p), index);
115 }
116 }
117
118 /**
119 * @param p
120 * @return an ISO 8601 like time representations of the form yyyyMMdd
121 */
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??
125 //
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;
130 return result;
131 }
132
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);
137 }else{
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;
145 }
146 return result;
147 }
148 }
149
150 public Object deepCopy(Object value) throws HibernateException
151 {
152 if (value == null)
153 {
154 return null;
155 }
156
157 return new Partial((Partial)value);
158 }
159
160 public boolean isMutable()
161 {
162 return false;
163 }
164
165 public Serializable disassemble(Object value) throws HibernateException
166 {
167 return (Serializable) value;
168 }
169
170 public Object assemble(Serializable cached, Object value) throws HibernateException
171 {
172 return cached;
173 }
174
175 public Object replace(Object original, Object target, Object owner) throws HibernateException
176 {
177 return original;
178 }
179
180
181 }
182