Project

General

Profile

Download (3.5 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
package eu.etaxonomy.cdm.hibernate;
10

    
11
import java.io.Serializable;
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.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18
import org.hibernate.HibernateException;
19
import org.hibernate.engine.spi.SharedSessionContractImplementor;
20
import org.hibernate.type.StandardBasicTypes;
21
import org.hibernate.usertype.UserType;
22
import org.jadira.usertype.spi.shared.AbstractUserType;
23

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

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

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

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

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

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

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

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

    
83

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

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

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

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

    
121
}
(10-10/13)