Skip to content
Permalink
Browse files

Improved: OrderReadHelper.getOrderItemAttributes as non static function

(OFBIZ-11208)
Currenlty if we use the java helper class OrderReadHelper to resolve the orderItemAttributes, it didn't use the a local cache and call each time the delegator throw a static function.
I added a non static function to optimise database calling when you have multiple get to do.

	public String getOrderItemAttribute(String orderItemSeqId, String attributeName) {
	     GenericValue orderItemAttribute = null;
	     if (orderHeader != null) {
		 if (orderItemAttributes == null) {
		     try{
		           orderItemAttributes = EntityQuery.use(orderHeader.getDelegator()) .from("OrderItemAttribute") .where("orderId", getOrderId()) .queryList();
		    } catch (GenericEntityException e) { Debug.logError(e, module); }
		 }
		 orderItemAttribute = EntityUtil.getFirst(
		     EntityUtil.filterByAnd(orderItemAttributes,
		         UtilMisc.toMap("orderItemSeqId", orderItemSeqId, "attrName", attributeName)));
	     }
	     return orderItemAttribute != null ? orderItemAttribute.getString("attrValue"): null;
	 }
 

At the first call, we populate the OrderReadHelper with all orderItemAttributes liked to an order, and after just filter with the orderItemSeqId and the attribute name wanted

Example:
    OrderReadHelper orh = new OrderReadHelper(orderHeader)
    ...
    for (GenericValue orderItem : orh.getOrderItems()) {
                listAttr << 
                       [startValue   : orh.getOrderItemAttribute(orderItem.orderItemSeqId, "MyStartValue"),
                        endValue     : orh.getOrderItemAttribute(orderItem.orderItemSeqId, "MyEndValue")]
    }

We request only for the first passage, after for all other line we use the local OrderReadHelper cache.

Added also the same logical for OrderReadHelper.getOrderAttributes


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1867590 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
nmalin committed Sep 26, 2019
1 parent 086f2e8 commit 9d9d633ce96b09c90f39e6a57c79be27ab0f0cda
Showing with 56 additions and 8 deletions.
  1. +56 −8 applications/order/src/main/java/org/apache/ofbiz/order/order/OrderReadHelper.java
@@ -84,6 +84,8 @@
protected List<GenericValue> orderItemShipGrpInvResList = null;
protected List<GenericValue> orderItemIssuances = null;
protected List<GenericValue> orderReturnItems = null;
protected Map<String, GenericValue> orderAttributeMap = null;
protected List<GenericValue> orderItemAttributes = null;
protected BigDecimal totalPrice = null;

protected OrderReadHelper() {}
@@ -2844,6 +2846,35 @@ public static BigDecimal getOrderItemInvoicedQuantity(GenericValue orderItem) {
return EntityUtil.orderBy(EntityUtil.filterByAnd(newOrderStatuses, contraints2), UtilMisc.toList("-statusDatetime"));
}

/**
* When you call this function after a OrderReadHelper instantiation
* all OrderItemAttributes related to the orderHeader are load on local cache
* to optimize database call, after we just filter the cache with attributeName and
* orderItemSeqId wanted.
* @param orderItemSeqId
* @param attributeName
* @return
*/
public String getOrderItemAttribute(String orderItemSeqId, String attributeName) {
GenericValue orderItemAttribute = null;
if (orderHeader != null) {
if (orderItemAttributes == null) {
try {
orderItemAttributes = EntityQuery.use(orderHeader.getDelegator())
.from("OrderItemAttribute")
.where("orderId", getOrderId())
.queryList();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
orderItemAttribute = EntityUtil.getFirst(
EntityUtil.filterByAnd(orderItemAttributes,
UtilMisc.toMap("orderItemSeqId", orderItemSeqId, "attrName", attributeName)));
}
return orderItemAttribute != null ? orderItemAttribute.getString("attrValue"): null;
}

public static String getOrderItemAttribute(GenericValue orderItem, String attributeName) {
String attributeValue = null;
if (orderItem != null) {
@@ -2859,19 +2890,36 @@ public static String getOrderItemAttribute(GenericValue orderItem, String attrib
return attributeValue;
}

/**
* When you call this function after a OrderReadHelper instantiation
* all OrderAttributes related to the orderHeader are load on local cache
* to optimize database call, after we just filter the cache with attributeName wanted.
* @param attributeName
* @return
*/
public String getOrderAttribute(String attributeName) {
String attributeValue = null;
GenericValue orderAttribute = null;
if (orderHeader != null) {
try {
GenericValue orderAttribute = EntityUtil.getFirst(orderHeader.getRelated("OrderAttribute", UtilMisc.toMap("attrName", attributeName), null, false));
if (orderAttribute != null) {
attributeValue = orderAttribute.getString("attrValue");
if (orderAttributeMap == null) {
orderAttributeMap = new HashMap<>();
}
if (!orderAttributeMap.containsKey(attributeName)) {
try {
orderAttribute = EntityQuery.use(orderHeader.getDelegator())
.from("OrderAttribute")
.where("orderId", getOrderId(), "attrName", attributeName)
.queryFirst();
if (orderAttribute != null) {
orderAttributeMap.put(attributeName, orderAttribute);
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
} else {
orderAttribute = orderAttributeMap.get(attributeName);
}
}
return attributeValue;
return orderAttribute != null ? orderAttribute.getString("attrValue"): null;
}

public static Map<String, Object> getOrderTaxByTaxAuthGeoAndParty(List<GenericValue> orderAdjustments) {

0 comments on commit 9d9d633

Please sign in to comment.
You can’t perform that action at this time.