[OFBiz] SVN: r7521 - trunk/applications/order/src/org/ofbiz/order/shoppingcart

jonesde@svn.ofbiz.org jonesde at svn.ofbiz.org
Fri May 5 05:56:39 CDT 2006


Author: jonesde
Date: 2006-05-05 05:56:31 -0500 (Fri, 05 May 2006)
New Revision: 7521

Modified:
   trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java
   trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartHelper.java
   trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java
Log:
Fixed NPE in add to cart when productId is not valid; added various helpful methods to the ShoppingCart object for dealing with lists of items in custom cart management events and such; added feature to bulk add to cart to allow specifying different groups for different products being added

Modified: trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java
===================================================================
--- trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java	2006-05-05 10:35:38 UTC (rev 7520)
+++ trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java	2006-05-05 10:56:31 UTC (rev 7521)
@@ -548,6 +548,10 @@
 
     /** Get all ShoppingCartItems from the cart object with the given productId. */
     public List findAllCartItems(String productId) {
+        return this.findAllCartItems(productId, null);
+    }
+    /** Get all ShoppingCartItems from the cart object with the given productId and optional groupNumber to limit it to a specific item group */
+    public List findAllCartItems(String productId, String groupNumber) {
         if (productId == null) return this.items();
 
         List itemsToReturn = FastList.newInstance();
@@ -555,6 +559,9 @@
         Iterator cartItemIter = this.cartLines.iterator();
         while (cartItemIter.hasNext()) {
             ShoppingCartItem cartItem = (ShoppingCartItem) cartItemIter.next();
+            if (UtilValidate.isNotEmpty(groupNumber) && !cartItem.isInItemGroup(groupNumber)) {
+                continue;
+            }
             if (productId.equals(cartItem.getProductId())) {
                 itemsToReturn.add(cartItem);
             }
@@ -562,7 +569,7 @@
         return itemsToReturn;
     }
 
-    /** Get all ShoppingCartItems from the cart object with the given productCategoryId and optional groupNumber to limit it to a specific group */
+    /** Get all ShoppingCartItems from the cart object with the given productCategoryId and optional groupNumber to limit it to a specific item group */
     public List findAllCartItemsInCategory(String productCategoryId, String groupNumber) {
         if (productCategoryId == null) return this.items();
 
@@ -607,6 +614,85 @@
         }
     }
 
