1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.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.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  /**
55   * <a href="ShoppingCouponLocalServiceImpl.java.html"><b><i>View Source</i></b>
56   * </a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Huang Jie
60   *
61   */
62  public class ShoppingCouponLocalServiceImpl
63      extends ShoppingCouponLocalServiceBaseImpl {
64  
65      public ShoppingCoupon addCoupon(
66              long userId, long plid, String code, boolean autoCode, String name,
67              String description, int startDateMonth, int startDateDay,
68              int startDateYear, int startDateHour, int startDateMinute,
69              int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
70              int endDateMinute, boolean neverExpire, boolean active,
71              String limitCategories, String limitSkus, double minOrder,
72              double discount, String discountType)
73          throws PortalException, SystemException {
74  
75          // Coupon
76  
77          User user = userPersistence.findByPrimaryKey(userId);
78          long groupId = PortalUtil.getScopeGroupId(plid);
79  
80          code = code.trim().toUpperCase();
81  
82          if (autoCode) {
83              code = getCode();
84          }
85  
86          Date startDate = PortalUtil.getDate(
87              startDateMonth, startDateDay, startDateYear, startDateHour,
88              startDateMinute, user.getTimeZone(),
89              new CouponStartDateException());
90  
91          Date endDate = null;
92  
93          if (!neverExpire) {
94              endDate = PortalUtil.getDate(
95                  endDateMonth, endDateDay, endDateYear, endDateHour,
96                  endDateMinute, user.getTimeZone(),
97                  new CouponEndDateException());
98          }
99  
100         if ((endDate != null) && (startDate.after(endDate))) {
101             throw new CouponDateException();
102         }
103 
104         Date now = new Date();
105 
106         validate(
107             user.getCompanyId(), groupId, code, autoCode, name, description,
108             limitCategories, limitSkus, minOrder, discount);
109 
110         long couponId = counterLocalService.increment();
111 
112         ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
113 
114         coupon.setGroupId(groupId);
115         coupon.setCompanyId(user.getCompanyId());
116         coupon.setUserId(user.getUserId());
117         coupon.setUserName(user.getFullName());
118         coupon.setCreateDate(now);
119         coupon.setModifiedDate(now);
120         coupon.setCode(code);
121         coupon.setName(name);
122         coupon.setDescription(description);
123         coupon.setStartDate(startDate);
124         coupon.setEndDate(endDate);
125         coupon.setActive(active);
126         coupon.setLimitCategories(limitCategories);
127         coupon.setLimitSkus(limitSkus);
128         coupon.setMinOrder(minOrder);
129         coupon.setDiscount(discount);
130         coupon.setDiscountType(discountType);
131 
132         shoppingCouponPersistence.update(coupon, false);
133 
134         return coupon;
135     }
136 
137     public void deleteCoupon(long couponId)
138         throws PortalException, SystemException {
139 
140         shoppingCouponPersistence.remove(couponId);
141     }
142 
143     public void deleteCoupons(long groupId) throws SystemException {
144         shoppingCouponPersistence.removeByGroupId(groupId);
145     }
146 
147     public ShoppingCoupon getCoupon(long couponId)
148         throws PortalException, SystemException {
149 
150         return shoppingCouponPersistence.findByPrimaryKey(couponId);
151     }
152 
153     public ShoppingCoupon getCoupon(String code)
154         throws PortalException, SystemException {
155 
156         code = code.trim().toUpperCase();
157 
158         return shoppingCouponPersistence.findByCode(code);
159     }
160 
161     public List<ShoppingCoupon> search(
162             long plid, long companyId, String code, boolean active,
163             String discountType, boolean andOperator, int start, int end)
164         throws SystemException {
165 
166         long groupId = PortalUtil.getScopeGroupId(plid);
167 
168         return shoppingCouponFinder.findByG_C_C_A_DT(
169             groupId, companyId, code, active, discountType, andOperator,
170             start, end);
171     }
172 
173     public int searchCount(
174             long groupId, long companyId, String code, boolean active,
175             String discountType, boolean andOperator)
176         throws SystemException {
177 
178         return shoppingCouponFinder.countByG_C_C_A_DT(
179             groupId, companyId, code, active, discountType, andOperator);
180     }
181 
182     public ShoppingCoupon updateCoupon(
183             long userId, long couponId, String name, String description,
184             int startDateMonth, int startDateDay, int startDateYear,
185             int startDateHour, int startDateMinute, int endDateMonth,
186             int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
187             boolean neverExpire, boolean active, String limitCategories,
188             String limitSkus, double minOrder, double discount,
189             String discountType)
190         throws PortalException, SystemException {
191 
192         User user = userPersistence.findByPrimaryKey(userId);
193 
194         ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
195             couponId);
196 
197         Date startDate = PortalUtil.getDate(
198             startDateMonth, startDateDay, startDateYear, startDateHour,
199             startDateMinute, user.getTimeZone(),
200             new CouponStartDateException());
201 
202         Date endDate = null;
203 
204         if (!neverExpire) {
205             endDate = PortalUtil.getDate(
206                 endDateMonth, endDateDay, endDateYear, endDateHour,
207                 endDateMinute, user.getTimeZone(),
208                 new CouponEndDateException());
209         }
210 
211         if ((endDate != null) && (startDate.after(endDate))) {
212             throw new CouponDateException();
213         }
214 
215         validate(
216             coupon.getCompanyId(), coupon.getGroupId(), name, description,
217             limitCategories, limitSkus, minOrder, discount);
218 
219         coupon.setModifiedDate(new Date());
220         coupon.setName(name);
221         coupon.setDescription(description);
222         coupon.setStartDate(startDate);
223         coupon.setEndDate(endDate);
224         coupon.setActive(active);
225         coupon.setLimitCategories(limitCategories);
226         coupon.setLimitSkus(limitSkus);
227         coupon.setMinOrder(minOrder);
228         coupon.setDiscount(discount);
229         coupon.setDiscountType(discountType);
230 
231         shoppingCouponPersistence.update(coupon, false);
232 
233         return coupon;
234     }
235 
236     protected String getCode() throws SystemException {
237         String code =
238             PwdGenerator.getPassword(PwdGenerator.KEY1 + PwdGenerator.KEY2, 8);
239 
240         try {
241             shoppingCouponPersistence.findByCode(code);
242 
243             return getCode();
244         }
245         catch (NoSuchCouponException nsce) {
246             return code;
247         }
248     }
249 
250     protected void validate(
251             long companyId, long groupId, String code, boolean autoCode,
252             String name, String description, String limitCategories,
253             String limitSkus, double minOrder, double discount)
254         throws PortalException, SystemException {
255 
256         if (!autoCode) {
257             if ((Validator.isNull(code)) ||
258                 (Validator.isNumber(code)) ||
259                 (code.indexOf(StringPool.SPACE) != -1)) {
260 
261                 throw new CouponCodeException();
262             }
263 
264             if (shoppingCouponPersistence.fetchByCode(code) != null) {
265                 throw new DuplicateCouponCodeException();
266             }
267         }
268 
269         validate(
270             companyId, groupId, name, description, limitCategories, limitSkus,
271             minOrder, discount);
272     }
273 
274     protected void validate(
275             long companyId, long groupId, String name, String description,
276             String limitCategories, String limitSkus, double minOrder,
277             double discount)
278         throws PortalException, SystemException {
279 
280         if (Validator.isNull(name)) {
281             throw new CouponNameException();
282         }
283         else if (Validator.isNull(description)) {
284             throw new CouponDescriptionException();
285         }
286 
287         // Category IDs
288 
289         long[] categoryIds = StringUtil.split(limitCategories, 0L);
290 
291         List<Long> invalidCategoryIds = new ArrayList<Long>();
292 
293         for (long categoryId : categoryIds) {
294             ShoppingCategory category =
295                 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
296 
297             if ((category == null) || (category.getGroupId() != groupId)) {
298                 invalidCategoryIds.add(categoryId);
299             }
300         }
301 
302         if (invalidCategoryIds.size() > 0) {
303             CouponLimitCategoriesException clce =
304                 new CouponLimitCategoriesException();
305 
306             clce.setCategoryIds(invalidCategoryIds);
307 
308             throw clce;
309         }
310 
311         // SKUs
312 
313         String[] skus = StringUtil.split(limitSkus);
314 
315         List<String> invalidSkus = new ArrayList<String>();
316 
317         for (String sku : skus) {
318             ShoppingItem item = shoppingItemPersistence.fetchByC_S(
319                 companyId, sku);
320 
321             if (item != null) {
322                 ShoppingCategory category = item.getCategory();
323 
324                 if (category.getGroupId() != groupId) {
325                     invalidSkus.add(sku);
326                 }
327             }
328             else {
329                 invalidSkus.add(sku);
330             }
331         }
332 
333         if (invalidSkus.size() > 0) {
334             CouponLimitSKUsException clskue = new CouponLimitSKUsException();
335 
336             clskue.setSkus(invalidSkus);
337 
338             throw clskue;
339         }
340 
341         if (minOrder < 0) {
342             throw new CouponMinimumOrderException();
343         }
344 
345         if (discount < 0) {
346             throw new CouponDiscountException();
347         }
348     }
349 
350 }