svn commit: r585807 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder: ByAndFinder.java ByConditionFinder.java Finder.java ListFinder.java PrimaryKeyFinder.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r585807 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder: ByAndFinder.java ByConditionFinder.java Finder.java ListFinder.java PrimaryKeyFinder.java

doogie-3
Author: doogie
Date: Wed Oct 17 20:03:04 2007
New Revision: 585807

URL: http://svn.apache.org/viewvc?rev=585807&view=rev
Log:
Refactor finder code.

Added:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/Finder.java
      - copied, changed from r585803, ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ListFinder.java
      - copied, changed from r585764, ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java
Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByConditionFinder.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java?rev=585807&r1=585806&r2=585807&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java Wed Oct 17 20:03:04 2007
@@ -19,208 +19,39 @@
 package org.ofbiz.entity.finder;
 
 import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.UtilXml;
-import org.ofbiz.base.util.UtilMisc;
-import org.ofbiz.base.util.collections.FlexibleMapAccessor;
-import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.entity.GenericDelegator;
-import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.condition.EntityFieldMap;
 import org.ofbiz.entity.condition.EntityOperator;
 import org.ofbiz.entity.condition.EntityCondition;
-import org.ofbiz.entity.condition.EntityConditionList;
-import org.ofbiz.entity.condition.EntityJoinOperator;
-import org.ofbiz.entity.finder.EntityFinderUtil.GetAll;
-import org.ofbiz.entity.finder.EntityFinderUtil.LimitRange;
-import org.ofbiz.entity.finder.EntityFinderUtil.LimitView;
-import org.ofbiz.entity.finder.EntityFinderUtil.OutputHandler;
-import org.ofbiz.entity.finder.EntityFinderUtil.UseIterator;
 import org.ofbiz.entity.model.ModelEntity;
-import org.ofbiz.entity.transaction.TransactionUtil;
-import org.ofbiz.entity.util.EntityFindOptions;
-import org.ofbiz.entity.util.EntityListIterator;
-import org.ofbiz.entity.util.EntityUtil;
 import org.w3c.dom.Element;
 
-import java.io.Serializable;
-import java.sql.ResultSet;
-
 /**
  * Uses the delegator to find entity values by a and
  *
  */
