001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.shopping.CouponCodeException;
026    import com.liferay.portlet.shopping.CouponDateException;
027    import com.liferay.portlet.shopping.CouponDescriptionException;
028    import com.liferay.portlet.shopping.CouponDiscountException;
029    import com.liferay.portlet.shopping.CouponEndDateException;
030    import com.liferay.portlet.shopping.CouponLimitCategoriesException;
031    import com.liferay.portlet.shopping.CouponLimitSKUsException;
032    import com.liferay.portlet.shopping.CouponMinimumOrderException;
033    import com.liferay.portlet.shopping.CouponNameException;
034    import com.liferay.portlet.shopping.CouponStartDateException;
035    import com.liferay.portlet.shopping.DuplicateCouponCodeException;
036    import com.liferay.portlet.shopping.NoSuchCouponException;
037    import com.liferay.portlet.shopping.model.ShoppingCategory;
038    import com.liferay.portlet.shopping.model.ShoppingCoupon;
039    import com.liferay.portlet.shopping.model.ShoppingItem;
040    import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
041    import com.liferay.util.PwdGenerator;
042    
043    import java.util.ArrayList;
044    import java.util.Date;
045    import java.util.List;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Huang Jie
050     */
051    public class ShoppingCouponLocalServiceImpl
052            extends ShoppingCouponLocalServiceBaseImpl {
053    
054            @Override
055            public ShoppingCoupon addCoupon(
056                            long userId, String code, boolean autoCode, String name,
057                            String description, int startDateMonth, int startDateDay,
058                            int startDateYear, int startDateHour, int startDateMinute,
059                            int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
060                            int endDateMinute, boolean neverExpire, boolean active,
061                            String limitCategories, String limitSkus, double minOrder,
062                            double discount, String discountType, ServiceContext serviceContext)
063                    throws PortalException, SystemException {
064    
065                    User user = userPersistence.findByPrimaryKey(userId);
066                    long groupId = serviceContext.getScopeGroupId();
067    
068                    code = code.trim().toUpperCase();
069    
070                    if (autoCode) {
071                            code = getCode();
072                    }
073    
074                    Date startDate = PortalUtil.getDate(
075                            startDateMonth, startDateDay, startDateYear, startDateHour,
076                            startDateMinute, user.getTimeZone(),
077                            CouponStartDateException.class);
078    
079                    Date endDate = null;
080    
081                    if (!neverExpire) {
082                            endDate = PortalUtil.getDate(
083                                    endDateMonth, endDateDay, endDateYear, endDateHour,
084                                    endDateMinute, user.getTimeZone(),
085                                    CouponEndDateException.class);
086                    }
087    
088                    if ((endDate != null) && startDate.after(endDate)) {
089                            throw new CouponDateException();
090                    }
091    
092                    Date now = new Date();
093    
094                    validate(
095                            user.getCompanyId(), groupId, code, autoCode, name, description,
096                            limitCategories, limitSkus, minOrder, discount);
097    
098                    long couponId = counterLocalService.increment();
099    
100                    ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
101    
102                    coupon.setGroupId(groupId);
103                    coupon.setCompanyId(user.getCompanyId());
104                    coupon.setUserId(user.getUserId());
105                    coupon.setUserName(user.getFullName());
106                    coupon.setCreateDate(now);
107                    coupon.setModifiedDate(now);
108                    coupon.setCode(code);
109                    coupon.setName(name);
110                    coupon.setDescription(description);
111                    coupon.setStartDate(startDate);
112                    coupon.setEndDate(endDate);
113                    coupon.setActive(active);
114                    coupon.setLimitCategories(limitCategories);
115                    coupon.setLimitSkus(limitSkus);
116                    coupon.setMinOrder(minOrder);
117                    coupon.setDiscount(discount);
118                    coupon.setDiscountType(discountType);
119    
120                    shoppingCouponPersistence.update(coupon, false);
121    
122                    return coupon;
123            }
124    
125            @Override
126            public void deleteCoupon(long couponId)
127                    throws PortalException, SystemException {
128    
129                    ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
130                            couponId);
131    
132                    deleteCoupon(coupon);
133            }
134    
135            @Override
136            public void deleteCoupon(ShoppingCoupon coupon) throws SystemException {
137                    shoppingCouponPersistence.remove(coupon);
138            }
139    
140            @Override
141            public void deleteCoupons(long groupId) throws SystemException {
142                    List<ShoppingCoupon> coupons = shoppingCouponPersistence.findByGroupId(
143                            groupId);
144    
145                    for (ShoppingCoupon coupon : coupons) {
146                            deleteCoupon(coupon);
147                    }
148            }
149    
150            @Override
151            public ShoppingCoupon getCoupon(long couponId)
152                    throws PortalException, SystemException {
153    
154                    return shoppingCouponPersistence.findByPrimaryKey(couponId);
155            }
156    
157            @Override
158            public ShoppingCoupon getCoupon(String code)
159                    throws PortalException, SystemException {
160    
161                    code = code.trim().toUpperCase();
162    
163                    return shoppingCouponPersistence.findByCode(code);
164            }
165    
166            @Override
167            public List<ShoppingCoupon> search(
168                            long groupId, long companyId, String code, boolean active,
169                            String discountType, boolean andOperator, int start, int end)
170                    throws SystemException {
171    
172                    return shoppingCouponFinder.findByG_C_C_A_DT(
173                            groupId, companyId, code, active, discountType, andOperator, start,
174                            end);
175            }
176    
177            @Override
178            public int searchCount(
179                            long groupId, long companyId, String code, boolean active,
180                            String discountType, boolean andOperator)
181                    throws SystemException {
182    
183                    return shoppingCouponFinder.countByG_C_C_A_DT(
184                            groupId, companyId, code, active, discountType, andOperator);
185            }
186    
187            @Override
188            public ShoppingCoupon updateCoupon(
189                            long userId, long couponId, String name, String description,
190                            int startDateMonth, int startDateDay, int startDateYear,
191                            int startDateHour, int startDateMinute, int endDateMonth,
192                            int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
193                            boolean neverExpire, boolean active, String limitCategories,
194                            String limitSkus, double minOrder, double discount,
195                            String discountType, ServiceContext serviceContext)
196                    throws PortalException, SystemException {
197    
198                    User user = userPersistence.findByPrimaryKey(userId);
199    
200                    ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
201                            couponId);
202    
203                    Date startDate = PortalUtil.getDate(
204                            startDateMonth, startDateDay, startDateYear, startDateHour,
205                            startDateMinute, user.getTimeZone(),
206                            CouponStartDateException.class);
207    
208                    Date endDate = null;
209    
210                    if (!neverExpire) {
211                            endDate = PortalUtil.getDate(
212                                    endDateMonth, endDateDay, endDateYear, endDateHour,
213                                    endDateMinute, user.getTimeZone(),
214                                    CouponEndDateException.class);
215                    }
216    
217                    if ((endDate != null) && startDate.after(endDate)) {
218                            throw new CouponDateException();
219                    }
220    
221                    validate(
222                            coupon.getCompanyId(), coupon.getGroupId(), name, description,
223                            limitCategories, limitSkus, minOrder, discount);
224    
225                    coupon.setModifiedDate(new Date());
226                    coupon.setName(name);
227                    coupon.setDescription(description);
228                    coupon.setStartDate(startDate);
229                    coupon.setEndDate(endDate);
230                    coupon.setActive(active);
231                    coupon.setLimitCategories(limitCategories);
232                    coupon.setLimitSkus(limitSkus);
233                    coupon.setMinOrder(minOrder);
234                    coupon.setDiscount(discount);
235                    coupon.setDiscountType(discountType);
236    
237                    shoppingCouponPersistence.update(coupon, false);
238    
239                    return coupon;
240            }
241    
242            protected String getCode() throws SystemException {
243                    String code = PwdGenerator.getPassword(
244                            PwdGenerator.KEY1 + PwdGenerator.KEY2, 8);
245    
246                    try {
247                            shoppingCouponPersistence.findByCode(code);
248    
249                            return getCode();
250                    }
251                    catch (NoSuchCouponException nsce) {
252                            return code;
253                    }
254            }
255    
256            protected void validate(
257                            long companyId, long groupId, String code, boolean autoCode,
258                            String name, String description, String limitCategories,
259                            String limitSkus, double minOrder, double discount)
260                    throws PortalException, SystemException {
261    
262                    if (!autoCode) {
263                            if (Validator.isNull(code) || Validator.isNumber(code) ||
264                                    (code.indexOf(CharPool.SPACE) != -1)) {
265    
266                                    throw new CouponCodeException();
267                            }
268    
269                            if (shoppingCouponPersistence.fetchByCode(code) != null) {
270                                    throw new DuplicateCouponCodeException();
271                            }
272                    }
273    
274                    validate(
275                            companyId, groupId, name, description, limitCategories, limitSkus,
276                            minOrder, discount);
277            }
278    
279            protected void validate(
280                            long companyId, long groupId, String name, String description,
281                            String limitCategories, String limitSkus, double minOrder,
282                            double discount)
283                    throws PortalException, SystemException {
284    
285                    if (Validator.isNull(name)) {
286                            throw new CouponNameException();
287                    }
288                    else if (Validator.isNull(description)) {
289                            throw new CouponDescriptionException();
290                    }
291    
292                    // Category IDs
293    
294                    long[] categoryIds = StringUtil.split(limitCategories, 0L);
295    
296                    List<Long> invalidCategoryIds = new ArrayList<Long>();
297    
298                    for (long categoryId : categoryIds) {
299                            ShoppingCategory category =
300                                    shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
301    
302                            if ((category == null) || (category.getGroupId() != groupId)) {
303                                    invalidCategoryIds.add(categoryId);
304                            }
305                    }
306    
307                    if (invalidCategoryIds.size() > 0) {
308                            CouponLimitCategoriesException clce =
309                                    new CouponLimitCategoriesException();
310    
311                            clce.setCategoryIds(invalidCategoryIds);
312    
313                            throw clce;
314                    }
315    
316                    // SKUs
317    
318                    String[] skus = StringUtil.split(limitSkus);
319    
320                    List<String> invalidSkus = new ArrayList<String>();
321    
322                    for (String sku : skus) {
323                            ShoppingItem item = shoppingItemPersistence.fetchByC_S(
324                                    companyId, sku);
325    
326                            if (item != null) {
327                                    ShoppingCategory category = item.getCategory();
328    
329                                    if (category.getGroupId() != groupId) {
330                                            invalidSkus.add(sku);
331                                    }
332                            }
333                            else {
334                                    invalidSkus.add(sku);
335                            }
336                    }
337    
338                    if (invalidSkus.size() > 0) {
339                            CouponLimitSKUsException clskue = new CouponLimitSKUsException();
340    
341                            clskue.setSkus(invalidSkus);
342    
343                            throw clskue;
344                    }
345    
346                    if (minOrder < 0) {
347                            throw new CouponMinimumOrderException();
348                    }
349    
350                    if (discount < 0) {
351                            throw new CouponDiscountException();
352                    }
353            }
354    
355    }