KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.pentaho.repository.usertypes;
19
20 import java.io.Serializable JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Types JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.hibernate.Hibernate;
28 import org.hibernate.HibernateException;
29 import org.hibernate.usertype.UserType;
30 import org.hibernate.util.EqualsHelper;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.messages.Messages;
33
34 public class EmptyStringUserType implements UserType {
35     private static Log log = LogFactory.getLog(EmptyStringUserType.class);
36
37     private final static boolean debug = PentahoSystem.debug;
38
39     private static final String JavaDoc PENTAHOEMPTY = Messages.getString("EMPTYSTRTYPE.CODE_PENTAHO_EMPTY_STRING"); //$NON-NLS-1$
40

41     private static final int[] SQLTYPE = { Types.VARCHAR };
42
43     /*
44      * (non-Javadoc)
45      *
46      * @see org.hibernate.usertype.UserType#sqlTypes()
47      */

48     public int[] sqlTypes() {
49         return SQLTYPE;
50     }
51
52     /*
53      * (non-Javadoc)
54      *
55      * @see org.hibernate.usertype.UserType#returnedClass()
56      */

57     public Class JavaDoc returnedClass() {
58         return String JavaDoc.class;
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.hibernate.usertype.UserType#equals(java.lang.Object,
65      * java.lang.Object)
66      */

67     public boolean equals(Object JavaDoc arg0, Object JavaDoc arg1) throws HibernateException {
68         return EqualsHelper.equals(arg0, arg1);
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
75      */

76     public int hashCode(Object JavaDoc arg0) throws HibernateException {
77         return arg0.hashCode();
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
84      * java.lang.String[], java.lang.Object)
85      */

86     public Object JavaDoc nullSafeGet(ResultSet JavaDoc arg0, String JavaDoc[] arg1, Object JavaDoc arg2) throws HibernateException, SQLException JavaDoc {
87         if (debug)
88             log.debug(Messages.getString("EMPTYSTRTYPE.DEBUG_NULL_SAFE_GET")); //$NON-NLS-1$
89
String JavaDoc colValue = (String JavaDoc) Hibernate.STRING.nullSafeGet(arg0, arg1[0]);
90         // _PENTAHOEMPTY_ shouldn't appear in the wild. So, check the string in
91
// the DB for this flag,
92
// and if it's there, then this must be an empty string.
93
return ((colValue != null) ? (colValue.equals(PENTAHOEMPTY) ? "" : colValue) : null); //$NON-NLS-1$
94
}
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
100      * java.lang.Object, int)
101      */

102     public void nullSafeSet(PreparedStatement JavaDoc arg0, Object JavaDoc arg1, int arg2) throws HibernateException, SQLException JavaDoc {
103         // If this is an empty string, write _PENTAHOEMPTY_ into the database.
104
if (debug)
105             log.debug(Messages.getString("EMPTYSTRTYPE.DEBUG_NULL_SAFE_SET")); //$NON-NLS-1$
106
Hibernate.STRING.nullSafeSet(arg0, (arg1 != null) ? ((((String JavaDoc) arg1).length() > 0) ? arg1 : PENTAHOEMPTY) : arg1, arg2);
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
113      */

114     public Object JavaDoc deepCopy(Object JavaDoc arg0) throws HibernateException {
115         return arg0;
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.hibernate.usertype.UserType#isMutable()
122      */

123     public boolean isMutable() {
124         return false;
125     }
126
127     /*
128      * (non-Javadoc)
129      *
130      * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
131      */

132     public Serializable JavaDoc disassemble(Object JavaDoc arg0) throws HibernateException {
133         return (Serializable JavaDoc) arg0;
134     }
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
140      * java.lang.Object)
141      */

142     public Object JavaDoc assemble(Serializable JavaDoc arg0, Object JavaDoc arg1) throws HibernateException {
143         return arg0;
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.hibernate.usertype.UserType#replace(java.lang.Object,
150      * java.lang.Object, java.lang.Object)
151      */

152     public Object JavaDoc replace(Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2) throws HibernateException {
153         return arg0;
154     }
155
156 }
157
Popular Tags