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