1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.shopping.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.PropsKeys;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.util.PropsUtil;
24  import com.liferay.portlet.shopping.CartMinQuantityException;
25  import com.liferay.portlet.shopping.CouponActiveException;
26  import com.liferay.portlet.shopping.CouponEndDateException;
27  import com.liferay.portlet.shopping.CouponStartDateException;
28  import com.liferay.portlet.shopping.NoSuchCouponException;
29  import com.liferay.portlet.shopping.model.ShoppingCart;
30  import com.liferay.portlet.shopping.model.ShoppingCartItem;
31  import com.liferay.portlet.shopping.model.ShoppingCategory;
32  import com.liferay.portlet.shopping.model.ShoppingCoupon;
33  import com.liferay.portlet.shopping.model.ShoppingItem;
34  import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
35  import com.liferay.portlet.shopping.service.base.ShoppingCartLocalServiceBaseImpl;
36  import com.liferay.portlet.shopping.util.ShoppingUtil;
37  
38  import java.util.ArrayList;
39  import java.util.Date;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.TreeMap;
44  
45  /**
46   * <a href="ShoppingCartLocalServiceImpl.java.html"><b><i>View Source</i></b>
47   * </a>
48   *
49   * @author Brian Wing Shun Chan
50   */
51  public class ShoppingCartLocalServiceImpl
52      extends ShoppingCartLocalServiceBaseImpl {
53  
54      public void deleteGroupCarts(long groupId) throws SystemException {
55          shoppingCartPersistence.removeByGroupId(groupId);
56      }
57  
58      public void deleteUserCarts(long userId) throws SystemException {
59          shoppingCartPersistence.removeByUserId(userId);
60      }
61  
62      public ShoppingCart getCart(long userId, long groupId)
63          throws PortalException, SystemException {
64  
65          return shoppingCartPersistence.findByG_U(groupId, userId);
66      }
67  
68      public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
69          throws SystemException {
70  
71          Map<ShoppingCartItem, Integer> items =
72              new TreeMap<ShoppingCartItem, Integer>();
73  
74          String[] itemIdsArray = StringUtil.split(itemIds);
75  
76          for (int i = 0; i < itemIdsArray.length; i++) {
77              long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
78              String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
79  
80              ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
81                  itemId);
82  
83              if (item != null) {
84                  ShoppingCategory category = item.getCategory();
85  
86                  if (category.getGroupId() == groupId) {
87                      ShoppingCartItem cartItem = new ShoppingCartItemImpl(
88                          item, fields);
89  
90                      Integer count = items.get(cartItem);
91  
92                      if (count == null) {
93                          count = new Integer(1);
94                      }
95                      else {
96                          count = new Integer(count.intValue() + 1);
97                      }
98  
99                      items.put(cartItem, count);
100                 }
101             }
102         }
103 
104         return items;
105     }
106 
107     public ShoppingCart updateCart(
108             long userId, long groupId, String itemIds, String couponCodes,
109             int altShipping, boolean insure)
110         throws PortalException, SystemException {
111 
112         List<Long> badItemIds = new ArrayList<Long>();
113 
114         Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
115 
116         boolean minQtyMultiple = GetterUtil.getBoolean(PropsUtil.get(
117             PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
118 
119         Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
120             items.entrySet().iterator();
121 
122         while (itr.hasNext()) {
123             Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
124 
125             ShoppingCartItem cartItem = entry.getKey();
126             Integer count = entry.getValue();
127 
128             ShoppingItem item = cartItem.getItem();
129 
130             int minQuantity = ShoppingUtil.getMinQuantity(item);
131 
132             if (minQuantity <= 0) {
133                 continue;
134             }
135 
136             if (minQtyMultiple) {
137                 if ((count.intValue() % minQuantity) > 0) {
138                     badItemIds.add(item.getItemId());
139                 }
140             }
141             else {
142                 if (count.intValue() < minQuantity) {
143                     badItemIds.add(item.getItemId());
144                 }
145             }
146         }
147 
148         if (badItemIds.size() > 0) {
149             throw new CartMinQuantityException(StringUtil.merge(
150                 badItemIds.toArray(new Long[badItemIds.size()])));
151         }
152 
153         String[] couponCodesArray = StringUtil.split(couponCodes);
154 
155         for (int i = 0; i < couponCodesArray.length; i++) {
156             try {
157                 ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
158                     couponCodesArray[i]);
159 
160                 if (coupon.getGroupId() != groupId) {
161                     throw new NoSuchCouponException(couponCodesArray[i]);
162                 }
163                 else if (!coupon.isActive()) {
164                     throw new CouponActiveException(couponCodesArray[i]);
165                 }
166                 else if (!coupon.hasValidStartDate()) {
167                     throw new CouponStartDateException(couponCodesArray[i]);
168                 }
169                 else if (!coupon.hasValidEndDate()) {
170                     throw new CouponEndDateException(couponCodesArray[i]);
171                 }
172             }
173             catch (NoSuchCouponException nsce) {
174                 throw new NoSuchCouponException(couponCodesArray[i]);
175             }
176 
177             // Temporarily disable stacking of coupon codes
178 
179             break;
180         }
181 
182         User user = userPersistence.findByPrimaryKey(userId);
183         Date now = new Date();
184 
185         ShoppingCart cart = null;
186 
187         if (user.isDefaultUser()) {
188             cart = shoppingCartPersistence.create(0);
189 
190             cart.setGroupId(groupId);
191             cart.setCompanyId(user.getCompanyId());
192             cart.setUserId(userId);
193             cart.setUserName(user.getFullName());
194             cart.setCreateDate(now);
195         }
196         else {
197             cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
198 
199             if (cart == null) {
200                 long cartId = counterLocalService.increment();
201 
202                 cart = shoppingCartPersistence.create(cartId);
203 
204                 cart.setGroupId(groupId);
205                 cart.setCompanyId(user.getCompanyId());
206                 cart.setUserId(userId);
207                 cart.setUserName(user.getFullName());
208                 cart.setCreateDate(now);
209             }
210         }
211 
212         cart.setModifiedDate(now);
213         cart.setItemIds(checkItemIds(groupId, itemIds));
214         cart.setCouponCodes(couponCodes);
215         cart.setAltShipping(altShipping);
216         cart.setInsure(insure);
217 
218         if (!user.isDefaultUser()) {
219             shoppingCartPersistence.update(cart, false);
220         }
221 
222         return cart;
223     }
224 
225     protected String checkItemIds(long groupId, String itemIds) {
226         String[] itemIdsArray = StringUtil.split(itemIds);
227 
228         for (int i = 0; i < itemIdsArray.length; i++) {
229             long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
230 
231             ShoppingItem item = null;
232 
233             try {
234                 item = shoppingItemPersistence.findByPrimaryKey(itemId);
235 
236                 ShoppingCategory category = item.getCategory();
237 
238                 if (category.getGroupId() != groupId) {
239                     item = null;
240                 }
241             }
242             catch (Exception e) {
243             }
244 
245             if (item == null) {
246                 itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
247             }
248         }
249 
250         return itemIds;
251     }
252 
253 }