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

nmalin 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 e67062a6ab Improved: Allow multiple contactMech with same purpose on 
ShoppingCart (OFBIZ-13199)
e67062a6ab is described below

commit e67062a6ab7e9ae21c050c1cba9be6cbfc7545f3
Author: Nicolas Malin <nicolas.ma...@nereide.fr>
AuthorDate: Thu Jan 23 09:25:59 2025 +0100

    Improved: Allow multiple contactMech with same purpose on ShoppingCart 
(OFBIZ-13199)
    
    During the shopping cart process, you can add some contactMech to your 
order but only one by purpose.
    
    For someone, it would be logical like SHIPPING_LOCATION (although we could 
sequence it) but for some case like ORDER_EMAIL it's seem more complicated.
    
    No reason to block multiple email address that will receive order 
notification.
    
    For that we authorize the shopping cart to load a unique list of contact 
mech id by purpose instead only one
---
 .../thirdparty/paypal/PayPalServices.java          |  6 +--
 .../ofbiz/order/order/CheckoutServices.groovy      | 10 ++---
 .../ofbiz/order/shoppingcart/ShoppingCart.java     | 45 ++++++++++++++--------
 .../order/shoppingcart/ShoppingCartServices.java   |  2 +-
 4 files changed, 37 insertions(+), 26 deletions(-)

diff --git 
a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
 
b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
index 3c1888fea3..730676f69e 100644
--- 
a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
+++ 
b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalServices.java
@@ -484,7 +484,7 @@ public class PayPalServices {
                 return ServiceUtil.returnError(e.getMessage());
             }
         }
-        cart.addContactMech("ORDER_EMAIL", emailContactMechId);
+        cart.addContactMechId("ORDER_EMAIL", emailContactMechId);
 
         // Phone number
         String phoneNumber = decoder.get("PHONENUM");
