1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.servlet.SessionMessages;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portlet.shopping.CartMinQuantityException;
33  import com.liferay.portlet.shopping.CouponActiveException;
34  import com.liferay.portlet.shopping.CouponEndDateException;
35  import com.liferay.portlet.shopping.CouponStartDateException;
36  import com.liferay.portlet.shopping.NoSuchCouponException;
37  import com.liferay.portlet.shopping.NoSuchItemException;
38  import com.liferay.portlet.shopping.model.ShoppingCart;
39  import com.liferay.portlet.shopping.model.ShoppingItem;
40  import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
41  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
42  import com.liferay.portlet.shopping.util.ShoppingUtil;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="CartAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class CartAction extends PortletAction {
61  
62  public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          try {
68              updateCart(actionRequest);
69  
70              String redirect = ParamUtil.getString(actionRequest, "redirect");
71  
72              if (Validator.isNotNull(redirect)) {
73                  actionResponse.sendRedirect(redirect);
74              }
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchItemException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.shopping.error");
83              }
84              else if (e instanceof CartMinQuantityException ||
85                       e instanceof CouponActiveException ||
86                       e instanceof CouponEndDateException ||
87                       e instanceof CouponStartDateException ||
88                       e instanceof NoSuchCouponException) {
89  
90                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
91              }
92              else {
93                  throw e;
94              }
95          }
96      }
97  
98      public ActionForward render(
99              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
100             RenderRequest renderRequest, RenderResponse renderResponse)
101         throws Exception {
102 
103         return mapping.findForward(
104             getForward(renderRequest, "portlet.shopping.cart"));
105     }
106 
107     protected void updateCart(ActionRequest actionRequest) throws Exception {
108         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
109 
110         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
111 
112         if (cmd.equals(Constants.ADD)) {
113             long itemId = ParamUtil.getLong(actionRequest, "itemId");
114 
115             String fields = ParamUtil.getString(actionRequest, "fields");
116 
117             if (Validator.isNotNull(fields)) {
118                 fields = "|" + fields;
119             }
120 
121             ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
122 
123             if (item.getMinQuantity() > 0) {
124                 for (int i = 0; i < item.getMinQuantity(); i++) {
125                     cart.addItemId(itemId, fields);
126                 }
127             }
128             else {
129                 cart.addItemId(itemId, fields);
130             }
131         }
132         else {
133             String itemIds = ParamUtil.getString(actionRequest, "itemIds");
134             String couponCodes = ParamUtil.getString(
135                 actionRequest, "couponCodes");
136             int altShipping = ParamUtil.getInteger(
137                 actionRequest, "altShipping");
138             boolean insure = ParamUtil.getBoolean(actionRequest, "insure");
139 
140             cart.setItemIds(itemIds);
141             cart.setCouponCodes(couponCodes);
142             cart.setAltShipping(altShipping);
143             cart.setInsure(insure);
144         }
145 
146         ShoppingCartLocalServiceUtil.updateCart(
147             cart.getUserId(), cart.getGroupId(), cart.getItemIds(),
148             cart.getCouponCodes(), cart.getAltShipping(), cart.isInsure());
149 
150         if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
151             SessionMessages.add(actionRequest, "request_processed");
152         }
153     }
154 
155 }