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