@@ -508,7 +508,7 @@ public class PayPalServices {
             try {
                 outMap = dispatcher.runSync("createUpdatePartyTelecomNumber", 
inMap);
                 phoneContactId = (String) outMap.get("contactMechId");
-                cart.addContactMech("PHONE_BILLING", phoneContactId);
+                cart.addContactMechId("PHONE_BILLING", phoneContactId);
             } catch (GenericServiceException e) {
                 Debug.logError(e, MODULE);
             }
@@ -517,7 +517,7 @@ public class PayPalServices {
         String postalContactId = null;
         boolean needsShippingPurpose = true;
         // if the cart for some reason already has a billing address, we'll 
leave it be
-        boolean needsBillingPurpose = (cart.getContactMech("BILLING_LOCATION") 
== null);
+        boolean needsBillingPurpose = 
(cart.getContactMechId("BILLING_LOCATION") == null);
         Map<String, Object> postalMap = new HashMap<>();
         postalMap.put("toName", decoder.get("SHIPTONAME"));
         postalMap.put("address1", decoder.get("SHIPTOSTREET"));
diff --git 
a/applications/order/src/main/groovy/org/apache/ofbiz/order/order/CheckoutServices.groovy
 
b/applications/order/src/main/groovy/org/apache/ofbiz/order/order/CheckoutServices.groovy
index eb69276ced..f799ef1cc2 100644
--- 
a/applications/order/src/main/groovy/org/apache/ofbiz/order/order/CheckoutServices.groovy
+++ 
b/applications/order/src/main/groovy/org/apache/ofbiz/order/order/CheckoutServices.groovy
@@ -86,7 +86,7 @@ Map createUpdateCustomerAndShippingAddress() {
     result.shipToPhoneContactMechId = serviceResultCUPTN.contactMechId
 
     if (shipToPhoneContactMechId) {
-        shoppingCart.addContactMech('PHONE_SHIPPING', shipToPhoneContactMechId)
+        shoppingCart.addContactMechId('PHONE_SHIPPING', 
shipToPhoneContactMechId)
     }
     // Create Update email address
     Map createUpdatePartyEmailCtx = emailAddressCtx
@@ -98,10 +98,10 @@ Map createUpdateCustomerAndShippingAddress() {
     result.emailContactMechId = serviceResultCUPEM.contactMechId
     result.partyId = partyId
     if (parameters.emailContactMechId) {
-        shoppingCart.addContactMech('ORDER_EMAIL', 
parameters.emailContactMechId)
+        shoppingCart.addContactMechId('ORDER_EMAIL', 
parameters.emailContactMechId)
     }
     shoppingCart.setUserLogin(userLogin, dispatcher)
-    shoppingCart.addContactMech('SHIPPING_LOCATION', 
parameters.shipToContactMechId)
+    shoppingCart.addContactMechId('SHIPPING_LOCATION', 
parameters.shipToContactMechId)
     shoppingCart.setAllShippingContactMechId(parameters.shipToContactMechId)
     shoppingCart.setOrderPartyId(partyId)
     return result
@@ -149,7 +149,7 @@ Map createUpdateBillingAddressAndPaymentMethod() {
     parameters.billToContactMechId = serviceResultCUBA.contactMechId
     result.contactMechId = serviceResultCUBA.contactMechId
     if (parameters.billToContactMechId) {
-        shoppingCart.addContactMech('BILLING_LOCATION', 
parameters.billToContactMechId)
+        shoppingCart.addContactMechId('BILLING_LOCATION', 
parameters.billToContactMechId)
     }
     // Create Update Billing Telecom Number
     Map createUpdatePartyTelecomNumberCtx = billToPhoneContext
@@ -165,7 +165,7 @@ Map createUpdateBillingAddressAndPaymentMethod() {
     String billToPhoneContactMechId = serviceResultCUPTN.contactMechId
     result.billToPhoneContactMechId = serviceResultCUPTN.contactMechId
     if (billToPhoneContactMechId) {
-        shoppingCart.addContactMech('PHONE_BILLING', billToPhoneContactMechId)
+        shoppingCart.addContactMechId('PHONE_BILLING', 
billToPhoneContactMechId)
     }
     // Create Update credit card
     Map creditCartCtx = parameters
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 431c6cfb12..196cb99c16 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
@@ -139,7 +139,7 @@ public class ShoppingCart implements 
Iterable<ShoppingCartItem>, Serializable {
     private long nextGroupNumber = 1;
     private List<CartPaymentInfo> paymentInfo = new LinkedList<>();
     private List<CartShipInfo> shipInfo = new LinkedList<>();
-    private Map<String, String> contactMechIdsMap = new HashMap<>();
+    private Map<String, Set<String>> contactMechIdsMap = new HashMap<>();
     private Map<String, String> orderAttributes = new HashMap<>();
     private Map<String, Object> attributes = new HashMap<>(); // user defined 
attributes
     // Lists of internal/public notes: when the order is stored they are 
transformed into OrderHeaderNotes
@@ -3465,28 +3465,41 @@ public class ShoppingCart implements 
Iterable<ShoppingCartItem>, Serializable {
     }
 
     /** Add a contact mech to this purpose; the contactMechPurposeTypeId is 
required */
-    public void addContactMech(String contactMechPurposeTypeId, String 
contactMechId) {
+    public void addContactMechId(String contactMechPurposeTypeId, String 
contactMechId) {
         if (contactMechPurposeTypeId == null) {
             throw new IllegalArgumentException("You must specify a 
contactMechPurposeTypeId to add a ContactMech");
         }
-        contactMechIdsMap.put(contactMechPurposeTypeId, contactMechId);
+        UtilMisc.addToSetInMap(contactMechId, contactMechIdsMap, 
contactMechPurposeTypeId);
     }
 
     /** Get the contactMechId for this cart given the contactMechPurposeTypeId 
*/
-    public String getContactMech(String contactMechPurposeTypeId) {
-        return contactMechIdsMap.get(contactMechPurposeTypeId);
+    public String getContactMechId(String contactMechPurposeTypeId) {
+        return 
UtilValidate.isNotEmpty(getContactMechIds(contactMechPurposeTypeId))
+                ? getContactMechIds(contactMechPurposeTypeId).get(0)
+                : null;
     }
 
-    /** Remove the contactMechId from this cart given the 
contactMechPurposeTypeId */
-    public String removeContactMech(String contactMechPurposeTypeId) {
-        return contactMechIdsMap.remove(contactMechPurposeTypeId);
+    /** Get the contactMechIds list for this cart given the 
contactMechPurposeTypeId */
+    public List<String> getContactMechIds(String contactMechPurposeTypeId) {
+        Set<String> contactMechIds = 
contactMechIdsMap.get(contactMechPurposeTypeId);
+        return contactMechIds != null
+                ? new ArrayList<>(contactMechIds)
+                : List.of();
+    }
+
+    /** Remove the contactMechIds list from this cart given the 
contactMechPurposeTypeId */
+    public List<String> removeContactMechId(String contactMechPurposeTypeId) {
+        Set<String> contactMechIds = 
contactMechIdsMap.remove(contactMechPurposeTypeId);
+        return contactMechIds != null
+                ? new ArrayList<>(contactMechIds)
+                : List.of();
     }
 
     /**
      * Gets order contact mech ids.
      * @return the order contact mech ids
      */
-    public Map<String, String> getOrderContactMechIds() {
+    public Map<String, Set<String>> getOrderContactMechIds() {
         return this.contactMechIdsMap;
     }
 
@@ -4616,17 +4629,15 @@ public class ShoppingCart implements 
Iterable<ShoppingCartItem>, Serializable {
     public List<GenericValue> makeAllOrderContactMechs() {
         List<GenericValue> allOrderContactMechs = new LinkedList<>();
 
-        Map<String, String> contactMechIds = this.getOrderContactMechIds();
-
+        Map<String, Set<String>> contactMechIds = 
this.getOrderContactMechIds();
         if (contactMechIds != null) {
-            for (Map.Entry<String, String> entry : contactMechIds.entrySet()) {
-                GenericValue orderContactMech = 
getDelegator().makeValue("OrderContactMech");
-                orderContactMech.set("contactMechPurposeTypeId", 
entry.getKey());
-                orderContactMech.set("contactMechId", entry.getValue());
-                allOrderContactMechs.add(orderContactMech);
+            for (Map.Entry<String, Set<String>> entry : 
contactMechIds.entrySet()) {
+                entry.getValue().forEach(contactMechId ->
+                        
allOrderContactMechs.add(getDelegator().makeValue("OrderContactMech",
+                                Map.of("contactMechPurposeTypeId", 
entry.getKey(),
+                                        "contactMechId", contactMechId))));
             }
         }
-
         return allOrderContactMechs;
     }
 
diff --git 
a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartServices.java
 
b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartServices.java
index d25583c86b..20f2dd8493 100644
--- 
a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartServices.java
+++ 
b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartServices.java
@@ -329,7 +329,7 @@ public class ShoppingCartServices {
         }
         if (UtilValidate.isNotEmpty(orderContactMechs)) {
             for (GenericValue orderContactMech : orderContactMechs) {
-                
cart.addContactMech(orderContactMech.getString("contactMechPurposeTypeId"), 
orderContactMech.getString("contactMechId"));
+                
cart.addContactMechId(orderContactMech.getString("contactMechPurposeTypeId"), 
orderContactMech.getString("contactMechId"));
             }
         }
         List<GenericValue> orderItemShipGroupList = 
orh.getOrderItemShipGroups();

Reply via email to