Project

General

Profile

Download (4.32 KB) Statistics
| Branch: | Tag: | Revision:
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.AbstractUserType;
23
import org.joda.time.DateTimeFieldType;
24
import org.joda.time.Partial;
25

    
26
/**
27
 * Persist {@link org.joda.time.Partial} via hibernate.
28
 * This is a preliminary implementation that fulfills the needs of CDM but does not fully store a Partial.
29
 * Only year, month and day is stored
30
 * @author a.mueller
31
 * @created 11.11.2008
32
 * @version 2.0
33
 */
34
public class PartialUserType extends AbstractUserType implements UserType /* extends AbstractSingleColumnUserType<Partial, String, ColumnMapper<Partial,String>> implements UserType */ {
35
	private static final long serialVersionUID = -5323104403077597869L;
36

    
37
	private static final Logger logger = Logger.getLogger(PartialUserType.class);
38

    
39
	//not required
40
	public final static PartialUserType INSTANCE = new PartialUserType();
41

    
42
	private static final int[] SQL_TYPES = new int[]{
43
	    Types.VARCHAR,
44
	};
45

    
46

    
47
	@Override
48
	public Partial nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) 
49
			throws HibernateException, SQLException {
50
		String partial = (String)StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
51
		Partial result = new Partial(); 
52
		if (partial == null || partial.length() != 8) {
53
			return null;
54
		}
55
		Integer year = Integer.valueOf(partial.substring(0,4));
56
		Integer month = Integer.valueOf(partial.substring(4,6));
57
		Integer day = Integer.valueOf(partial.substring(6,8));
58
		
59
		if (year != 0){
60
			result = result.with(DateTimeFieldType.year(), year);
61
		}
62
		if (month != 0){
63
			result = result.with(DateTimeFieldType.monthOfYear(), month);
64
		}
65
		if (day != 0){
66
			result = result.with(DateTimeFieldType.dayOfMonth(), day);
67
		}
68
		return result;
69
	}
70

    
71
	@Override
72
	public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index,
73
			SessionImplementor session) throws HibernateException, SQLException {
74
		if (value == null){
75
			StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index, session);
76
		}else {
77
			Partial p = ((Partial) value);
78
			StandardBasicTypes.STRING.nullSafeSet(preparedStatement, partialToString(p), index, session);
79
		}
80
	}
81

    
82
	/**
83
	 * @param p
84
	 * @return an ISO 8601 like time representations of the form yyyyMMdd
85
	 */
86
	public static String partialToString(Partial p) {
87
		//FIXME reduce code by use org.joda.time.format.ISODateTimeFormat.basicDate() instead ?
88
		//      for a date with unknown day this will produce e.g. 195712?? 
89
		// 		
90
		String strYear = getNullFilledString(p, DateTimeFieldType.year(),4);
91
		String strMonth = getNullFilledString(p, DateTimeFieldType.monthOfYear(),2);
92
		String strDay = getNullFilledString(p, DateTimeFieldType.dayOfMonth(),2);
93
		String result = strYear + strMonth + strDay;
94
		return result;
95
	}
96
	
97
	private static String getNullFilledString(Partial partial, DateTimeFieldType type, int count){
98
		String nul = "0000000000";
99
		if (! partial.isSupported(type)){
100
			return nul.substring(0, count);
101
		}else{
102
			int value = partial.get(type);
103
			String result = String.valueOf(value);
104
			if (result.length() > count){
105
				logger.error("value to long");
106
				result = result.substring(0, count);
107
			}else if (result.length() < count){
108
				result = nul.substring(0, count - result.length()) +  result;
109
			}
110
			return result;
111
		}
112
	}
113

    
114
    public Object deepCopy(Object value) throws HibernateException {
115
        if (value == null) {
116
            return null;
117
        }
118

    
119
        return new Partial((Partial)value);
120
    }
121

    
122
	@Override
123
	public int[] sqlTypes() {
124
		// TODO Auto-generated method stub
125
		return SQL_TYPES;
126
	}
127

    
128
	@Override
129
	public Class returnedClass() {
130
		// TODO Auto-generated method stub
131
		return null;
132
	}	
133

    
134
}
135

    
(6-6/11)