Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * BroadleafCommerce Framework
   * %%
   * Copyright (C) 2009 - 2013 Broadleaf Commerce
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 package org.broadleafcommerce.core.order.strategy;
 
 
 import java.util.List;
 import java.util.Map;
 

Author(s):
Andre Azzolini (apazzolini)
 
 @Service("blFulfillmentGroupItemStrategy")
     
     @Resource(name = "blFulfillmentGroupService")
     
     @Resource(name = "blOrderItemService")
     protected OrderItemService orderItemService;
     
     @Resource(name = "blOrderService")
     protected OrderService orderService;
     
     @Resource(name = "blFulfillmentGroupItemDao")
     protected FulfillmentGroupItemDao fgItemDao;
     
     protected boolean removeEmptyFulfillmentGroups = true;
     
     @Override
         Order order = request.getOrder();
         OrderItem orderItem = request.getOrderItem();
         Map<FulfillmentTypeFulfillmentGroupfulfillmentGroups = new HashMap<FulfillmentTypeFulfillmentGroup>();
         FulfillmentGroup nullFulfillmentTypeGroup = null;
         
         //First, let's organize fulfillment groups according to their fulfillment type
         //We'll use the first of each type that we find. Implementors can choose to move groups / items around later.
         if (order.getFulfillmentGroups() != null) {
             for (FulfillmentGroup group : order.getFulfillmentGroups()) {
                 if (group.getType() == null) {
                     if (nullFulfillmentTypeGroup == null) {
                         nullFulfillmentTypeGroup = group;
                     }
                 } else {
                     if (fulfillmentGroups.get(group.getType()) == null) {
                         fulfillmentGroups.put(group.getType(), group);
                     }
                 }
             }
         }
         
         if (orderItem instanceof BundleOrderItem) {
             //We only care about the discrete order items
             List<DiscreteOrderItemitemsToAdd = new ArrayList<DiscreteOrderItem>(((BundleOrderItemorderItem).getDiscreteOrderItems());
             for (DiscreteOrderItem doi : itemsToAdd) {
                 FulfillmentGroup fulfillmentGroup = null;
                 FulfillmentType type = resolveFulfillmentType(doi);
                 if (type == null) {
                     //Use the fulfillment group with a null type
                     fulfillmentGroup = nullFulfillmentTypeGroup;
                } else {
                    if (..equals(type)) {
                        //This is really a special case. "PICKUP_OR_SHIP" is convenient to allow a sku to be picked up or shipped.
                        //However, it is ambiguous when actually trying to create a fulfillment group. So we default to "PHYSICAL_SHIP".
                        type = .;
                    }
                    
                    //Use the fulfillment group with the specified type
                    fulfillmentGroup = fulfillmentGroups.get(type);
                }
                
                //If the null type or specified type, above were null, then we need to create a new fulfillment group
                boolean createdFulfillmentGroup = false;
                if (fulfillmentGroup == null) {
                    fulfillmentGroup = .createEmptyFulfillmentGroup();
                    //Set the type
                    fulfillmentGroup.setType(type);
                    fulfillmentGroup.setOrder(order);
                    order.getFulfillmentGroups().add(fulfillmentGroup);
                    
                    createdFulfillmentGroup = true;
                }
                
                fulfillmentGroup = addItemToFulfillmentGroup(orderdoidoi.getQuantity() * orderItem.getQuantity(), fulfillmentGroup);
                order = fulfillmentGroup.getOrder();
                
                // If we had to create a new fulfillment group, then ensure that this will operate correctly for the next set
                // of fulfillment groups
                if (createdFulfillmentGroup) {
                    if (type == null) {
                        nullFulfillmentTypeGroup = fulfillmentGroup;
                    } else {
                        fulfillmentGroups.put(typefulfillmentGroup);
                    }
                }
            }
        } else if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem doi = (DiscreteOrderItem)orderItem;
            FulfillmentGroup fulfillmentGroup = null;
            FulfillmentType type = resolveFulfillmentType(doi);
            if (type == null) {
                //Use the fulfillment group with a null type
                fulfillmentGroup = nullFulfillmentTypeGroup;
            } else {
                if (..equals(type)) {
                    //This is really a special case. "PICKUP_OR_SHIP" is convenient to allow a sku to be picked up or shipped.
                    //However, it is ambiguous when actually trying to create a fulfillment group. So we default to "PHYSICAL_SHIP".
                    type = .;
                }
                
                //Use the fulfillment group with the specified type
                fulfillmentGroup = fulfillmentGroups.get(type);
            }
            
            //If the null type or specified type, above were null, then we need to create a new fulfillment group
            if (fulfillmentGroup == null) {
                fulfillmentGroup = .createEmptyFulfillmentGroup();
                //Set the type
                fulfillmentGroup.setType(type);
                fulfillmentGroup.setOrder(order);
                order.getFulfillmentGroups().add(fulfillmentGroup);
            }
            
            fulfillmentGroup = addItemToFulfillmentGroup(orderorderItemfulfillmentGroup);
            order = fulfillmentGroup.getOrder();
        } else {
            FulfillmentGroup fulfillmentGroup = nullFulfillmentTypeGroup;
            if (fulfillmentGroup == null) {
                fulfillmentGroup = .createEmptyFulfillmentGroup();
                fulfillmentGroup.setOrder(order);
                order.getFulfillmentGroups().add(fulfillmentGroup);
            }
            
            fulfillmentGroup = addItemToFulfillmentGroup(orderorderItemfulfillmentGroup);
        }
        
        return request;
    }
    
    
