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.model.ShoppingCategory;
037    import com.liferay.portlet.shopping.model.ShoppingCoupon;
038    import com.liferay.portlet.shopping.model.ShoppingItem;
039    import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
040    import com.liferay.util.PwdGenerator;
041    
042    import java.util.ArrayList;
043    import java.util.Date;
044    import java.util.List;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Huang Jie
049     */
050    public class ShoppingCouponLocalServiceImpl
051            extends ShoppingCouponLocalServiceBaseImpl {
052    
053            public ShoppingCoupon addCoupon(
054                            long userId, String code, boolean autoCode, String name,
055                            String description, int startDateMonth, int startDateDay,
056                            int startDateYear, int startDateHour, int startDateMinute,
057                            int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
058                            int endDateMinute, boolean neverExpire, boolean active,
059                            String limitCategories, String limitSkus, double minOrder,
060                            double discount, String discountType, ServiceContext serviceContext)
061                    throws PortalException, SystemException {
062    
063                    User user = userPersistence.findByPrimaryKey(userId);
064                    long groupId = serviceContext.getScopeGroupId();
065    
066                    code = code.trim().toUpperCase();
067    
068                    if (autoCode) {
069                            code = getCode();
070                    }
071    
072                    Date startDate = PortalUtil.getDate(
073                            startDateMonth, startDateDay, startDateYear, startDateHour,
074                            startDateMinute, user.getTimeZone(),
075                            CouponStartDateException.class);
076    
077                    Date endDate = null;
078    
079                    if (!neverExpire) {
080                            endDate = PortalUtil.getDate(
081                                    endDateMonth, endDateDay, endDateYear, endDateHour,
082                                    endDateMinute, user.getTimeZone(),
083                                    CouponEndDateException.class);
084                    }
085    
086                    if ((endDate != null) && startDate.after(endDate)) {
087                            throw new CouponDateException();
088                    }
089    
090                    Date now = new Date();
091    
092                    validate(
093                            user.getCompanyId(), groupId, code, autoCode, name, description,
094                            limitCategories, limitSkus, minOrder, discount);
095    
096                    long couponId = counterLocalService.increment();
097    
098                    ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
099    
100                    coupon.setGroupId(groupId);
101                    coupon.setCompanyId(user.getCompanyId());
102                    coupon.setUserId(user.getUserId());
103                    coupon.setUserName(user.getFullName());
104                    coupon.setCreateDate(now);
105                    coupon.setModifiedDate(now);
106                    coupon.setCode(code);
107                    coupon.setName(name);
108                    coupon.setDescription(description);
109                    coupon.setStartDate(startDate);
110                    coupon.setEndDate(endDate);
111                    coupon.setActive(active);
112                    coupon.setLimitCategories(limitCategories);
113                    coupon.setLimitSkus(limitSkus);
114                    coupon.setMinOrder(minOrder);
115                    coupon.setDiscount(discount);
116                    coupon.setDiscountType(discountType);
117    
118                    shoppingCouponPersistence.update(coupon);
119    
120                    return coupon;
121            }
122    
123            public void deleteCoupon(long couponId)
124                    throws PortalException, SystemException {
125    
126                    ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
127                            couponId);
128    
129                    deleteCoupon(coupon);
130            }
131    
132            public void deleteCoupon(ShoppingCoupon coupon) throws SystemException {
133                    shoppingCouponPersistence.remove(coupon);
134            }
135    
136            public void deleteCoupons(long groupId) throws SystemException {
137                    List<ShoppingCoupon> coupons = shoppingCouponPersistence.findByGroupId(
138                            groupId);
139    
140                    for (ShoppingCoupon coupon : coupons) {
141                            deleteCoupon(coupon);
142                    }
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, start,
166                            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                            CouponStartDateException.class);
197    
198                    Date endDate = null;
199    
200                    if (!neverExpire) {
201                            endDate = PortalUtil.getDate(
202                                    endDateMonth, endDateDay, endDateYear, endDateHour,
203                                    endDateMinute, user.getTimeZone(),
204                                    CouponEndDateException.class);
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);
228    
229                    return coupon;
230            }
231    
232            protected String getCode() throws SystemException {
233                    String code = PwdGenerator.getPassword(
234                            PwdGenerator.KEY1 + PwdGenerator.KEY2, 8);
235    
236                    ShoppingCoupon coupon = shoppingCouponPersistence.fetchByCode(code);
237    
238                    if (coupon != null) {
239                            return coupon.getCode();
240                    }
241    
242                    return code;
243            }
244    
245            protected void validate(
246                            long companyId, long groupId, String code, boolean autoCode,
247                            String name, String description, String limitCategories,
248                            String limitSkus, double minOrder, double discount)
249                    throws PortalException, SystemException {
250    
251                    if (!autoCode) {
252                            if (Validator.isNull(code) || Validator.isNumber(code) ||
253                                    (code.indexOf(CharPool.SPACE) != -1)) {
254    
255                                    throw new CouponCodeException();
256                            }
257    
258                            if (shoppingCouponPersistence.fetchByCode(code) != null) {
259                                    throw new DuplicateCouponCodeException();
260                            }
261                    }
262    
263                    validate(
264                            companyId, groupId, name, description, limitCategories, limitSkus,
265                            minOrder, discount);
266            }
267    
268            protected void validate(
269                            long companyId, long groupId, String name, String description,
270                            String limitCategories, String limitSkus, double minOrder,
271                            double discount)
272                    throws PortalException, SystemException {
273    
274                    if (Validator.isNull(name)) {
275                            throw new CouponNameException();
276                    }
277                    else if (Validator.isNull(description)) {
278                            throw new CouponDescriptionException();
279                    }
280    
281                    // Category IDs
282    
283                    long[] categoryIds = StringUtil.split(limitCategories, 0L);
284    
285                    List<Long> invalidCategoryIds = new ArrayList<Long>();
286    
287                    for (long categoryId : categoryIds) {
288                            ShoppingCategory category =
289                                    shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
290    
291                            if ((category == null) || (category.getGroupId() != groupId)) {
292                                    invalidCategoryIds.add(categoryId);
293                            }
294                    }
295    
296                    if (invalidCategoryIds.size() > 0) {
297                            CouponLimitCategoriesException clce =
298                                    new CouponLimitCategoriesException();
299    
300                            clce.setCategoryIds(invalidCategoryIds);
301    
302                            throw clce;
303                    }
304    
305                    // SKUs
306    
307                    String[] skus = StringUtil.split(limitSkus);
308    
309                    List<String> invalidSkus = new ArrayList<String>();
310    
311                    for (String sku : skus) {
312                            ShoppingItem item = shoppingItemPersistence.fetchByC_S(
313                                    companyId, sku);
314    
315                            if (item != null) {
316                                    ShoppingCategory category = item.getCategory();
317    
318                                    if (category.getGroupId() != groupId) {
319                                            invalidSkus.add(sku);
320                                    }
321                            }
322                            else {
323                                    invalidSkus.add(sku);
324                            }
325                    }
326    
327                    if (invalidSkus.size() > 0) {
328                            CouponLimitSKUsException clskue = new CouponLimitSKUsException();
329    
330                            clskue.setSkus(invalidSkus);
331    
332                            throw clskue;
333                    }
334    
335                    if (minOrder < 0) {
336                            throw new CouponMinimumOrderException();
337                    }
338    
339                    if (discount < 0) {
340                            throw new CouponDiscountException();
341                    }
342            }
343    
344    }