You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by mr...@apache.org on 2026/10/30 13:52:25 UTC

(ofbiz-framework) branch trunk updated: Copy item groups parent first in the ShoppingCart copy constructor

This is an automated email from the ASF dual-hosted git repository.

mridulpathak pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new eabcf68a6f Copy item groups parent first in the ShoppingCart copy constructor
eabcf68a6f is described below

commit eabcf68a6f1010bef8d87fa5d506977dc9809730
Author: Mridul Pathak <mr...@gmail.com>
AuthorDate: Fri Oct 30 18:39:13 2026 +0530

    Copy item groups parent first in the ShoppingCart copy constructor
    
    - The copy constructor cloned the item groups in HashMap order and looked each parent up among the groups cloned so far, so a group returned before its parent (group 11 among groups 01 to 11) lost its parent. A private static copyItemGroup now copies each parent before its children, which also removes the this-escape warning from the getItemGroupByNumber call.
    - The copied items kept the groups of the original cart; they now belong to the copied groups.
    - The copy now continues nextGroupNumber and nextItemSeq instead of restarting them at 1, so a group added to the copy no longer replaces group 01.
---
 .../ofbiz/order/shoppingcart/ShoppingCart.java     | 36 ++++++++++++++++------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java
index ff43d1f643..5647b1a888 100644
--- a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java
+++ b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java
@@ -246,26 +246,42 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable {
             this.additionalPartyRole.put(me.getKey(), new LinkedList<>(me.getValue()));
         }
 
-        // clone the groups
+        // clone the groups, each parent before its children, and continue their numbering
         for (ShoppingCartItemGroup itemGroup : cart.itemGroupByNumberMap.values()) {
-            // get the new parent group by number from the existing set; as before the parent must come before all children to work...
-            ShoppingCartItemGroup parentGroup = null;
-            if (itemGroup.getParentGroup() != null) {
-                parentGroup = this.getItemGroupByNumber(itemGroup.getParentGroup().getGroupNumber());
-            }
-            ShoppingCartItemGroup newGroup = new ShoppingCartItemGroup(itemGroup, parentGroup);
-            itemGroupByNumberMap.put(newGroup.getGroupNumber(), newGroup);
+            copyItemGroup(itemGroup, this.itemGroupByNumberMap);
         }
+        this.nextGroupNumber = cart.nextGroupNumber;
+        this.nextItemSeq = cart.nextItemSeq;
 
-        // clone the items
+        // clone the items, moving them to the cloned groups
         for (ShoppingCartItem item : cart.items()) {
-            cartLines.add(new ShoppingCartItem(item));
+            ShoppingCartItem newItem = new ShoppingCartItem(item);
+            if (item.getItemGroup() != null) {
+                newItem.setItemGroup(this.itemGroupByNumberMap.get(item.getItemGroup().getGroupNumber()));
+            }
+            cartLines.add(newItem);
         }
 
         this.facilityId = cart.facilityId;
         this.webSiteId = cart.webSiteId;
     }
 
+    /**
+     * Copies an item group into the given map of copies, copying its parent groups first.
+     * @param itemGroup the item group to copy
+     * @param copies the copies made so far, by group number
+     * @return the copy of the item group
+     */
+    private static ShoppingCartItemGroup copyItemGroup(ShoppingCartItemGroup itemGroup, Map<String, ShoppingCartItemGroup> copies) {
+        ShoppingCartItemGroup copy = copies.get(itemGroup.getGroupNumber());
+        if (copy == null) {
+            ShoppingCartItemGroup parentGroup = itemGroup.getParentGroup();
+            copy = new ShoppingCartItemGroup(itemGroup, parentGroup == null ? null : copyItemGroup(parentGroup, copies));
+            copies.put(copy.getGroupNumber(), copy);
+        }
+        return copy;
+    }
+
     /** Creates new empty ShoppingCart object. */
     public ShoppingCart(LocalDispatcher dispatcher, String productStoreId, String webSiteId, Locale locale, String currencyUom,
                         String billToCustomerPartyId, String billFromVendorPartyId) {