KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > CharType


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.access.types;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.sql.CallableStatement JavaDoc;
28 import java.sql.Clob JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Types JavaDoc;
33
34 import org.apache.cayenne.CayenneException;
35 import org.apache.cayenne.map.DbAttribute;
36 import org.apache.cayenne.validation.BeanValidationFailure;
37 import org.apache.cayenne.validation.ValidationResult;
38
39 /**
40  * Handles <code>java.lang.String</code>, mapping it as either of JDBC types - CLOB or
41  * (VAR)CHAR. Can be configured to trim trailing spaces.
42  *
43  * @author Andrus Adamchik
44  */

45 public class CharType extends AbstractType {
46
47     private static final int BUF_SIZE = 8 * 1024;
48
49     protected boolean trimmingChars;
50     protected boolean usingClobs;
51
52     public CharType(boolean trimingChars, boolean usingClobs) {
53         this.trimmingChars = trimingChars;
54         this.usingClobs = usingClobs;
55     }
56
57     /**
58      * Returns "java.lang.String".
59      */

60     public String JavaDoc getClassName() {
61         return String JavaDoc.class.getName();
62     }
63
64     /**
65      * Validates string property.
66      *
67      * @since 1.1
68      * @deprecated since 3.0 as validation should not be done at the DataNode level.
69      */

70     public boolean validateProperty(
71             Object JavaDoc source,
72             String JavaDoc property,
73             Object JavaDoc value,
74             DbAttribute dbAttribute,
75             ValidationResult validationResult) {
76
77         if (!(value instanceof String JavaDoc)) {
78             return true;
79         }
80
81         if (dbAttribute.getMaxLength() <= 0) {
82             return true;
83         }
84
85         String JavaDoc string = (String JavaDoc) value;
86         if (string.length() > dbAttribute.getMaxLength()) {
87             String JavaDoc message = "\""
88                     + property
89                     + "\" exceeds maximum allowed length ("
90                     + dbAttribute.getMaxLength()
91                     + " chars): "
92                     + string.length();
93             validationResult.addFailure(new BeanValidationFailure(
94                     source,
95                     property,
96                     message));
97
98             return false;
99         }
100
101         return true;
102     }
103
104     /** Return trimmed string. */
105     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
106
107         String JavaDoc val = null;
108
109         // CLOB handling
110
if (type == Types.CLOB) {
111             val = (isUsingClobs()) ? readClob(rs.getClob(index)) : readCharStream(
112                     rs,
113                     index);
114         }
115         else {
116
117             val = rs.getString(index);
118
119             // trim CHAR type
120
if (val != null && type == Types.CHAR && isTrimmingChars()) {
121                 val = val.trim();
122             }
123         }
124
125         return val;
126     }
127
128     /** Return trimmed string. */
129     public Object JavaDoc materializeObject(CallableStatement JavaDoc cs, int index, int type)
130             throws Exception JavaDoc {
131
132         String JavaDoc val = null;
133
134         // CLOB handling
135
if (type == Types.CLOB) {
136             if (!isUsingClobs()) {
137                 throw new CayenneException(
138                         "Character streams are not supported in stored procedure parameters.");
139             }
140
141             val = readClob(cs.getClob(index));
142         }
143         else {
144
145             val = cs.getString(index);
146
147             // trim CHAR type
148
if (val != null && type == Types.CHAR && isTrimmingChars()) {
149                 val = val.trim();
150             }
151         }
152
153         return val;
154     }
155
156     public void setJdbcObject(
157             PreparedStatement JavaDoc st,
158             Object JavaDoc val,
159             int pos,
160             int type,
161             int precision) throws Exception JavaDoc {
162
163         // if this is a CLOB column, set the value as "String"
164
// instead. This should work with most drivers
165
if (type == Types.CLOB) {
166             st.setString(pos, (String JavaDoc) val);
167         }
168         else {
169             super.setJdbcObject(st, val, pos, type, precision);
170         }
171     }
172
173     protected String JavaDoc readClob(Clob JavaDoc clob) throws IOException JavaDoc, SQLException JavaDoc {
174         if (clob == null) {
175             return null;
176         }
177
178         // sanity check on size
179
if (clob.length() > Integer.MAX_VALUE) {
180             throw new IllegalArgumentException JavaDoc(
181                     "CLOB is too big to be read as String in memory: " + clob.length());
182         }
183
184         int size = (int) clob.length();
185         if (size == 0) {
186             return "";
187         }
188
189         int bufSize = (size < BUF_SIZE) ? size : BUF_SIZE;
190
191         Reader JavaDoc in = clob.getCharacterStream();
192         return (in != null) ? readValueStream(
193                 new BufferedReader JavaDoc(in, bufSize),
194                 size,
195                 bufSize) : null;
196     }
197
198     protected String JavaDoc readCharStream(ResultSet JavaDoc rs, int index) throws IOException JavaDoc,
199             SQLException JavaDoc {
200         Reader JavaDoc in = rs.getCharacterStream(index);
201
202         return (in != null) ? readValueStream(in, -1, BUF_SIZE) : null;
203     }
204
205     protected String JavaDoc readValueStream(Reader JavaDoc in, int streamSize, int bufSize)
206             throws IOException JavaDoc {
207         char[] buf = new char[bufSize];
208         int read;
209         StringWriter JavaDoc out = (streamSize > 0)
210                 ? new StringWriter JavaDoc(streamSize)
211                 : new StringWriter JavaDoc();
212
213         try {
214             while ((read = in.read(buf, 0, bufSize)) >= 0) {
215                 out.write(buf, 0, read);
216             }
217             return out.toString();
218         }
219         finally {
220             in.close();
221         }
222     }
223
224     /**
225      * Returns <code>true</code> if 'materializeObject' method should trim trailing
226      * spaces from the CHAR columns. This addresses an issue with some JDBC drivers (e.g.
227      * Oracle), that return Strings for CHAR columsn padded with spaces.
228      */

229     public boolean isTrimmingChars() {
230         return trimmingChars;
231     }
232
233     public void setTrimmingChars(boolean trimingChars) {
234         this.trimmingChars = trimingChars;
235     }
236
237     public boolean isUsingClobs() {
238         return usingClobs;
239     }
240
241     public void setUsingClobs(boolean usingClobs) {
242         this.usingClobs = usingClobs;
243     }
244 }
245
Popular Tags