Project

General

Profile

Download (3.42 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.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.HibernateException;
20
import org.hibernate.engine.spi.SessionImplementor;
21
import org.hibernate.type.StandardBasicTypes;
22
import org.hibernate.usertype.UserType;
23
import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
24

    
25
import eu.etaxonomy.cdm.model.molecular.SingleReadAlignment.Shift;
26

    
27
/**
28
 * Hibernate user type for arrays of int(eger).
29
 * @author a.mueller
30
 * @since 03.12.2014
31
 */
32
public class ShiftUserType  extends AbstractUserType implements UserType {
33
	private static final long serialVersionUID = -2507496252811101383L;
34
	@SuppressWarnings("unused")
35
	private static final Logger logger = Logger.getLogger(ShiftUserType.class);
36

    
37
	private static final String SHIFT_SEPARATOR = ";";
38
	private static final String ATTR_SEPARATOR = ",";
39

    
40
	private static final int[] SQL_TYPES = { Types.CLOB };
41

    
42
	@Override
43
	public Object deepCopy(Object o) throws HibernateException {
44
		return o;  //do we need more?
45
	}
46

    
47

    
48
	@Override
49
	public Serializable disassemble(Object value) throws HibernateException {
50
		if(value == null) {
51
			return null;
52
		} else {
53
			Shift[] ints = (Shift[]) value;
54
		    String result = "";
55
		    for (Shift shift : ints){
56
		    	if (shift != null){  //null should never happen, but to be on the safe side
57
		    	    result += SHIFT_SEPARATOR + String.valueOf(shift.position);
58
		    	    result += ATTR_SEPARATOR + String.valueOf(shift.shift);
59
		    	}
60
		    }
61
		    if (result.length() > 0){
62
		    	result = result.substring(1);
63
		    }
64
		    return result;
65
		}
66
	}
67

    
68
	@Override
69
	public Shift[] nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
70
			throws HibernateException, SQLException {
71
        String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
72

    
73
		if(val == null) {
74
			return null;
75
		} else {
76
            try {
77
            	Shift[] result = nullSafeGet(val);
78
			    return result;
79
		    } catch (IllegalArgumentException e) {
80
			    throw new HibernateException(e);
81
		    }
82
		}
83
	}
84

    
85

    
86
	protected Shift[] nullSafeGet(String val) {
87
		if (val.length() == 0){
88
			return new Shift[0];
89
		}else{
90
			String[] splits = val.split(SHIFT_SEPARATOR);
91
			Shift[] result = new Shift[splits.length];
92
			for (int i = 0; i< splits.length ; i++){
93
				result[i] = new Shift();
94
				String[] split = splits[i].split(ATTR_SEPARATOR);
95
				result[i].position = Integer.valueOf(split[0]);
96
				result[i].shift = Integer.valueOf(split[1]);
97
			}
98
			return result;
99
		}
100
	}
101

    
102
	@Override
103
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
104
			throws HibernateException, SQLException {
105
		if (value == null) {
106
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
107
        } else {
108
        	String str = (String)disassemble(value);
109
            StandardBasicTypes.STRING.nullSafeSet(statement, str, index, session);
110
        }
111
	}
112

    
113
	@Override
114
	public Class<?> returnedClass() {
115
		return Shift[].class;
116
	}
117

    
118
	@Override
119
	public int[] sqlTypes() {
120
		return SQL_TYPES;
121
	}
122

    
123
}
(8-8/11)