1   /**
2    * Copyright (c) 2000-2009 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.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.shopping.CouponCodeException;
36  import com.liferay.portlet.shopping.CouponDateException;
37  import com.liferay.portlet.shopping.CouponDescriptionException;
38  import com.liferay.portlet.shopping.CouponDiscountException;
39  import com.liferay.portlet.shopping.CouponEndDateException;
40  import com.liferay.portlet.shopping.CouponLimitCategoriesException;
41  import com.liferay.portlet.shopping.CouponLimitSKUsException;
42  import com.liferay.portlet.shopping.CouponMinimumOrderException;
43  import com.liferay.portlet.shopping.CouponNameException;
44  import com.liferay.portlet.shopping.CouponStartDateException;
45  import com.liferay.portlet.shopping.DuplicateCouponCodeException;
46  import com.liferay.portlet.shopping.NoSuchCouponException;
47  import com.liferay.portlet.shopping.model.ShoppingCoupon;
48  import com.liferay.portlet.shopping.service.ShoppingCouponServiceUtil;
49  
50  import java.util.Calendar;
51  
52  import javax.portlet.ActionRequest;
53  import javax.portlet.ActionResponse;
54  import javax.portlet.PortletConfig;
55  import javax.portlet.RenderRequest;
56  import javax.portlet.RenderResponse;
57  
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionForward;
60  import org.apache.struts.action.ActionMapping;
61  
62  /**
63   * <a href="EditCouponAction.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   * @author Huang Jie
67   *
68   */
69  public class EditCouponAction extends PortletAction {
70  
71      public void processAction(
72              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
73              ActionRequest actionRequest, ActionResponse actionResponse)
74          throws Exception {
75  
76          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
77  
78          try {
79              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
80                  updateCoupon(actionRequest);
81              }
82              else if (cmd.equals(Constants.DELETE)) {
83                  deleteCoupons(actionRequest);
84              }
85  
86              sendRedirect(actionRequest, actionResponse);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchCouponException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(actionRequest, e.getClass().getName());
93  
94                  setForward(actionRequest, "portlet.shopping.error");
95              }
96              else if (e instanceof CouponCodeException ||
97                       e instanceof CouponDateException ||
98                       e instanceof CouponDescriptionException ||
99                       e instanceof CouponDiscountException ||
100                      e instanceof CouponEndDateException ||
101                      e instanceof CouponLimitCategoriesException ||
102                      e instanceof CouponLimitSKUsException ||
103                      e instanceof CouponMinimumOrderException ||
104                      e instanceof CouponNameException ||
105                      e instanceof CouponStartDateException ||
106                      e instanceof DuplicateCouponCodeException) {
107 
108                 if (e instanceof CouponLimitCategoriesException) {
109                     CouponLimitCategoriesException clce =
110                         (CouponLimitCategoriesException)e;
111 
112                     SessionErrors.add(
113                         actionRequest, e.getClass().getName(),
114                         clce.getCategoryIds());
115                 }
116                 else if (e instanceof CouponLimitSKUsException) {
117                     CouponLimitSKUsException clskue =
118                         (CouponLimitSKUsException)e;
119 
120                     SessionErrors.add(
121                         actionRequest, e.getClass().getName(),
122                         clskue.getSkus());
123                 }
124                 else {
125                     SessionErrors.add(actionRequest, e.getClass().getName());
126                 }
127             }
128             else {
129                 throw e;
130             }
131         }
132     }
133 
134     public ActionForward render(
135             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
136             RenderRequest renderRequest, RenderResponse renderResponse)
137         throws Exception {
138 
139         try {
140             ActionUtil.getCoupon(renderRequest);
141         }
142         catch (Exception e) {
143             if (e instanceof NoSuchCouponException ||
144                 e instanceof PrincipalException) {
145 
146                 SessionErrors.add(renderRequest, e.getClass().getName());
147 
148                 return mapping.findForward("portlet.shopping.error");
149             }
150             else {
151                 throw e;
152             }
153         }
154 
155         return mapping.findForward(
156             getForward(renderRequest, "portlet.shopping.edit_coupon"));
157     }
158 
159     protected void deleteCoupons(ActionRequest actionRequest) throws Exception {
160         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
161             WebKeys.THEME_DISPLAY);
162 
163         long[] deleteCouponIds = StringUtil.split(
164             ParamUtil.getString(actionRequest, "deleteCouponIds"), 0L);
165 
166         for (int i = 0; i < deleteCouponIds.length; i++) {
167             ShoppingCouponServiceUtil.deleteCoupon(
168                 themeDisplay.getScopeGroupId(), deleteCouponIds[i]);
169         }
170     }
171 
172     protected void updateCoupon(ActionRequest actionRequest) throws Exception {
173         long couponId = ParamUtil.getLong(actionRequest, "couponId");
174 
175         String code = ParamUtil.getString(actionRequest, "code");
176         boolean autoCode = ParamUtil.getBoolean(actionRequest, "autoCode");
177 
178         String name = ParamUtil.getString(actionRequest, "name");
179         String description = ParamUtil.getString(actionRequest, "description");
180 
181         int startDateMonth = ParamUtil.getInteger(
182             actionRequest, "startDateMonth");
183         int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay");
184         int startDateYear = ParamUtil.getInteger(
185             actionRequest, "startDateYear");
186         int startDateHour = ParamUtil.getInteger(
187             actionRequest, "startDateHour");
188         int startDateMinute = ParamUtil.getInteger(
189             actionRequest, "startDateMinute");
190         int startDateAmPm = ParamUtil.getInteger(
191             actionRequest, "startDateAmPm");
192 
193         if (startDateAmPm == Calendar.PM) {
194             startDateHour += 12;
195         }
196 
197         int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth");
198         int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay");
199         int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear");
200         int endDateHour = ParamUtil.getInteger(actionRequest, "endDateHour");
201         int endDateMinute = ParamUtil.getInteger(
202             actionRequest, "endDateMinute");
203         int endDateAmPm = ParamUtil.getInteger(actionRequest, "endDateAmPm");
204         boolean neverExpire = ParamUtil.getBoolean(
205             actionRequest, "neverExpire");
206 
207         if (endDateAmPm == Calendar.PM) {
208             endDateHour += 12;
209         }
210 
211         boolean active = ParamUtil.getBoolean(actionRequest, "active");
212         String limitCategories = ParamUtil.getString(
213             actionRequest, "limitCategories");
214         String limitSkus = ParamUtil.getString(actionRequest, "limitSkus");
215         double minOrder = ParamUtil.getDouble(actionRequest, "minOrder", -1.0);
216         double discount = ParamUtil.getDouble(actionRequest, "discount", -1.0);
217         String discountType = ParamUtil.getString(
218             actionRequest, "discountType");
219 
220         ServiceContext serviceContext = ServiceContextFactory.getInstance(
221             ShoppingCoupon.class.getName(), actionRequest);
222 
223         if (couponId <= 0) {
224 
225             // Add coupon
226 
227             ShoppingCouponServiceUtil.addCoupon(
228                 code, autoCode, name, description, startDateMonth, startDateDay,
229                 startDateYear, startDateHour, startDateMinute, endDateMonth,
230                 endDateDay, endDateYear, endDateHour, endDateMinute,
231                 neverExpire, active, limitCategories, limitSkus, minOrder,
232                 discount, discountType, serviceContext);
233         }
234         else {
235 
236             // Update coupon
237 
238             ShoppingCouponServiceUtil.updateCoupon(
239                 couponId, name, description, startDateMonth, startDateDay,
240                 startDateYear, startDateHour, startDateMinute, endDateMonth,
241                 endDateDay, endDateYear, endDateHour, endDateMinute,
242                 neverExpire, active, limitCategories, limitSkus, minOrder,
243                 discount, discountType, serviceContext);
244         }
245     }
246 
247 }