View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.jpetstore.web.actions;
17  
18  import java.util.Iterator;
19  
20  import javax.servlet.http.HttpServletRequest;
21  
22  import net.sourceforge.stripes.action.ForwardResolution;
23  import net.sourceforge.stripes.action.Resolution;
24  import net.sourceforge.stripes.action.SessionScope;
25  import net.sourceforge.stripes.integration.spring.SpringBean;
26  
27  import org.mybatis.jpetstore.domain.Cart;
28  import org.mybatis.jpetstore.domain.CartItem;
29  import org.mybatis.jpetstore.domain.Item;
30  import org.mybatis.jpetstore.service.CatalogService;
31  
32  /**
33   * The Class CartActionBean.
34   *
35   * @author Eduardo Macarron
36   */
37  @SessionScope
38  public class CartActionBean extends AbstractActionBean {
39  
40    private static final long serialVersionUID = -4038684592582714235L;
41  
42    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
43    private static final String CHECK_OUT = "/WEB-INF/jsp/cart/Checkout.jsp";
44  
45    @SpringBean
46    private transient CatalogService catalogService;
47  
48    private Cart cart = new Cart();
49    private String workingItemId;
50  
51    public Cart getCart() {
52      return cart;
53    }
54  
55    public void setCart(Cart cart) {
56      this.cart = cart;
57    }
58  
59    public void setWorkingItemId(String workingItemId) {
60      this.workingItemId = workingItemId;
61    }
62  
63    /**
64     * Adds the item to cart.
65     *
66     * @return the resolution
67     */
68    public Resolution addItemToCart() {
69  
70      if (workingItemId == null || workingItemId.trim().isEmpty()) {
71        setMessage("Invalid item ID: cannot add item to cart.");
72        return new ForwardResolution(ERROR);
73      }
74  
75      if (cart.containsItemId(workingItemId)) {
76        cart.incrementQuantityByItemId(workingItemId);
77      } else {
78        // isInStock is a "real-time" property that must be updated
79        // every time an item is added to the cart, even if other
80        // item details are cached.
81        boolean isInStock = catalogService.isItemInStock(workingItemId);
82        Item item = catalogService.getItem(workingItemId);
83        cart.addItem(item, isInStock);
84      }
85  
86      return new ForwardResolution(VIEW_CART);
87    }
88  
89    /**
90     * Removes the item from cart.
91     *
92     * @return the resolution
93     */
94    public Resolution removeItemFromCart() {
95  
96      if (workingItemId == null || workingItemId.trim().isEmpty()) {
97        setMessage("Invalid item ID: cannot remove item from cart.");
98        return new ForwardResolution(ERROR);
99      }
100 
101     Item item = cart.removeItemById(workingItemId);
102 
103     if (item == null) {
104       setMessage("Attempted to remove null CartItem from Cart.");
105       return new ForwardResolution(ERROR);
106     } else {
107       return new ForwardResolution(VIEW_CART);
108     }
109   }
110 
111   /**
112    * Update cart quantities.
113    *
114    * @return the resolution
115    */
116   public Resolution updateCartQuantities() {
117     HttpServletRequest request = context.getRequest();
118 
119     Iterator<CartItem> cartItems = getCart().getAllCartItems();
120     while (cartItems.hasNext()) {
121       CartItem cartItem = cartItems.next();
122       String itemId = cartItem.getItem().getItemId();
123       try {
124         int quantity = Integer.parseInt(request.getParameter(itemId));
125         getCart().setQuantityByItemId(itemId, quantity);
126         if (quantity < 1) {
127           cartItems.remove();
128         }
129       } catch (NumberFormatException e) {
130         // ignore invalid numeric input on purpose
131       }
132     }
133 
134     return new ForwardResolution(VIEW_CART);
135   }
136 
137   public ForwardResolution viewCart() {
138     return new ForwardResolution(VIEW_CART);
139   }
140 
141   public ForwardResolution checkOut() {
142     return new ForwardResolution(CHECK_OUT);
143   }
144 
145   public void clear() {
146     cart = new Cart();
147     workingItemId = null;
148   }
149 
150 }