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.dayOfYear(), 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
|
String strYear = getNullFilledString(p, DateTimeFieldType.year(),4);
|
115
|
String strMonth = getNullFilledString(p, DateTimeFieldType.monthOfYear(),2);
|
116
|
String strDay = getNullFilledString(p, DateTimeFieldType.dayOfMonth(),2);
|
117
|
String result = strYear + strMonth + strDay;
|
118
|
Hibernate.STRING.nullSafeSet(preparedStatement, result, index);
|
119
|
}
|
120
|
}
|
121
|
|
122
|
private String getNullFilledString(Partial partial, DateTimeFieldType type, int count){
|
123
|
String nul = "0000000000";
|
124
|
if (! partial.isSupported(type)){
|
125
|
return nul.substring(0, count);
|
126
|
}else{
|
127
|
int value = partial.get(type);
|
128
|
String result = String.valueOf(value);
|
129
|
if (result.length() > count){
|
130
|
logger.error("value to long");
|
131
|
result = result.substring(0, count);
|
132
|
}else if (result.length() < count){
|
133
|
result = nul.substring(0, count - result.length()) + result;
|
134
|
}
|
135
|
return result;
|
136
|
}
|
137
|
}
|
138
|
|
139
|
public Object deepCopy(Object value) throws HibernateException
|
140
|
{
|
141
|
if (value == null)
|
142
|
{
|
143
|
return null;
|
144
|
}
|
145
|
|
146
|
return new Partial((Partial)value);
|
147
|
}
|
148
|
|
149
|
public boolean isMutable()
|
150
|
{
|
151
|
return false;
|
152
|
}
|
153
|
|
154
|
public Serializable disassemble(Object value) throws HibernateException
|
155
|
{
|
156
|
return (Serializable) value;
|
157
|
}
|
158
|
|
159
|
public Object assemble(Serializable cached, Object value) throws HibernateException
|
160
|
{
|
161
|
return cached;
|
162
|
}
|
163
|
|
164
|
public Object replace(Object original, Object target, Object owner) throws HibernateException
|
165
|
{
|
166
|
return original;
|
167
|
}
|
168
|
|
169
|
|
170
|
}
|
171
|
|