001
014
015 package com.liferay.portlet.shopping.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.util.Constants;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.security.auth.PrincipalException;
022 import com.liferay.portal.service.ServiceContext;
023 import com.liferay.portal.service.ServiceContextFactory;
024 import com.liferay.portal.struts.PortletAction;
025 import com.liferay.portal.theme.ThemeDisplay;
026 import com.liferay.portal.util.WebKeys;
027 import com.liferay.portlet.shopping.CouponCodeException;
028 import com.liferay.portlet.shopping.CouponDateException;
029 import com.liferay.portlet.shopping.CouponDescriptionException;
030 import com.liferay.portlet.shopping.CouponDiscountException;
031 import com.liferay.portlet.shopping.CouponEndDateException;
032 import com.liferay.portlet.shopping.CouponLimitCategoriesException;
033 import com.liferay.portlet.shopping.CouponLimitSKUsException;
034 import com.liferay.portlet.shopping.CouponMinimumOrderException;
035 import com.liferay.portlet.shopping.CouponNameException;
036 import com.liferay.portlet.shopping.CouponStartDateException;
037 import com.liferay.portlet.shopping.DuplicateCouponCodeException;
038 import com.liferay.portlet.shopping.NoSuchCouponException;
039 import com.liferay.portlet.shopping.model.ShoppingCoupon;
040 import com.liferay.portlet.shopping.service.ShoppingCouponServiceUtil;
041
042 import java.util.Calendar;
043
044 import javax.portlet.ActionRequest;
045 import javax.portlet.ActionResponse;
046 import javax.portlet.PortletConfig;
047 import javax.portlet.RenderRequest;
048 import javax.portlet.RenderResponse;
049
050 import org.apache.struts.action.ActionForm;
051 import org.apache.struts.action.ActionForward;
052 import org.apache.struts.action.ActionMapping;
053
054
058 public class EditCouponAction extends PortletAction {
059
060 public void processAction(
061 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062 ActionRequest actionRequest, ActionResponse actionResponse)
063 throws Exception {
064
065 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066
067 try {
068 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069 updateCoupon(actionRequest);
070 }
071 else if (cmd.equals(Constants.DELETE)) {
072 deleteCoupons(actionRequest);
073 }
074
075 sendRedirect(actionRequest, actionResponse);
076 }
077 catch (Exception e) {
078 if (e instanceof NoSuchCouponException ||
079 e instanceof PrincipalException) {
080
081 SessionErrors.add(actionRequest, e.getClass().getName());
082
083 setForward(actionRequest, "portlet.shopping.error");
084 }
085 else if (e instanceof CouponCodeException ||
086 e instanceof CouponDateException ||
087 e instanceof CouponDescriptionException ||
088 e instanceof CouponDiscountException ||
089 e instanceof CouponEndDateException ||
090 e instanceof CouponLimitCategoriesException ||
091 e instanceof CouponLimitSKUsException ||
092 e instanceof CouponMinimumOrderException ||
093 e instanceof CouponNameException ||
094 e instanceof CouponStartDateException ||
095 e instanceof DuplicateCouponCodeException) {
096
097 if (e instanceof CouponLimitCategoriesException) {
098 CouponLimitCategoriesException clce =
099 (CouponLimitCategoriesException)e;
100
101 SessionErrors.add(
102 actionRequest, e.getClass().getName(),
103 clce.getCategoryIds());
104 }
105 else if (e instanceof CouponLimitSKUsException) {
106 CouponLimitSKUsException clskue =
107 (CouponLimitSKUsException)e;
108
109 SessionErrors.add(
110 actionRequest, e.getClass().getName(),
111 clskue.getSkus());
112 }
113 else {
114 SessionErrors.add(actionRequest, e.getClass().getName());
115 }
116 }
117 else {
118 throw e;
119 }
120 }
121 }
122
123 public ActionForward render(
124 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
125 RenderRequest renderRequest, RenderResponse renderResponse)
126 throws Exception {
127
128 try {
129 ActionUtil.getCoupon(renderRequest);
130 }
131 catch (Exception e) {
132 if (e instanceof NoSuchCouponException ||
133 e instanceof PrincipalException) {
134
135 SessionErrors.add(renderRequest, e.getClass().getName());
136
137 return mapping.findForward("portlet.shopping.error");
138 }
139 else {
140 throw e;
141 }
142 }
143
144 return mapping.findForward(
145 getForward(renderRequest, "portlet.shopping.edit_coupon"));
146 }
147
148 protected void deleteCoupons(ActionRequest actionRequest) throws Exception {
149 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
150 WebKeys.THEME_DISPLAY);
151
152 long[] deleteCouponIds = StringUtil.split(
153 ParamUtil.getString(actionRequest, "deleteCouponIds"), 0L);
154
155 for (int i = 0; i < deleteCouponIds.length; i++) {
156 ShoppingCouponServiceUtil.deleteCoupon(
157 themeDisplay.getScopeGroupId(), deleteCouponIds[i]);
158 }
159 }
160
161 protected void updateCoupon(ActionRequest actionRequest) throws Exception {
162 long couponId = ParamUtil.getLong(actionRequest, "couponId");
163
164 String code = ParamUtil.getString(actionRequest, "code");
165 boolean autoCode = ParamUtil.getBoolean(actionRequest, "autoCode");
166
167 String name = ParamUtil.getString(actionRequest, "name");
168 String description = ParamUtil.getString(actionRequest, "description");
169
170 int startDateMonth = ParamUtil.getInteger(
171 actionRequest, "startDateMonth");
172 int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay");
173 int startDateYear = ParamUtil.getInteger(
174 actionRequest, "startDateYear");
175 int startDateHour = ParamUtil.getInteger(
176 actionRequest, "startDateHour");
177 int startDateMinute = ParamUtil.getInteger(
178 actionRequest, "startDateMinute");
179 int startDateAmPm = ParamUtil.getInteger(
180 actionRequest, "startDateAmPm");
181
182 if (startDateAmPm == Calendar.PM) {
183 startDateHour += 12;
184 }
185
186 int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth");
187 int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay");
188 int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear");
189 int endDateHour = ParamUtil.getInteger(actionRequest, "endDateHour");
190 int endDateMinute = ParamUtil.getInteger(
191 actionRequest, "endDateMinute");
192 int endDateAmPm = ParamUtil.getInteger(actionRequest, "endDateAmPm");
193 boolean neverExpire = ParamUtil.getBoolean(
194 actionRequest, "neverExpire");
195
196 if (endDateAmPm == Calendar.PM) {
197 endDateHour += 12;
198 }
199
200 boolean active = ParamUtil.getBoolean(actionRequest, "active");
201 String limitCategories = ParamUtil.getString(
202 actionRequest, "limitCategories");
203 String limitSkus = ParamUtil.getString(actionRequest, "limitSkus");
204 double minOrder = ParamUtil.getDouble(actionRequest, "minOrder", -1.0);
205 double discount = ParamUtil.getDouble(actionRequest, "discount", -1.0);
206 String discountType = ParamUtil.getString(
207 actionRequest, "discountType");
208
209 ServiceContext serviceContext = ServiceContextFactory.getInstance(
210 ShoppingCoupon.class.getName(), actionRequest);
211
212 if (couponId <= 0) {
213
214
215
216 ShoppingCouponServiceUtil.addCoupon(
217 code, autoCode, name, description, startDateMonth, startDateDay,
218 startDateYear, startDateHour, startDateMinute, endDateMonth,
219 endDateDay, endDateYear, endDateHour, endDateMinute,
220 neverExpire, active, limitCategories, limitSkus, minOrder,
221 discount, discountType, serviceContext);
222 }
223 else {
224
225
226
227 ShoppingCouponServiceUtil.updateCoupon(
228 couponId, 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 }
235
236 }