1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class CartAction extends PortletAction {
60  
61  public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
63              ActionRequest actionRequest, ActionResponse actionResponse)
64          throws Exception {
65  
66          try {
67              updateCart(actionRequest);
68  
69              String redirect = ParamUtil.getString(actionRequest, "redirect");
70  
71              if (Validator.isNotNull(redirect)) {
72                  actionResponse.sendRedirect(redirect);
73              }
74          }
75          catch (Exception e) {
76              if (e instanceof NoSuchItemException ||
77                  e instanceof PrincipalException) {
78  
79                  SessionErrors.add(actionRequest, e.getClass().getName());
80  
81                  setForward(actionRequest, "portlet.shopping.error");
82              }
83              else if (e instanceof CartMinQuantityException ||
84                       e instanceof CouponActiveException ||
85                       e instanceof CouponEndDateException ||
86                       e instanceof CouponStartDateException ||
87                       e instanceof NoSuchCouponException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
90              }
91              else {
92                  throw e;
93              }
94          }
95      }
96  
97      public ActionForward render(
98              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
99              RenderRequest renderRequest, RenderResponse renderResponse)
100         throws Exception {
101 
102         return mapping.findForward(
103             getForward(renderRequest, "portlet.shopping.cart"));
104     }
105 
106     protected void updateCart(ActionRequest actionRequest) throws Exception {
107         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
108 
109         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
110 
111         if (cmd.equals(Constants.ADD)) {
112             long itemId = ParamUtil.getLong(actionRequest, "itemId");
113 
114             String fields = ParamUtil.getString(actionRequest, "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(actionRequest, "itemIds");
133             String couponCodes = ParamUtil.getString(
134                 actionRequest, "couponCodes");
135             int altShipping = ParamUtil.getInteger(
136                 actionRequest, "altShipping");
137             boolean insure = ParamUtil.getBoolean(actionRequest, "insure");
138 
139             cart.setItemIds(itemIds);
140             cart.setCouponCodes(couponCodes);
141             cart.setAltShipping(altShipping);
142             cart.setInsure(insure);
143         }
144 
145         ShoppingCartLocalServiceUtil.updateCart(
146             cart.getUserId(), cart.getGroupId(), cart.getItemIds(),
147             cart.getCouponCodes(), cart.getAltShipping(), cart.isInsure());
148 
149         if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
150             SessionMessages.add(actionRequest, "request_processed");
151         }
152     }
153 
154 }