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