1
22
23 package com.liferay.portlet.shopping.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.persistence.UserUtil;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.portlet.shopping.CouponCodeException;
35 import com.liferay.portlet.shopping.CouponDateException;
36 import com.liferay.portlet.shopping.CouponDescriptionException;
37 import com.liferay.portlet.shopping.CouponEndDateException;
38 import com.liferay.portlet.shopping.CouponLimitCategoriesException;
39 import com.liferay.portlet.shopping.CouponLimitSKUsException;
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.NoSuchCategoryException;
44 import com.liferay.portlet.shopping.NoSuchCouponException;
45 import com.liferay.portlet.shopping.NoSuchItemException;
46 import com.liferay.portlet.shopping.model.ShoppingCategory;
47 import com.liferay.portlet.shopping.model.ShoppingCoupon;
48 import com.liferay.portlet.shopping.model.ShoppingItem;
49 import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
50 import com.liferay.portlet.shopping.service.persistence.ShoppingCategoryUtil;
51 import com.liferay.portlet.shopping.service.persistence.ShoppingCouponFinder;
52 import com.liferay.portlet.shopping.service.persistence.ShoppingCouponUtil;
53 import com.liferay.portlet.shopping.service.persistence.ShoppingItemUtil;
54 import com.liferay.util.PwdGenerator;
55
56 import java.util.ArrayList;
57 import java.util.Date;
58 import java.util.List;
59
60
67 public class ShoppingCouponLocalServiceImpl
68 extends ShoppingCouponLocalServiceBaseImpl {
69
70 public ShoppingCoupon addCoupon(
71 long userId, long plid, String code, boolean autoCode, String name,
72 String description, int startDateMonth, int startDateDay,
73 int startDateYear, int startDateHour, int startDateMinute,
74 int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
75 int endDateMinute, boolean neverExpire, boolean active,
76 String limitCategories, String limitSkus, double minOrder,
77 double discount, String discountType)
78 throws PortalException, SystemException {
79
80
82 User user = UserUtil.findByPrimaryKey(userId);
83 long groupId = PortalUtil.getPortletGroupId(plid);
84
85 code = code.trim().toUpperCase();
86
87 if (autoCode) {
88 code = getCode();
89 }
90
91 Date now = new Date();
92
93 Date startDate = PortalUtil.getDate(
94 startDateMonth, startDateDay, startDateYear, startDateHour,
95 startDateMinute, user.getTimeZone(),
96 new CouponStartDateException());
97
98 Date endDate = null;
99
100 if (!neverExpire) {
101 endDate = PortalUtil.getDate(
102 endDateMonth, endDateDay, endDateYear, endDateHour,
103 endDateMinute, user.getTimeZone(),
104 new CouponEndDateException());
105 }
106
107 if ((endDate != null) && (startDate.after(endDate))) {
108 throw new CouponDateException();
109 }
110
111 validate(
112 user.getCompanyId(), groupId, code, autoCode, name, description,
113 limitCategories, limitSkus);
114
115 long couponId = CounterLocalServiceUtil.increment();
116
117 ShoppingCoupon coupon = ShoppingCouponUtil.create(couponId);
118
119 coupon.setGroupId(groupId);
120 coupon.setCompanyId(user.getCompanyId());
121 coupon.setUserId(user.getUserId());
122 coupon.setUserName(user.getFullName());
123 coupon.setCreateDate(now);
124 coupon.setModifiedDate(now);
125 coupon.setCode(code);
126 coupon.setName(name);
127 coupon.setDescription(description);
128 coupon.setStartDate(startDate);
129 coupon.setEndDate(endDate);
130 coupon.setActive(active);
131 coupon.setLimitCategories(limitCategories);
132 coupon.setLimitSkus(limitSkus);
133 coupon.setMinOrder(minOrder);
134 coupon.setDiscount(discount);
135 coupon.setDiscountType(discountType);
136
137 ShoppingCouponUtil.update(coupon);
138
139 return coupon;
140 }
141
142 public void deleteCoupon(long couponId)
143 throws PortalException, SystemException {
144
145 ShoppingCouponUtil.remove(couponId);
146 }
147
148 public void deleteCoupons(long groupId) throws SystemException {
149 ShoppingCouponUtil.removeByGroupId(groupId);
150 }
151
152 public ShoppingCoupon getCoupon(long couponId)
153 throws PortalException, SystemException {
154
155 return ShoppingCouponUtil.findByPrimaryKey(couponId);
156 }
157
158 public ShoppingCoupon getCoupon(String code)
159 throws PortalException, SystemException {
160
161 code = code.trim().toUpperCase();
162
163 return ShoppingCouponUtil.findByCode(code);
164 }
165
166 public List search(
167 long plid, long companyId, String code, boolean active,
168 String discountType, boolean andOperator, int begin, int end)
169 throws SystemException {
170
171 long groupId = PortalUtil.getPortletGroupId(plid);
172
173 return ShoppingCouponFinder.findByG_C_C_A_DT(
174 groupId, companyId, code, active, discountType, andOperator,
175 begin, end);
176 }
177
178 public int searchCount(
179 long groupId, long companyId, String code, boolean active,
180 String discountType, boolean andOperator)
181 throws SystemException {
182
183 return ShoppingCouponFinder.countByG_C_C_A_DT(
184 groupId, companyId, code, active, discountType, andOperator);
185 }
186
187 public ShoppingCoupon updateCoupon(
188 long userId, long couponId, String name, String description,
189 int startDateMonth, int startDateDay, int startDateYear,
190 int startDateHour, int startDateMinute, int endDateMonth,
191 int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
192 boolean neverExpire, boolean active, String limitCategories,
193 String limitSkus, double minOrder, double discount,
194 String discountType)
195 throws PortalException, SystemException {
196
197 User user = UserUtil.findByPrimaryKey(userId);
198
199 ShoppingCoupon coupon = ShoppingCouponUtil.findByPrimaryKey(couponId);
200
201 Date startDate = PortalUtil.getDate(
202 startDateMonth, startDateDay, startDateYear, startDateHour,
203 startDateMinute, user.getTimeZone(),
204 new CouponStartDateException());
205
206 Date endDate = null;
207
208 if (!neverExpire) {
209 endDate = PortalUtil.getDate(
210 endDateMonth, endDateDay, endDateYear, endDateHour,
211 endDateMinute, user.getTimeZone(),
212 new CouponEndDateException());
213 }
214
215 if ((endDate != null) && (startDate.after(endDate))) {
216 throw new CouponDateException();
217 }
218
219 validate(
220 coupon.getCompanyId(), coupon.getGroupId(), name, description,
221 limitCategories, limitSkus);
222
223 coupon.setModifiedDate(new Date());
224 coupon.setName(name);
225 coupon.setDescription(description);
226 coupon.setStartDate(startDate);
227 coupon.setEndDate(endDate);
228 coupon.setActive(active);
229 coupon.setLimitCategories(limitCategories);
230 coupon.setLimitSkus(limitSkus);
231 coupon.setMinOrder(minOrder);
232 coupon.setDiscount(discount);
233 coupon.setDiscountType(discountType);
234
235 ShoppingCouponUtil.update(coupon);
236
237 return coupon;
238 }
239
240 protected String getCode() throws SystemException {
241 String code =
242 PwdGenerator.getPassword(PwdGenerator.KEY1 + PwdGenerator.KEY2, 8);
243
244 try {
245 ShoppingCouponUtil.findByCode(code);
246
247 return getCode();
248 }
249 catch (NoSuchCouponException nsce) {
250 return code;
251 }
252 }
253
254 protected void validate(
255 long companyId, long groupId, String code, boolean autoCode,
256 String name, String description, String limitCategories,
257 String limitSkus)
258 throws PortalException, SystemException {
259
260 if (!autoCode) {
261 if ((Validator.isNull(code)) ||
262 (Validator.isNumber(code)) ||
263 (code.indexOf(StringPool.SPACE) != -1)) {
264
265 throw new CouponCodeException();
266 }
267
268 try {
269 ShoppingCouponUtil.findByCode(code);
270
271 throw new DuplicateCouponCodeException();
272 }
273 catch (NoSuchCouponException nsce) {
274 }
275 }
276
277 validate(
278 companyId, groupId, name, description, limitCategories, limitSkus);
279 }
280
281 protected void validate(
282 long companyId, long groupId, String name, String description,
283 String limitCategories, String limitSkus)
284 throws PortalException, SystemException {
285
286 if (Validator.isNull(name)) {
287 throw new CouponNameException();
288 }
289 else if (Validator.isNull(description)) {
290 throw new CouponDescriptionException();
291 }
292
293
295 long[] categoryIds = StringUtil.split(limitCategories, 0L);
296
297 List invalidCategoryIds = new ArrayList();
298
299 for (int i = 0; i < categoryIds.length; i++) {
300 try {
301 ShoppingCategory category =
302 ShoppingCategoryUtil.findByPrimaryKey(categoryIds[i]);
303
304 if (category.getGroupId() != groupId) {
305 invalidCategoryIds.add(new Long(categoryIds[i]));
306 }
307 }
308 catch (NoSuchCategoryException nsce) {
309 invalidCategoryIds.add(new Long(categoryIds[i]));
310 }
311 }
312
313 if (invalidCategoryIds.size() > 0) {
314 CouponLimitCategoriesException clce =
315 new CouponLimitCategoriesException();
316
317 clce.setCategoryIds(invalidCategoryIds);
318
319 throw clce;
320 }
321
322
324 String[] skus = StringUtil.split(limitSkus);
325
326 List invalidSkus = new ArrayList();
327
328 for (int i = 0; i < skus.length; i++) {
329 try {
330 ShoppingItem item =
331 ShoppingItemUtil.findByC_S(companyId, skus[i]);
332
333 ShoppingCategory category = item.getCategory();
334
335 if (category.getGroupId() != groupId) {
336 invalidSkus.add(skus[i]);
337 }
338 }
339 catch (NoSuchItemException nsie) {
340 invalidSkus.add(skus[i]);
341 }
342 }
343
344 if (invalidSkus.size() > 0) {
345 CouponLimitSKUsException clskue = new CouponLimitSKUsException();
346
347 clskue.setSkus(invalidSkus);
348
349 throw clskue;
350 }
351 }
352
353 }