[cvs] expresso commit by lhamel: refactor search code to allow (external)

JCorporate Ltd jcorp at jcorporate.com
Thu Jan 27 03:28:18 UTC 2005


Log Message:
-----------
refactor search code to allow (external) usage of retrieval mechanism to populate objects

Modified Files:
--------------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj:
        MultiDBObject.java

Revision Data
-------------
Index: MultiDBObject.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/MultiDBObject.java,v
retrieving revision 1.66
retrieving revision 1.67
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/MultiDBObject.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/MultiDBObject.java -u -r1.66 -r1.67
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/MultiDBObject.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/MultiDBObject.java
@@ -89,6 +89,7 @@
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -1526,11 +1527,7 @@
      * @throws DBException If the search could not be completed
      */
     public synchronized List searchAndRetrieveList() throws DBException {
-
-        DBObject oneObj = null;
-
         HashMap rtrvListByTable = new HashMap();
-
         DBConnectionPool myPool = null;
         DBConnection myConnection = null;
 
@@ -1550,87 +1547,110 @@
 
             recordSet.clear();
 
-            myConnection.execute(getSQLSelectStatement(myPool, myConnection));
+            String sqlSelectStatement = getSQLSelectStatement(myPool, myConnection);
+            retrieveAndPopulate(myConnection, sqlSelectStatement, rtrvListByTable);
+        } finally {
+            if (localConnection == null) {
+                myConnection.release();
+            }
+        }
 
-            int returnRecordCount = 0;
-            int loopCount = 0;
-            while (myConnection.next()) {
-                loopCount++;
-
-                //If there's limitation syntax on, then the first record will be the
-                //maximum record.
-                if (loopCount <= offsetRecord && offsetRecord > 0 &&
-                        myConnection.getLimitationPosition() ==
-                        DBConnection.LIMITATION_DISABLED) {
-                    continue;
-                }
+        return recordSet;
+    }
 
