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.hibernate;
|
11
|
|
12
|
import java.sql.PreparedStatement;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.sql.Types;
|
16
|
import java.time.LocalDate;
|
17
|
import java.time.Year;
|
18
|
import java.time.YearMonth;
|
19
|
import java.time.temporal.ChronoField;
|
20
|
import java.time.temporal.Temporal;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.hibernate.HibernateException;
|
24
|
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
25
|
import org.hibernate.type.StandardBasicTypes;
|
26
|
import org.hibernate.usertype.UserType;
|
27
|
import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
|
28
|
import org.joda.time.Partial;
|
29
|
|
30
|
|
31
|
|
32
|
/**
|
33
|
* Persist {@link org.joda.time.Partial} via hibernate.
|
34
|
* This is a preliminary implementation that fulfills the needs of CDM but does not fully store a Partial.
|
35
|
* Only year, month and day is stored
|
36
|
* @author a.mueller
|
37
|
* @created 11.11.2008
|
38
|
* @version 2.0
|
39
|
*/
|
40
|
public class PartialUserType extends AbstractUserType implements UserType /* extends AbstractSingleColumnUserType<Partial, String, ColumnMapper<Partial,String>> implements UserType */ {
|
41
|
private static final long serialVersionUID = -5323104403077597869L;
|
42
|
|
43
|
private static final Logger logger = Logger.getLogger(PartialUserType.class);
|
44
|
|
45
|
//not required
|
46
|
public final static PartialUserType INSTANCE = new PartialUserType();
|
47
|
|
48
|
private static final int[] SQL_TYPES = new int[]{
|
49
|
Types.VARCHAR,
|
50
|
};
|
51
|
|
52
|
|
53
|
@Override
|
54
|
public Temporal nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
|
55
|
throws HibernateException, SQLException {
|
56
|
String partial = (String)StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
|
57
|
Temporal result = null;
|
58
|
if (partial == null || partial.length() != 8) {
|
59
|
return null;
|
60
|
}
|
61
|
Integer year = Integer.valueOf(partial.substring(0,4));
|
62
|
Integer month = Integer.valueOf(partial.substring(4,6));
|
63
|
Integer day = Integer.valueOf(partial.substring(6,8));
|
64
|
|
65
|
if (year != 0){
|
66
|
result = Year.of(year);
|
67
|
}
|
68
|
if (month != 0){
|
69
|
result = YearMonth.of(year, month);
|
70
|
}
|
71
|
if (day != 0){
|
72
|
result = LocalDate.of(year, month, day);
|
73
|
}
|
74
|
return result;
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index,
|
79
|
SharedSessionContractImplementor session) throws HibernateException, SQLException {
|
80
|
if (value == null){
|
81
|
StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index, session);
|
82
|
}else {
|
83
|
Temporal p = ((Temporal) value);
|
84
|
StandardBasicTypes.STRING.nullSafeSet(preparedStatement, partialToString(p), index, session);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* @param p
|
90
|
* @return an ISO 8601 like time representations of the form yyyyMMdd
|
91
|
*/
|
92
|
public static String partialToString(Temporal p) {
|
93
|
//FIXME reduce code by use org.joda.time.format.ISODateTimeFormat.basicDate() instead ?
|
94
|
// for a date with unknown day this will produce e.g. 195712??
|
95
|
//
|
96
|
String strYear = getNullFilledString(p, ChronoField.YEAR,4);
|
97
|
String strMonth = getNullFilledString(p, ChronoField.MONTH_OF_YEAR,2);
|
98
|
String strDay = getNullFilledString(p, ChronoField.DAY_OF_MONTH,2);
|
99
|
String result = strYear + strMonth + strDay;
|
100
|
return result;
|
101
|
}
|
102
|
|
103
|
private static String getNullFilledString(Temporal partial, ChronoField type, int count){
|
104
|
String nul = "0000000000";
|
105
|
if (! partial.isSupported(type)){
|
106
|
return nul.substring(0, count);
|
107
|
}else{
|
108
|
int value = partial.get(type);
|
109
|
String result = String.valueOf(value);
|
110
|
if (result.length() > count){
|
111
|
logger.error("value to long");
|
112
|
result = result.substring(0, count);
|
113
|
}else if (result.length() < count){
|
114
|
result = nul.substring(0, count - result.length()) + result;
|
115
|
}
|
116
|
return result;
|
117
|
}
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public Object deepCopy(Object value) throws HibernateException {
|
122
|
if (value == null) {
|
123
|
return null;
|
124
|
}
|
125
|
|
126
|
return new Partial((Partial)value);
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
public int[] sqlTypes() {
|
131
|
// TODO Auto-generated method stub
|
132
|
return SQL_TYPES;
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
public Class returnedClass() {
|
137
|
// TODO Auto-generated method stub
|
138
|
return null;
|
139
|
}
|
140
|
|
141
|
}
|
142
|
|