2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.hibernate
;
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
;
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
;
25 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleReadAlignment
.Shift
;
28 * Hibernate user type for arrays of int(eger).
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);
37 private static final String SHIFT_SEPARATOR
= ";";
38 private static final String ATTR_SEPARATOR
= ",";
40 private static final int[] SQL_TYPES
= { Types
.CLOB
};
43 public Object
deepCopy(Object o
) throws HibernateException
{
44 return o
; //do we need more?
49 public Serializable
disassemble(Object value
) throws HibernateException
{
53 Shift
[] ints
= (Shift
[]) value
;
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
);
61 if (result
.length() > 0){
62 result
= result
.substring(1);
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
);
77 Shift
[] result
= nullSafeGet(val
);
79 } catch (IllegalArgumentException e
) {
80 throw new HibernateException(e
);
86 protected Shift
[] nullSafeGet(String val
) {
87 if (val
.length() == 0){
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]);
103 public void nullSafeSet(PreparedStatement statement
, Object value
, int index
, SessionImplementor session
)
104 throws HibernateException
, SQLException
{
106 StandardBasicTypes
.STRING
.nullSafeSet(statement
, value
, index
, session
);
108 String str
= (String
)disassemble(value
);
109 StandardBasicTypes
.STRING
.nullSafeSet(statement
, str
, index
, session
);
114 public Class
<?
> returnedClass() {
115 return Shift
[].class;
119 public int[] sqlTypes() {