-                returnRecordCount++;
+    /**
+     * Retrieve rows found by statement param, and put retrieved strings into
+     * DBObjects according to the mapping given in parameter, using prototype
+     * DBObjects set by addDBObj().
+     * <p/>
+     * This code was refactored from searchAndRetrieveList(), so see its usage there.
+     *
+     * @param myConnection    connection to use; caller is expected to suppy open connection and close after call
+     * @param sqlSelectStatement  SQL statement for retrieval
+     * @param rtrvListByTable map of fields for retrieval, keyed by table name, value is list of field names to be retrieved for the (key-designated) table
+     * @return A list of new MultiDBObjects retrieved from the sql statement
 
-                // maxRecords = 0 by default, so I guess this means 0 doesn't count as max number... should default to -1 ??  LAH 12/03
-                if ((returnRecordCount > maxRecords) && (maxRecords > 0)) {
-                    break;
-                }
+     * @see #searchAndRetrieveList()
+     * @see #addDBObj
+     */
+    public List retrieveAndPopulate(
+            DBConnection myConnection,
+            String sqlSelectStatement,
+            Map rtrvListByTable) throws DBException {
 
-                if (log.isDebugEnabled()) {
-                    log.debug("Returning row " + loopCount);
-                }
+        myConnection.execute(sqlSelectStatement);
 
-                MultiDBObject myObj = getThisMultiDBObj();
-                String oneFieldValue = null;
-                int i = 1;
+        DBObject oneObj;
+        int returnRecordCount = 0;
+        int loopCount = 0;
+        while (myConnection.next()) {
+            loopCount++;
 
-                for (Enumeration eachObj = myDBObjects.elements();
-                     eachObj.hasMoreElements();) {
-                    oneObj = (DBObject) eachObj.nextElement();
+            //If there's limitation syntax on, then the first record will be the maximum record.
+            if (loopCount <= offsetRecord && offsetRecord > 0 &&
+                    myConnection.getLimitationPosition() ==
+                    DBConnection.LIMITATION_DISABLED) {
+                continue;
+            }
 
-                    ArrayList retrievedFieldList = (ArrayList) rtrvListByTable.get(getTableName(oneObj));
-                    // The following should never happen .... but CYA
-                    if (retrievedFieldList == null) {
-                        retrievedFieldList = new ArrayList();
-                    }
-                    for (Iterator it = retrievedFieldList.listIterator();
-                         it.hasNext();) {
-                        java.lang.String fieldName = (String) it.next();
-                        DataFieldMetaData metaData = oneObj.getFieldMetaData(fieldName);
-
-                        if (!metaData.isVirtual() && !metaData.isBinaryObjectType()) {
-                            try {
-                                oneFieldValue = myConnection.getString(i);
-                            } catch (DBException de) {
-                                String myName = (thisClass + "searchAndRetrieve()");
-                                throw new DBException(myName +
-                                        ":Error retrieving field '" +
-                                        getTableName(oneObj) +
-                                        "." + fieldName + "'",
-                                        de);
-                            }
-
-                            i++;
-
-                            if (log.isDebugEnabled()) {
-                                log.debug("Setting " +
-                                        getTableName(oneObj) + "." +
-                                        fieldName + " to " + oneFieldValue);
-                            }
+            returnRecordCount++;
+
+            // maxRecords = 0 by default, so I guess this means 0 doesn't count as max number... should default to -1 ??  LAH 12/03
+            if ((returnRecordCount > maxRecords) && (maxRecords > 0)) {
+                break;
+            }
+
+            if (log.isDebugEnabled()) {
+                log.debug("Returning row " + loopCount);
+            }
+
+            // create new obj for this row; to be populated below
+            MultiDBObject myObj = getThisMultiDBObj();
+
+
+            String oneFieldValue = null;
+            int rowStringIndex = 1;
+            for (Enumeration eachObj = myDBObjects.elements(); eachObj.hasMoreElements();) {
+                oneObj = (DBObject) eachObj.nextElement();
+
+                ArrayList retrievedFieldList = (ArrayList) rtrvListByTable.get(getTableName(oneObj));
+                // The following should never happen .... but CYA
+                if (retrievedFieldList == null) {
+                    retrievedFieldList = new ArrayList();
+                }
+                for (Iterator it = retrievedFieldList.listIterator(); it.hasNext();) {
+                    String fieldName = (String) it.next();
+                    DataFieldMetaData metaData = oneObj.getFieldMetaData(fieldName);
+
+                    if (!metaData.isVirtual() && !metaData.isBinaryObjectType()) {
+                        try {
+                            oneFieldValue = myConnection.getString(rowStringIndex);
+                        } catch (DBException de) {
+                            String myName = (thisClass + "searchAndRetrieve()");
+                            throw new DBException(myName +
+                                    ":Error retrieving field '" +
+                                    getTableName(oneObj) +
+                                    "." + fieldName + "'",
+                                    de);
+                        }
+
+                        rowStringIndex++;
 
-                            myObj.setField((String) oneObj.getAttribute(SHORT_NAME),
-                                    fieldName, oneFieldValue);
+                        if (log.isDebugEnabled()) {
+                            log.debug("Setting " +
+                                    getTableName(oneObj) + "." +
+                                    fieldName + " to " + oneFieldValue);
                         }
+
+                        myObj.setField((String) oneObj.getAttribute(SHORT_NAME),
+                                fieldName, oneFieldValue);
                     }
                 }
-                /* each db object */
-
-                myObj.setDataContext(getDataContext());
-                recordSet.add(myObj);
-            }
+            } /* each db object */
 
-            /* each row retrieved from the db */
-        } finally {
-            if (localConnection == null) {
-                myPool.release(myConnection);
-            }
-        }
+            myObj.setDataContext(getDataContext());
+            recordSet.add(myObj);
+        } // all rows
 
         return recordSet;
     }


More information about the cvs mailing list