001
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.portlet.shopping.CartMinQuantityException;
024 import com.liferay.portlet.shopping.CouponActiveException;
025 import com.liferay.portlet.shopping.CouponEndDateException;
026 import com.liferay.portlet.shopping.CouponStartDateException;
027 import com.liferay.portlet.shopping.NoSuchCouponException;
028 import com.liferay.portlet.shopping.NoSuchItemException;
029 import com.liferay.portlet.shopping.model.ShoppingCart;
030 import com.liferay.portlet.shopping.model.ShoppingItem;
031 import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
032 import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
033 import com.liferay.portlet.shopping.util.ShoppingUtil;
034
035 import javax.portlet.ActionRequest;
036 import javax.portlet.ActionResponse;
037 import javax.portlet.PortletConfig;
038 import javax.portlet.RenderRequest;
039 import javax.portlet.RenderResponse;
040
041 import org.apache.struts.action.ActionForm;
042 import org.apache.struts.action.ActionForward;
043 import org.apache.struts.action.ActionMapping;
044
045
048 public class CartAction extends PortletAction {
049
050 public void processAction(
051 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052 ActionRequest actionRequest, ActionResponse actionResponse)
053 throws Exception {
054
055 try {
056 updateCart(actionRequest, actionResponse);
057
058 String redirect = ParamUtil.getString(actionRequest, "redirect");
059
060 if (Validator.isNotNull(redirect)) {
061 actionResponse.sendRedirect(redirect);
062 }
063 }
064 catch (Exception e) {
065 if (e instanceof NoSuchItemException ||
066 e instanceof PrincipalException) {
067
068 SessionErrors.add(actionRequest, e.getClass().getName());
069
070 setForward(actionRequest, "portlet.shopping.error");
071 }
072 else if (e instanceof CartMinQuantityException ||
073 e instanceof CouponActiveException ||
074 e instanceof CouponEndDateException ||
075 e instanceof CouponStartDateException ||
076 e instanceof NoSuchCouponException) {
077
078 SessionErrors.add(actionRequest, e.getClass().getName(), e);
079 }
080 else {
081 throw e;
082 }
083 }
084 }
085
086 public ActionForward render(
087 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
088 RenderRequest renderRequest, RenderResponse renderResponse)
089 throws Exception {
090
091 return mapping.findForward(
092 getForward(renderRequest, "portlet.shopping.cart"));
093 }
094
095 protected void updateCart(
096 ActionRequest actionRequest, ActionResponse actionResponse)
097 throws Exception {
098
099 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
100
101 ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
102
103 if (cmd.equals(Constants.ADD)) {
104 long itemId = ParamUtil.getLong(actionRequest, "itemId");
105
106 String fields = ParamUtil.getString(actionRequest, "fields");
107
108 if (Validator.isNotNull(fields)) {
109 fields = "|" + fields;
110 }
111
112 ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
113
114 if (item.getMinQuantity() > 0) {
115 for (int i = 0; i < item.getMinQuantity(); i++) {
116 cart.addItemId(itemId, fields);
117 }
118 }
119 else {
120 cart.addItemId(itemId, fields);
121 }
122 }
123 else {
124 String itemIds = ParamUtil.getString(actionRequest, "itemIds");
125 String couponCodes = ParamUtil.getString(
126 actionRequest, "couponCodes");
127 int altShipping = ParamUtil.getInteger(
128 actionRequest, "altShipping");
129 boolean insure = ParamUtil.getBoolean(actionRequest, "insure");
130
131 cart.setItemIds(itemIds);
132 cart.setCouponCodes(couponCodes);
133 cart.setAltShipping(altShipping);
134 cart.setInsure(insure);
135 }
136
137 ShoppingCartLocalServiceUtil.updateCart(
138 cart.getUserId(), cart.getGroupId(), cart.getItemIds(),
139 cart.getCouponCodes(), cart.getAltShipping(), cart.isInsure());
140
141 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
142 addSuccessMessage(actionRequest, actionResponse);
143 }
144 }
145
146 }