diff --git a/sandbox/projects/appex/src/java/search/Broker.java b/sandbox/projects/appex/src/java/search/Broker.java
new file mode 100644
index 00000000000..2ee5a9e7400
--- /dev/null
+++ b/sandbox/projects/appex/src/java/search/Broker.java
@@ -0,0 +1,94 @@
+package search;
+
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache Lucene" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache Lucene", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+import org.apache.torque.pool.DBConnection;
+import org.apache.torque.util.Criteria;
+
+import java.util.List;
+
+/**
+ * An interface to shield clients from knowledge of the underlying persistence
+ * implementation.
+ *
+ * @author Kelvin Tan
+ */
+public interface Broker
+{
+ /**
+ * Returns a list of objects given the Criteria.
+ */
+ List doSelect(Criteria crit) throws Exception;
+
+ /**
+ * Returns a list of objects given the Criteria.
+ */
+ List doSelect(Criteria crit, DBConnection dbCon) throws Exception;
+
+ /**
+ * Convenience method to obtain a single object via this broker.
+ */
+ Object getSingleObject(Criteria crit) throws ObjectNotFoundException, Exception;
+
+ /**
+ * Returns an object using it's primary keys.
+ */
+ Object retrieveByPK(String[] pk) throws Exception;
+
+ /**
+ * Returns an object using it's primary key.
+ */
+ Object retrieveByPK(String pk) throws Exception;
+}
diff --git a/sandbox/projects/appex/src/java/search/BrokerFactory.java b/sandbox/projects/appex/src/java/search/BrokerFactory.java
new file mode 100644
index 00000000000..007f8538f4b
--- /dev/null
+++ b/sandbox/projects/appex/src/java/search/BrokerFactory.java
@@ -0,0 +1,68 @@
+package search;
+
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache Lucene" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache Lucene", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+/**
+ * Factory class to obtain broker objects.
+ *
+ * @author Kelvin Tan
+ */
+public abstract class BrokerFactory
+{
+ public static Broker getBroker(Class bObj)
+ {
+ return new TorqueBroker(bObj);
+ }
+}
diff --git a/sandbox/projects/appex/src/java/search/NoSuchBrokerException.java b/sandbox/projects/appex/src/java/search/NoSuchBrokerException.java
new file mode 100644
index 00000000000..dc01a757d03
--- /dev/null
+++ b/sandbox/projects/appex/src/java/search/NoSuchBrokerException.java
@@ -0,0 +1,71 @@
+package search;
+
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache Lucene" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache Lucene", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+/**
+ * Thrown when a broker cannot be instantiated.
+ *
+ * @author Kelvin Tan
+ */
+public class NoSuchBrokerException extends RuntimeException
+{
+
+ /** Creates new GroupNotFoundException */
+ public NoSuchBrokerException(String message)
+ {
+ super(message);
+ }
+
+}
diff --git a/sandbox/projects/appex/src/java/search/ObjectNotFoundException.java b/sandbox/projects/appex/src/java/search/ObjectNotFoundException.java
new file mode 100644
index 00000000000..ff94419e678
--- /dev/null
+++ b/sandbox/projects/appex/src/java/search/ObjectNotFoundException.java
@@ -0,0 +1,90 @@
+package search;
+
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache Lucene" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache Lucene", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+import org.apache.torque.util.Criteria;
+
+/**
+ * Thrown when a Torque-persisted object can't be found when it is expected.
+ *
+ * @author Phang Soon-Ping
+ */
+public class ObjectNotFoundException extends Exception
+{
+ public ObjectNotFoundException(Class objectClass, String objectId)
+ {
+ super(generateMessage(objectClass, objectId));
+ }
+
+ public ObjectNotFoundException(Criteria crit)
+ {
+ super("Criteria :'" + crit.toString() + "' did not result in any object "
+ + " when one was expected.");
+ }
+
+ private static String generateMessage(Class c, String id)
+ {
+ String cName = c.getName();
+ int lastDot = cName.lastIndexOf('.');
+ if (lastDot < 0)
+ {
+ return cName + " with id " + id + " not found.";
+ }
+ else
+ {
+ return cName.substring(lastDot + 1) + " with ID \"" + id + "\" not found.";
+ }
+ }
+}
diff --git a/sandbox/projects/appex/src/java/search/SearchResultFactory.java b/sandbox/projects/appex/src/java/search/SearchResultFactory.java
new file mode 100644
index 00000000000..ce383fe52cb
--- /dev/null
+++ b/sandbox/projects/appex/src/java/search/SearchResultFactory.java
@@ -0,0 +1,216 @@
+package search;
+
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache Lucene" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache Lucene", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+import org.apache.log4j.Category;
+import org.apache.lucene.document.Document;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Factory class to materialize the objects which
+ * are search results.
+ */
+public abstract class SearchResultFactory
+{
+ /**
+ * Name of method for objects which don't have a broker, but which need to
+ * be initialized somehow.
+ */
+ public static final String INIT_OBJECT_METHOD = "initializeObject";
+
+ /**
+ * Arguments for INIT_OBJECT_METHOD.
+ */
+ public static final Class[] INIT_OBJECT_METHOD_ARGS = new Class[]{String.class};
+
+ public static final Class[] SEARCH_RESULT_WRAPPER_CTOR_ARGS
+ = new Class[]{Object.class};
+
+ private static Category cat = Category.getInstance(SearchResultFactory.class);
+
+ /**
+ *
+ * Materializes the object represented by the
+ * {@link org.apache.lucene.document.Document}.
+ *
+ *
+ * Objects which need to be returned via this method (essentially
+ * any object which implements
+ * {@link com.marketingbright.core.services.search.SearchResult},
+ * must either have an associated broker, or a no-arg ctor and
+ * INIT_OBJECT_METHOD with INIT_OBJECT_METHOD_ARGS.
+ *
+ */
+ public static Object getDocAsObject(Document doc) throws Exception
+ {
+ Class clazz = Class.forName(doc.get(DataSource.OBJECT_CLASS));
+ String id = doc.get(DataSource.OBJECT_IDENTIFIER);
+ Object o = null;
+ try
+ {
+ Broker broker = BrokerFactory.getBroker(clazz);
+ // assume it's a two-part compound pk if there's a comma
+ int indexOfComma = id.indexOf(',');
+ if (indexOfComma != -1)
+ {
+ String[] pks = new String[2];
+ pks[0] = id.substring(0, indexOfComma);
+ pks[1] = id.substring(indexOfComma + 1);
+ o = broker.retrieveByPK(pks);
+ }
+ else
+ {
+ o = broker.retrieveByPK(id);
+ }
+ }
+ catch (NoSuchBrokerException nsbe)
+ {
+ /**
+ * Some objects don't have brokers or peers, this offers an
+ * alternative.
+ */
+ o = clazz.newInstance();
+ invokeMethod(o, INIT_OBJECT_METHOD,
+ INIT_OBJECT_METHOD_ARGS, new Object[]{id});
+ }
+ String searchResultClassname = null;
+ searchResultClassname = doc.get(DataSource.SEARCH_RESULT_CLASSNAME);
+ return generateObject(searchResultClassname,
+ SEARCH_RESULT_WRAPPER_CTOR_ARGS,
+ new Object[]{o});
+ }
+
+ /**
+ * Utility method to invoke an object's method.
+ * @param o Object to invoke the method on.
+ * @param methodname Name of the method to invoke.
+ * @param parameter Method parameters.
+ * @param args Arguments the method requires.
+ * @return Object returned by the method.
+ */
+ private static Object invokeMethod(Object o, String methodname,
+ Class[] parameter, Object[] args)
+ {
+ Class c = o.getClass();
+ try
+ {
+ Method m = c.getMethod(methodname, parameter);
+ return m.invoke(o, args);
+ }
+ catch (NoSuchMethodException nsme)
+ {
+ cat.error(nsme.getMessage() + " This method doesn't exist..", nsme);
+ }
+ catch (IllegalAccessException iae)
+ {
+ cat.error("No access to " + iae.getMessage() + ".", iae);
+ }
+ catch (InvocationTargetException ite)
+ {
+ cat.error("Trouble invoking " + ite.getMessage(), ite);
+ }
+ return null;
+ }
+
+ /**
+ * Utility method to return an object based on its class name.
+ *
+ * @param type Class name of object to be generated
+ * @param clazz Class array of parameters.
+ * @param args Object array of arguments.
+ * @return Object
+ */
+ public static Object generateObject(String className,
+ Class[] clazz,
+ Object[] args)
+ {
+ Object o = null;
+ try
+ {
+ Class c = Class.forName(className);
+ Constructor con = c.getConstructor(clazz);
+ if (con != null)
+ {
+ o = con.newInstance(args);
+ }
+ else
+ throw new InstantiationException("Constructor with arguments:" + clazz.toString() + " non-existent.");
+ }
+ catch (ClassNotFoundException cnfe)
+ {
+ cat.error(cnfe.getMessage() + " No class named '" + className + "' was found.", cnfe);
+ }
+ catch (InstantiationException ie)
+ {
+ cat.error(ie.getMessage() + " Class named '" + className + "' could not be instantiated.", ie);
+ }
+ catch (IllegalAccessException iae)
+ {
+ cat.error(iae.getMessage() + " No access to class named '" + className + "'.", iae);
+ }
+ catch (NoSuchMethodException nsme)
+ {
+ cat.error(nsme.getMessage() + " No method in class named '" + className + "'.", nsme);
+ }
+ catch (InvocationTargetException ite)
+ {
+ cat.error(ite.getMessage() + " in class named '" + className + "'.", ite);
+ }
+ return o;
+ }
+}
diff --git a/sandbox/projects/appex/src/java/search/TorqueBroker.java b/sandbox/projects/appex/src/java/search/TorqueBroker.java
new file mode 100644
index 00000000000..962f8cb5518
--- /dev/null
+++ b/sandbox/projects/appex/src/java/search/TorqueBroker.java
@@ -0,0 +1,203 @@
+package search;
+
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache Lucene" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache Lucene", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+
+
+import org.apache.log4j.Category;
+import org.apache.torque.om.ComboKey;
+import org.apache.torque.om.ObjectKey;
+import org.apache.torque.om.StringKey;
+import org.apache.torque.pool.DBConnection;
+import org.apache.torque.util.Criteria;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Torque implementation of the Broker interface.
+ *
+ * @author Phang Soon-Ping
+ */
+public class TorqueBroker implements Broker
+{
+ protected static Map classMap = new Hashtable();
+ private static Category cat = Category.getInstance(TorqueBroker.class);
+ private static final String PEER_SUFFIX = "Peer";
+
+ protected Object peer;
+
+ public TorqueBroker(Class bObj) throws NoSuchBrokerException
+ {
+ String className = bObj.getName();
+ peer = classMap.get(bObj);
+ if (peer == null)
+ {
+ String peerClassName = className + PEER_SUFFIX;
+ try
+ {
+ peer = Class.forName(peerClassName).newInstance();
+ classMap.put(bObj, peer);
+ }
+ catch (Exception e)
+ {
+ throw new NoSuchBrokerException("Unable to obtain a broker for "
+ + bObj.getName() + '.');
+ }
+ }
+ }
+
+ public synchronized List doSelect(Criteria crit) throws Exception
+ {
+ return (List) invokeMethod(
+ peer,
+ "doSelect",
+ new Class[]{Criteria.class},
+ new Object[]{crit}
+ );
+ }
+
+ public synchronized List doSelect(Criteria crit, DBConnection dbCon)
+ throws Exception
+ {
+ return (List) invokeMethod(
+ peer,
+ "doSelect",
+ new Class[]{Criteria.class, DBConnection.class},
+ new Object[]{crit, dbCon}
+ );
+ }
+
+ public synchronized Object getSingleObject(Criteria crit)
+ throws ObjectNotFoundException, Exception
+ {
+ List matchingObjects = doSelect(crit);
+ if (!matchingObjects.isEmpty())
+ {
+ return matchingObjects.get(0);
+ }
+ else
+ {
+ throw new ObjectNotFoundException(crit);
+ }
+ }
+
+ public synchronized Object retrieveByPK(String[] pk) throws Exception
+ {
+ ObjectKey oKey = null;
+ if (pk.length > 1)
+ {
+ StringKey[] strKeys = new StringKey[pk.length];
+ for (int i = 0; i < pk.length; i++)
+ {
+ strKeys[i] = new StringKey(pk[i]);
+ }
+ oKey = new ComboKey(strKeys);
+ }
+ else
+ {
+ oKey = new StringKey(pk[0]);
+ }
+ return invokeMethod(
+ peer,
+ "retrieveByPK",
+ new Class[]{ObjectKey.class},
+ new Object[]{oKey});
+ }
+
+ public synchronized Object retrieveByPK(String pk) throws Exception
+ {
+ ObjectKey oKey = new StringKey(pk);
+ return invokeMethod(
+ peer,
+ "retrieveByPK",
+ new Class[]{ObjectKey.class},
+ new Object[]{oKey});
+ }
+
+ /**
+ * Utility method to invoke an object's method.
+ * @param o Object to invoke the method on.
+ * @param methodname Name of the method to invoke.
+ * @param parameter Method parameters.
+ * @param args Arguments the method requires.
+ * @return Object returned by the method.
+ */
+ private static Object invokeMethod(Object o, String methodname,
+ Class[] parameter, Object[] args)
+ {
+ Class c = o.getClass();
+ try
+ {
+ Method m = c.getMethod(methodname, parameter);
+ return m.invoke(o, args);
+ }
+ catch (NoSuchMethodException nsme)
+ {
+ cat.error(nsme.getMessage() + " This method doesn't exist..", nsme);
+ }
+ catch (IllegalAccessException iae)
+ {
+ cat.error("No access to " + iae.getMessage() + ".", iae);
+ }
+ catch (InvocationTargetException ite)
+ {
+ cat.error("Trouble invoking " + ite.getMessage(), ite);
+ }
+ return null;
+ }
+}