-public class ByAndFinder implements Serializable {
+public class ByAndFinder extends ListFinder {
     
     public static final String module = ByAndFinder.class.getName();        
     
-    protected FlexibleStringExpander entityNameExdr;
-    protected FlexibleStringExpander useCacheStrExdr;
-    protected FlexibleStringExpander filterByDateStrExdr;
-    protected FlexibleStringExpander distinctStrExdr;
-    protected FlexibleStringExpander delegatorNameExdr;
-    protected FlexibleMapAccessor listAcsr;
-    protected FlexibleStringExpander resultSetTypeExdr;
-    
     protected Map fieldMap;
-    protected List selectFieldExpanderList;
-    protected List orderByExpanderList;
-    protected OutputHandler outputHandler;
 
     public ByAndFinder(Element element) {
-        this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name"));
-        this.useCacheStrExdr = new FlexibleStringExpander(element.getAttribute("use-cache"));
-        this.filterByDateStrExdr = new FlexibleStringExpander(element.getAttribute("filter-by-date"));
-        this.distinctStrExdr = new FlexibleStringExpander(element.getAttribute("distinct"));
-        this.delegatorNameExdr = new FlexibleStringExpander(element.getAttribute("delegator-name"));
-        this.listAcsr = new FlexibleMapAccessor(element.getAttribute("list-name"));
-        this.resultSetTypeExdr = new FlexibleStringExpander(element.getAttribute("result-set-type"));
+        super(element, "and");
         
         // process field-map
         this.fieldMap = EntityFinderUtil.makeFieldMap(element);
-
-        // process select-field
-        selectFieldExpanderList = EntityFinderUtil.makeSelectFieldExpanderList(element);
-        
-        // process order-by
-        List orderByElementList = UtilXml.childElementList(element, "order-by");
-        if (orderByElementList.size() > 0) {
-            orderByExpanderList = new LinkedList();
-            Iterator orderByElementIter = orderByElementList.iterator();
-            while (orderByElementIter.hasNext()) {
-                Element orderByElement = (Element) orderByElementIter.next();
-                orderByExpanderList.add(new FlexibleStringExpander(orderByElement.getAttribute("field-name")));
-            }
-        }
-
-        // process limit-range | limit-view | use-iterator
-        Element limitRangeElement = UtilXml.firstChildElement(element, "limit-range");
-        Element limitViewElement = UtilXml.firstChildElement(element, "limit-view");
-        Element useIteratorElement = UtilXml.firstChildElement(element, "use-iterator");
-        if ((limitRangeElement != null && limitViewElement != null) || (limitRangeElement != null && useIteratorElement != null) || (limitViewElement != null && useIteratorElement != null)) {
-            throw new IllegalArgumentException("In entity find by and element, cannot have more than one of the following: limit-range, limit-view, and use-iterator");
-        }
-        if (limitRangeElement != null) {
-            outputHandler = new LimitRange(limitRangeElement);
-        } else if (limitViewElement != null) {
-            outputHandler = new LimitView(limitViewElement);
-        } else if (useIteratorElement != null) {
-            outputHandler = new UseIterator(useIteratorElement);
-        } else {
-            // default to get all
-            outputHandler = new GetAll();
-        }
     }
 
-    public void runFind(Map context, GenericDelegator delegator) throws GeneralException {
-        String entityName = this.entityNameExdr.expandString(context);
-        String useCacheStr = this.useCacheStrExdr.expandString(context);
-        String filterByDateStr = this.filterByDateStrExdr.expandString(context);
-        String distinctStr = this.distinctStrExdr.expandString(context);
-        String delegatorName = this.delegatorNameExdr.expandString(context);
-        ModelEntity modelEntity = delegator.getModelEntity(entityName);
-        String resultSetTypeString = this.resultSetTypeExdr.expandString(context);
-        
-        if (modelEntity == null) {
-            throw new IllegalArgumentException("In find entity by and could not find definition for entity with name [" + entityName + "].");
-        }
-        
-        boolean useCache = "true".equals(useCacheStr);
-        boolean filterByDate = "true".equals(filterByDateStr);
-        boolean distinct = "true".equals(distinctStr);
-        int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
-        if ("forward".equals(resultSetTypeString))
-            resultSetType = ResultSet.TYPE_FORWARD_ONLY;
-        
-        if (delegatorName != null && delegatorName.length() > 0) {
-            delegator = GenericDelegator.getGenericDelegator(delegatorName);
-        }
-
-        if (useCache) {
-            // if useCache == true && outputHandler instanceof UseIterator, throw exception; not a valid combination
-            if (outputHandler instanceof UseIterator) {
-                throw new IllegalArgumentException("In find entity by and cannot have use-cache set to true and select use-iterator for the output type.");
-            }
-            if (distinct) {
-                throw new IllegalArgumentException("In find entity by and cannot have use-cache set to true and set distinct to true.");
-            }
-        }
-
+    protected EntityCondition getWhereEntityCondition(Map context, ModelEntity modelEntity, GenericDelegator delegator) {
         // create the by and map
         Map entityContext = new HashMap();
         EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, entityContext);
         // then convert the types...
         modelEntity.convertFieldMapInPlace(entityContext, delegator);
-        
-        
-        // get the list of fieldsToSelect from selectFieldExpanderList
-        Set fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);
-
-        // get the list of orderByFields from orderByExpanderList
-        List orderByFields = EntityFinderUtil.makeOrderByFieldList(this.orderByExpanderList, context);
-        
-        try {
-            // if filterByDate, do a date filter on the results based on the now-timestamp
-            EntityCondition whereEntityCondition = new EntityFieldMap(entityContext, EntityOperator.AND);
-            if (filterByDate) {
-                EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr();
-                if (whereEntityCondition != null) {
-                    whereEntityCondition = new EntityConditionList(UtilMisc.toList(whereEntityCondition, filterByDateCondition), EntityJoinOperator.AND);
-                } else {
-                    whereEntityCondition = filterByDateCondition;
-                }
-            }
-            
-            if (useCache) {
-                List results = delegator.findByConditionCache(entityName, whereEntityCondition, fieldsToSelect, orderByFields);
-                this.outputHandler.handleOutput(results, context, listAcsr);
-            } else {
-                boolean useTransaction = true;
-                if (this.outputHandler instanceof UseIterator && !TransactionUtil.isTransactionInPlace()) {
-                    Exception newE = new Exception("Stack Trace");
-                    Debug.logError(newE, "ERROR: Cannot do a by and find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
-                    useTransaction = false;
-                }
-                
-                EntityFindOptions options = new EntityFindOptions();
-                options.setDistinct(distinct);
-                options.setResultSetType(resultSetType);
-                boolean beganTransaction = false;
-                try {
-                    if (useTransaction) {
-                        beganTransaction = TransactionUtil.begin();
-                    }
-
-                    EntityListIterator eli = delegator.findListIteratorByCondition(entityName, whereEntityCondition, null, fieldsToSelect, orderByFields, options);
-                    this.outputHandler.handleOutput(eli, context, listAcsr);
-                } catch (GenericEntityException e) {
-                    String errMsg = "Failure in by and find operation, rolling back transaction";
-                    Debug.logError(e, errMsg, module);
-                    try {
-                        // only rollback the transaction if we started one...
-                        TransactionUtil.rollback(beganTransaction, errMsg, e);
-                    } catch (GenericEntityException e2) {
-                        Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
-                    }
-                    // after rolling back, rethrow the exception
-                    throw e;
-                } finally {
-                    // only commit the transaction if we started one... this will throw an exception if it fails
-                    TransactionUtil.commit(beganTransaction);
-                }
-            }
-        } catch (GenericEntityException e) {
-            String errMsg = "Error doing find by and: " + e.toString();
-            Debug.logError(e, module);
-            throw new GeneralException(errMsg, e);
-        }
+        return new EntityFieldMap(entityContext, EntityOperator.AND);
     }
 }
 

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByConditionFinder.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByConditionFinder.java?rev=585807&r1=585806&r2=585807&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByConditionFinder.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByConditionFinder.java Wed Oct 17 20:03:04 2007
@@ -18,69 +18,30 @@
  *******************************************************************************/
 package org.ofbiz.entity.finder;
 
-import java.io.Serializable;
-import java.sql.ResultSet;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GeneralException;
 import org.ofbiz.base.util.UtilXml;
-import org.ofbiz.base.util.collections.FlexibleMapAccessor;
-import org.ofbiz.base.util.string.FlexibleStringExpander;
-import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.entity.GenericDelegator;
-import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.condition.EntityCondition;
-import org.ofbiz.entity.condition.EntityConditionList;
-import org.ofbiz.entity.condition.EntityJoinOperator;
 import org.ofbiz.entity.finder.EntityFinderUtil.Condition;
 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionExpr;
 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionList;
 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionObject;
-import org.ofbiz.entity.finder.EntityFinderUtil.GetAll;
-import org.ofbiz.entity.finder.EntityFinderUtil.LimitRange;
-import org.ofbiz.entity.finder.EntityFinderUtil.LimitView;
-import org.ofbiz.entity.finder.EntityFinderUtil.OutputHandler;
-import org.ofbiz.entity.finder.EntityFinderUtil.UseIterator;
-import org.ofbiz.entity.transaction.TransactionUtil;
-import org.ofbiz.entity.util.EntityFindOptions;
-import org.ofbiz.entity.util.EntityListIterator;
-import org.ofbiz.entity.util.EntityUtil;
+import org.ofbiz.entity.model.ModelEntity;
 import org.w3c.dom.Element;
 
 /**
  * Uses the delegator to find entity values by a condition
  *
  */
-public class ByConditionFinder implements Serializable {
-    public static final String module = ByConditionFinder.class.getName();        
-    
-    protected FlexibleStringExpander entityNameExdr;
-    protected FlexibleStringExpander useCacheStrExdr;
-    protected FlexibleStringExpander filterByDateStrExdr;
-    protected FlexibleStringExpander distinctStrExdr;
-    protected FlexibleStringExpander delegatorNameExdr;
-    protected FlexibleMapAccessor listAcsr;
-    protected FlexibleStringExpander resultSetTypeExdr;
-    
+public class ByConditionFinder extends ListFinder {
+    public static final String module = ByConditionFinder.class.getName();
+
     protected Condition whereCondition;
     protected Condition havingCondition;
-    protected List selectFieldExpanderList;
-    protected List orderByExpanderList;
-    protected OutputHandler outputHandler;
 
     public ByConditionFinder(Element element) {
-        this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name"));
-        this.useCacheStrExdr = new FlexibleStringExpander(element.getAttribute("use-cache"));
-        this.filterByDateStrExdr = new FlexibleStringExpander(element.getAttribute("filter-by-date"));
-        this.distinctStrExdr = new FlexibleStringExpander(element.getAttribute("distinct"));
-        this.delegatorNameExdr = new FlexibleStringExpander(element.getAttribute("delegator-name"));
-        this.listAcsr = new FlexibleMapAccessor(element.getAttribute("list-name"));
-        this.resultSetTypeExdr = new FlexibleStringExpander(element.getAttribute("result-set-type"));
+        super(element, "condition");
 
         // NOTE: the whereCondition can be null, ie (condition-expr | condition-list) is optional; if left out, means find all, or with no condition in essense  
         // process condition-expr | condition-list
@@ -94,155 +55,22 @@
         } else if (conditionObjectElement != null) {
             this.whereCondition = new ConditionObject(conditionObjectElement);
         }
-        
-        // process having-condition-list
-        Element havingConditionListElement = UtilXml.firstChildElement(element, "having-condition-list");
-        if (havingConditionListElement != null) {
-            this.havingCondition = new ConditionList(havingConditionListElement);
-        }
-
-        // process select-field
-        selectFieldExpanderList = EntityFinderUtil.makeSelectFieldExpanderList(element);
-        
-        // process order-by
-        List orderByElementList = UtilXml.childElementList(element, "order-by");
-        if (orderByElementList.size() > 0) {
-            orderByExpanderList = new LinkedList();
-            Iterator orderByElementIter = orderByElementList.iterator();
-            while (orderByElementIter.hasNext()) {
-                Element orderByElement = (Element) orderByElementIter.next();
-                orderByExpanderList.add(new FlexibleStringExpander(orderByElement.getAttribute("field-name")));
-            }
-        }
-
-        // process limit-range | limit-view | use-iterator
-        Element limitRangeElement = UtilXml.firstChildElement(element, "limit-range");
-        Element limitViewElement = UtilXml.firstChildElement(element, "limit-view");
-        Element useIteratorElement = UtilXml.firstChildElement(element, "use-iterator");
-        if ((limitRangeElement != null && limitViewElement != null) || (limitRangeElement != null && useIteratorElement != null) || (limitViewElement != null && useIteratorElement != null)) {
-            throw new IllegalArgumentException("In entity find by condition element, cannot have more than one of the following: limit-range, limit-view, and use-iterator");
-        }
-        if (limitRangeElement != null) {
-            outputHandler = new LimitRange(limitRangeElement);
-        } else if (limitViewElement != null) {
-            outputHandler = new LimitView(limitViewElement);
-        } else if (useIteratorElement != null) {
-            outputHandler = new UseIterator(useIteratorElement);
-        } else {
-            // default to get all
-            outputHandler = new GetAll();
-        }
     }
 
-    public void runFind(Map context, GenericDelegator delegator) throws GeneralException {
-        String entityName = this.entityNameExdr.expandString(context);
-        String useCacheStr = this.useCacheStrExdr.expandString(context);
-        String filterByDateStr = this.filterByDateStrExdr.expandString(context);
-        String distinctStr = this.distinctStrExdr.expandString(context);
-        String delegatorName = this.delegatorNameExdr.expandString(context);
-        String resultSetTypeString = this.resultSetTypeExdr.expandString(context);
-        
-        boolean useCache = "true".equals(useCacheStr);
-        boolean filterByDate = "true".equals(filterByDateStr);
-        boolean distinct = "true".equals(distinctStr);
-        int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
-        if ("forward".equals(resultSetTypeString))
-            resultSetType = ResultSet.TYPE_FORWARD_ONLY;
-        
-        if (delegatorName != null && delegatorName.length() > 0) {
-            delegator = GenericDelegator.getGenericDelegator(delegatorName);
-        }
-
+    protected EntityCondition getWhereEntityCondition(Map context, ModelEntity modelEntity, GenericDelegator delegator) {
         // create whereEntityCondition from whereCondition
-        EntityCondition whereEntityCondition = null;
         if (this.whereCondition != null) {
-            whereEntityCondition = this.whereCondition.createCondition(context, entityName, delegator);
+            return this.whereCondition.createCondition(context, modelEntity.getEntityName(), delegator);
         }
+        return null;
+    }
 
+    protected EntityCondition getHavingEntityCondition(Map context, ModelEntity modelEntity, GenericDelegator delegator) {
         // create havingEntityCondition from havingCondition
-        EntityCondition havingEntityCondition = null;
         if (this.havingCondition != null) {
-            havingEntityCondition = this.havingCondition.createCondition(context, entityName, delegator);
-        }
-
-        if (useCache) {
-            // if useCache == true && outputHandler instanceof UseIterator, throw exception; not a valid combination
-            if (outputHandler instanceof UseIterator) {
-                throw new IllegalArgumentException("In find entity by condition cannot have use-cache set to true and select use-iterator for the output type.");
-            }
-            if (distinct) {
-                throw new IllegalArgumentException("In find entity by condition cannot have use-cache set to true and set distinct to true.");
-            }
-            if (havingEntityCondition != null) {
-                throw new IllegalArgumentException("In find entity by condition cannot have use-cache set to true and specify a having-condition-list (can only use a where condition with condition-expr or condition-list).");
-            }
-        }
-        
-        // get the list of fieldsToSelect from selectFieldExpanderList
-        Set fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);
-
-        //if fieldsToSelect != null and useCacheBool is true, throw an error
-        if (fieldsToSelect != null && useCache) {
-            throw new IllegalArgumentException("Error in entity query by condition definition, cannot specify select-field elements when use-cache is set to true");
-        }
-        
-        // get the list of orderByFields from orderByExpanderList
-        List orderByFields = EntityFinderUtil.makeOrderByFieldList(this.orderByExpanderList, context);
-        
-        try {
-            // if filterByDate, do a date filter on the results based on the now-timestamp
-            if (filterByDate) {
-                EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr();
-                if (whereEntityCondition != null) {
-                    whereEntityCondition = new EntityConditionList(UtilMisc.toList(whereEntityCondition, filterByDateCondition), EntityJoinOperator.AND);
-                } else {
-                    whereEntityCondition = filterByDateCondition;
-                }
-            }
-            
-            if (useCache) {
-                List results = delegator.findByConditionCache(entityName, whereEntityCondition, fieldsToSelect, orderByFields);
-                this.outputHandler.handleOutput(results, context, listAcsr);
-            } else {
-                boolean useTransaction = true;
-                if (this.outputHandler instanceof UseIterator && !TransactionUtil.isTransactionInPlace()) {
-                    Exception newE = new Exception("Stack Trace");
-                    Debug.logError(newE, "ERROR: Cannot do a by condition find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
-                    useTransaction = false;
-                }
-                
-                EntityFindOptions options = new EntityFindOptions();
-                options.setDistinct(distinct);
-                options.setResultSetType(resultSetType);
-                boolean beganTransaction = false;
-                try {
-                    if (useTransaction) {
-                        beganTransaction = TransactionUtil.begin();
-                    }
-
-                    EntityListIterator eli = delegator.findListIteratorByCondition(entityName, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderByFields, options);
-                    this.outputHandler.handleOutput(eli, context, listAcsr);
-                } catch (GenericEntityException e) {
-                    String errMsg = "Failure in by condition find operation, rolling back transaction";
-                    Debug.logError(e, errMsg, module);
-                    try {
-                        // only rollback the transaction if we started one...
-                        TransactionUtil.rollback(beganTransaction, errMsg, e);
-                    } catch (GenericEntityException e2) {
-                        Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
-                    }
-                    // after rolling back, rethrow the exception
-                    throw e;
-                } finally {
-                    // only commit the transaction if we started one... this will throw an exception if it fails
-                    TransactionUtil.commit(beganTransaction);
-                }
-            }
-        } catch (GenericEntityException e) {
-            String errMsg = "Error doing find by condition: " + e.toString();
-            Debug.logError(e, module);
-            throw new GeneralException(errMsg, e);
+            return this.havingCondition.createCondition(context, modelEntity.getEntityName(), delegator);
         }
+        return null;
     }
 }
 

Copied: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/Finder.java (from r585803, ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java)
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/Finder.java?p2=ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/Finder.java&p1=ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java&r1=585803&r2=585807&rev=585807&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/Finder.java Wed Oct 17 20:03:04 2007
@@ -18,126 +18,23 @@
  *******************************************************************************/
 package org.ofbiz.entity.finder;
 
-import java.io.Serializable;
-import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
-import java.util.Set;
+import java.io.Serializable;
+import org.w3c.dom.Element;
 
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.collections.FlexibleMapAccessor;
 import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.entity.GenericDelegator;
-import org.ofbiz.entity.GenericEntityException;
-import org.ofbiz.entity.GenericPK;
-import org.ofbiz.entity.GenericValue;
-import org.ofbiz.entity.model.ModelEntity;
-import org.w3c.dom.Element;
 
-/**
- * Uses the delegator to find entity values by a condition
- *
- */
-public class PrimaryKeyFinder implements Serializable {
-    public static final String module = PrimaryKeyFinder.class.getName();        
-    
+public abstract class Finder implements Serializable {
     protected FlexibleStringExpander entityNameExdr;
-    protected FlexibleMapAccessor valueNameAcsr;
-    protected FlexibleStringExpander useCacheExdr;
-    protected FlexibleStringExpander autoFieldMapExdr;
-    protected Map fieldMap;
-    protected List selectFieldExpanderList;
+    protected FlexibleStringExpander useCacheStrExdr;
 
-    public PrimaryKeyFinder(Element entityOneElement) {
-        this.entityNameExdr = new FlexibleStringExpander(entityOneElement.getAttribute("entity-name"));
-        if (UtilValidate.isNotEmpty(entityOneElement.getAttribute("value-name")))
-            this.valueNameAcsr = new FlexibleMapAccessor(entityOneElement.getAttribute("value-name"));
-        this.useCacheExdr = new FlexibleStringExpander(entityOneElement.getAttribute("use-cache"));
-        this.autoFieldMapExdr = new FlexibleStringExpander(entityOneElement.getAttribute("auto-field-map"));
-
-        // process field-map
-        this.fieldMap = EntityFinderUtil.makeFieldMap(entityOneElement);
-
-        // process select-field
-        selectFieldExpanderList = EntityFinderUtil.makeSelectFieldExpanderList(entityOneElement);
+    protected Finder(Element element) {
+        this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name"));
+        this.useCacheStrExdr = new FlexibleStringExpander(element.getAttribute("use-cache"));
     }
 
-    public void runFind(Map context, GenericDelegator delegator) throws GeneralException {
-        String entityName = this.entityNameExdr.expandString(context);
-        ModelEntity modelEntity = delegator.getModelEntity(entityName);
-        
-        String useCacheString = this.useCacheExdr.expandString(context);
-        // default to false
-        boolean useCacheBool = "true".equals(useCacheString);
-
-        String autoFieldMapString = this.autoFieldMapExdr.expandString(context);
-        // default to true
-        boolean autoFieldMapBool = !"false".equals(autoFieldMapString);
-
-        // assemble the field map
-        Map entityContext = new HashMap();
-        if (autoFieldMapBool) {
-            GenericValue tempVal = delegator.makeValue(entityName);
-
-            // try a map called "parameters", try it first so values from here are overriden by values in the main context
-            Object parametersObj = context.get("parameters");
-            if (parametersObj != null && parametersObj instanceof Map) {
-                tempVal.setAllFields((Map) parametersObj, true, null, Boolean.TRUE);
-            }
-
-            // just get the primary keys, and hopefully will get all of them, if not they must be manually filled in below in the field-maps
-            tempVal.setAllFields(context, true, null, Boolean.TRUE);
-
-            entityContext.putAll(tempVal);
-        }
-        EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, entityContext);
-        //Debug.logInfo("PrimaryKeyFinder: entityContext=" + entityContext, module);
-        // then convert the types...
-        modelEntity.convertFieldMapInPlace(entityContext, delegator);
-        
-        // get the list of fieldsToSelect from selectFieldExpanderList
-        Set fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);
-        
-        //if fieldsToSelect != null and useCacheBool is true, throw an error
-        if (fieldsToSelect != null && useCacheBool) {
-            throw new IllegalArgumentException("Error in entity-one definition, cannot specify select-field elements when use-cache is set to true");
-        }
-        
-        try {
-            GenericValue valueOut = null;
-            GenericPK entityPK = delegator.makePK(entityName, entityContext);
-
-            // make sure we have a full primary key, if any field is null then just log a warning and return null instead of blowing up
-            if (entityPK.containsPrimaryKey(true)) {
-                if (useCacheBool) {
-                    valueOut = delegator.findByPrimaryKeyCache(entityPK);
-                } else {
-                    if (fieldsToSelect != null) {
-                        valueOut = delegator.findByPrimaryKeyPartial(entityPK, fieldsToSelect);
-                    } else {
-                        valueOut = delegator.findByPrimaryKey(entityPK);
-                    }
-                }
-            } else {
-                if (Debug.infoOn()) Debug.logInfo("Returning null because found incomplete primary key in find: " + entityPK, module);
-            }
-            
-            //Debug.logInfo("PrimaryKeyFinder: valueOut=" + valueOut, module);
-            //Debug.logInfo("PrimaryKeyFinder: going into=" + this.valueNameAcsr.getOriginalName(), module);
-            if (valueNameAcsr != null) {
-               this.valueNameAcsr.put(context, valueOut);
-            } else {
-               if (valueOut != null) {
-                   context.putAll(valueOut);
-               }
-            }
-        } catch (GenericEntityException e) {
-            String errMsg = "Error finding entity value by primary key with entity-one: " + e.toString();
-            Debug.logError(e, errMsg, module);
-            throw new IllegalArgumentException(errMsg);
-        }
-    }
+    public abstract void runFind(Map<String, Object> context, GenericDelegator delegator) throws GeneralException;
 }
 

Copied: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ListFinder.java (from r585764, ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java)
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ListFinder.java?p2=ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ListFinder.java&p1=ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java&r1=585764&r2=585807&rev=585807&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ByAndFinder.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/ListFinder.java Wed Oct 17 20:03:04 2007
@@ -18,13 +18,14 @@
  *******************************************************************************/
 package org.ofbiz.entity.finder;
 
-import java.util.HashMap;
 import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import javolution.util.FastList;
+import javolution.util.FastMap;
+
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralException;
 import org.ofbiz.base.util.UtilXml;
@@ -33,8 +34,7 @@
 import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.entity.GenericDelegator;
 import org.ofbiz.entity.GenericEntityException;
-import org.ofbiz.entity.condition.EntityFieldMap;
-import org.ofbiz.entity.condition.EntityOperator;
+import org.ofbiz.entity.GenericValue;
 import org.ofbiz.entity.condition.EntityCondition;
 import org.ofbiz.entity.condition.EntityConditionList;
 import org.ofbiz.entity.condition.EntityJoinOperator;
@@ -57,34 +57,30 @@
  * Uses the delegator to find entity values by a and
  *
  */
-public class ByAndFinder implements Serializable {
-    
-    public static final String module = ByAndFinder.class.getName();        
+public abstract class ListFinder extends Finder {
+    public static final String module = ListFinder.class.getName();
+
+    protected String label;
     
-    protected FlexibleStringExpander entityNameExdr;
-    protected FlexibleStringExpander useCacheStrExdr;
     protected FlexibleStringExpander filterByDateStrExdr;
     protected FlexibleStringExpander distinctStrExdr;
     protected FlexibleStringExpander delegatorNameExdr;
     protected FlexibleMapAccessor listAcsr;
     protected FlexibleStringExpander resultSetTypeExdr;
     
-    protected Map fieldMap;
     protected List selectFieldExpanderList;
     protected List orderByExpanderList;
     protected OutputHandler outputHandler;
 
-    public ByAndFinder(Element element) {
-        this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name"));
-        this.useCacheStrExdr = new FlexibleStringExpander(element.getAttribute("use-cache"));
+    protected ListFinder(Element element, String label) {
+        super(element);
+        this.label = label;
+
         this.filterByDateStrExdr = new FlexibleStringExpander(element.getAttribute("filter-by-date"));
         this.distinctStrExdr = new FlexibleStringExpander(element.getAttribute("distinct"));
         this.delegatorNameExdr = new FlexibleStringExpander(element.getAttribute("delegator-name"));
         this.listAcsr = new FlexibleMapAccessor(element.getAttribute("list-name"));
         this.resultSetTypeExdr = new FlexibleStringExpander(element.getAttribute("result-set-type"));
-        
-        // process field-map
-        this.fieldMap = EntityFinderUtil.makeFieldMap(element);
 
         // process select-field
         selectFieldExpanderList = EntityFinderUtil.makeSelectFieldExpanderList(element);
@@ -92,7 +88,7 @@
         // process order-by
         List orderByElementList = UtilXml.childElementList(element, "order-by");
         if (orderByElementList.size() > 0) {
-            orderByExpanderList = new LinkedList();
+            orderByExpanderList = FastList.newInstance();
             Iterator orderByElementIter = orderByElementList.iterator();
             while (orderByElementIter.hasNext()) {
                 Element orderByElement = (Element) orderByElementIter.next();
@@ -105,7 +101,7 @@
         Element limitViewElement = UtilXml.firstChildElement(element, "limit-view");
         Element useIteratorElement = UtilXml.firstChildElement(element, "use-iterator");
         if ((limitRangeElement != null && limitViewElement != null) || (limitRangeElement != null && useIteratorElement != null) || (limitViewElement != null && useIteratorElement != null)) {
-            throw new IllegalArgumentException("In entity find by and element, cannot have more than one of the following: limit-range, limit-view, and use-iterator");
+            throw new IllegalArgumentException("In entity find by " + label + " element, cannot have more than one of the following: limit-range, limit-view, " + label + " use-iterator");
         }
         if (limitRangeElement != null) {
             outputHandler = new LimitRange(limitRangeElement);
@@ -129,7 +125,7 @@
         String resultSetTypeString = this.resultSetTypeExdr.expandString(context);
         
         if (modelEntity == null) {
-            throw new IllegalArgumentException("In find entity by and could not find definition for entity with name [" + entityName + "].");
+            throw new IllegalArgumentException("In find entity by " + label + " could not find definition for entity with name [" + entityName + "].");
         }
         
         boolean useCache = "true".equals(useCacheStr);
@@ -143,32 +139,35 @@
             delegator = GenericDelegator.getGenericDelegator(delegatorName);
         }
 
+        EntityCondition whereEntityCondition = getHavingEntityCondition(context, modelEntity, delegator);
+        EntityCondition havingEntityCondition = getHavingEntityCondition(context, modelEntity, delegator);
         if (useCache) {
             // if useCache == true && outputHandler instanceof UseIterator, throw exception; not a valid combination
             if (outputHandler instanceof UseIterator) {
-                throw new IllegalArgumentException("In find entity by and cannot have use-cache set to true and select use-iterator for the output type.");
+                throw new IllegalArgumentException("In find entity by " + label + " cannot have use-cache set to true " + label + " select use-iterator for the output type.");
             }
             if (distinct) {
-                throw new IllegalArgumentException("In find entity by and cannot have use-cache set to true and set distinct to true.");
+                throw new IllegalArgumentException("In find entity by " + label + " cannot have use-cache set to true " + label + " set distinct to true.");
+            }
+            if (havingEntityCondition != null) {
+                throw new IllegalArgumentException("In find entity by " + label + " cannot have use-cache set to true and specify a having-condition-list (can only use a where condition with condition-expr or condition-list).");
             }
         }
-
-        // create the by and map
-        Map entityContext = new HashMap();
-        EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, entityContext);
-        // then convert the types...
-        modelEntity.convertFieldMapInPlace(entityContext, delegator);
         
         
         // get the list of fieldsToSelect from selectFieldExpanderList
         Set fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);
 
+        //if fieldsToSelect != null and useCacheBool is true, throw an error
+        if (fieldsToSelect != null && useCache) {
+            throw new IllegalArgumentException("Error in entity query by " + label + " definition, cannot specify select-field elements when use-cache is set to true");
+        }
+
         // get the list of orderByFields from orderByExpanderList
         List orderByFields = EntityFinderUtil.makeOrderByFieldList(this.orderByExpanderList, context);
         
         try {
             // if filterByDate, do a date filter on the results based on the now-timestamp
-            EntityCondition whereEntityCondition = new EntityFieldMap(entityContext, EntityOperator.AND);
             if (filterByDate) {
                 EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr();
                 if (whereEntityCondition != null) {
@@ -185,7 +184,7 @@
                 boolean useTransaction = true;
                 if (this.outputHandler instanceof UseIterator && !TransactionUtil.isTransactionInPlace()) {
                     Exception newE = new Exception("Stack Trace");
-                    Debug.logError(newE, "ERROR: Cannot do a by and find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
+                    Debug.logError(newE, "ERROR: Cannot do a by " + label + " find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
                     useTransaction = false;
                 }
                 
@@ -198,10 +197,10 @@
                         beganTransaction = TransactionUtil.begin();
                     }
 
-                    EntityListIterator eli = delegator.findListIteratorByCondition(entityName, whereEntityCondition, null, fieldsToSelect, orderByFields, options);
+                    EntityListIterator eli = delegator.findListIteratorByCondition(entityName, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderByFields, options);
                     this.outputHandler.handleOutput(eli, context, listAcsr);
                 } catch (GenericEntityException e) {
-                    String errMsg = "Failure in by and find operation, rolling back transaction";
+                    String errMsg = "Failure in by " + label + " find operation, rolling back transaction";
                     Debug.logError(e, errMsg, module);
                     try {
                         // only rollback the transaction if we started one...
@@ -217,10 +216,18 @@
                 }
             }
         } catch (GenericEntityException e) {
-            String errMsg = "Error doing find by and: " + e.toString();
+            String errMsg = "Error doing find by " + label + ": " + e.toString();
             Debug.logError(e, module);
             throw new GeneralException(errMsg, e);
         }
+    }
+
+    protected EntityCondition getWhereEntityCondition(Map context, ModelEntity modelEntity, GenericDelegator delegator) {
+        return null;
+    }
+
+    protected EntityCondition getHavingEntityCondition(Map context, ModelEntity modelEntity, GenericDelegator delegator) {
+        return null;
     }
 }
 

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java?rev=585807&r1=585806&r2=585807&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/finder/PrimaryKeyFinder.java Wed Oct 17 20:03:04 2007
@@ -40,21 +40,18 @@
  * Uses the delegator to find entity values by a condition
  *
  */
-public class PrimaryKeyFinder implements Serializable {
+public class PrimaryKeyFinder extends Finder {
     public static final String module = PrimaryKeyFinder.class.getName();        
     
-    protected FlexibleStringExpander entityNameExdr;
     protected FlexibleMapAccessor valueNameAcsr;
-    protected FlexibleStringExpander useCacheExdr;
     protected FlexibleStringExpander autoFieldMapExdr;
     protected Map fieldMap;
     protected List selectFieldExpanderList;
 
     public PrimaryKeyFinder(Element entityOneElement) {
-        this.entityNameExdr = new FlexibleStringExpander(entityOneElement.getAttribute("entity-name"));
+        super(entityOneElement);
         if (UtilValidate.isNotEmpty(entityOneElement.getAttribute("value-name")))
             this.valueNameAcsr = new FlexibleMapAccessor(entityOneElement.getAttribute("value-name"));
-        this.useCacheExdr = new FlexibleStringExpander(entityOneElement.getAttribute("use-cache"));
         this.autoFieldMapExdr = new FlexibleStringExpander(entityOneElement.getAttribute("auto-field-map"));
 
         // process field-map
@@ -68,7 +65,7 @@
         String entityName = this.entityNameExdr.expandString(context);
         ModelEntity modelEntity = delegator.getModelEntity(entityName);
         
-        String useCacheString = this.useCacheExdr.expandString(context);
+        String useCacheString = this.useCacheStrExdr.expandString(context);
         // default to false
         boolean useCacheBool = "true".equals(useCacheString);