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 @Override
061 public void processAction(
062 ActionMapping actionMapping, ActionForm actionForm,
063 PortletConfig portletConfig, ActionRequest actionRequest,
064 ActionResponse actionResponse)
065 throws Exception {
066
067 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068
069 try {
070 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
071 updateCoupon(actionRequest);
072 }
073 else if (cmd.equals(Constants.DELETE)) {
074 deleteCoupons(actionRequest);
075 }
076
077 sendRedirect(actionRequest, actionResponse);
078 }
079 catch (Exception e) {
080 if (e instanceof NoSuchCouponException ||
081 e instanceof PrincipalException) {
082
083 SessionErrors.add(actionRequest, e.getClass());
084
085 setForward(actionRequest, "portlet.shopping.error");
086 }
087 else if (e instanceof CouponCodeException ||
088 e instanceof CouponDateException ||
089 e instanceof CouponDescriptionException ||
090 e instanceof CouponDiscountException ||
091 e instanceof CouponEndDateException ||
092 e instanceof CouponLimitCategoriesException ||
093 e instanceof CouponLimitSKUsException ||
094 e instanceof CouponMinimumOrderException ||
095 e instanceof CouponNameException ||
096 e instanceof CouponStartDateException ||
097 e instanceof DuplicateCouponCodeException) {
098
099 if (e instanceof CouponLimitCategoriesException) {
100 CouponLimitCategoriesException clce =
101 (CouponLimitCategoriesException)e;
102
103 SessionErrors.add(
104 actionRequest, e.getClass(), clce.getCategoryIds());
105 }
106 else if (e instanceof CouponLimitSKUsException) {
107 CouponLimitSKUsException clskue =
108 (CouponLimitSKUsException)e;
109
110 SessionErrors.add(
111 actionRequest, e.getClass(), clskue.getSkus());
112 }
113 else {
114 SessionErrors.add(actionRequest, e.getClass());
115 }
116 }
117 else {
118 throw e;
119 }
120 }
121 }
122
123 @Override
124 public ActionForward render(
125 ActionMapping actionMapping, ActionForm actionForm,
126 PortletConfig portletConfig, RenderRequest renderRequest,
127 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());
138
139 return actionMapping.findForward("portlet.shopping.error");
140 }
141 else {
142 throw e;
143 }
144 }
145
146 return actionMapping.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
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
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 }