1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.shopping.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portlet.shopping.CouponCodeException;
25  import com.liferay.portlet.shopping.CouponDateException;
26  import com.liferay.portlet.shopping.CouponDescriptionException;
27  import com.liferay.portlet.shopping.CouponDiscountException;
28  import com.liferay.portlet.shopping.CouponEndDateException;
29  import com.liferay.portlet.shopping.CouponLimitCategoriesException;
30  import com.liferay.portlet.shopping.CouponLimitSKUsException;
31  import com.liferay.portlet.shopping.CouponMinimumOrderException;
32  import com.liferay.portlet.shopping.CouponNameException;
33  import com.liferay.portlet.shopping.CouponStartDateException;
34  import com.liferay.portlet.shopping.DuplicateCouponCodeException;
35  import com.liferay.portlet.shopping.NoSuchCouponException;
36  import com.liferay.portlet.shopping.model.ShoppingCategory;
37  import com.liferay.portlet.shopping.model.ShoppingCoupon;
38  import com.liferay.portlet.shopping.model.ShoppingItem;
39  import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
40  import com.liferay.util.PwdGenerator;
41  
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.List;
45  
46  /**
47   * <a href="ShoppingCouponLocalServiceImpl.java.html"><b><i>View Source</i></b>
48   * </a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Huang Jie
52   */
53  public class ShoppingCouponLocalServiceImpl
54      extends ShoppingCouponLocalServiceBaseImpl {
55  
56      public ShoppingCoupon addCoupon(
57              long userId, long plid, String code, boolean autoCode, String name,
58              String description, int startDateMonth, int startDateDay,
59              int startDateYear, int startDateHour, int startDateMinute,
60              int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
61              int endDateMinute, boolean neverExpire, boolean active,
62              String limitCategories, String limitSkus, double minOrder,
63              double discount, String discountType)
64          throws PortalException, SystemException {
65  
66          User user = userPersistence.findByPrimaryKey(userId);
67          long groupId = PortalUtil.getScopeGroupId(plid);
68  
69          code = code.trim().toUpperCase();
70  
71          if (autoCode) {
72              code = getCode();
73          }
74  
75          Date startDate = PortalUtil.getDate(
76              startDateMonth, startDateDay, startDateYear, startDateHour,
77              startDateMinute, user.getTimeZone(),
78              new CouponStartDateException());
79  
80          Date endDate = null;
81  
82          if (!neverExpire) {
83              endDate = PortalUtil.getDate(
84                  endDateMonth, endDateDay, endDateYear, endDateHour,
85                  endDateMinute, user.getTimeZone(),
86                  new CouponEndDateException());
87          }
88  
89          if ((endDate != null) && (startDate.after(endDate))) {
90              throw new CouponDateException();
91          }
92  
93          Date now = new Date();
94  
95          validate(
96              user.getCompanyId(), groupId, code, autoCode, name, description,
97              limitCategories, limitSkus, minOrder, discount);
98  
99          long couponId = counterLocalService.increment();
100 
101         ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
102 
103         coupon.setGroupId(groupId);
104         coupon.setCompanyId(user.getCompanyId());
105         coupon.setUserId(user.getUserId());
106         coupon.setUserName(user.getFullName());
107         coupon.setCreateDate(now);
108         coupon.setModifiedDate(now);
109         coupon.setCode(code);
110         coupon.setName(name);
111         coupon.setDescription(description);
112         coupon.setStartDate(startDate);
113         coupon.setEndDate(endDate);
114         coupon.setActive(active);
115         coupon.setLimitCategories(limitCategories);
116         coupon.setLimitSkus(limitSkus);
117         coupon.setMinOrder(minOrder);
118         coupon.setDiscount(discount);
119         coupon.setDiscountType(discountType);
120 
121         shoppingCouponPersistence.update(coupon, false);
122 
123         return coupon;
124     }
125 
126     public void deleteCoupon(long couponId)
127         throws PortalException, SystemException {
128 
129         shoppingCouponPersistence.remove(couponId);
130     }
131 
132     public void deleteCoupons(long groupId) throws SystemException {
133         shoppingCouponPersistence.removeByGroupId(groupId);
134     }
135 
136     public ShoppingCoupon getCoupon(long couponId)
137         throws PortalException, SystemException {
138 
139         return shoppingCouponPersistence.findByPrimaryKey(couponId);
140     }
141 
142     public ShoppingCoupon getCoupon(String code)
143         throws PortalException, SystemException {
144 
145         code = code.trim().toUpperCase();
146 
147         return shoppingCouponPersistence.findByCode(code);
148     }
149 
150     public List<ShoppingCoupon> search(
151             long plid, long companyId, String code, boolean active,
152             String discountType, boolean andOperator, int start, int end)
153         throws SystemException {
154 
155         long groupId = PortalUtil.getScopeGroupId(plid);
156 
157         return shoppingCouponFinder.findByG_C_C_A_DT(
158             groupId, companyId, code, active, discountType, andOperator,
159             start, end);
160     }
161 
162     public int searchCount(
163             long groupId, long companyId, String code, boolean active,
164             String discountType, boolean andOperator)
165         throws SystemException {
166 
167         return shoppingCouponFinder.countByG_C_C_A_DT(
168             groupId, companyId, code, active, discountType, andOperator);
169     }
170 
171     public ShoppingCoupon updateCoupon(
172             long userId, long couponId, String name, String description,
173             int startDateMonth, int startDateDay, int startDateYear,
174             int startDateHour, int startDateMinute, int endDateMonth,
175             int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
176             boolean neverExpire, boolean active, String limitCategories,
177             String limitSkus, double minOrder, double discount,
178             String discountType)
179         throws PortalException, SystemException {
180 
181         User user = userPersistence.findByPrimaryKey(userId);
182 
183         ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
184             couponId);
185 
186         Date startDate = PortalUtil.getDate(
187             startDateMonth, startDateDay, startDateYear, startDateHour,
188             startDateMinute, user.getTimeZone(),
189             new CouponStartDateException());
190 
191         Date endDate = null;
192 
193         if (!neverExpire) {
194             endDate = PortalUtil.getDate(
195                 endDateMonth, endDateDay, endDateYear, endDateHour,
196                 endDateMinute, user.getTimeZone(),
197                 new CouponEndDateException());
198         }
199 
200         if ((endDate != null) && (startDate.after(endDate))) {
201             throw new CouponDateException();
202         }
203 
204         validate(
205             coupon.getCompanyId(), coupon.getGroupId(), name, description,
206             limitCategories, limitSkus, minOrder, discount);
207 
208         coupon.setModifiedDate(new Date());
209         coupon.setName(name);
210         coupon.setDescription(description);
211         coupon.setStartDate(startDate);
212         coupon.setEndDate(endDate);
213         coupon.setActive(active);
214         coupon.setLimitCategories(limitCategories);
215         coupon.setLimitSkus(limitSkus);
216         coupon.setMinOrder(minOrder);
217         coupon.setDiscount(discount);
218         coupon.setDiscountType(discountType);
219 
220         shoppingCouponPersistence.update(coupon, false);
221 
222         return coupon;
223     }
224 
225     protected String getCode() throws SystemException {
226         String code =
227             PwdGenerator.getPassword(PwdGenerator.KEY1 + PwdGenerator.KEY2, 8);
228 
229         try {
230             shoppingCouponPersistence.findByCode(code);
231 
232             return getCode();
233         }
234         catch (NoSuchCouponException nsce) {
235             return code;
236         }
237     }
238 
239     protected void validate(
240             long companyId, long groupId, String code, boolean autoCode,
241             String name, String description, String limitCategories,
242             String limitSkus, double minOrder, double discount)
243         throws PortalException, SystemException {
244 
245         if (!autoCode) {
246             if ((Validator.isNull(code)) ||
247                 (Validator.isNumber(code)) ||
248                 (code.indexOf(StringPool.SPACE) != -1)) {
249 
250                 throw new CouponCodeException();
251             }
252 
253             if (shoppingCouponPersistence.fetchByCode(code) != null) {
254                 throw new DuplicateCouponCodeException();
255             }
256         }
257 
258         validate(
259             companyId, groupId, name, description, limitCategories, limitSkus,
260             minOrder, discount);
261     }
262 
263     protected void validate(
264             long companyId, long groupId, String name, String description,
265             String limitCategories, String limitSkus, double minOrder,
266             double discount)
267         throws PortalException, SystemException {
268 
269         if (Validator.isNull(name)) {
270             throw new CouponNameException();
271         }
272         else if (Validator.isNull(description)) {
273             throw new CouponDescriptionException();
274         }
275 
276         // Category IDs
277 
278         long[] categoryIds = StringUtil.split(limitCategories, 0L);
279 
280         List<Long> invalidCategoryIds = new ArrayList<Long>();
281 
282         for (long categoryId : categoryIds) {
283             ShoppingCategory category =
284                 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
285 
286             if ((category == null) || (category.getGroupId() != groupId)) {
287                 invalidCategoryIds.add(categoryId);
288             }
289         }
290 
291         if (invalidCategoryIds.size() > 0) {
292             CouponLimitCategoriesException clce =
293                 new CouponLimitCategoriesException();
294 
295             clce.setCategoryIds(invalidCategoryIds);
296 
297             throw clce;
298         }
299 
300         // SKUs
301 
302         String[] skus = StringUtil.split(limitSkus);
303 
304         List<String> invalidSkus = new ArrayList<String>();
305 
306         for (String sku : skus) {
307             ShoppingItem item = shoppingItemPersistence.fetchByC_S(
308                 companyId, sku);
309 
310             if (item != null) {
311                 ShoppingCategory category = item.getCategory();
312 
313                 if (category.getGroupId() != groupId) {
314                     invalidSkus.add(sku);
315                 }
316             }
317             else {
318                 invalidSkus.add(sku);
319             }
320         }
321 
322         if (invalidSkus.size() > 0) {
323             CouponLimitSKUsException clskue = new CouponLimitSKUsException();
324 
325             clskue.setSkus(invalidSkus);
326 
327             throw clskue;
328         }
329 
330         if (minOrder < 0) {
331             throw new CouponMinimumOrderException();
332         }
333 
334         if (discount < 0) {
335             throw new CouponDiscountException();
336         }
337     }
338 
339 }