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:
030: import biz.hammurapi.config.RuntimeConfigurationException;
031: import biz.hammurapi.convert.CompositeConverter;
032: import biz.hammurapi.convert.Converter;
033:
034: /**
035: * Base class for SQLC generated projectors.
036: * @author Pavel Vlasov
037: *
038: * @version $Revision: 1.3 $
039: */
040: public class SmartProjector implements Projector {
041: private Converter converter;
042: private Class targetClass;
043: private Class sourceClass;
044: private Constructor sourceClassConstructor;
045:
046: public Object project(ResultSet rs) throws SQLException {
047: Object ret;
048: if (targetClass == null) {
049: ret = defaultProject(rs);
050: } else {
051: if (sourceClass.isAssignableFrom(targetClass)) {
052: ret = project(rs, targetClass);
053: } else {
054: ret = CompositeConverter.getDefaultConverter().convert(
055: defaultProject(rs), targetClass, false);
056: }
057: }
058:
059: if (converter == null) {
060: return ret;
061: }
062:
063: return converter.convert(ret);
064: }
065:
066: /**
067: * @param rs
068: * @param ret
069: * @return
070: * @throws SQLException
071: */
072: private Object defaultProject(ResultSet rs) throws SQLException {
073: try {
074: return sourceClassConstructor
075: .newInstance(new Object[] { rs });
076: } catch (IllegalArgumentException e) {
077: throw new SQLExceptionEx(e);
078: } catch (InstantiationException e) {
079: throw new SQLExceptionEx(e);
080: } catch (IllegalAccessException e) {
081: throw new SQLExceptionEx(e);
082: } catch (InvocationTargetException e) {
083: throw new SQLExceptionEx(e);
084: }
085: }
086:
087: /**
088: * @param rs
089: * @return
090: * @throws SQLException
091: * @throws InstantiationException
092: * @throws IllegalAccessException
093: * @throws InvocationTargetException
094: * @throws NoSuchMethodException
095: */
096: private Object project(ResultSet rs, Class clazz)
097: throws SQLException {
098: try {
099: return clazz
100: .getConstructor(new Class[] { ResultSet.class })
101: .newInstance(new Object[] { rs });
102: } catch (IllegalArgumentException e) {
103: throw new SQLExceptionEx(e);
104: } catch (SecurityException e) {
105: throw new SQLExceptionEx(e);
106: } catch (InstantiationException e) {
107: throw new SQLExceptionEx(e);
108: } catch (IllegalAccessException e) {
109: throw new SQLExceptionEx(e);
110: } catch (InvocationTargetException e) {
111: throw new SQLExceptionEx(e);
112: } catch (NoSuchMethodException e) {
113: throw new SQLExceptionEx(e);
114: }
115: }
116:
117: public SmartProjector(Class sourceClass) {
118: this .sourceClass = sourceClass;
119: try {
120: this .sourceClassConstructor = sourceClass
121: .getConstructor(new Class[] { ResultSet.class });
122: } catch (SecurityException e) {
123: throw new RuntimeConfigurationException("Caused by: " + e,
124: e);
125: } catch (NoSuchMethodException e) {
126: throw new RuntimeConfigurationException(
127: "Constructor from ResultSet not found in "
128: + sourceClass + ": " + e, e);
129: }
130: }
131:
132: public SmartProjector(Class sourceClass, Class targetClass,
133: Converter converter) {
134: this .converter = converter;
135: this .targetClass = targetClass;
136: this .sourceClass = sourceClass;
137: try {
138: this .sourceClassConstructor = sourceClass
139: .getConstructor(new Class[] { ResultSet.class });
140: } catch (SecurityException e) {
141: throw new RuntimeConfigurationException("Caused by: " + e,
142: e);
143: } catch (NoSuchMethodException e) {
144: throw new RuntimeConfigurationException(
145: "Constructor from ResultSet not found in "
146: + sourceClass + ": " + e, e);
147: }
148: }
149: }
|