[ofbiz-framework] branch trunk updated: Fixed: EntityQuery queryCount is throwing error with distinct method (OFBIZ-11294)

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

[ofbiz-framework] branch trunk updated: Fixed: EntityQuery queryCount is throwing error with distinct method (OFBIZ-11294)

Pawan Verma-2
This is an automated email from the ASF dual-hosted git repository.

pawan pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 031592c  Fixed: EntityQuery queryCount is throwing error with distinct method (OFBIZ-11294)
031592c is described below

commit 031592cc32b49ebe873515741190f87f4365cb91
Author: Pawan Verma <[hidden email]>
AuthorDate: Sun Jun 28 01:40:33 2020 +0530

    Fixed: EntityQuery queryCount is throwing error with distinct method (OFBIZ-11294)
   
    Added missing support for selectFields in queryCount and all the subsequent methods.
   
    Thanks: Deepak Dixit for the review.
---
 .../java/org/apache/ofbiz/entity/Delegator.java    | 14 ++++++++++++
 .../org/apache/ofbiz/entity/GenericDelegator.java  | 26 ++++++++++++++++++++--
 .../ofbiz/entity/datasource/GenericHelper.java     |  5 +++++
 .../ofbiz/entity/datasource/GenericHelperDAO.java  |  7 ++++++
 .../ofbiz/entity/datasource/ReadOnlyHelperDAO.java |  7 ++++++
 .../org/apache/ofbiz/entity/util/EntityQuery.java  |  3 ++-
 6 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/Delegator.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/Delegator.java
