/*
 * #%L
 * BroadleafCommerce Framework Web
 * %%
 * 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.web.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.common.web.BroadleafRequestContext;
import org.broadleafcommerce.core.catalog.domain.ProductOptionValue;
import org.broadleafcommerce.core.catalog.domain.Sku;
import org.broadleafcommerce.core.catalog.service.CatalogService;
import org.broadleafcommerce.core.extension.ExtensionResultHolder;
import org.broadleafcommerce.core.extension.ExtensionResultStatusType;
import org.broadleafcommerce.core.order.domain.BundleOrderItem;
import org.broadleafcommerce.core.order.domain.DiscreteOrderItem;
import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.order.domain.OrderItem;
import org.broadleafcommerce.core.order.domain.OrderItemAttribute;
import org.broadleafcommerce.core.order.domain.OrderItemAttributeImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author bpolster
 */
@Service("bli18nUpdateCartServiceExtensionHandler")
public class i18nUpdateCartServiceExtensionHandler extends AbstractUpdateCartServiceExtensionHandler
        implements UpdateCartServiceExtensionHandler {

    protected static final Log LOG = LogFactory.getLog(i18nUpdateCartServiceExtensionHandler.class);

    @Value("${clearCartOnLocaleSwitch}")
    protected boolean clearCartOnLocaleSwitch = false;

    @Resource(name = "blCatalogService")
    protected CatalogService catalogService;

    @Resource(name = "blUpdateCartServiceExtensionManager")
    protected UpdateCartServiceExtensionManager extensionManager;

    @PostConstruct
    public void init() {
        if (isEnabled()) {
            extensionManager.getHandlers().add(this);
        }
    }

    /**
     * If the locale of the cart does not match the current locale, then this extension handler will
     * attempt to translate the order items.  
     * 
     * The property "clearCartOnLocaleSwitch" can be set to true if the implementation desires to 
     * create a new cart when the locale is switched (3.0.6 and prior behavior).
     * 
     * @param cart
     * @param resultHolder
     * @return
     */
    public ExtensionResultStatusType updateAndValidateCart(Order cart, ExtensionResultHolder resultHolder) {
        if (BroadleafRequestContext.hasLocale()) {
            BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
            if (!brc.getLocale().getLocaleCode().matches(cart.getLocale().getLocaleCode())) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("The cart Locale [" + cart.getLocale().getLocaleCode() +
                            "] does not match the current locale [" + brc.getLocale().getLocaleCode() + "]");
                }

                if (clearCartOnLocaleSwitch) {
                    resultHolder.getContextMap().put("clearCart", Boolean.TRUE);
                } else {
                    fixTranslations(cart);
                    cart.setLocale(brc.getLocale());
                    resultHolder.getContextMap().put("saveCart", Boolean.TRUE);
                }
            }
        }
        return ExtensionResultStatusType.HANDLED_CONTINUE;
    }

    protected void fixTranslations(Order cart) {
        for (DiscreteOrderItem orderItem : cart.getDiscreteOrderItems()) {
            Sku sku = orderItem.getSku();
            translateOrderItem(orderItem, sku);
        }

        for (OrderItem orderItem : cart.getOrderItems()) {
            if (orderItem instanceof BundleOrderItem) {
                BundleOrderItem bundleItem = (BundleOrderItem) orderItem;
                Sku sku = bundleItem.getSku();
                translateOrderItem(orderItem, sku);
            }
        }
    }

    protected void translateOrderItem(OrderItem orderItem, Sku sku) {
        if (sku != null) {
            orderItem.setName(sku.getName());
        }

        if (sku.getProductOptionValues() != null) {
            for (ProductOptionValue optionValue : sku.getProductOptionValues()) {
                String key = optionValue.getProductOption().getAttributeName();
                OrderItemAttribute attr = orderItem.getOrderItemAttributes().get(key);
                if (attr != null) {
                    attr.setValue(optionValue.getAttributeValue());
                } else {
                    OrderItemAttribute attribute = new OrderItemAttributeImpl();
                    attribute.setName(key);
                    attribute.setValue(optionValue.getAttributeValue());
                    attribute.setOrderItem(orderItem);
                    orderItem.getOrderItemAttributes().put(key, attribute);
                }
            }
        }
    }
}

@@ -4702,31 +4702,24 @@
 ame());%0A    
-    %7D%0A%0A
         if (
@@ -4766,24 +4766,28 @@
             
+   
+ 
 for (Product
@@ -4860,16 +4860,20 @@
         
+    
 String k
@@ -4924,24 +4924,28 @@
 buteName();%0A
+    
             
@@ -5031,24 +5031,28 @@
             
+    
 if (attr != 
@@ -5051,32 +5051,36 @@
 attr != null) %7B%0A
+    
                 
@@ -5139,32 +5139,36 @@
                 
+ 
+   
 %7D else %7B%0A       
@@ -5152,32 +5152,36 @@
        %7D else %7B%0A
+    
                 
@@ -5257,32 +5257,36 @@
                 
+  
+  
 attribute.setNam
@@ -5289,24 +5289,28 @@
 tName(key);%0A
+    
             
@@ -5382,32 +5382,36 @@
                 
+   
+ 
 attribute.setOrd
@@ -5429,16 +5429,20 @@
 rItem);%0A
+    
         
@@ -5509,16 +5509,38 @@
 ibute);%0A
+                    %7D%0A
         

Remove possible null pointer dereference (cherry picked from commit ebd3b528c5d16467f6ce1c9bf295e0aa7e2b35f6)