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
git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1802660 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -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;
|
||||
@@ -137,6 +138,29 @@ public class EntityQueryTestSuite extends EntityTestCase {
|
||||
assertEquals("queryOne(): Record matched = description", findOneByEntityEngine.getString("description"), queryOneByEntityQuery.getString("description"));
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
|
||||
@@ -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.GenericEntityException;
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user