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