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