[ofbiz-framework] branch release18.12 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 release18.12 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 release18.12
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release18.12 by this push:
     new 11dedfa  Fixed: EntityQuery queryCount is throwing error with distinct method (OFBIZ-11294)
11dedfa is described below

commit 11dedfa79fdab3ee2eb701bc8a43c6f5ccfe116c
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 58f1dc2..2271b6c 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 7cdfbcf..87ba91b 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
@@ -1641,11 +1641,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 {
@@ -1665,9 +1676,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 228f0f4..2551980 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;
@@ -150,6 +151,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 61a7275..c97a1c2 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;
@@ -140,6 +141,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 8edeec4..1ff7ec1 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
@@ -436,7 +436,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 {