merge trunk to hibernate4
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / 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.hibernate;
11
12 import java.sql.PreparedStatement;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.sql.Types;
16
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;
27
28 /**
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
32 * @author a.mueller
33 * @created 11.11.2008
34 * @version 2.0
35 */
36 public class PartialUserType extends AbstractUserType implements UserType /* extends AbstractSingleColumnUserType<Partial, String, ColumnMapper<Partial,String>> implements UserType */ {
37 private static final long serialVersionUID = -5323104403077597869L;
38
39 private static final Logger logger = Logger.getLogger(PartialUserType.class);
40
41 //not required
42 public final static PartialUserType INSTANCE = new PartialUserType();
43
44 private static final int[] SQL_TYPES = new int[]{
45 Types.VARCHAR,
46 };
47
48
49 @Override
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) {
55 return null;
56 }
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));
60
61 if (year != 0){
62 result = result.with(DateTimeFieldType.year(), year);
63 }
64 if (month != 0){
65 result = result.with(DateTimeFieldType.monthOfYear(), month);
66 }
67 if (day != 0){
68 result = result.with(DateTimeFieldType.dayOfMonth(), day);
69 }
70 return result;
71 }
72
73 @Override
74 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index,
75 SessionImplementor session) throws HibernateException, SQLException {
76 if (value == null){
77 StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index, session);
78 }else {
79 Partial p = ((Partial) value);
80 StandardBasicTypes.STRING.nullSafeSet(preparedStatement, partialToString(p), index, session);
81 }
82 }
83
84 /**
85 * @param p
86 * @return an ISO 8601 like time representations of the form yyyyMMdd
87 */
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??
91 //
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;
96 return result;
97 }
98
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);
103 }else{
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;
111 }
112 return result;
113 }
114 }
115
116 public Object deepCopy(Object value) throws HibernateException {
117 if (value == null) {
118 return null;
119 }
120
121 return new Partial((Partial)value);
122 }
123
124 @Override
125 public int[] sqlTypes() {
126 // TODO Auto-generated method stub
127 return SQL_TYPES;
128 }
129
130 @Override
131 public Class returnedClass() {
132 // TODO Auto-generated method stub
133 return null;
134 }
135
136 }
137