PropertyProjector.java

biz/hammurapi/sql/PropertyProjector.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Constructor documentation is too short. It is only 1 words. Should be at least 3 words. 2 55:9
Java Inspector 089 Javadoc contains tag for non-existent parameter lenient 2 55:9
Java Inspector 089 Javadoc contains tag for non-existent parameter toLowerCase 2 55:9
Java Inspector 089 Undocumented parameter rs 2 64:9
Java Inspector 089 Undocumented exception SQLException 2 64:9
Java Inspector 089 Method return value is not documented 2 64:9

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;
24
25import java.sql.ResultSet;
26import java.sql.ResultSetMetaData;
27import java.sql.SQLException;
28import java.util.HashMap;
29import java.util.Map;
30
31import biz.hammurapi.config.ConfigurationException;
32import biz.hammurapi.config.DomConfigFactory;
33import biz.hammurapi.config.MapContext;
34
35/**
36 * Projects fields from result set to object properties (fields or setters)
37 * @author Pavel Vlasov
38 * @version $Revision: 1.3 $
39 */
40public class PropertyProjector extends BaseReflectionProjector implements Projector {
41 private Class objectClass;
42 private Map fieldMap;
43
44 /**
45 * Constructor
46 * @param objectClass Class to instantiate. Must have no-argument public constructor
47 * @param typeMap {@link java.sql.ResultSet#getObject(java.lang.String, java.util.Map)}
48 * @param fieldMap Field map. Can be null. If value of some mapping is null then that field
49 * is suppressed and is not set.
50 * @param lenient If it is set to false than exception will be thrown if property
51 * to set does not exist.
52 * @param toLowerCase If set to true then database field names will be converted to lower case
53 * for field/setters discovery.
54 */
55 public PropertyProjector(Class objectClass, Map typeMap, Map fieldMap) {
56 super(typeMap);
57 this.objectClass=objectClass;
58 this.fieldMap=fieldMap;
59 }
60
61 /**
62 * Instantiates object using default constructors and then sets properties.
63 */
64 public Object project(ResultSet rs) throws SQLException {
65 try {
66 Object instance = objectClass.newInstance();
67 ResultSetMetaData metaData = rs.getMetaData();
68 int cc=metaData.getColumnCount();
69 Map contextMap=new HashMap();
70 for (int i=1; i<=cc; i++) {
71 String colName=metaData.getColumnName(i);
72 String propertyName=propertyName(colName);
73
74 if (fieldMap!=null && fieldMap.containsKey(propertyName)) {
75 propertyName=(String) fieldMap.get(propertyName);
76 }
77
78 if (propertyName!=null) {
79 contextMap.put(propertyName, getColumn(rs, colName));
80 }
81 }
82 DomConfigFactory.inject(instance, new MapContext(contextMap));
83 return instance;
84 } catch (SQLException e) {
85 throw e;
86 } catch (Exception e) {
87 throw new SQLExceptionEx(e);
88 }
89 }
90
91}
92