View Javadoc
1   /*
2    *    Copyright 2010-2026 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    /** The serial version uid. */
41    private static final long serialVersionUID = -4038684592582714235L;
42  
43    /** The view cart. */
44    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
45    /** The check out. */
46    private static final String CHECK_OUT = "/WEB-INF/jsp/cart/Checkout.jsp";
47  
48    /** The catalog service. */
49    @SpringBean
50    private transient CatalogService catalogService;
51  
52    /** The cart. */
53    private Cart cart = new Cart();
54    /** The working item id. */
55    private String workingItemId;
56  
57    /**
58     * Gets the cart.
59     *
60     * @return the cart
61     */
62    public Cart getCart() {
63      return cart;
64    }
65  
66    /**
67     * Sets the cart.
68     *
69     * @param cart
70     *          the cart
71     */
72    public void setCart(Cart cart) {
73      this.cart = cart;
74    }
75  
76    /**
77     * Sets the working item id.
78     *
79     * @param workingItemId
80     *          the working item id
81     */
82    public void setWorkingItemId(String workingItemId) {
83      this.workingItemId = workingItemId;
84    }
85  
86    /**
87     * Adds the item to cart.
88     *
89     * @return the resolution
90     */
91    public Resolution addItemToCart() {
92  
93      if (workingItemId == null || workingItemId.trim().isEmpty()) {
94        setMessage("Invalid item ID: cannot add item to cart.");
95        return new ForwardResolution(ERROR);
96      }
97  
98      if (cart.containsItemId(workingItemId)) {
99        cart.incrementQuantityByItemId(workingItemId);
100     } else {
101       // isInStock is a "real-time" property that must be updated
102       // every time an item is added to the cart, even if other
103       // item details are cached.
104       boolean isInStock = catalogService.isItemInStock(workingItemId);
105       Item item = catalogService.getItem(workingItemId);
106       cart.addItem(item, isInStock);
107     }
108 
109     return new ForwardResolution(VIEW_CART);
110   }
111 
112   /**
113    * Removes the item from cart.
114    *
115    * @return the resolution
116    */
117   public Resolution removeItemFromCart() {
118 
119     if (workingItemId == null || workingItemId.trim().isEmpty()) {
120       setMessage("Invalid item ID: cannot remove item from cart.");
121       return new ForwardResolution(ERROR);
122     }
123 
124     Item item = cart.removeItemById(workingItemId);
125 
126     if (item == null) {
127       setMessage("Attempted to remove null CartItem from Cart.");
128       return new ForwardResolution(ERROR);
129     } else {
130       return new ForwardResolution(VIEW_CART);
131     }
132   }
133 
134   /**
135    * Update cart quantities.
136    *
137    * @return the resolution
138    */
139   public Resolution updateCartQuantities() {
140     HttpServletRequest request = context.getRequest();
141 
142     Iterator<CartItem> cartItems = getCart().getAllCartItems();
143     while (cartItems.hasNext()) {
144       CartItem cartItem = cartItems.next();
145       String itemId = cartItem.getItem().getItemId();
146       try {
147         int quantity = Integer.parseInt(request.getParameter(itemId));
148         getCart().setQuantityByItemId(itemId, quantity);
149         if (quantity < 1) {
150           cartItems.remove();
151         }
152       } catch (NumberFormatException e) {
153         // ignore invalid numeric input on purpose
154       }
155     }
156 
157     return new ForwardResolution(VIEW_CART);
158   }
159 
160   /**
161    * View cart.
162    *
163    * @return the forward resolution
164    */
165   public ForwardResolution viewCart() {
166     return new ForwardResolution(VIEW_CART);
167   }
168 
169   /**
170    * Check out.
171    *
172    * @return the forward resolution
173    */
174   public ForwardResolution checkOut() {
175     return new ForwardResolution(CHECK_OUT);
176   }
177 
178   /**
179    * Clear.
180    */
181   public void clear() {
182     cart = new Cart();
183     workingItemId = null;
184   }
185 
186 }