index 813081e..4b7213c 100644
--- a/framework/entity/src/main/java/org/apache/ofbiz/entity/Delegator.java
+++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/Delegator.java
@@ -302,6 +302,20 @@ public interface Delegator {
     long findCountByCondition(String entityName, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException;
 
     /**
+     * Gets the hit count of GenericValues for the given EntityCondition objects.
+     *
+     * @param entityName
+     * @param whereEntityCondition
+     * @param fieldsToSelect
+     * @param havingEntityCondition
+     * @param findOptions
+     * @return long value with hit count
+     * @throws GenericEntityException
+     */
+    long findCountByCondition(String entityName, EntityCondition whereEntityCondition, Set<String> fieldsToSelect,
+                              EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException;
+
+    /**
      * Finds GenericValues by the conditions specified in the EntityCondition
      * object, the the EntityCondition javadoc for more details.
      *
diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java
index ad57813..3988742 100644
--- a/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java
+++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericDelegator.java
@@ -1622,11 +1622,22 @@ public class GenericDelegator implements Delegator {
     }
 
     /* (non-Javadoc)
-     * @see org.apache.ofbiz.entity.Delegator#findCountByCondition(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.util.EntityFindOptions)
+     * @see org.apache.ofbiz.entity.Delegator#findCountByCondition(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition,
+     *  org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.util.EntityFindOptions)
      */
     @Override
     public long findCountByCondition(String entityName, EntityCondition whereEntityCondition,
             EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException {
+        return findCountByCondition(entityName, whereEntityCondition, null, havingEntityCondition, findOptions);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.ofbiz.entity.Delegator#findCountByCondition(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition,
+     *  java.util.Set, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.util.EntityFindOptions)
+     */
+    @Override
+    public long findCountByCondition(String entityName, EntityCondition whereEntityCondition, Set<String> fieldsToSelect,
+                                     EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException {
 
         boolean beganTransaction = false;
         try {
@@ -1646,9 +1657,20 @@ public class GenericDelegator implements Delegator {
                 havingEntityCondition.checkCondition(modelEntity);
             }
 
+            List<ModelField> selectFields = new LinkedList<>();
+            if (UtilValidate.isNotEmpty(fieldsToSelect)) {
+                for (String fieldToSelect : fieldsToSelect) {
+                    ModelField curField = modelEntity.getField(fieldToSelect);
+                    if (curField != null) {
+                        selectFields.add(curField);
+                    }
+                }
+            }
+
             ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, dummyValue, false);
             GenericHelper helper = getEntityHelper(modelEntity.getEntityName());
-            long count = helper.findCountByCondition(this, modelEntity, whereEntityCondition, havingEntityCondition, findOptions);
+            long count = helper.findCountByCondition(this, modelEntity, whereEntityCondition,
+                                            havingEntityCondition, selectFields, findOptions);
 
             ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, dummyValue, false);
             TransactionUtil.commit(beganTransaction);
diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelper.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelper.java
index 00a0051..3eb9a0f 100644
--- a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelper.java
+++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelper.java
@@ -31,6 +31,7 @@ import org.apache.ofbiz.entity.GenericPK;
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.entity.condition.EntityCondition;
 import org.apache.ofbiz.entity.model.ModelEntity;
+import org.apache.ofbiz.entity.model.ModelField;
 import org.apache.ofbiz.entity.model.ModelRelation;
 import org.apache.ofbiz.entity.util.EntityFindOptions;
 import org.apache.ofbiz.entity.util.EntityListIterator;
@@ -98,6 +99,10 @@ public interface GenericHelper {
     public long findCountByCondition(Delegator delegator, ModelEntity modelEntity, EntityCondition whereEntityCondition,
             EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException;
 
+    long findCountByCondition(Delegator delegator, ModelEntity modelEntity, EntityCondition whereEntityCondition,
+                                     EntityCondition havingEntityCondition, List<ModelField> selectFields,
+                                     EntityFindOptions findOptions) throws GenericEntityException;
+
     /** Removes/deletes Generic Entity records found by all the specified condition
      *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
      *@param condition The condition that restricts the list of removed values
diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelperDAO.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelperDAO.java
index ef9c25d..6e7816b 100644
--- a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelperDAO.java
+++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericHelperDAO.java
@@ -31,6 +31,7 @@ import org.apache.ofbiz.entity.GenericPK;
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.entity.condition.EntityCondition;
 import org.apache.ofbiz.entity.model.ModelEntity;
+import org.apache.ofbiz.entity.model.ModelField;
 import org.apache.ofbiz.entity.model.ModelRelation;
 import org.apache.ofbiz.entity.util.EntityFindOptions;
 import org.apache.ofbiz.entity.util.EntityListIterator;
@@ -159,6 +160,12 @@ public class GenericHelperDAO implements GenericHelper {
         return genericDAO.selectCountByCondition(delegator, modelEntity, whereEntityCondition, havingEntityCondition, findOptions);
     }
 
+    @Override
+    public long findCountByCondition(Delegator delegator, ModelEntity modelEntity, EntityCondition whereEntityCondition,
+            EntityCondition havingEntityCondition, List<ModelField> selectFields, EntityFindOptions findOptions) throws GenericEntityException {
+        return genericDAO.selectCountByCondition(delegator, modelEntity, whereEntityCondition, havingEntityCondition, selectFields, findOptions);
+    }
+
     /** Removes/deletes Generic Entity records found by all the specified condition
      *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
      *@param condition The condition that restricts the list of removed values
diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/ReadOnlyHelperDAO.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/ReadOnlyHelperDAO.java
index ab8941f..ca68172 100644
--- a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/ReadOnlyHelperDAO.java
+++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/ReadOnlyHelperDAO.java
@@ -30,6 +30,7 @@ import org.apache.ofbiz.entity.GenericPK;
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.entity.condition.EntityCondition;
 import org.apache.ofbiz.entity.model.ModelEntity;
+import org.apache.ofbiz.entity.model.ModelField;
 import org.apache.ofbiz.entity.model.ModelRelation;
 import org.apache.ofbiz.entity.util.EntityFindOptions;
 import org.apache.ofbiz.entity.util.EntityListIterator;
@@ -149,6 +150,12 @@ public class ReadOnlyHelperDAO implements GenericHelper {
         return genericDAO.selectCountByCondition(delegator, modelEntity, whereEntityCondition, havingEntityCondition, findOptions);
     }
 
+    @Override
+    public long findCountByCondition(Delegator delegator, ModelEntity modelEntity, EntityCondition whereEntityCondition,
+             EntityCondition havingEntityCondition, List<ModelField> selectFields, EntityFindOptions findOptions) throws GenericEntityException {
+        return genericDAO.selectCountByCondition(delegator, modelEntity, whereEntityCondition, havingEntityCondition, selectFields, findOptions);
+    }
+
 
     /** Read only, no remove realize on the database
      *@return 0
diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java
index 450e03c..2564908 100644
--- a/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java
+++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java
@@ -455,7 +455,8 @@ public class EntityQuery {
                 return iterator.getResultsSizeAfterPartialList();
             }
         }
-        return delegator.findCountByCondition(entityName, makeWhereCondition(false), havingEntityCondition, makeEntityFindOptions());
+        return delegator.findCountByCondition(entityName, makeWhereCondition(false), fieldsToSelect,
+                havingEntityCondition, makeEntityFindOptions());
     }
 
     private List<GenericValue> query(EntityFindOptions efo) throws GenericEntityException {