001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql;
024:
025: import java.lang.reflect.Constructor;
026: import java.lang.reflect.InvocationTargetException;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import java.util.Map;
030:
031: /**
032: * This projector constructs objects using database field values
033: * @author Pavel Vlasov
034: * @version $Revision: 1.2 $
035: */
036: public class ConstructorProjector extends BaseReflectionProjector
037: implements Projector {
038: public static class ColumnName {
039: private String name;
040:
041: public ColumnName(String name) {
042: this .name = name;
043: }
044: }
045:
046: private Object[] args;
047: private Constructor constructor;
048:
049: /**
050: * Use this constructor if target constructor shall take not only values from database fields as
051: * parameter, but other objects as well.
052: * @param constructor Constructor to instantiate object.
053: * @param args Constructor arguments. Arguments of type {@link ColumnName} will be replaced
054: * with values from corresponding fields from the database.
055: * @param typeMap See {@link java.sql.ResultSet#getObject(java.lang.String, java.util.Map)}. Can be null.
056: */
057: public ConstructorProjector(Constructor constructor, Object[] args,
058: Map typeMap) {
059: super (typeMap);
060: if (constructor.getParameterTypes().length != args.length) {
061: throw new IllegalArgumentException(
062: "argNames length shall be equal to the number of constructor parameters");
063: }
064: this .constructor = constructor;
065: this .args = args;
066: }
067:
068: /**
069: * Use this constructor if only database fields values are used as target constructor parameters.
070: * @param constructor Constructor to instantiate object
071: * @param columnNames Names of columns that shall be passed as parameters to constructor.
072: * @param typeMap See {@link java.sql.ResultSet#getObject(java.lang.String, java.util.Map)}. Can be null.
073: */
074: public ConstructorProjector(Constructor constructor,
075: String[] columnNames, Map typeMap) {
076: this (constructor, columnNamesToArgs(columnNames), typeMap);
077: }
078:
079: /**
080: * Use constructor for positioned projection. Column values will be passed to as constructor
081: * arguments according to their position.
082: * @param constructor
083: * @param typeMap
084: */
085: public ConstructorProjector(Constructor constructor, Map typeMap) {
086: super (typeMap);
087: this .constructor = constructor;
088: }
089:
090: /**
091: *
092: * @param fieldNames
093: * @return
094: */
095: private static Object[] columnNamesToArgs(String[] fieldNames) {
096: Object[] ret = new Object[fieldNames.length];
097: for (int i = 0; i < fieldNames.length; i++) {
098: ret[i] = new ColumnName(fieldNames[i]);
099: }
100: return ret;
101: }
102:
103: public Object project(ResultSet rs) throws SQLException {
104: Object[] params = new Object[constructor.getParameterTypes().length];
105: for (int i = 0; i < params.length; i++) {
106: if (args == null) {
107: params[i] = getColumn(rs, i + 1);
108: } else if (args[i] instanceof ColumnName) {
109: params[i] = getColumn(rs, ((ColumnName) args[i]).name);
110: } else {
111: params[i] = args[i];
112: }
113: }
114:
115: try {
116: return constructor.newInstance(params);
117: } catch (InvocationTargetException e) {
118: throw new SQLExceptionEx(e);
119: } catch (InstantiationException e) {
120: throw new SQLExceptionEx(e);
121: } catch (IllegalAccessException e) {
122: throw new SQLExceptionEx(e);
123: }
124: }
125:
126: }
|