Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 015 | Do not change parameter value. For comprehensibility, formal parameters should be final | 2 | 186:25 |
Java Inspector 070-A | Cyclomatic complexity is too high: 18, maximum allowed is 12 | 2 | 183:9 |
Java Inspector 070-A | Cyclomatic complexity is too high: 17, maximum allowed is 12 | 2 | 260:9 |
Java Inspector 077 | To reduce probability of NullPointerException, use "string literal".equals(variable) instead of variable.equals("string literal"). | 2 | 201:43 |
Java Inspector 077 | To reduce probability of NullPointerException, use "string literal".equals(variable) instead of variable.equals("string literal"). | 2 | 372:75 |
Java Inspector 089 | Undocumented method | 2 | 71:9 |
Java Inspector 089 | Method is not properly documented | 2 | 89:9 |
Java Inspector 089 | Method is not properly documented | 2 | 96:9 |
Java Inspector 089 | Undocumented method | 2 | 100:9 |
Java Inspector 089 | Constructor is not properly documented | 2 | 111:5 |
Java Inspector 089 | Method is not properly documented | 2 | 121:5 |
Java Inspector 089 | Parameter ps is not documented | 2 | 132:5 |
Java Inspector 089 | Undocumented exception SQLException | 2 | 132:5 |
Java Inspector 089 | Undocumented parameter ps | 2 | 148:5 |
Java Inspector 089 | Undocumented parameter idx | 2 | 148:5 |
Java Inspector 089 | Undocumented exception SQLException | 2 | 148:5 |
Java Inspector 089 | Undocumented parameter ps | 2 | 153:5 |
Java Inspector 089 | Undocumented parameter idx | 2 | 153:5 |
Java Inspector 089 | Undocumented exception SQLException | 2 | 153:5 |
Java Inspector 089 | Method is not properly documented | 2 | 158:5 |
Java Inspector 089 | Method is not properly documented | 2 | 165:9 |
Java Inspector 089 | Return value documentation is too short. It is only 1 words. Should be at least 3 words. | 2 | 165:9 |
Java Inspector 089 | Undocumented method | 2 | 169:9 |
Java Inspector 089 | Method return value is not properly documented | 2 | 178:9 |
Java Inspector 089 | Method is not properly documented | 2 | 183:9 |
Java Inspector 089 | Parameter owner is not documented | 2 | 183:9 |
Java Inspector 089 | Undocumented parameter nodeName | 2 | 183:9 |
Java Inspector 089 | Undocumented parameter originalValue | 2 | 183:9 |
Java Inspector 089 | Undocumented method | 2 | 247:9 |
Java Inspector 089 | Method is not properly documented | 2 | 252:9 |
Java Inspector 089 | Method return value is not properly documented | 2 | 252:9 |
Java Inspector 089 | Method is not properly documented | 2 | 260:9 |
Java Inspector 089 | Parameter typeName is not documented | 2 | 260:9 |
Java Inspector 089 | Method is not properly documented | 2 | 285:9 |
Java Inspector 089 | Undocumented field | 2 | 289:9 |
Java Inspector 089 | Method is not properly documented | 2 | 295:9 |
Java Inspector 089 | Parameter force is not documented | 2 | 295:9 |
Java Inspector 089 | Method is not properly documented | 2 | 305:9 |
Java Inspector 089 | Parameter rs is not documented | 2 | 305:9 |
Java Inspector 089 | Parameter name is not documented | 2 | 305:9 |
Java Inspector 089 | Undocumented method | 2 | 321:9 |
Java Inspector 089 | Method is not properly documented | 2 | 328:5 |
Java Inspector 089 | Parameter label is not documented | 2 | 336:5 |
Java Inspector 089 | Parameter source is not documented | 2 | 355:9 |
Java Inspector 089 | Undocumented method | 2 | 359:9 |
Java Inspector 089 | Undocumented method | 2 | 363:9 |
Java Inspector 089 | Undocumented method | 2 | 367:9 |
Java Inspector 089 | Undocumented method | 2 | 371:9 |
Java Inspector 089 | Undocumented method | 2 | 389:9 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 114:71 |
Java Inspector 024 | Avoid hardwired character literals | 3 | 114:76 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 137:50 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 137:86 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 196:41 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 201:44 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 206:48 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 219:49 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 219:60 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 226:49 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 226:64 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 229:49 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 229:61 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 233:45 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 236:41 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 238:41 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 242:76 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 253:24 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 261:21 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 263:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 265:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 267:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 269:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 271:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 273:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 275:28 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 372:58 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 372:76 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 379:85 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 384:58 |
Java Inspector 040 | Parameter name force clashes with field name in Column | 3 | 132:60 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 261:17 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 263:24 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 265:24 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 267:24 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 269:24 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 271:24 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 273:24 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 275:24 |
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.io.Serializable;
26import java.sql.PreparedStatement;
27import java.sql.ResultSet;
28import java.sql.ResultSetMetaData;
29import java.sql.SQLException;
30import java.util.HashMap;
31import java.util.Map;
32
33import org.w3c.dom.Element;
34import org.w3c.dom.Node;
35import org.xml.sax.ContentHandler;
36import org.xml.sax.SAXException;
37
38import biz.hammurapi.config.ConfigurationException;
39import biz.hammurapi.config.Context;
40import biz.hammurapi.config.ContextConfigurable;
41import biz.hammurapi.config.DomConfigFactory;
42import biz.hammurapi.config.DomConfigurable;
43import biz.hammurapi.convert.ConvertingService;
44import biz.hammurapi.sql.SQLExceptionEx;
45import biz.hammurapi.util.Attributable;
46import biz.hammurapi.xml.dom.AbstractDomObject;
47import biz.hammurapi.xml.dom.DOMUtils;
48import biz.hammurapi.xml.dom.DomSerializable;
49
50
51/**
52 * Base class for different column types
53 * @author Pavel Vlasov
54 * @version $Revision: 1.14 $
55 */
56public abstract class Column
57implements
58 Cloneable,
59 ContextConfigurable,
60 DomConfigurable,
61 Attributable,
62 Serializable {
63
64 private ColumnChangeListener listener;
65 private String name;
66 private boolean isModified;
67 private boolean isPrimaryKey;
68 private String label;
69 private String xmlName;
70
71 public abstract void setOriginal();
72
73 /**
74 * Clears modified flag.
75 */
76 public void clearModified() {
77 isModified=false;
78 }
79
80 /**
81 * Sets field value to default and clears modified flag.
82 *
83 */
84 public abstract void clear();
85
86 /**
87 * @param listener The listener to set.
88 */
89 public void setListener(ColumnChangeListener listener) {
90 this.listener = listener;
91 }
92
93 /**
94 * @return Returns the listener.
95 */
96 public ColumnChangeListener getListener() {
97 return listener;
98 }
99
100 protected void onChange() {
101 isModified=true;
102 if (listener!=null) {
103 listener.onChange(this);
104 }
105 }
106
107 /**
108 * @param name Column name
109 * @param isPrimaryKey 'true' if column is part of primary key
110 */
111 public Column(String name, boolean isPrimaryKey) {
112 super();
113 this.name = name;
114 this.xmlName = name==null ? null : name.toLowerCase().replace('_', '-');
115 this.isPrimaryKey = isPrimaryKey;
116 }
117
118 /**
119 * @return Column name if value was modified, null otherwise.
120 */
121 public String listName() {
122 return isModified ? name : null;
123 }
124
125 /**
126 * Parameterizes prepared statement.
127 * @param ps
128 * @param idx Parameter index
129 * @param force If true column is treated a modified
130 * @return idx+1 if column was modified and prepared statement was parameterized, idx otherwise.
131 */
132 public int parameterize(PreparedStatement ps, int idx, boolean force) throws SQLException {
133 if (isModified || force) {
134 try {
135 parameterizeInternal(ps, idx);
136 } catch (SQLException e) {
137 throw new SQLExceptionEx("Could not parameterize "+getName()+": "+e, e);
138 }
139 return idx+1;
140 }
141
142 return idx;
143 }
144
145 /**
146 * Implement this method in subclasses.
147 */
148 protected abstract void parameterizeInternal(PreparedStatement ps, int idx) throws SQLException;
149
150 /**
151 * Implement this method in subclasses.
152 */
153 public abstract void parameterizeOriginal(PreparedStatement ps, int idx) throws SQLException;
154
155 /**
156 * @return Returns the isPrimaryKey.
157 */
158 public boolean isPrimaryKey() {
159 return isPrimaryKey;
160 }
161
162 /**
163 * @return name
164 */
165 public String getName() {
166 return name;
167 }
168
169 public String getXmlName() {
170 return xmlName;
171 }
172
173 /**
174 * Returns column value as object
175 * @param originalValue if true this method returns original value instead of current value.
176 * @return
177 */
178 public abstract Object getObjectValue(boolean originalValue);
179
180 /**
181 * @param owner
182 */
183 public void toDom(Element owner, String nodeName, boolean originalValue) {
184// String nodeName = domName==null ? name : domName;
185 if (nodeName==null) {
186 nodeName=getName();
187 }
188
189 Object objectValue=getObjectValue(false);
190
191 if (nodeName.length()==0) {
192 // Zero mapping
193 return;
194 }
195
196 if (nodeName.startsWith("@")) {
197 // Attribute mapping
198 if (objectValue!=null) {
199 owner.setAttribute(nodeName.substring(1), objectValue.toString());
200 }
201 } else if (nodeName.equals(".")) {
202 // Text mapping
203 if (objectValue!=null) {
204 owner.appendChild(owner.getOwnerDocument().createTextNode(objectValue.toString()));
205 }
206 } else if (nodeName.startsWith("!")) {
207 // Simple mapping
208 if (objectValue!=null) {
209 Element el=owner.getOwnerDocument().createElement(nodeName.substring(1));
210 owner.appendChild(el);
211 el.appendChild(owner.getOwnerDocument().createTextNode(objectValue.toString()));
212 }
213 } else {
214 // full mapping
215 Element el=owner.getOwnerDocument().createElement(nodeName);
216 owner.appendChild(el);
217
218 if (objectValue==null) {
219 el.setAttribute("is-null", "yes");
220 } else {
221 DomSerializable cds = (DomSerializable) ConvertingService.convert(objectValue, DomSerializable.class);
222 cds.toDom(el);
223 }
224
225 if (isPrimaryKey) {
226 el.setAttribute("primary-key", "yes");
227 }
228 if (isModified) {
229 el.setAttribute("modified", "yes");
230 }
231
232 if (label!=null) {
233 el.setAttribute("label", label);
234 }
235
236 el.setAttribute("column-type", getType());
237
238 el.setAttribute("align", getAlignment());
239
240 if (!attributes.isEmpty()) {
241 DomSerializable ads = (DomSerializable) ConvertingService.convert(attributes, DomSerializable.class);
242 ads.toDom(AbstractDomObject.addElement(el, "column-attributes"));
243 }
244 }
245 }
246
247 protected abstract String getType();
248
249 /**
250 * @return
251 */
252 protected String getAlignment() {
253 return "left";
254 }
255
256 /**
257 * @param typeName
258 * @return Column type for Java type
259 */
260 public static Class columnType(String typeName) {
261 if ("boolean".equals(typeName)) {
262 return BooleanColumn.class;
263 } else if ("byte".equals(typeName)) {
264 return ByteColumn.class;
265 } else if ("char".equals(typeName)) {
266 return CharColumn.class;
267 } else if ("double".equals(typeName)) {
268 return DoubleColumn.class;
269 } else if ("float".equals(typeName)) {
270 return FloatColumn.class;
271 } else if ("int".equals(typeName)) {
272 return IntColumn.class;
273 } else if ("long".equals(typeName)) {
274 return LongColumn.class;
275 } else if ("short".equals(typeName)) {
276 return ShortColumn.class;
277 } else {
278 return ObjectColumn.class;
279 }
280 }
281
282 /**
283 * @return Returns the isModified.
284 */
285 public boolean isModified() {
286 return isModified;
287 }
288
289 protected boolean force;
290// private String domName;
291
292 /**
293 * @param force
294 */
295 public void setForce(boolean force) {
296 this.force=force;
297 }
298
299 /**
300 * @param rs
301 * @param name
302 * @return True if result set has a column with specified name.
303 * @throws SQLException
304 */
305 public static boolean hasColumn(ResultSet rs, String name) throws SQLException {
306 ResultSetMetaData metaData = rs.getMetaData();
307 for (int i=1, c=metaData.getColumnCount(); i<=c; i++) {
308 if (name.equals(metaData.getColumnName(i))) {
309 return true;
310 }
311 }
312 return false;
313 }
314
315 /**
316 * Loads value from XML Element
317 * @param textValue Text value
318 */
319 public abstract void load(String textValue);
320
321 public Object clone() throws CloneNotSupportedException {
322 return super.clone();
323 }
324
325 /**
326 * @return display label for column
327 */
328 public String getLabel() {
329 return label;
330 }
331
332 /**
333 * Sets display label for column
334 * @param label
335 */
336 public void setLabel(String label) {
337 this.label = label;
338 }
339
340// /**
341// * @param domName
342// */
343// public void setDomName(String domName) {
344// this.domName=domName;
345// }
346//
347// public String getDomName() {
348// return domName;
349// }
350
351 /**
352 * Copies values from source column
353 * @param source
354 */
355 public abstract void set(Column source);
356
357 private Map attributes=new HashMap();
358
359 public void setAttribute(Object key, Object value) {
360 attributes.put(key, value);
361 }
362
363 public Object getAttribute(Object key) {
364 return attributes.get(key);
365 }
366
367 public Object removeAttribute(Object key) {
368 return attributes.remove(key);
369 }
370
371 public void configure(Node configNode, Context context, ClassLoader classLoader) throws ConfigurationException {
372 if (!((Element) configNode).getAttribute("is-null").equals("yes")) {
373 load(AbstractDomObject.getElementText(configNode));
374 }
375
376 DomConfigFactory dcf=new DomConfigFactory();
377 attributes.clear();
378 try {
379 Node attributesNode = DOMUtils.selectSingleNode(configNode, "column-attributes");
380 if (attributesNode!=null) {
381 attributes.putAll((Map) dcf.create(attributesNode));
382 }
383 } catch (Exception e) {
384 throw new ConfigurationException("Cannot load column attributes: "+e, e);
385 }
386
387 }
388
389 public void toSax(String uri, ContentHandler contentHandler) throws SAXException {
390 Object objectValue=getObjectValue(false);
391 if (objectValue!=null && xmlName!=null) {
392 contentHandler.startElement(uri, xmlName, xmlName, null);
393 char[] strValue = String.valueOf(objectValue).toCharArray(); // TODO SAX Serializable, composite serializer like with DOM.
394 contentHandler.characters(strValue, 0, strValue.length);
395 contentHandler.endElement(uri, xmlName, xmlName);
396 }
397 }
398}
399