Resolves the fulfillment type based on the order item. The OOB implementation uses the org.broadleafcommerce.core.order.domain.DiscreteOrderItem.getSku() to then invoke resolveFulfillmentType(org.broadleafcommerce.core.catalog.domain.Sku).

Parameters:
discreteOrderItem
Returns:
    protected FulfillmentType resolveFulfillmentType(DiscreteOrderItem discreteOrderItem) {
        return resolveFulfillmentType(discreteOrderItem.getSku());
    }
    
    protected FulfillmentType resolveFulfillmentType(Sku sku) {
        if (sku.getFulfillmentType() != null) {
            return sku.getFulfillmentType();
        }
        if (sku.getDefaultProduct() != null && sku.getDefaultProduct().getDefaultCategory() != null) {
            return sku.getDefaultProduct().getDefaultCategory().getFulfillmentType();
        }
        return null;
    }
    
    protected FulfillmentGroup addItemToFulfillmentGroup(Order orderOrderItem orderItemFulfillmentGroup fulfillmentGroupthrows PricingException {
        return this.addItemToFulfillmentGroup(orderorderItemorderItem.getQuantity(), fulfillmentGroup);
    }
    protected FulfillmentGroup addItemToFulfillmentGroup(Order orderOrderItem orderItemint quantityFulfillmentGroup fulfillmentGroupthrows PricingException {
        FulfillmentGroupItemRequest fulfillmentGroupItemRequest = new FulfillmentGroupItemRequest();
        fulfillmentGroupItemRequest.setOrder(order);
        fulfillmentGroupItemRequest.setOrderItem(orderItem);
        fulfillmentGroupItemRequest.setQuantity(quantity);
        fulfillmentGroupItemRequest.setFulfillmentGroup(fulfillmentGroup);
        return .addItemToFulfillmentGroup(fulfillmentGroupItemRequestfalsefalse);
    }
    
    @Override
        Order order = request.getOrder();
        OrderItem orderItem = request.getOrderItem();
        Integer orderItemQuantityDelta = request.getOrderItemQuantityDelta();
        
        if (orderItemQuantityDelta == 0) {
            // If the quantity didn't change, nothing needs to happen
            return request;
        } else {
            List<FulfillmentGroupItemfgisToDelete = new ArrayList<FulfillmentGroupItem>();
            if (orderItem instanceof BundleOrderItem) {
                List<OrderItemitemsToUpdate = new ArrayList<OrderItem>(((BundleOrderItemorderItem).getDiscreteOrderItems());
                for (OrderItem oi : itemsToUpdate) {
                    int quantityPer = oi.getQuantity();
                    fgisToDelete.addAll(updateItemQuantity(orderoi, (quantityPer * orderItemQuantityDelta)));
                }
            } else {
                fgisToDelete.addAll(updateItemQuantity(orderorderItemorderItemQuantityDelta));
            }
            request.setFgisToDelete(fgisToDelete);
        }
        
        return request;
    }
        
    protected List<FulfillmentGroupItemupdateItemQuantity(Order orderOrderItem orderItem
            Integer orderItemQuantityDeltathrows PricingException {
        List<FulfillmentGroupItemfgisToDelete = new ArrayList<FulfillmentGroupItem>();
        boolean done = false;
        
        if (orderItemQuantityDelta > 0) {
            // If the quantity is now greater, we can simply add quantity to the first
            // fulfillment group we find that has that order item in it. 
            for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                for (FulfillmentGroupItem fgItem : fg.getFulfillmentGroupItems()) {
                    if (!done && fgItem.getOrderItem().equals(orderItem)) {
                        fgItem.setQuantity(fgItem.getQuantity() + orderItemQuantityDelta);
                        done = true;
                    }
                }
            }
        } else {
            // The quantity has been decremented. We must ensure that the appropriate number
            // of fulfillment group items are decremented as well.
            int remainingToDecrement = -1 * orderItemQuantityDelta;
            
            for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                for (FulfillmentGroupItem fgItem : fg.getFulfillmentGroupItems()) {
                    if (fgItem.getOrderItem().equals(orderItem)) {
                        if (!done &&fgItem.getQuantity() == remainingToDecrement) {
                            // Quantity matches exactly. Simply remove the item.
                            fgisToDelete.add(fgItem);
                            done = true;
                        } else if (!done && fgItem.getQuantity() > remainingToDecrement) {
                            // We have enough quantity in this fg item to facilitate the entire requsted update
                            fgItem.setQuantity(fgItem.getQuantity() - remainingToDecrement);
                            done = true;
                        } else if (!done) {
                            // We do not have enough quantity. We'll remove this item and continue searching
                            // for the remainder.
                            remainingToDecrement = remainingToDecrement - fgItem.getQuantity();
                            fgisToDelete.add(fgItem);
                        }
                    }
                }
            }
        }
        
        if (!done) {
            throw new IllegalStateException("Could not find matching fulfillment group item for the given order item");
        }
        
        return fgisToDelete;
    }
    @Override
        Order order = request.getOrder();
        OrderItem orderItem = request.getOrderItem();
        
        if (orderItem instanceof BundleOrderItem) {
            List<OrderItemitemsToRemove = new ArrayList<OrderItem>(((BundleOrderItemorderItem).getDiscreteOrderItems());
            for (OrderItem oi : itemsToRemove) {
                request.getFgisToDelete().addAll(.getFulfillmentGroupItemsForOrderItem(orderoi));
            }
        } else {
            request.getFgisToDelete().addAll(.getFulfillmentGroupItemsForOrderItem(orderorderItem));
        }
        
        return request;
    }
    
    @Override
        Order order = request.getOrder();
        
        if (isRemoveEmptyFulfillmentGroups() && order.getFulfillmentGroups() != null) {
            ListIterator<FulfillmentGroupfgIter = order.getFulfillmentGroups().listIterator();
            while (fgIter.hasNext()) {
                FulfillmentGroup fg = fgIter.next();
                if (fg.getFulfillmentGroupItems() == null || fg.getFulfillmentGroupItems().size() == 0) {
                    fgIter.remove();
                    .delete(fg);
                }
            }
        }
        
        Map<LongIntegeroiQuantityMap = new HashMap<LongInteger>();
        List<OrderItemexpandedOrderItems = new ArrayList<OrderItem>();
        
        for (OrderItem oi : order.getOrderItems()) {
            if (oi instanceof BundleOrderItem) {
                for (DiscreteOrderItem doi : ((BundleOrderItemoi).getDiscreteOrderItems()) {
                    expandedOrderItems.add(doi);
                }
            } else if (oi instanceof DiscreteOrderItem) {
                expandedOrderItems.add(oi);
            } else {
                expandedOrderItems.add(oi);
            }
        }
        
        for (OrderItem oi : expandedOrderItems) {
            Integer oiQuantity = oiQuantityMap.get(oi.getId());
            if (oiQuantity == null) {
                oiQuantity = 0;
            }
            
            if (oi instanceof DiscreteOrderItem && ((DiscreteOrderItemoi).getBundleOrderItem() != null) {
                oiQuantity += ((DiscreteOrderItemoi).getBundleOrderItem().getQuantity() * oi.getQuantity();
            } else {
                oiQuantity += oi.getQuantity();
            }
            
            oiQuantityMap.put(oi.getId(), oiQuantity);
        }
        
        for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
            for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
                Long oiId = fgi.getOrderItem().getId();
                Integer oiQuantity = oiQuantityMap.get(oiId);
                
                if (oiQuantity == null) {
                    throw new IllegalStateException("Fulfillment group items and discrete order items are not in sync. DiscreteOrderItem id: " + oiId);
                }
                
                oiQuantity -= fgi.getQuantity();
                oiQuantityMap.put(oiIdoiQuantity);
            }
        }
        
        for (Entry<LongIntegerentry : oiQuantityMap.entrySet()) {
            if (!entry.getValue().equals(0)) {
                throw new IllegalStateException("Not enough fulfillment group items found for DiscreteOrderItem id: " + entry.getKey());
            }
        }
        
        return request;
    }
    @Override
    public boolean isRemoveEmptyFulfillmentGroups() {
        return ;
    }
    @Override
    public void setRemoveEmptyFulfillmentGroups(boolean removeEmptyFulfillmentGroups) {
        this. = removeEmptyFulfillmentGroups;
    }
New to GrepCode? Check out our FAQ X