/*
 * Copyright 2008-2009 the original author or authors.
 *
 * 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.
 */
package org.broadleafcommerce.checkout.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.broadleafcommerce.checkout.service.exception.CheckoutException;
import org.broadleafcommerce.checkout.service.workflow.CheckoutResponse;
import org.broadleafcommerce.checkout.service.workflow.CheckoutSeed;
import org.broadleafcommerce.order.domain.Order;
import org.broadleafcommerce.order.service.CartService;
import org.broadleafcommerce.payment.domain.PaymentInfo;
import org.broadleafcommerce.payment.domain.Referenced;
import org.broadleafcommerce.pricing.service.exception.PricingException;
import org.broadleafcommerce.util.DateUtil;
import org.broadleafcommerce.workflow.SequenceProcessor;
import org.broadleafcommerce.workflow.WorkflowException;
import org.springframework.stereotype.Service;

@Service("blCheckoutService")
public class CheckoutServiceImpl implements CheckoutService {

    @Resource(name="blCheckoutWorkflow")
    protected SequenceProcessor checkoutWorkflow;

    @Resource(name="blCartService")
    protected CartService cartService;

    /* (non-Javadoc)
     * @see org.broadleafcommerce.checkout.service.CheckoutService#performCheckout(org.broadleafcommerce.order.domain.Order, java.util.Map)
     */
    public CheckoutResponse performCheckout(Order order, final Map<PaymentInfo, Referenced> payments) throws CheckoutException {
        /*
         * TODO add validation that checks the order and payment information for validity.
         */
        /*
         * TODO remove this simple validation and encapsulate using our real validation strategy
         */
        for (PaymentInfo info : payments.keySet()) {
            if (info.getReferenceNumber() == null) {
                throw new CheckoutException("PaymentInfo reference number cannot be null");
            }
        }
        for (Referenced referenced : payments.values()) {
            if (referenced.getReferenceNumber() == null) {
                throw new CheckoutException("Referenced reference number cannot be null");
            }
        }

        try {
            order.setSubmitDate(new Date(DateUtil.getNow()));
            order = cartService.save(order, false);

            final CheckoutSeed seed = new CheckoutSeed(order, payments, new HashMap<String, Object>());
            checkoutWorkflow.doActivities(seed);

            return seed;
        } catch (PricingException e) {
            throw new CheckoutException("Unable to checkout order -- id: " + order.getId(), e);
        } catch (WorkflowException e) {
            Throwable cause = e;
            while (e.getCause() != null) {
                if (cause.equals(e.getCause())) {
                    break;
                }
                cause = e.getCause();
            }
            throw new CheckoutException("Unable to checkout order -- id: " + order.getId(), cause);
        }
    }

    /* (non-Javadoc)
     * @see org.broadleafcommerce.checkout.service.CheckoutService#performCheckout(org.broadleafcommerce.order.domain.Order)
     */
    public CheckoutResponse performCheckout(final Order order) throws CheckoutException {
        return performCheckout(order, null);
    }

}

@@ -2035,32 +2035,62 @@
 koutException %7B%0A
+    %09if (payments != null) %7B%0A%09
         /*%0A     
@@ -2076,32 +2076,33 @@
 ) %7B%0A%09        /*%0A
+%09
          * TODO 
@@ -2176,16 +2176,17 @@
 lidity.%0A
+%09
         
@@ -2181,32 +2181,33 @@
 y.%0A%09         */%0A
+%09
         /*%0A     
@@ -2201,16 +2201,17 @@
      /*%0A
+%09
         
@@ -2299,16 +2299,17 @@
 trategy%0A
+%09
         
@@ -2308,24 +2308,25 @@
          */%0A
+%09
         for 
@@ -2362,24 +2362,25 @@
 keySet()) %7B%0A
+%09
             
@@ -2412,32 +2412,33 @@
 er() == null) %7B%0A
+%09
                 
@@ -2505,32 +2505,33 @@
 nnot be null%22);%0A
+%09
             %7D%0A  
@@ -2520,32 +2520,33 @@
 %0A%09            %7D%0A
+%09
         %7D%0A      
@@ -2531,32 +2531,33 @@
    %7D%0A%09        %7D%0A
+%09
         for (Ref
@@ -2594,24 +2594,25 @@
 values()) %7B%0A
+%09
             
@@ -2650,32 +2650,33 @@
 er() == null) %7B%0A
+%09
                 
@@ -2750,16 +2750,17 @@
 null%22);%0A
+%09
         
@@ -2757,32 +2757,33 @@
 %0A%09            %7D%0A
+%09
         %7D%0A%0A     
@@ -2771,24 +2771,31 @@
 %7D%0A%09        %7D
+%0A    %09%7D
 %0A%0A        tr

fix an NPE when callling performCheckout with just the order.