001    /**
002     * Copyright (c) 2000-present 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.util.CharPool;
019    import com.liferay.portal.kernel.util.PwdGenerator;
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    
041    import java.util.ArrayList;
042    import java.util.Date;
043    import java.util.List;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Huang Jie
048     */
049    public class ShoppingCouponLocalServiceImpl
050            extends ShoppingCouponLocalServiceBaseImpl {
051    
052            @Override
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 {
062    
063                    User user = userPersistence.findByPrimaryKey(userId);
064                    long groupId = serviceContext.getScopeGroupId();
065    
066                    code = StringUtil.toUpperCase(code.trim());
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            @Override
124            public void deleteCoupon(long couponId) throws PortalException {
125                    ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
126                            couponId);
127    
128                    deleteCoupon(coupon);
129            }
130    
131            @Override
132            public void deleteCoupon(ShoppingCoupon coupon) {
133                    shoppingCouponPersistence.remove(coupon);
134            }
135    
136            @Override
137            public void deleteCoupons(long groupId) {
138                    List<ShoppingCoupon> coupons = shoppingCouponPersistence.findByGroupId(
139                            groupId);
140    
141                    for (ShoppingCoupon coupon : coupons) {
142                            deleteCoupon(coupon);
143                    }
144            }
145    
146            @Override
147            public ShoppingCoupon getCoupon(long couponId) throws PortalException {
148                    return shoppingCouponPersistence.findByPrimaryKey(couponId);
149            }
150    
151            @Override
152            public ShoppingCoupon getCoupon(String code) throws PortalException {
153                    code = StringUtil.toUpperCase(code.trim());
154    
155                    return shoppingCouponPersistence.findByCode(code);
156            }
157    
158            @Override
159            public List<ShoppingCoupon> search(
160                    long groupId, long companyId, String code, boolean active,
161                    String discountType, boolean andOperator, int start, int end) {
162    
163                    return shoppingCouponFinder.findByG_C_C_A_DT(
164                            groupId, companyId, code, active, discountType, andOperator, start,
165                            end);
166            }
167    
168            @Override
169            public int searchCount(
170                    long groupId, long companyId, String code, boolean active,
171                    String discountType, boolean andOperator) {
172    
173                    return shoppingCouponFinder.countByG_C_C_A_DT(
174                            groupId, companyId, code, active, discountType, andOperator);
175            }
176    
177            @Override
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 {
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() {
233                    String code = PwdGenerator.getPassword(
234                            8, PwdGenerator.KEY1, PwdGenerator.KEY2);
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 {
250    
251                    if (!autoCode) {
252                            if (Validator.isNull(code) || Validator.isNumber(code) ||
253                                    (code.indexOf(CharPool.COMMA) != -1) ||
254                                    (code.indexOf(CharPool.SPACE) != -1)) {
255    
256                                    throw new CouponCodeException();
257                            }
258    
259                            if (shoppingCouponPersistence.fetchByCode(code) != null) {
260                                    throw new DuplicateCouponCodeException("{code=" + code + "}");
261                            }
262                    }
263    
264                    validate(
265                            companyId, groupId, name, description, limitCategories, limitSkus,
266                            minOrder, discount);
267            }
268    
269            protected void validate(
270                            long companyId, long groupId, String name, String description,
271                            String limitCategories, String limitSkus, double minOrder,
272                            double discount)
273                    throws PortalException {
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                    List<Long> categoryIds = new ArrayList<Long>();
285    
286                    String[] categoryNames = StringUtil.split(limitCategories);
287    
288                    for (String categoryName : categoryNames) {
289                            ShoppingCategory category = shoppingCategoryPersistence.fetchByG_N(
290                                    groupId, categoryName);
291    
292                            categoryIds.add(category.getCategoryId());
293                    }
294    
295                    List<Long> invalidCategoryIds = new ArrayList<Long>();
296    
297                    for (long categoryId : categoryIds) {
298                            ShoppingCategory category =
299                                    shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
300    
301                            if ((category == null) || (category.getGroupId() != groupId)) {
302                                    invalidCategoryIds.add(categoryId);
303                            }
304                    }
305    
306                    if (!invalidCategoryIds.isEmpty()) {
307                            CouponLimitCategoriesException clce =
308                                    new CouponLimitCategoriesException();
309    
310                            clce.setCategoryIds(invalidCategoryIds);
311    
312                            throw clce;
313                    }
314    
315                    // SKUs
316    
317                    String[] skus = StringUtil.split(limitSkus);
318    
319                    List<String> invalidSkus = new ArrayList<String>();
320    
321                    for (String sku : skus) {
322                            ShoppingItem item = shoppingItemPersistence.fetchByC_S(
323                                    companyId, sku);
324    
325                            if (item != null) {
326                                    ShoppingCategory category = item.getCategory();
327    
328                                    if (category.getGroupId() != groupId) {
329                                            invalidSkus.add(sku);
330                                    }
331                            }
332                            else {
333                                    invalidSkus.add(sku);
334                            }
335                    }
336    
337                    if (!invalidSkus.isEmpty()) {
338                            CouponLimitSKUsException clskue = new CouponLimitSKUsException();
339    
340                            clskue.setSkus(invalidSkus);
341    
342                            throw clskue;
343                    }
344    
345                    if (minOrder < 0) {
346                            throw new CouponMinimumOrderException();
347                    }
348    
349                    if (discount < 0) {
350                            throw new CouponDiscountException();
351                    }
352            }
353    
354    }