1   /**
2    * Copyright (c) 2000-2008 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.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portlet.shopping.CartMinQuantityException;
31  import com.liferay.portlet.shopping.CouponActiveException;
32  import com.liferay.portlet.shopping.CouponEndDateException;
33  import com.liferay.portlet.shopping.CouponStartDateException;
34  import com.liferay.portlet.shopping.NoSuchCouponException;
35  import com.liferay.portlet.shopping.NoSuchItemException;
36  import com.liferay.portlet.shopping.model.ShoppingCart;
37  import com.liferay.portlet.shopping.model.ShoppingItem;
38  import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
39  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
40  import com.liferay.portlet.shopping.util.ShoppingUtil;
41  import com.liferay.util.servlet.SessionErrors;
42  import com.liferay.util.servlet.SessionMessages;
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 config,
64              ActionRequest req, ActionResponse res)
65          throws Exception {
66  
67          try {
68              updateCart(req);
69  
70              String redirect = ParamUtil.getString(req, "redirect");
71  
72              if (Validator.isNotNull(redirect)) {
73                  res.sendRedirect(redirect);
74              }
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchItemException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(req, e.getClass().getName());
81  
82                  setForward(req, "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(req, e.getClass().getName(), e);
91              }
92              else {
93                  throw e;
94              }
95          }
96      }
97  
98      public ActionForward render(
99              ActionMapping mapping, ActionForm form, PortletConfig config,
100             RenderRequest req, RenderResponse res)
101         throws Exception {
102 
103         return mapping.findForward(getForward(req, "portlet.shopping.cart"));
104     }
105 
106     protected void updateCart(ActionRequest req) throws Exception {
107         String cmd = ParamUtil.getString(req, Constants.CMD);
108 
109         ShoppingCart cart = ShoppingUtil.getCart(req);
110 
111         if (cmd.equals(Constants.ADD)) {
112             long itemId = ParamUtil.getLong(req, "itemId");
113 
114             String fields = ParamUtil.getString(req, "fields");
115 
116             if (Validator.isNotNull(fields)) {
117                 fields = "|" + fields;
118             }
119 
120             ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
121 
122             if (item.getMinQuantity() > 0) {
123                 for (int i = 0; i < item.getMinQuantity(); i++) {
124                     cart.addItemId(itemId, fields);
125                 }
126             }
127             else {
128                 cart.addItemId(itemId, fields);
129             }
130         }
131         else {
132             String itemIds = ParamUtil.getString(req, "itemIds");
133             String couponCodes = ParamUtil.getString(req, "couponCodes");
134             int altShipping = ParamUtil.getInteger(req, "altShipping");
135             boolean insure = ParamUtil.getBoolean(req, "insure");
136 
137             cart.setItemIds(itemIds);
138             cart.setCouponCodes(couponCodes);
139             cart.setAltShipping(altShipping);
140             cart.setInsure(insure);
141         }
142 
143         ShoppingCartLocalServiceUtil.updateCart(
144             cart.getUserId(), cart.getGroupId(), cart.getItemIds(),
145             cart.getCouponCodes(), cart.getAltShipping(), cart.isInsure());
146 
147         if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
148             SessionMessages.add(req, "request_processed");
149         }
150     }
151 
152 }