KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > usertypes > LongStringUserType


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 20, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 /*
19  * This class is built to support saving and loading of long strings from hibernate. Specifically,
20  * strings greater than 254 characters will be saved as CLOBs instead of varchars. The 254 character
21  * limit was specifically chosen as the limit because MySQL has a 255 character limit on the length
22  * of a varchar.
23  *
24  * This implementation will return the CLOBs as StringBuffers.
25  */

26 package org.pentaho.repository.usertypes;
27
28 import java.io.Serializable JavaDoc;
29 import java.io.StringReader JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Types JavaDoc;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.hibernate.HibernateException;
37 import org.hibernate.usertype.UserType;
38 import org.pentaho.core.system.PentahoSystem;
39 import org.pentaho.messages.Messages;
40
41 public class LongStringUserType implements UserType {
42     private static Log log = LogFactory.getLog(LongStringUserType.class);
43
44     private final static boolean debug = PentahoSystem.debug;
45
46     private static final int[] SQLTYPE = { Types.CLOB }; // Persists as CLOBs
47

48     /*
49      * (non-Javadoc)
50      *
51      * @see org.hibernate.usertype.UserType#sqlTypes()
52      */

53     public int[] sqlTypes() {
54         return SQLTYPE;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see org.hibernate.usertype.UserType#returnedClass()
61      */

62     public Class JavaDoc returnedClass() {
63         return StringBuffer JavaDoc.class;
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see org.hibernate.usertype.UserType#equals(java.lang.Object,
70      * java.lang.Object)
71      */

72     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
73         if (x == y) {
74             return true;
75         }
76         if ((x == null) || (y == null) || (!(x instanceof StringBuffer JavaDoc)) || (!(y instanceof StringBuffer JavaDoc))) {
77             return false;
78         }
79         return x.toString().equals(y.toString());
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
86      */

87     public int hashCode(Object JavaDoc x) throws HibernateException {
88         return x.hashCode();
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
95      * java.lang.String[], java.lang.Object)
96      */

97     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
98         if (debug)
99             log.debug(Messages.getString("LONGSTRTYPE.DEBUG_NULL_SAFE_GET")); //$NON-NLS-1$
100
String JavaDoc longStr = rs.getString(names[0]);
101         return (longStr != null) ? new StringBuffer JavaDoc(longStr) : null;
102     }
103
104     /*
105      * (non-Javadoc)
106      *
107      * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
108      * java.lang.Object, int)
109      */

110     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
111         if (debug)
112             log.debug(Messages.getString("LONGSTRTYPE.DEBUG_NULL_SAFE_SET")); //$NON-NLS-1$
113
if (value != null) {
114             StringReader JavaDoc rdr = new StringReader JavaDoc(value.toString());
115             int sLen = ((StringBuffer JavaDoc) value).length();
116             st.setCharacterStream(index, rdr, sLen);
117         } else {
118             st.setNull(index, SQLTYPE[0]);
119         }
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
126      */

127     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
128         return new StringBuffer JavaDoc(value.toString());
129     }
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see org.hibernate.usertype.UserType#isMutable()
135      */

136     public boolean isMutable() {
137         return false;
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
144      */

145     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
146         return (Serializable JavaDoc) value;
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
153      * java.lang.Object)
154      */

155     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException {
156         return cached;
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see org.hibernate.usertype.UserType#replace(java.lang.Object,
163      * java.lang.Object, java.lang.Object)
164      */

165     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) throws HibernateException {
166         return original;
167     }
168 }
169
Popular Tags