001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of JPersist.
006: *
007: * JPersist is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * JPersist is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with JPersist; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jpersist;
020:
021: import java.lang.reflect.InvocationTargetException;
022: import jcommontk.utils.StringUtils;
023: import java.lang.reflect.InvocationHandler;
024: import java.lang.reflect.Method;
025: import java.sql.SQLException;
026: import java.util.ListIterator;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029: import jcommontk.object.ObjectConverter;
030: import jpersist.interfaces.ResultObject;
031:
032: @SuppressWarnings("unchecked")
033: // working to complete a Java 1.5 version
034: class ResultProxy implements InvocationHandler {
035: private static Logger logger = Logger.getLogger(ResultProxy.class
036: .getName());
037:
038: Class objectClass;
039: Result result;
040: Object[] handlers;
041:
042: ResultProxy(Result result, Class objectClass, Object[] handlers)
043: throws JPersistException, SQLException {
044: this .result = result;
045: this .objectClass = objectClass;
046: this .handlers = handlers;
047: }
048:
049: public Object invoke(Object proxy, Method method, Object[] args)
050: throws Throwable {
051: String name = method.getName();
052: Object returnValue = null;
053:
054: try {
055: if (method.getDeclaringClass().equals(ResultObject.class)
056: || method.getDeclaringClass().equals(
057: ListIterator.class)) {
058: if (name.equals("next")) {
059: result.next();
060:
061: returnValue = proxy;
062: } else if (name.equals("previous")) {
063: result.previous();
064:
065: returnValue = proxy;
066: } else
067: returnValue = result.getClass().getDeclaredMethod(
068: method.getName(),
069: method.getParameterTypes()).invoke(result,
070: args);
071: } else {
072: Object object;
073:
074: if (handlers != null
075: && handlers.length > 0
076: && (object = checkHandlers(method, args)) != null)
077: return object;
078: else if (name.startsWith("get"))
079: return getDBValue(method, name.substring(3));
080: else if (name.startsWith("set"))
081: return setDBValue(name.substring(3), args[0]);
082: }
083: } catch (Exception e) {
084: throw new JPersistException(e);
085: }
086:
087: return returnValue;
088: }
089:
090: Object checkHandlers(Method m, Object[] args)
091: throws IllegalAccessException, InvocationTargetException {
092: Class classes[] = null;
093: Object object = null;
094:
095: if (args != null && args.length > 0) {
096: classes = new Class[args.length];
097:
098: for (int i = 0; i < classes.length; i++)
099: classes[i] = args[i].getClass();
100: }
101:
102: for (int i = 0; i < handlers.length; i++) {
103: try {
104: Method method = handlers[i].getClass().getMethod(
105: m.getName(), classes);
106:
107: if (method != null)
108: object = method.invoke(handlers[i], args);
109: } catch (NoSuchMethodException e) {
110: if (logger.isLoggable(Level.FINEST))
111: logger.log(Level.FINEST, e.getMessage(), e);
112: }
113: }
114:
115: return object;
116: }
117:
118: Object getDBValue(Method method, String name)
119: throws JPersistException, SQLException {
120: Object obj;
121: String newName = null;
122:
123: try {
124: obj = result.getColumnValue(method.getReturnType(),
125: getColumnName(name));
126: } catch (Exception e) {
127: MetaData metaData = result.getDatabase().getMetaData();
128:
129: try {
130: newName = metaData.getStoresCase() == MetaData.STORES_UNKNOWN
131: || metaData.getStoresCase() == MetaData.STORES_LOWERCASE ? name
132: .toUpperCase()
133: : name.toLowerCase();
134:
135: obj = result.getColumnValue(method.getReturnType(),
136: newName);
137: } catch (Exception e2) {
138: newName = metaData.getStoresCase() == MetaData.STORES_UNKNOWN
139: || metaData.getStoresCase() == MetaData.STORES_LOWERCASE ? StringUtils
140: .camelCaseToUpperCaseUnderline(name)
141: : StringUtils
142: .camelCaseToLowerCaseUnderline(name);
143: obj = result.getColumnValue(method.getReturnType(),
144: newName);
145: }
146: }
147:
148: return ObjectConverter.convertObject(method.getReturnType(),
149: obj);
150: }
151:
152: Object setDBValue(String name, Object value)
153: throws JPersistException, SQLException {
154: result.setColumnValue(getColumnName(name), value);
155:
156: return null;
157: }
158:
159: String getColumnName(String name) {
160: try {
161: Database db = result.getDatabase();
162: MetaData.Table table = db.getMetaData().getTable(
163: db.getConnection(), db.getTableMapper(),
164: db.getColumnMapper(), db.getCatalogPattern(),
165: db.getSchemaPattern(),
166: ObjectSupport.getTableName(objectClass), null);
167:
168: if (table != null) {
169: MetaData.Table.Column column = table.getColumn(db
170: .getColumnMapper(), name, null);
171:
172: if (column != null)
173: return column.getColumnName();
174: }
175:
176: } catch (RuntimeException e) {
177: throw e;
178: } catch (Exception e) {
179: logger.log(Level.SEVERE, e.getMessage(), e);
180: }
181:
182: return objectClass.getName();
183: }
184: }
|