01: /*
02: * hgcommons 7
03: * Hammurapi Group Common Library
04: * Copyright (C) 2003 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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: */
23: package biz.hammurapi.sql;
24:
25: import java.sql.ResultSet;
26: import java.sql.ResultSetMetaData;
27: import java.sql.SQLException;
28: import java.util.HashMap;
29: import java.util.Map;
30:
31: import biz.hammurapi.config.ConfigurationException;
32: import biz.hammurapi.config.DomConfigFactory;
33: import 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: */
40: public class PropertyProjector extends BaseReflectionProjector
41: implements Projector {
42: private Class objectClass;
43: private Map fieldMap;
44:
45: /**
46: * Constructor
47: * @param objectClass Class to instantiate. Must have no-argument public constructor
48: * @param typeMap {@link java.sql.ResultSet#getObject(java.lang.String, java.util.Map)}
49: * @param fieldMap Field map. Can be null. If value of some mapping is null then that field
50: * is suppressed and is not set.
51: * @param lenient If it is set to false than exception will be thrown if property
52: * to set does not exist.
53: * @param toLowerCase If set to true then database field names will be converted to lower case
54: * for field/setters discovery.
55: */
56: public PropertyProjector(Class objectClass, Map typeMap,
57: Map fieldMap) {
58: super (typeMap);
59: this .objectClass = objectClass;
60: this .fieldMap = fieldMap;
61: }
62:
63: /**
64: * Instantiates object using default constructors and then sets properties.
65: */
66: public Object project(ResultSet rs) throws SQLException {
67: try {
68: Object instance = objectClass.newInstance();
69: ResultSetMetaData metaData = rs.getMetaData();
70: int cc = metaData.getColumnCount();
71: Map contextMap = new HashMap();
72: for (int i = 1; i <= cc; i++) {
73: String colName = metaData.getColumnName(i);
74: String propertyName = propertyName(colName);
75:
76: if (fieldMap != null
77: && fieldMap.containsKey(propertyName)) {
78: propertyName = (String) fieldMap.get(propertyName);
79: }
80:
81: if (propertyName != null) {
82: contextMap
83: .put(propertyName, getColumn(rs, colName));
84: }
85: }
86: DomConfigFactory.inject(instance,
87: new MapContext(contextMap));
88: return instance;
89: } catch (InstantiationException e) {
90: throw new SQLExceptionEx(e);
91: } catch (IllegalAccessException e) {
92: throw new SQLExceptionEx(e);
93: } catch (ConfigurationException e) {
94: throw new SQLExceptionEx(e);
95: }
96: }
97:
98: }
|