1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.servlet.SessionMessages;
24  import com.liferay.portal.kernel.util.Constants;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portlet.shopping.CartMinQuantityException;
30  import com.liferay.portlet.shopping.CouponActiveException;
31  import com.liferay.portlet.shopping.CouponEndDateException;
32  import com.liferay.portlet.shopping.CouponStartDateException;
33  import com.liferay.portlet.shopping.NoSuchCouponException;
34  import com.liferay.portlet.shopping.NoSuchItemException;
35  import com.liferay.portlet.shopping.model.ShoppingCart;
36  import com.liferay.portlet.shopping.model.ShoppingItem;
37  import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
38  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
39  import com.liferay.portlet.shopping.util.ShoppingUtil;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="CartAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class CartAction extends PortletAction {
58  
59  public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          try {
65              updateCart(actionRequest);
66  
67              String redirect = ParamUtil.getString(actionRequest, "redirect");
68  
69              if (Validator.isNotNull(redirect)) {
70                  actionResponse.sendRedirect(redirect);
71              }
72          }
73          catch (Exception e) {
74              if (e instanceof NoSuchItemException ||
75                  e instanceof PrincipalException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78  
79                  setForward(actionRequest, "portlet.shopping.error");
80              }
81              else if (e instanceof CartMinQuantityException ||
82                       e instanceof CouponActiveException ||
83                       e instanceof CouponEndDateException ||
84                       e instanceof CouponStartDateException ||
85                       e instanceof NoSuchCouponException) {
86  
87                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
88              }
89              else {
90                  throw e;
91              }
92          }
93      }
94  
95      public ActionForward render(
96              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
97              RenderRequest renderRequest, RenderResponse renderResponse)
98          throws Exception {
99  
100         return mapping.findForward(
101             getForward(renderRequest, "portlet.shopping.cart"));
102     }
103 
104     protected void updateCart(ActionRequest actionRequest) throws Exception {
105         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
106 
107         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
108 
109         if (cmd.equals(Constants.ADD)) {
110             long itemId = ParamUtil.getLong(actionRequest, "itemId");
111 
112             String fields = ParamUtil.getString(actionRequest, "fields");
113 
114             if (Validator.isNotNull(fields)) {
115                 fields = "|" + fields;
116             }
117 
118             ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
119 
120             if (item.getMinQuantity() > 0) {
121                 for (int i = 0; i < item.getMinQuantity(); i++) {
122                     cart.addItemId(itemId, fields);
123                 }
124             }
125             else {
126                 cart.addItemId(itemId, fields);
127             }
128         }
129         else {
130             String itemIds = ParamUtil.getString(actionRequest, "itemIds");
131             String couponCodes = ParamUtil.getString(
132                 actionRequest, "couponCodes");
133             int altShipping = ParamUtil.getInteger(
134                 actionRequest, "altShipping");
135             boolean insure = ParamUtil.getBoolean(actionRequest, "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(actionRequest, "request_processed");
149         }
150     }
151 
152 }