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