1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.shopping.CouponCodeException;
33  import com.liferay.portlet.shopping.CouponDateException;
34  import com.liferay.portlet.shopping.CouponDescriptionException;
35  import com.liferay.portlet.shopping.CouponEndDateException;
36  import com.liferay.portlet.shopping.CouponLimitCategoriesException;
37  import com.liferay.portlet.shopping.CouponLimitSKUsException;
38  import com.liferay.portlet.shopping.CouponNameException;
39  import com.liferay.portlet.shopping.CouponStartDateException;
40  import com.liferay.portlet.shopping.DuplicateCouponCodeException;
41  import com.liferay.portlet.shopping.NoSuchCouponException;
42  import com.liferay.portlet.shopping.service.ShoppingCouponServiceUtil;
43  import com.liferay.util.servlet.SessionErrors;
44  
45  import java.util.Calendar;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.RenderRequest;
51  import javax.portlet.RenderResponse;
52  
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="EditCouponAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class EditCouponAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig config,
67              ActionRequest req, ActionResponse res)
68          throws Exception {
69  
70          String cmd = ParamUtil.getString(req, Constants.CMD);
71  
72          try {
73              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
74                  updateCoupon(req);
75              }
76              else if (cmd.equals(Constants.DELETE)) {
77                  deleteCoupons(req);
78              }
79  
80              sendRedirect(req, res);
81          }
82          catch (Exception e) {
83              if (e instanceof NoSuchCouponException ||
84                  e instanceof PrincipalException) {
85  
86                  SessionErrors.add(req, e.getClass().getName());
87  
88                  setForward(req, "portlet.shopping.error");
89              }
90              else if (e instanceof CouponCodeException ||
91                       e instanceof CouponDateException ||
92                       e instanceof CouponDescriptionException ||
93                       e instanceof CouponEndDateException ||
94                       e instanceof CouponLimitCategoriesException ||
95                       e instanceof CouponLimitSKUsException ||
96                       e instanceof CouponNameException ||
97                       e instanceof CouponStartDateException ||
98                       e instanceof DuplicateCouponCodeException) {
99  
100                 if (e instanceof CouponLimitCategoriesException) {
101                     CouponLimitCategoriesException clce =
102                         (CouponLimitCategoriesException)e;
103 
104                     SessionErrors.add(
105                         req, e.getClass().getName(), clce.getCategoryIds());
106                 }
107                 else if (e instanceof CouponLimitSKUsException) {
108                     CouponLimitSKUsException clskue =
109                         (CouponLimitSKUsException)e;
110 
111                     SessionErrors.add(
112                         req, e.getClass().getName(), clskue.getSkus());
113                 }
114                 else {
115                     SessionErrors.add(req, e.getClass().getName());
116                 }
117             }
118             else {
119                 throw e;
120             }
121         }
122     }
123 
124     public ActionForward render(
125             ActionMapping mapping, ActionForm form, PortletConfig config,
126             RenderRequest req, RenderResponse res)
127         throws Exception {
128 
129         try {
130             ActionUtil.getCoupon(req);
131         }
132         catch (Exception e) {
133             if (e instanceof NoSuchCouponException ||
134                 e instanceof PrincipalException) {
135 
136                 SessionErrors.add(req, e.getClass().getName());
137 
138                 return mapping.findForward("portlet.shopping.error");
139             }
140             else {
141                 throw e;
142             }
143         }
144 
145         return mapping.findForward(
146             getForward(req, "portlet.shopping.edit_coupon"));
147     }
148 
149     protected void deleteCoupons(ActionRequest req) throws Exception {
150         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
151 
152         long[] deleteCouponIds = StringUtil.split(
153             ParamUtil.getString(req, "deleteCouponIds"), 0L);
154 
155         for (int i = 0; i < deleteCouponIds.length; i++) {
156             ShoppingCouponServiceUtil.deleteCoupon(
157                 layout.getPlid(), deleteCouponIds[i]);
158         }
159     }
160 
161     protected void updateCoupon(ActionRequest req) throws Exception {
162         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
163 
164         long couponId = ParamUtil.getLong(req, "couponId");
165 
166         String code = ParamUtil.getString(req, "code");
167         boolean autoCode = ParamUtil.getBoolean(req, "autoCode");
168 
169         String name = ParamUtil.getString(req, "name");
170         String description = ParamUtil.getString(req, "description");
171 
172         int startDateMonth = ParamUtil.getInteger(req, "startDateMonth");
173         int startDateDay = ParamUtil.getInteger(req, "startDateDay");
174         int startDateYear = ParamUtil.getInteger(req, "startDateYear");
175         int startDateHour = ParamUtil.getInteger(req, "startDateHour");
176         int startDateMinute = ParamUtil.getInteger(req, "startDateMinute");
177         int startDateAmPm = ParamUtil.getInteger(req, "startDateAmPm");
178 
179         if (startDateAmPm == Calendar.PM) {
180             startDateHour += 12;
181         }
182 
183         int endDateMonth = ParamUtil.getInteger(req, "endDateMonth");
184         int endDateDay = ParamUtil.getInteger(req, "endDateDay");
185         int endDateYear = ParamUtil.getInteger(req, "endDateYear");
186         int endDateHour = ParamUtil.getInteger(req, "endDateHour");
187         int endDateMinute = ParamUtil.getInteger(req, "endDateMinute");
188         int endDateAmPm = ParamUtil.getInteger(req, "endDateAmPm");
189         boolean neverExpire = ParamUtil.getBoolean(req, "neverExpire");
190 
191         if (endDateAmPm == Calendar.PM) {
192             endDateHour += 12;
193         }
194 
195         boolean active = ParamUtil.getBoolean(req, "active");
196         String limitCategories = ParamUtil.getString(req, "limitCategories");
197         String limitSkus = ParamUtil.getString(req, "limitSkus");
198         double minOrder = ParamUtil.getDouble(req, "minOrder");
199         double discount = ParamUtil.getDouble(req, "discount");
200         String discountType = ParamUtil.getString(req, "discountType");
201 
202         if (couponId <= 0) {
203 
204             // Add coupon
205 
206             ShoppingCouponServiceUtil.addCoupon(
207                 layout.getPlid(), code, autoCode, name, description,
208                 startDateMonth, startDateDay, startDateYear, startDateHour,
209                 startDateMinute, endDateMonth, endDateDay, endDateYear,
210                 endDateHour, endDateMinute, neverExpire, active,
211                 limitCategories, limitSkus, minOrder, discount, discountType);
212         }
213         else {
214 
215             // Update coupon
216 
217             ShoppingCouponServiceUtil.updateCoupon(
218                 layout.getPlid(), couponId, name, description, startDateMonth,
219                 startDateDay, startDateYear, startDateHour, startDateMinute,
220                 endDateMonth, endDateDay, endDateYear, endDateHour,
221                 endDateMinute, neverExpire, active, limitCategories, limitSkus,
222                 minOrder, discount, discountType);
223         }
224     }
225 
226 }