001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.shopping.CartMinQuantityException;
025    import com.liferay.portlet.shopping.CouponActiveException;
026    import com.liferay.portlet.shopping.CouponEndDateException;
027    import com.liferay.portlet.shopping.CouponStartDateException;
028    import com.liferay.portlet.shopping.NoSuchCouponException;
029    import com.liferay.portlet.shopping.NoSuchItemException;
030    import com.liferay.portlet.shopping.model.ShoppingCart;
031    import com.liferay.portlet.shopping.model.ShoppingItem;
032    import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
033    import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
034    import com.liferay.portlet.shopping.util.ShoppingUtil;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class CartAction extends PortletAction {
050    
051            @Override
052            public void processAction(
053                            ActionMapping actionMapping, ActionForm actionForm,
054                            PortletConfig portletConfig, ActionRequest actionRequest,
055                            ActionResponse actionResponse)
056                    throws Exception {
057    
058                    try {
059                            updateCart(actionRequest, actionResponse);
060    
061                            String redirect = PortalUtil.escapeRedirect(
062                                    ParamUtil.getString(actionRequest, "redirect"));
063    
064                            if (Validator.isNotNull(redirect)) {
065                                    actionResponse.sendRedirect(redirect);
066                            }
067                    }
068                    catch (Exception e) {
069                            if (e instanceof NoSuchItemException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass());
073    
074                                    setForward(actionRequest, "portlet.shopping.error");
075                            }
076                            else if (e instanceof CartMinQuantityException ||
077                                             e instanceof CouponActiveException ||
078                                             e instanceof CouponEndDateException ||
079                                             e instanceof CouponStartDateException ||
080                                             e instanceof NoSuchCouponException) {
081    
082                                    SessionErrors.add(actionRequest, e.getClass(), e);
083                            }
084                            else {
085                                    throw e;
086                            }
087                    }
088            }
089    
090            @Override
091            public ActionForward render(
092                            ActionMapping actionMapping, ActionForm actionForm,
093                            PortletConfig portletConfig, RenderRequest renderRequest,
094                            RenderResponse renderResponse)
095                    throws Exception {
096    
097                    return actionMapping.findForward(
098                            getForward(renderRequest, "portlet.shopping.cart"));
099            }
100    
101            protected void updateCart(
102                            ActionRequest actionRequest, ActionResponse actionResponse)
103                    throws Exception {
104    
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                            addSuccessMessage(actionRequest, actionResponse);
149                    }
150            }
151    
152    }