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.StringUtil;
026: import com.liferay.portal.model.Layout;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portal.struts.PortletAction;
029: import com.liferay.portal.util.WebKeys;
030: import com.liferay.portlet.shopping.CouponCodeException;
031: import com.liferay.portlet.shopping.CouponDateException;
032: import com.liferay.portlet.shopping.CouponDescriptionException;
033: import com.liferay.portlet.shopping.CouponEndDateException;
034: import com.liferay.portlet.shopping.CouponLimitCategoriesException;
035: import com.liferay.portlet.shopping.CouponLimitSKUsException;
036: import com.liferay.portlet.shopping.CouponNameException;
037: import com.liferay.portlet.shopping.CouponStartDateException;
038: import com.liferay.portlet.shopping.DuplicateCouponCodeException;
039: import com.liferay.portlet.shopping.NoSuchCouponException;
040: import com.liferay.portlet.shopping.service.ShoppingCouponServiceUtil;
041: import com.liferay.util.servlet.SessionErrors;
042:
043: import java.util.Calendar;
044:
045: import javax.portlet.ActionRequest;
046: import javax.portlet.ActionResponse;
047: import javax.portlet.PortletConfig;
048: import javax.portlet.RenderRequest;
049: import javax.portlet.RenderResponse;
050:
051: import org.apache.struts.action.ActionForm;
052: import org.apache.struts.action.ActionForward;
053: import org.apache.struts.action.ActionMapping;
054:
055: /**
056: * <a href="EditCouponAction.java.html"><b><i>View Source</i></b></a>
057: *
058: * @author Brian Wing Shun Chan
059: *
060: */
061: public class EditCouponAction extends PortletAction {
062:
063: public void processAction(ActionMapping mapping, ActionForm form,
064: PortletConfig config, ActionRequest req, ActionResponse res)
065: throws Exception {
066:
067: String cmd = ParamUtil.getString(req, Constants.CMD);
068:
069: try {
070: if (cmd.equals(Constants.ADD)
071: || cmd.equals(Constants.UPDATE)) {
072: updateCoupon(req);
073: } else if (cmd.equals(Constants.DELETE)) {
074: deleteCoupons(req);
075: }
076:
077: sendRedirect(req, res);
078: } catch (Exception e) {
079: if (e instanceof NoSuchCouponException
080: || e instanceof PrincipalException) {
081:
082: SessionErrors.add(req, e.getClass().getName());
083:
084: setForward(req, "portlet.shopping.error");
085: } else if (e instanceof CouponCodeException
086: || e instanceof CouponDateException
087: || e instanceof CouponDescriptionException
088: || e instanceof CouponEndDateException
089: || e instanceof CouponLimitCategoriesException
090: || e instanceof CouponLimitSKUsException
091: || e instanceof CouponNameException
092: || e instanceof CouponStartDateException
093: || e instanceof DuplicateCouponCodeException) {
094:
095: if (e instanceof CouponLimitCategoriesException) {
096: CouponLimitCategoriesException clce = (CouponLimitCategoriesException) e;
097:
098: SessionErrors.add(req, e.getClass().getName(), clce
099: .getCategoryIds());
100: } else if (e instanceof CouponLimitSKUsException) {
101: CouponLimitSKUsException clskue = (CouponLimitSKUsException) e;
102:
103: SessionErrors.add(req, e.getClass().getName(),
104: clskue.getSkus());
105: } else {
106: SessionErrors.add(req, e.getClass().getName());
107: }
108: } else {
109: throw e;
110: }
111: }
112: }
113:
114: public ActionForward render(ActionMapping mapping, ActionForm form,
115: PortletConfig config, RenderRequest req, RenderResponse res)
116: throws Exception {
117:
118: try {
119: ActionUtil.getCoupon(req);
120: } catch (Exception e) {
121: if (e instanceof NoSuchCouponException
122: || e instanceof PrincipalException) {
123:
124: SessionErrors.add(req, e.getClass().getName());
125:
126: return mapping.findForward("portlet.shopping.error");
127: } else {
128: throw e;
129: }
130: }
131:
132: return mapping.findForward(getForward(req,
133: "portlet.shopping.edit_coupon"));
134: }
135:
136: protected void deleteCoupons(ActionRequest req) throws Exception {
137: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
138:
139: long[] deleteCouponIds = StringUtil.split(ParamUtil.getString(
140: req, "deleteCouponIds"), 0L);
141:
142: for (int i = 0; i < deleteCouponIds.length; i++) {
143: ShoppingCouponServiceUtil.deleteCoupon(layout.getPlid(),
144: deleteCouponIds[i]);
145: }
146: }
147:
148: protected void updateCoupon(ActionRequest req) throws Exception {
149: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
150:
151: long couponId = ParamUtil.getLong(req, "couponId");
152:
153: String code = ParamUtil.getString(req, "code");
154: boolean autoCode = ParamUtil.getBoolean(req, "autoCode");
155:
156: String name = ParamUtil.getString(req, "name");
157: String description = ParamUtil.getString(req, "description");
158:
159: int startDateMonth = ParamUtil
160: .getInteger(req, "startDateMonth");
161: int startDateDay = ParamUtil.getInteger(req, "startDateDay");
162: int startDateYear = ParamUtil.getInteger(req, "startDateYear");
163: int startDateHour = ParamUtil.getInteger(req, "startDateHour");
164: int startDateMinute = ParamUtil.getInteger(req,
165: "startDateMinute");
166: int startDateAmPm = ParamUtil.getInteger(req, "startDateAmPm");
167:
168: if (startDateAmPm == Calendar.PM) {
169: startDateHour += 12;
170: }
171:
172: int endDateMonth = ParamUtil.getInteger(req, "endDateMonth");
173: int endDateDay = ParamUtil.getInteger(req, "endDateDay");
174: int endDateYear = ParamUtil.getInteger(req, "endDateYear");
175: int endDateHour = ParamUtil.getInteger(req, "endDateHour");
176: int endDateMinute = ParamUtil.getInteger(req, "endDateMinute");
177: int endDateAmPm = ParamUtil.getInteger(req, "endDateAmPm");
178: boolean neverExpire = ParamUtil.getBoolean(req, "neverExpire");
179:
180: if (endDateAmPm == Calendar.PM) {
181: endDateHour += 12;
182: }
183:
184: boolean active = ParamUtil.getBoolean(req, "active");
185: String limitCategories = ParamUtil.getString(req,
186: "limitCategories");
187: String limitSkus = ParamUtil.getString(req, "limitSkus");
188: double minOrder = ParamUtil.getDouble(req, "minOrder");
189: double discount = ParamUtil.getDouble(req, "discount");
190: String discountType = ParamUtil.getString(req, "discountType");
191:
192: if (couponId <= 0) {
193:
194: // Add coupon
195:
196: ShoppingCouponServiceUtil.addCoupon(layout.getPlid(), code,
197: autoCode, name, description, startDateMonth,
198: startDateDay, startDateYear, startDateHour,
199: startDateMinute, endDateMonth, endDateDay,
200: endDateYear, endDateHour, endDateMinute,
201: neverExpire, active, limitCategories, limitSkus,
202: minOrder, discount, discountType);
203: } else {
204:
205: // Update coupon
206:
207: ShoppingCouponServiceUtil.updateCoupon(layout.getPlid(),
208: couponId, name, description, startDateMonth,
209: startDateDay, startDateYear, startDateHour,
210: startDateMinute, endDateMonth, endDateDay,
211: endDateYear, endDateHour, endDateMinute,
212: neverExpire, active, limitCategories, limitSkus,
213: minOrder, discount, discountType);
214: }
215: }
216:
217: }
|