You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by nm...@apache.org on 2019/09/26 16:23:50 UTC

svn commit: r1867590 - /ofbiz/ofbiz-framework/trunk/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderReadHelper.java

Author: nmalin
Date: Thu Sep 26 16:23:50 2019
New Revision: 1867590

URL: http://svn.apache.org/viewvc?rev=1867590&view=rev
Log:
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

Modified:
    ofbiz/ofbiz-framework/trunk/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderReadHelper.java

Modified: ofbiz/ofbiz-framework/trunk/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderReadHelper.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderReadHelper.java?rev=1867590&r1=1867589&r2=1867590&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderReadHelper.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderReadHelper.java Thu Sep 26 16:23:50 2019
@@ -84,6 +84,8 @@ public class OrderReadHelper {
     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 class OrderReadHelper {
         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 class OrderReadHelper {
         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) {