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