001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.shopping.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.security.auth.PrincipalException;
027: import com.liferay.portal.struts.PortletAction;
028: import com.liferay.portlet.shopping.CartMinQuantityException;
029: import com.liferay.portlet.shopping.CouponActiveException;
030: import com.liferay.portlet.shopping.CouponEndDateException;
031: import com.liferay.portlet.shopping.CouponStartDateException;
032: import com.liferay.portlet.shopping.NoSuchCouponException;
033: import com.liferay.portlet.shopping.NoSuchItemException;
034: import com.liferay.portlet.shopping.model.ShoppingCart;
035: import com.liferay.portlet.shopping.model.ShoppingItem;
036: import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
037: import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
038: import com.liferay.portlet.shopping.util.ShoppingUtil;
039: import com.liferay.util.servlet.SessionErrors;
040: import com.liferay.util.servlet.SessionMessages;
041:
042: import javax.portlet.ActionRequest;
043: import javax.portlet.ActionResponse;
044: import javax.portlet.PortletConfig;
045: import javax.portlet.RenderRequest;
046: import javax.portlet.RenderResponse;
047:
048: import org.apache.struts.action.ActionForm;
049: import org.apache.struts.action.ActionForward;
050: import org.apache.struts.action.ActionMapping;
051:
052: /**
053: * <a href="CartAction.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public class CartAction extends PortletAction {
059:
060: public void processAction(ActionMapping mapping, ActionForm form,
061: PortletConfig config, ActionRequest req, ActionResponse res)
062: throws Exception {
063:
064: try {
065: updateCart(req);
066:
067: String redirect = ParamUtil.getString(req, "redirect");
068:
069: if (Validator.isNotNull(redirect)) {
070: res.sendRedirect(redirect);
071: }
072: } catch (Exception e) {
073: if (e instanceof NoSuchItemException
074: || e instanceof PrincipalException) {
075:
076: SessionErrors.add(req, e.getClass().getName());
077:
078: setForward(req, "portlet.shopping.error");
079: } else if (e instanceof CartMinQuantityException
080: || e instanceof CouponActiveException
081: || e instanceof CouponEndDateException
082: || e instanceof CouponStartDateException
083: || e instanceof NoSuchCouponException) {
084:
085: SessionErrors.add(req, e.getClass().getName(), e);
086: } else {
087: throw e;
088: }
089: }
090: }
091:
092: public ActionForward render(ActionMapping mapping, ActionForm form,
093: PortletConfig config, RenderRequest req, RenderResponse res)
094: throws Exception {
095:
096: return mapping.findForward(getForward(req,
097: "portlet.shopping.cart"));
098: }
099:
100: protected void updateCart(ActionRequest req) throws Exception {
101: String cmd = ParamUtil.getString(req, Constants.CMD);
102:
103: ShoppingCart cart = ShoppingUtil.getCart(req);
104:
105: if (cmd.equals(Constants.ADD)) {
106: long itemId = ParamUtil.getLong(req, "itemId");
107:
108: String fields = ParamUtil.getString(req, "fields");
109:
110: if (Validator.isNotNull(fields)) {
111: fields = "|" + fields;
112: }
113:
114: ShoppingItem item = ShoppingItemLocalServiceUtil
115: .getItem(itemId);
116:
117: if (item.getMinQuantity() > 0) {
118: for (int i = 0; i < item.getMinQuantity(); i++) {
119: cart.addItemId(itemId, fields);
120: }
121: } else {
122: cart.addItemId(itemId, fields);
123: }
124: } else {
125: String itemIds = ParamUtil.getString(req, "itemIds");
126: String couponCodes = ParamUtil
127: .getString(req, "couponCodes");
128: int altShipping = ParamUtil.getInteger(req, "altShipping");
129: boolean insure = ParamUtil.getBoolean(req, "insure");
130:
131: cart.setItemIds(itemIds);
132: cart.setCouponCodes(couponCodes);
133: cart.setAltShipping(altShipping);
134: cart.setInsure(insure);
135: }
136:
137: ShoppingCartLocalServiceUtil.updateCart(cart.getUserId(), cart
138: .getGroupId(), cart.getItemIds(),
139: cart.getCouponCodes(), cart.getAltShipping(), cart
140: .isInsure());
141:
142: if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
143: SessionMessages.add(req, "request_processed");
144: }
145: }
146:
147: }
|