ObjectColumn.java

biz/hammurapi/sql/columns/ObjectColumn.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 117:41
Java Inspector 081 Avoid static collections, they can grow in size over time. 2 90:9
Java Inspector 081 Avoid static collections, they can grow in size over time. 2 165:9
Java Inspector 082 Parenthesis are redundant. 2 117:30
Java Inspector 089 Type is not documented 2 48:1
Java Inspector 089 Undocumented method 2 55:9
Java Inspector 089 Undocumented method 2 59:9
Java Inspector 089 Undocumented method 2 84:9
Java Inspector 089 Method is not properly documented 2 109:9
Java Inspector 089 Method is not properly documented 2 116:9
Java Inspector 089 Undocumented method 2 126:9
Java Inspector 089 Constructor is not properly documented 2 135:9
Java Inspector 089 Parameter name is not documented 2 135:9
Java Inspector 089 Parameter isPrimaryKey is not documented 2 135:9
Java Inspector 089 Undocumented constructor 2 140:9
Java Inspector 089 Undocumented constructor 2 149:9
Java Inspector 089 Parameter type is not documented 2 172:9
Java Inspector 089 Method return value is not properly documented 2 172:9
Java Inspector 089 Undocumented method 2 213:9
Java Inspector 089 Parameter ps is not documented 2 248:9
Java Inspector 089 Parameter value is not documented 2 248:9
Java Inspector 089 Parameter idx is not documented 2 248:9
Java Inspector 089 Undocumented method 2 272:9
Java Inspector 089 Undocumented method 2 279:9
Java Inspector 089 Undocumented method 2 283:5
Java Inspector 089 Undocumented method 2 300:9
Java Inspector 089 Undocumented method 2 304:9
Java Inspector 089 Undocumented method 2 312:9
Java Inspector 089 Undocumented method 2 319:9
Java Inspector 089 Undocumented method 2 326:9
Java Inspector 089 Undocumented method 2 330:9
Java Inspector 024 Avoid hardwired character literals 3 177:65
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 98:59
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 178:86
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 178:129
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 66:37
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 79:58
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 98:133
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 119:62
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 119:106
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 144:54
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 144:98
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 157:70
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 157:114
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 184:64
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 220:45
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 233:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 233:102
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 237:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 237:87
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 237:110
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 255:45
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 264:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 268:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 268:87
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 280:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 280:56
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 280:60
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 280:79
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 280:115
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 327:38

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.sql.columns;
24
25import java.lang.reflect.Method;
26import java.sql.PreparedStatement;
27import java.sql.ResultSet;
28import java.sql.ResultSetMetaData;
29import java.sql.SQLException;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.HashMap;
33import java.util.Iterator;
34import java.util.LinkedList;
35import java.util.Map;
36
37import biz.hammurapi.config.Context;
38import biz.hammurapi.convert.Converter;
39import biz.hammurapi.convert.ConvertingService;
40import biz.hammurapi.sql.SQLExceptionEx;
41
42
43/**
44 * @author Pavel Vlasov
45 *
46 * @version $Revision: 1.13 $
47 */
48public class ObjectColumn extends Column {
49 private Object value;
50
51 // Original stuff
52 private Object originalValue;
53 private boolean isOriginalValueSet;
54
55 public Object getOriginalValue() {
56 return isOriginalValueSet ? originalValue : value;
57 }
58
59 public void parameterizeOriginal(PreparedStatement ps, int idx) throws SQLException {
60 Object theOriginalValue=getOriginalValue();
61 if (theOriginalValue==null) {
62 ps.setNull(idx, sqlType==null ? java.sql.Types.NULL : sqlType.intValue());
63 } else {
64 try {
65 Method setter = findSetter(theOriginalValue.getClass());
66 if ("setObject".equals(setter.getName())) {
67 if (sqlType==null) {
68 ps.setObject(idx, theOriginalValue);
69 } else {
70 ps.setObject(idx, theOriginalValue, sqlType.intValue());
71 }
72 } else {
73 setter.invoke(ps, new Object[] {new Integer(idx), theOriginalValue});
74 }
75 return;
76 } catch (SQLException e) {
77 throw e;
78 } catch (Exception e) {
79 throw new SQLExceptionEx("Caused by: "+e, e);
80 }
81 }
82 }
83
84 public void setOriginal() {
85 originalValue=value;
86 isOriginalValueSet=true;
87 }
88 // End of original stuff
89
90 private static final Collection ALL_SETTERS;
91
92 private Integer sqlType;
93
94 static {
95 Collection allSetters=new LinkedList();
96 for (int i=0, mc=PreparedStatement.class.getMethods().length; i<mc; i++) {
97 Method m=PreparedStatement.class.getMethods()[i];
98 if (m.getParameterTypes().length==2 && m.getParameterTypes()[0].equals(int.class) && m.getName().startsWith("set")) {
99 allSetters.add(m);
100 }
101 }
102 ALL_SETTERS=Collections.unmodifiableCollection(allSetters);
103 }
104
105
106 /**
107 * @return Returns the value.
108 */
109 public Object getValue() {
110 return value;
111 }
112
113 /**
114 * @param value The value to set.
115 */
116 public void setValue(Object value) {
117 if (force || (this.value!=value && (this.value==null || !this.value.equals(value)))) {
118 if (clazz!=null && value!=null && !clazz.isInstance(value)) {
119 throw new ClassCastException("Invalid class "+value.getClass().getName()+", expected "+clazz.getName());
120 }
121 this.value = value;
122 onChange();
123 }
124 }
125
126 public void setSqlType(int sqlType) {
127 this.sqlType=new Integer(sqlType);
128 }
129
130 /**
131 * @param name
132 * @param clazz Column class, can be null.
133 * @param isPrimaryKey
134 */
135 public ObjectColumn(String name, Class clazz, boolean isPrimaryKey) {
136 super(name, isPrimaryKey);
137 this.clazz=clazz;
138 }
139
140 public ObjectColumn(String name, Class clazz, boolean isPrimaryKey, Object value) {
141 super(name, isPrimaryKey);
142 this.clazz=clazz;
143 if (clazz!=null && value!=null && !clazz.isInstance(value)) {
144 throw new ClassCastException("Invalid class "+value.getClass().getName()+", expected "+clazz.getName());
145 }
146 this.value=value;
147 }
148
149 public ObjectColumn(String name, Class clazz, boolean isPrimaryKey, ResultSet rs) throws SQLException {
150 super(name, isPrimaryKey);
151 this.clazz=clazz;
152 ResultSetMetaData metaData = rs.getMetaData();
153 for (int i=1, c=metaData.getColumnCount(); i<=c; i++) {
154 if (name.equals(metaData.getColumnName(i))) {
155 Object value = rs.getObject(i);
156 if (clazz!=null && value!=null && !clazz.isInstance(value)) {
157 throw new ClassCastException("Invalid class "+value.getClass().getName()+", expected "+clazz.getName());
158 }
159 this.value=value;
160 break;
161 }
162 }
163 }
164
165 private static Map setterMap=new HashMap();
166
167 /**
168 * Finds parameter setter for object type
169 * @param type
170 * @return
171 */
172 public static Method findSetter(Class type) {
173 synchronized (setterMap) {
174 String typeName = type.getName();
175 Method ret=(Method) setterMap.get(typeName);
176 if (ret==null) {
177 int dotIdx=typeName.lastIndexOf('.');
178 String shortName=typeName.substring(dotIdx+1, dotIdx+2).toUpperCase()+typeName.substring(dotIdx+2);
179
180 // Name match
181 Iterator it=ALL_SETTERS.iterator();
182 while (it.hasNext()) {
183 Method m=(Method) it.next();
184 if (m.getName().equals("set"+shortName) && m.getParameterTypes()[1].getName().equals(typeName)) {
185 setterMap.put(typeName, m);
186 return m;
187 }
188 }
189
190 Method candidate=null;
191 // Parameter match
192 it=ALL_SETTERS.iterator();
193 while (it.hasNext()) {
194 Method m=(Method) it.next();
195 if (m.getParameterTypes()[1].isAssignableFrom(type)) {
196 if (candidate==null || candidate.getParameterTypes()[1].isAssignableFrom(m.getParameterTypes()[1])) {
197 candidate=m;
198 }
199 }
200 }
201
202 setterMap.put(typeName, candidate);
203 return candidate;
204 }
205
206 return ret;
207 }
208 }
209
210 /* (non-Javadoc)
211 * @see biz.hammurapi.sql.columns.Column#parameterize(java.sql.PreparedStatement, int)
212 */
213 protected void parameterizeInternal(PreparedStatement ps, int idx) throws SQLException {
214 try {
215 if (value==null) {
216 ps.setNull(idx, sqlType==null ? java.sql.Types.NULL : sqlType.intValue());
217 } else {
218 try {
219 Method setter = findSetter(value.getClass());
220 if ("setObject".equals(setter.getName())) {
221 if (sqlType==null) {
222 ps.setObject(idx, value);
223 } else {
224 ps.setObject(idx, value, sqlType.intValue());
225 }
226 } else {
227 setter.invoke(ps, new Object[] {new Integer(idx), value});
228 }
229 return;
230 } catch (SQLException e) {
231 throw e;
232 } catch (Exception e) {
233 throw new SQLExceptionEx("Could not parameterize "+getName()+": "+e, e);
234 }
235 }
236 } catch (SQLException e) {
237 throw new SQLExceptionEx("Cannot parameterize. Value: "+value+", SQL Type: "+sqlType+", Cause: "+e, e);
238 }
239 }
240
241 /**
242 * Parameterizes prepared statement. Automatically finds proper setter.
243 * @param ps
244 * @param value
245 * @param idx
246 * @throws SQLException
247 */
248 public static void parameterize(PreparedStatement ps, Object value, int idx) throws SQLException {
249 try {
250 if (value==null) {
251 ps.setNull(idx, java.sql.Types.NULL);
252 } else {
253 try {
254 Method setter = findSetter(value.getClass());
255 if ("setObject".equals(setter.getName())) {
256 ps.setObject(idx, value);
257 } else {
258 setter.invoke(ps, new Object[] {new Integer(idx), value});
259 }
260 return;
261 } catch (SQLException e) {
262 throw e;
263 } catch (Exception e) {
264 throw new SQLExceptionEx("Could not parameterize: "+e, e);
265 }
266 }
267 } catch (SQLException e) {
268 throw new SQLExceptionEx("Cannot parameterize. Value: "+value+", Cause: "+e, e);
269 }
270 }
271
272 public Object getObjectValue(boolean ov) {
273 if (ov) {
274 return isOriginalValueSet ? originalValue : null;
275 }
276 return value;
277 }
278
279 public String toString() {
280 return getName()+(isModified() ? "*" : "")+"("+(value==null ? "N/A" : value.getClass().getName())+")="+value;
281 }
282
283 public boolean equals(Object otherColumn) {
284 if (!(otherColumn instanceof ObjectColumn)) {
285 return false;
286 }
287
288 Object otherValue=((ObjectColumn) otherColumn).value;
289 if (value==null) {
290 return otherValue==null;
291 }
292
293 if (otherValue==null) {
294 return false;
295 }
296
297 return value.equals(otherValue);
298 }
299
300 public int hashCode() {
301 return getName().hashCode() ^ (value==null ? 0 : value.hashCode());
302 }
303
304 public void load(String textValue) {
305 if (clazz==null || textValue==null) {
306 setValue(textValue);
307 } else {
308 setValue(ConvertingService.convert(textValue, clazz));
309 }
310 }
311
312 public void clear() {
313 setValue(null);
314 clearModified();
315 }
316
317 private Class clazz;
318
319 public void configure(Context context, Converter converter) {
320 Object o=context.get(getName());
321 if (o!=null) {
322 setValue(clazz==null ? o : converter.convert(o, clazz, null));
323 }
324 }
325
326 protected String getType() {
327 return clazz==null ? "java.lang.Object" : clazz.getName();
328 }
329
330 public void set(Column source) {
331 setValue(((ObjectColumn) source).getValue());
332 }
333
334 /**
335 * Clears modified flag and sets original value to current value.
336 */
337 public void clearModified() {
338 super.clearModified();
339 originalValue=value;
340 }
341}
342