svn commit: r1802660 - in /ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity: test/EntityQueryTestSuite.java util/EntityQuery.java

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

svn commit: r1802660 - in /ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity: test/EntityQueryTestSuite.java util/EntityQuery.java

nmalin
Author: nmalin
Date: Sat Jul 22 09:02:44 2017
New Revision: 1802660

URL: http://svn.apache.org/viewvc?rev=1802660&view=rev
Log:
Improved: Improve EntityQuery.queryOne() function to resolve a record by auto matching the primary key on a given context. (OFBIZ-9447)
Like that :
     GenericValue product = from('Product').where(context).queryOne()
This improvement simulate the minilang element <entity-one entity-name=Product value-name=product/> for helping the minilang migration to groovy
Thanks Jacopo to help the conception, Taher for the review

Modified:
    ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityQueryTestSuite.java
    ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java

Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityQueryTestSuite.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityQueryTestSuite.java?rev=1802660&r1=1802659&r2=1802660&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityQueryTestSuite.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/test/EntityQueryTestSuite.java Sat Jul 22 09:02:44 2017
@@ -21,6 +21,7 @@ package org.apache.ofbiz.entity.test;
 import java.util.LinkedList;
 import java.util.List;
 
+import java.util.Map;
 import org.apache.ofbiz.base.util.UtilDateTime;
 import org.apache.ofbiz.base.util.UtilMisc;
 import org.apache.ofbiz.entity.GenericEntityException;
@@ -138,6 +139,29 @@ public class EntityQueryTestSuite extend
     }
 
     /*
+     * queryOne(): This method returns only one record based on the conditions given, resolve from a context Map.
+     * assert 1: Check the TestingType entity queryOneMap-2 has been resolve
+     * assert 2: Check the TestingType entity queryOneMap-3 has been resolve with the parameters map present in context
+     */
+    public void testQueryOneWithContext() throws GenericEntityException {
+        List<GenericValue> testingTypes = new LinkedList<GenericValue>();
+        testingTypes.add(delegator.makeValue("TestingType", "testingTypeId", "queryOneMap-1", "description", "query one by map"));
+        testingTypes.add(delegator.makeValue("TestingType", "testingTypeId", "queryOneMap-2", "description", "query two by map"));
+        testingTypes.add(delegator.makeValue("TestingType", "testingTypeId", "queryOneMap-3", "description", "query three by map"));
+        delegator.storeAll(testingTypes);
+
+        Map<String, Object> context = UtilMisc.toMap("testingTypeId", "queryOneMap-2", "description", "query two by map", "otherField", "otherValue");
+        GenericValue queryOneByEntityQueryAndContext = EntityQuery.use(delegator).from("TestingType").where(context).queryOne();
+        assertNotNull("queryOne() with context: Record found", queryOneByEntityQueryAndContext);
+
+        context = UtilMisc.toMap("description", "query two by map", "otherField", "otherValue",
+                "parameters", UtilMisc.toMap("testingTypeId", "queryOneMap-3", "description", "query three by map", "otherField", "otherValue"));
+        GenericValue queryOneByEntityQueryAndParameters = EntityQuery.use(delegator).from("TestingType").where(context).queryOne();
+        assertNotNull("queryOne() with parameters: Record found", queryOneByEntityQueryAndParameters);
+        assertEquals("queryOne() with parameters: Record is queryOneMap-3 ", "queryOneMap-3", queryOneByEntityQueryAndParameters.getString("testingTypeId"));
+    }
+
+    /*
      * select(): This method is used to select particular fields only from the entity.
      * assert 1: Compared value of first record of selected 'description' field by both EntityEngine method and EntityQuery method.
      * assert 2: Compared 'testingTypeId' field for null which is fetched by EntityQuery method.

Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java?rev=1802660&r1=1802659&r2=1802660&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityQuery.java Sat Jul 22 09:02:44 2017
@@ -22,6 +22,7 @@ import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
@@ -38,6 +39,7 @@ import org.apache.ofbiz.entity.GenericEn
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.entity.condition.EntityCondition;
 import org.apache.ofbiz.entity.model.DynamicViewEntity;
+import org.apache.ofbiz.entity.model.ModelEntity;
 
 /**
  * Used to setup various options for and subsequently execute entity queries.
@@ -67,6 +69,8 @@ public class EntityQuery {
     private boolean filterByDate = false;
     private Timestamp filterByDateMoment;
     private List<String> filterByFieldNames = null;
+    private boolean searchPkOnly = false;
+    private Map<String, Object> fieldMap = null;
 
 
 
@@ -148,7 +152,7 @@ public class EntityQuery {
      * @return this EntityQuery object, to enable chaining
      */
     public EntityQuery where(Map<String, Object> fieldMap) {
-        this.whereEntityCondition = EntityCondition.makeCondition(fieldMap);
+        this.fieldMap = fieldMap;
         return this;
     }
 
@@ -416,6 +420,7 @@ public class EntityQuery {
      * @return GenericValue representing the only result record from the query
      */
     public GenericValue queryOne() throws GenericEntityException {
+        this.searchPkOnly = true;
         GenericValue result =  EntityUtil.getOnly(queryList());
         return result;
     }
@@ -474,6 +479,25 @@ public class EntityQuery {
     }
 
     private EntityCondition makeWhereCondition(boolean usingCache) {
+        if (whereEntityCondition == null && fieldMap != null) {
+            if (this.searchPkOnly) {
+                //Resolve if the map contains a sub map parameters, use a containsKeys to avoid error when a GenericValue is given as map
+                Map<String, Object> parameters = fieldMap.containsKey("parameters") ? (Map<String, Object>) fieldMap.get("parameters") : null;
+                Map<String, Object> resolveFieldMap = new HashMap<>();
+                ModelEntity modelEntity = delegator.getModelEntity(entityName);
+                List<String> fieldNames = modelEntity.getPkFieldNames();
+                for (String fieldName : fieldNames) {
+                    if (fieldMap.containsKey(fieldName)) {
+                        resolveFieldMap.put(fieldName, fieldMap.get(fieldName));
+                    } else if (parameters != null && parameters.containsKey(fieldName)) {
+                        resolveFieldMap.put(fieldName, parameters.get(fieldName));
+                    }
+                }
+                this.whereEntityCondition = EntityCondition.makeCondition(resolveFieldMap);
+            } else {
+                this.whereEntityCondition = EntityCondition.makeCondition(fieldMap);
+            }
+        }
         // we don't use the useCache field here because not all queries will actually use the cache, e.g. findCountByCondition never uses the cache
         if (filterByDate && !usingCache) {
             if (whereEntityCondition != null) {