Clover coverage report - example-web - 0.6
Coverage timestamp: Wed Jun 30 2004 20:06:20 EDT
file stats: LOC: 244   Methods: 7
NCLOC: 118   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CartAction.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * CartAction.java
 3   
  * Copyright 2002-2004 Bill2, Inc.
 4   
  *
 5   
  * Licensed under the Apache License, Version 2.0 (the "License");
 6   
  * you may not use this file except in compliance with the License.
 7   
  * You may obtain a copy of the License at
 8   
  *
 9   
  *     http://www.apache.org/licenses/LICENSE-2.0
 10   
  *
 11   
  * Unless required by applicable law or agreed to in writing, software
 12   
  * distributed under the License is distributed on an "AS IS" BASIS,
 13   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14   
  * See the License for the specific language governing permissions and
 15   
  * limitations under the License.
 16   
  */
 17   
 package example.web;
 18   
 
 19   
 import command.SubmitInvoiceImpl;
 20   
 import example.command.SubmitInvoice;
 21   
 import example.entity.Address;
 22   
 import example.entity.CreditCard;
 23   
 import example.entity.Invoice;
 24   
 import example.entity.LineItem;
 25   
 import example.entity.SKU;
 26   
 import example.entity.SKUFactory;
 27   
 import example.web.form.CartForm;
 28   
 import org.apache.commons.beanutils.BeanUtils;
 29   
 import org.apache.struts.action.ActionErrors;
 30   
 import org.apache.struts.action.ActionForm;
 31   
 import org.apache.struts.action.ActionForward;
 32   
 import org.apache.struts.action.ActionMapping;
 33   
 import org.apache.struts.actions.DispatchAction;
 34   
 import org.dentaku.foundation.connector.DirectConnector;
 35   
 import org.dentaku.services.container.ContainerManager;
 36   
 import org.dentaku.services.persistence.PersistenceManager;
 37   
 
 38   
 import javax.servlet.http.HttpServletRequest;
 39   
 import javax.servlet.http.HttpServletResponse;
 40   
 import javax.servlet.http.HttpSession;
 41   
 import java.util.Collection;
 42   
 import java.util.Iterator;
 43   
 
 44   
 /**
 45   
  * @struts.action    name="cartForm" path="/cart"
 46   
  * scope="session"
 47   
  * validate="false"
 48   
  * unknown="false"
 49   
  * parameter="m"
 50   
  * @struts.action-forward name="basket" path="/cart/cart.jsp"
 51   
  * @struts.action-forward name="billing" path="/cart/checkout-billing.jsp"
 52   
  * @struts.action-forward name="shipping" path="/cart/checkout-shipping.jsp"
 53   
  * @struts.action-forward name="shippingMethods" path="/cart/checkout-shippingMethods.jsp"
 54   
  * @struts.action-forward name="confirm" path="/cart/checkout-confirm.jsp"
 55   
  * @struts.action-forward name="confirmation" path="/cart/checkout-confirmation.jsp"
 56   
  */
 57   
 public class CartAction extends DispatchAction {
 58   
     /**
 59   
      * Add an item to the cart based on the information in the form.
 60   
      */
 61  0
     public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 62   
 
 63  0
         CartForm cartForm = ((CartForm) form);
 64   
 
 65  0
         HttpSession session = request.getSession();
 66  0
         Invoice inv = (Invoice) session.getAttribute("invoice");
 67  0
         if (inv == null) {
 68  0
             inv = new Invoice();
 69  0
             session.setAttribute("invoice", inv);
 70   
         }
 71   
 
 72  0
         Long skuID = new Long(cartForm.getSku());
 73  0
         LineItem item = null;
 74  0
         for (Iterator it = inv.getLineItems().iterator(); it.hasNext();) {
 75  0
             item = (LineItem) it.next();
 76  0
             if (item.getId().equals(skuID)) {
 77  0
                 break;
 78   
             }
 79   
         }
 80   
 
 81  0
         if (item == null) {
 82   
             // create and populate a new line item entity
 83  0
             PersistenceManager pm = (PersistenceManager) ContainerManager.getInstance().getContainer().lookup(PersistenceManager.ROLE);
 84  0
             SKUFactory skuFactory = (SKUFactory) pm.getPersistenceFactory(SKUFactory.class.getName());
 85  0
             SKU sku = (SKU) skuFactory.findByPrimaryKey(skuID);
 86  0
             pm.releaseSession();
 87   
 
 88  0
             item = new LineItem();
 89  0
             item.setSku(sku);
 90  0
             BeanUtils.copyProperties(item, sku);
 91   
 
 92  0
             inv.getLineItems().add(item);
 93   
         }
 94   
 
 95   
         // we have a line item associated to a SKU, but don't have the quantity yet.  fill that out and calculate the other fields.
 96  0
         item.setQuantity(item.getQuantity() + cartForm.getQuantity());
 97  0
         item.calculate();
 98   
 
 99  0
         inv.compute();
 100   
 
 101  0
         return mapping.findForward("basket");
 102   
     }
 103   
 
 104   
     /**
 105   
      * Action to remove an item from the invoice, takes a URL parameter of 'item' with the SKU item id
 106   
      */
 107  0
     public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 108   
 
 109  0
         HttpSession session = request.getSession();
 110  0
         Invoice inv = (Invoice) session.getAttribute("invoice");
 111   
 
 112   
         // update invoice
 113  0
         Long skuID = new Long(request.getParameter("item"));
 114  0
         for (Iterator it = inv.getLineItems().iterator(); it.hasNext();) {
 115  0
             LineItem item = (LineItem) it.next();
 116  0
             if (item.getSku().getId().equals(skuID)) {
 117  0
                 inv.getLineItems().remove(item);
 118  0
                 break;
 119   
             }
 120   
         }
 121  0
         inv.compute();
 122   
 
 123  0
         return mapping.findForward("basket");
 124   
     }
 125   
 
 126   
     /**
 127   
      * Set up for the checkout process
 128   
      * Display form to collect shipping info.
 129   
      * BRAIN DAMAGE: may want to reset the form here.
 130   
      */
 131  0
     public ActionForward setShipping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 132  0
         CartForm cartForm = ((CartForm) form);
 133  0
         cartForm.setShippingAddress(new Address());
 134   
 
 135  0
         return mapping.findForward("shipping");
 136   
     }
 137   
 
 138   
     /**
 139   
      * collect shipping Method info from UPS, based on what's in the cart.
 140   
      * Display form to let user choose which method
 141   
      */
 142  0
     public ActionForward setShippingMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 143  0
         CartForm cartForm = ((CartForm) form);
 144   
 
 145  0
         if (cartForm.getAction().equals("Back")) {
 146  0
             return mapping.findForward("basket");
 147   
         } else {
 148   
 
 149  0
             HttpSession session = request.getSession();
 150  0
             Invoice inv = (Invoice) session.getAttribute("invoice");
 151   
 
 152   
             // add info from form to invoice
 153  0
             inv.setShipAddress(cartForm.getShippingAddress());
 154  0
             inv.compute();
 155  0
             return setPaymentMethod(mapping, form, request, response);
 156   
 
 157   
 //            // shipping methods and prices from UPS
 158   
 //            UPSConnector ups = UPSConnectorUtil.getHome().create();
 159   
 //            List results = ups.getPackageRatingList((LineItem [])inv.getLineItems().toArray(new LineItem[inv.getLineItems().size()]), cartForm.getShippingAddress().getPostalCode());
 160   
 //            session.setAttribute("shippingResults", results);
 161   
 //
 162   
 //            return mapping.findForward("shippingMethods");
 163   
         }
 164   
     }
 165   
 
 166   
     /**
 167   
      * Display form to collect payment method and billing info (if applicable)
 168   
      */
 169  0
     public ActionForward setPaymentMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 170  0
         CartForm cartForm = ((CartForm) form);
 171  0
         HttpSession session = request.getSession();
 172  0
         Invoice inv = (Invoice) session.getAttribute("invoice");
 173   
 
 174   
 //        Collection shippingResults = (Collection) session.getAttribute("shippingResults");
 175   
 //        for (Iterator iterator = shippingResults.iterator(); iterator.hasNext();) {
 176   
 //            UPSConnectorBean.RatedPackage ratedPackage = (UPSConnectorBean.RatedPackage) iterator.next();
 177   
 //
 178   
 //            if (cartForm.getShippingMethodCode().equals(ratedPackage.getServiceCode())) {
 179   
 //                ShippingMethod shipMethod = new ShippingMethodImpl();
 180   
 //                shipMethod.setPrice(ratedPackage.getTotalCharges());
 181   
 //                shipMethod.setDescription(ratedPackage.getServiceName());
 182   
 //                inv.setShipMethod(shipMethod);
 183   
 //                inv.compute();
 184   
 //            }
 185   
 //        }
 186   
 
 187  0
         cartForm.setCreditCard(new CreditCard());
 188  0
         cartForm.setBillingAddress(new Address());
 189   
 
 190  0
         return mapping.findForward("billing");
 191   
     }
 192   
 
 193   
     /**
 194   
      * Validate and process the checkout view, set up for the confirm view.  We just go to the order review screen for final confirmation.
 195   
      * BRAIN DAMAGE:  This action needs to calculate tax and shipping amounts before going to the confirmation screen, something it is
 196   
      * not currently doing properly.
 197   
      */
 198  0
     public ActionForward confirmOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 199  0
         ActionErrors errors = form.validate(mapping, request);
 200   
 //        if (!errors.isEmpty()) {
 201   
 //            saveErrors(request, errors);
 202   
 //            return mapping.findForward("billing");
 203   
 //        }
 204   
 
 205  0
         CartForm cartForm = ((CartForm) form);
 206  0
         HttpSession session = request.getSession();
 207  0
         Invoice inv = (Invoice) session.getAttribute("invoice");
 208   
 
 209  0
         inv.setCreditCard(cartForm.getCreditCard());
 210   
 
 211   
         // set billing address as appropriate
 212  0
         if (!cartForm.isBillingSameAsShipping()) {
 213  0
             inv.setBillAddress(inv.getShipAddress());
 214   
         } else {
 215  0
             inv.setBillAddress(cartForm.getBillingAddress());
 216   
         }
 217   
 
 218  0
         return mapping.findForward("confirm");
 219   
     }
 220   
 
 221   
     /**
 222   
      * Process the previous view, set up for the confirmation screen.  This places the order.
 223   
      */
 224  0
     public ActionForward processOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 225  0
         ActionErrors errors = form.validate(mapping, request);
 226   
 //        if (!errors.isEmpty()) {
 227   
 //            // this should never happen since there are no fields on the confirmation page, it's just to handle hackers that are trying
 228   
 //            // to circumvent the pages earlier
 229   
 //            saveErrors(request, errors);
 230   
 //            return mapping.findForward("confirm");
 231   
 //        }
 232   
         // this does all the work in the database and returns a fully aggregated Invoice object if all of the payment and order
 233   
         // processing succeeds.  We should have complete shipping details from the shipper at this point.
 234  0
         Invoice inv = (Invoice) request.getSession().getAttribute("invoice");
 235   
 
 236  0
         SubmitInvoice event = new SubmitInvoiceImpl();
 237  0
         event.setInvoice(inv);
 238  0
         DirectConnector.getInstance().fireEvent(event);
 239  0
         request.getSession().setAttribute("invoice", inv);
 240   
 
 241  0
         return mapping.findForward("confirmation");
 242   
     }
 243   
 }
 244