1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditCouponAction extends PortletAction {
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
76  
77          try {
78              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
79                  updateCoupon(actionRequest);
80              }
81              else if (cmd.equals(Constants.DELETE)) {
82                  deleteCoupons(actionRequest);
83              }
84  
85              sendRedirect(actionRequest, actionResponse);
86          }
87          catch (Exception e) {
88              if (e instanceof NoSuchCouponException ||
89                  e instanceof PrincipalException) {
90  
91                  SessionErrors.add(actionRequest, e.getClass().getName());
92  
93                  setForward(actionRequest, "portlet.shopping.error");
94              }
95              else if (e instanceof CouponCodeException ||
96                       e instanceof CouponDateException ||
97                       e instanceof CouponDescriptionException ||
98                       e instanceof CouponDiscountException ||
99                       e instanceof CouponEndDateException ||
100                      e instanceof CouponLimitCategoriesException ||
101                      e instanceof CouponLimitSKUsException ||
102                      e instanceof CouponMinimumOrderException ||
103                      e instanceof CouponNameException ||
104                      e instanceof CouponStartDateException ||
105                      e instanceof DuplicateCouponCodeException) {
106 
107                 if (e instanceof CouponLimitCategoriesException) {
108                     CouponLimitCategoriesException clce =
109                         (CouponLimitCategoriesException)e;
110 
111                     SessionErrors.add(
112                         actionRequest, e.getClass().getName(),
113                         clce.getCategoryIds());
114                 }
115                 else if (e instanceof CouponLimitSKUsException) {
116                     CouponLimitSKUsException clskue =
117                         (CouponLimitSKUsException)e;
118 
119                     SessionErrors.add(
120                         actionRequest, e.getClass().getName(),
121                         clskue.getSkus());
122                 }
123                 else {
124                     SessionErrors.add(actionRequest, e.getClass().getName());
125                 }
126             }
127             else {
128                 throw e;
129             }
130         }
131     }
132 
133     public ActionForward render(
134             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
135             RenderRequest renderRequest, RenderResponse renderResponse)
136         throws Exception {
137 
138         try {
139             ActionUtil.getCoupon(renderRequest);
140         }
141         catch (Exception e) {
142             if (e instanceof NoSuchCouponException ||
143                 e instanceof PrincipalException) {
144 
145                 SessionErrors.add(renderRequest, e.getClass().getName());
146 
147                 return mapping.findForward("portlet.shopping.error");
148             }
149             else {
150                 throw e;
151             }
152         }
153 
154         return mapping.findForward(
155             getForward(renderRequest, "portlet.shopping.edit_coupon"));
156     }
157 
158     protected void deleteCoupons(ActionRequest actionRequest) throws Exception {
159         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
160             WebKeys.THEME_DISPLAY);
161 
162         long[] deleteCouponIds = StringUtil.split(
163             ParamUtil.getString(actionRequest, "deleteCouponIds"), 0L);
164 
165         for (int i = 0; i < deleteCouponIds.length; i++) {
166             ShoppingCouponServiceUtil.deleteCoupon(
167                 themeDisplay.getScopeGroupId(), deleteCouponIds[i]);
168         }
169     }
170 
171     protected void updateCoupon(ActionRequest actionRequest) throws Exception {
172         long couponId = ParamUtil.getLong(actionRequest, "couponId");
173 
174         String code = ParamUtil.getString(actionRequest, "code");
175         boolean autoCode = ParamUtil.getBoolean(actionRequest, "autoCode");
176 
177         String name = ParamUtil.getString(actionRequest, "name");
178         String description = ParamUtil.getString(actionRequest, "description");
179 
180         int startDateMonth = ParamUtil.getInteger(
181             actionRequest, "startDateMonth");
182         int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay");
183         int startDateYear = ParamUtil.getInteger(
184             actionRequest, "startDateYear");
185         int startDateHour = ParamUtil.getInteger(
186             actionRequest, "startDateHour");
187         int startDateMinute = ParamUtil.getInteger(
188             actionRequest, "startDateMinute");
189         int startDateAmPm = ParamUtil.getInteger(
190             actionRequest, "startDateAmPm");
191 
192         if (startDateAmPm == Calendar.PM) {
193             startDateHour += 12;
194         }
195 
196         int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth");
197         int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay");
198         int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear");
199         int endDateHour = ParamUtil.getInteger(actionRequest, "endDateHour");
200         int endDateMinute = ParamUtil.getInteger(
201             actionRequest, "endDateMinute");
202         int endDateAmPm = ParamUtil.getInteger(actionRequest, "endDateAmPm");
203         boolean neverExpire = ParamUtil.getBoolean(
204             actionRequest, "neverExpire");
205 
206         if (endDateAmPm == Calendar.PM) {
207             endDateHour += 12;
208         }
209 
210         boolean active = ParamUtil.getBoolean(actionRequest, "active");
211         String limitCategories = ParamUtil.getString(
212             actionRequest, "limitCategories");
213         String limitSkus = ParamUtil.getString(actionRequest, "limitSkus");
214         double minOrder = ParamUtil.getDouble(actionRequest, "minOrder", -1.0);
215         double discount = ParamUtil.getDouble(actionRequest, "discount", -1.0);
216         String discountType = ParamUtil.getString(
217             actionRequest, "discountType");
218 
219         ServiceContext serviceContext = ServiceContextFactory.getInstance(
220             ShoppingCoupon.class.getName(), actionRequest);
221 
222         if (couponId <= 0) {
223 
224             // Add coupon
225 
226             ShoppingCouponServiceUtil.addCoupon(
227                 code, autoCode, name, description, startDateMonth, startDateDay,
228                 startDateYear, startDateHour, startDateMinute, endDateMonth,
229                 endDateDay, endDateYear, endDateHour, endDateMinute,
230                 neverExpire, active, limitCategories, limitSkus, minOrder,
231                 discount, discountType, serviceContext);
232         }
233         else {
234 
235             // Update coupon
236 
237             ShoppingCouponServiceUtil.updateCoupon(
238                 couponId, name, description, startDateMonth, startDateDay,
239                 startDateYear, startDateHour, startDateMinute, endDateMonth,
240                 endDateDay, endDateYear, endDateHour, endDateMinute,
241                 neverExpire, active, limitCategories, limitSkus, minOrder,
242                 discount, discountType, serviceContext);
243         }
244     }
245 
246 }