+    // =============== some misc utility methods, mostly for dealing with lists of items =================
+    public void removeExtraItems(List multipleItems, LocalDispatcher dispatcher, int maxItems) throws CartItemModifyException {
+        // if 1 or 0 items, do nothing
+        if (multipleItems.size() <= maxItems) return;
+        
+        // remove all except first <maxItems> in list from the cart, first because new cart items are added to the beginning...
+        List localList = FastList.newInstance();
+        localList.addAll(multipleItems);
+        // the ones to keep...
+        for (int i=0; i<maxItems; i++) localList.remove(0);
+        Iterator localIter = localList.iterator();
+        while (localIter.hasNext()) {
+            ShoppingCartItem item = (ShoppingCartItem) localIter.next();
+            this.removeCartItem(item, dispatcher);
+        }
+    }
+
+    public static double getItemsTotalQuantity(List cartItems) {
+        double totalQuantity = 0;
+        Iterator localIter = cartItems.iterator();
+        while (localIter.hasNext()) {
+            ShoppingCartItem item = (ShoppingCartItem) localIter.next();
+            totalQuantity += item.getQuantity();
+        }
+        return totalQuantity;
+    }
+    
+    public static List getItemsProducts(List cartItems) {
+        List productList = FastList.newInstance();
+        Iterator localIter = cartItems.iterator();
+        while (localIter.hasNext()) {
+            ShoppingCartItem item = (ShoppingCartItem) localIter.next();
+            GenericValue product = item.getProduct();
+            if (product != null) {
+                productList.add(product);
+            }
+        }
+        return productList;
+    }
+    
+    public void ensureItemsQuantity(List cartItems, LocalDispatcher dispatcher, double quantity) throws CartItemModifyException {
+        Iterator localIter = cartItems.iterator();
+        while (localIter.hasNext()) {
+            ShoppingCartItem item = (ShoppingCartItem) localIter.next();
+            if (item.getQuantity() != quantity) {
+                item.setQuantity(quantity, dispatcher, this);
+            }
+        }
+    }
+    
+    public double ensureItemsTotalQuantity(List cartItems, LocalDispatcher dispatcher, double quantity) throws CartItemModifyException {
+        double quantityRemoved = 0;
+        // go through the items and reduce quantityToKeep by the item quantities until it is 0, then remove the remaining...
+        double quantityToKeep = quantity;
+        Iterator localIter = cartItems.iterator();
+        while (localIter.hasNext()) {
+            ShoppingCartItem item = (ShoppingCartItem) localIter.next();
+            
+            if (quantityToKeep > item.getQuantity()) {
+                // quantityToKeep sufficient to keep it all... just reduce quantityToKeep and move on  
+                quantityToKeep = quantityToKeep - item.getQuantity();
+            } else {
+                // there is more in this than we want to keep, so reduce the quantity, or remove altogether...
+                if (quantityToKeep == 0) {
+                    // nothing left to keep, just remove it...
+                    this.removeCartItem(item, dispatcher);
+                    quantityRemoved += item.getQuantity();
+                } else {
+                    // there is some to keep, so reduce quantity by quantityToKeep, at this point we know we'll take up all of the rest of the quantityToKeep
+                    item.setQuantity(item.getQuantity() - quantityToKeep, dispatcher, this);
+                    quantityRemoved += quantityToKeep;
+                    quantityToKeep = 0;
+                }
+            }
+        }
+        return quantityRemoved;
+    }
+    
+    // ============== WorkEffort related methods ===============
     public boolean containAnyWorkEffortCartItems() {
         // Check for existing cart item.
         for (int i = 0; i < this.cartLines.size();) {

Modified: trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartHelper.java
===================================================================
--- trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartHelper.java	2006-05-05 10:35:38 UTC (rev 7520)
+++ trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartHelper.java	2006-05-05 10:56:31 UTC (rev 7521)
@@ -326,7 +326,12 @@
     /** 
      * Adds all products in a category according to quantity request parameter
      * for each; if no parameter for a certain product in the category, or if
-     * quantity is 0, do not add
+     * quantity is 0, do not add. 
+     * If a _ign_${itemGroupNumber} is appended to the name it will be put in that group instead of the default in the request parameter in itemGroupNumber
+     * 
+     * There are 2 options for the syntax:
+     *  - name="quantity_${productId}" value="${quantity}
+     *  - name="product_${whatever}" value="${productId}" (note: quantity is always 1)
      */
     public Map addToCartBulk(String catalogId, String categoryId, Map context) {
         String itemGroupNumber = (String) context.get("itemGroupNumber");
@@ -335,15 +340,26 @@
         // use this prefix for a different structure, useful for radio buttons; can have any suffix, name="product_${whatever}" value="${productId}" and quantity is always 1 
         String productQuantityKeyPrefix = "product_";
         
+        // If a _ign_${itemGroupNumber} is appended to the name it will be put in that group instead of the default in the request parameter in itemGroupNumber
+        String ignSeparator = "_ign_";
+        
         // iterate through the context and find all keys that start with "quantity_"
         Iterator entryIter = context.entrySet().iterator();
         while (entryIter.hasNext()) {
             Map.Entry entry = (Map.Entry) entryIter.next();
             String productId = null;
             String quantStr = null;
+            String itemGroupNumberToUse = itemGroupNumber;
             if (entry.getKey() instanceof String) {
                 String key = (String) entry.getKey();
                 //Debug.logInfo("Bulk Key: " + key, module);
+
+                int ignIndex = key.indexOf(ignSeparator);
+                if (ignIndex > 0) {
+                    itemGroupNumberToUse = key.substring(ignIndex + ignSeparator.length());
+                    key = key.substring(0, ignIndex);
+                }
+                
                 if (key.startsWith(keyPrefix)) {
                     productId = key.substring(keyPrefix.length());
                     quantStr = (String) entry.getValue();
@@ -368,7 +384,7 @@
                 if (quantity > 0.0) {
                     try {
                         if (Debug.verboseOn()) Debug.logVerbose("Bulk Adding to cart [" + quantity + "] of [" + productId + "] in Item Group [" + itemGroupNumber + "]", module);
-                        this.cart.addOrIncreaseItem(productId, 0.00, quantity, null, null, catalogId, itemGroupNumber, dispatcher);
+                        this.cart.addOrIncreaseItem(productId, 0.0, quantity, null, null, catalogId, itemGroupNumberToUse, dispatcher);
                     } catch (CartItemModifyException e) {
                         return ServiceUtil.returnError(e.getMessage());
                     } catch (ItemNotFoundException e) {

Modified: trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java
===================================================================
--- trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java	2006-05-05 10:35:38 UTC (rev 7520)
+++ trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java	2006-05-05 10:56:31 UTC (rev 7521)
@@ -265,7 +265,7 @@
     
             // first see if there is a purchase allow category and if this product is in it or not
             String purchaseProductCategoryId = CatalogWorker.getCatalogPurchaseAllowCategoryId(delegator, prodCatalogId);
-            if (purchaseProductCategoryId != null) {
+            if (product != null && purchaseProductCategoryId != null) {
                 if (!CategoryWorker.isProductInCategory(delegator, product.getString("productId"), purchaseProductCategoryId)) {
                     // a Purchase allow productCategoryId was found, but the product is not in the category, axe it...
                     product = null;



More information about the Svn mailing list