1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.GetterUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.portlet.shopping.CartMinQuantityException;
32  import com.liferay.portlet.shopping.CouponActiveException;
33  import com.liferay.portlet.shopping.CouponEndDateException;
34  import com.liferay.portlet.shopping.CouponStartDateException;
35  import com.liferay.portlet.shopping.NoSuchCartException;
36  import com.liferay.portlet.shopping.NoSuchCouponException;
37  import com.liferay.portlet.shopping.NoSuchItemException;
38  import com.liferay.portlet.shopping.model.ShoppingCart;
39  import com.liferay.portlet.shopping.model.ShoppingCartItem;
40  import com.liferay.portlet.shopping.model.ShoppingCategory;
41  import com.liferay.portlet.shopping.model.ShoppingCoupon;
42  import com.liferay.portlet.shopping.model.ShoppingItem;
43  import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
44  import com.liferay.portlet.shopping.service.base.ShoppingCartLocalServiceBaseImpl;
45  import com.liferay.portlet.shopping.util.ShoppingUtil;
46  
47  import java.util.ArrayList;
48  import java.util.Date;
49  import java.util.Iterator;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.TreeMap;
53  
54  /**
55   * <a href="ShoppingCartLocalServiceImpl.java.html"><b><i>View Source</i></b>
56   * </a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class ShoppingCartLocalServiceImpl
62      extends ShoppingCartLocalServiceBaseImpl {
63  
64      public void deleteGroupCarts(long groupId) throws SystemException {
65          shoppingCartPersistence.removeByGroupId(groupId);
66      }
67  
68      public void deleteUserCarts(long userId) throws SystemException {
69          shoppingCartPersistence.removeByUserId(userId);
70      }
71  
72      public ShoppingCart getCart(long userId, long groupId)
73          throws PortalException, SystemException {
74  
75          return shoppingCartPersistence.findByG_U(groupId, userId);
76      }
77  
78      public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
79          throws SystemException {
80  
81          Map<ShoppingCartItem, Integer> items =
82              new TreeMap<ShoppingCartItem, Integer>();
83  
84          String[] itemIdsArray = StringUtil.split(itemIds);
85  
86          for (int i = 0; i < itemIdsArray.length; i++) {
87              try {
88                  long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
89                  String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
90  
91                  ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(
92                      itemId);
93  
94                  ShoppingCategory category = item.getCategory();
95  
96                  if (category.getGroupId() == groupId) {
97                      ShoppingCartItem cartItem = new ShoppingCartItemImpl(
98                          item, fields);
99  
100                     Integer count = items.get(cartItem);
101 
102                     if (count == null) {
103                         count = new Integer(1);
104                     }
105                     else {
106                         count = new Integer(count.intValue() + 1);
107                     }
108 
109                     items.put(cartItem, count);
110                 }
111             }
112             catch (NoSuchItemException nsie) {
113             }
114         }
115 
116         return items;
117     }
118 
119     public ShoppingCart updateCart(
120             long userId, long groupId, String itemIds, String couponCodes,
121             int altShipping, boolean insure)
122         throws PortalException, SystemException {
123 
124         List<Long> badItemIds = new ArrayList<Long>();
125 
126         Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
127 
128         boolean minQtyMultiple = GetterUtil.getBoolean(PropsUtil.get(
129             PropsUtil.SHOPPING_CART_MIN_QTY_MULTIPLE));
130 
131         Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
132             items.entrySet().iterator();
133 
134         while (itr.hasNext()) {
135             Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
136 
137             ShoppingCartItem cartItem = entry.getKey();
138             Integer count = entry.getValue();
139 
140             ShoppingItem item = cartItem.getItem();
141 
142             int minQuantity = ShoppingUtil.getMinQuantity(item);
143 
144             if (minQuantity <= 0) {
145                 continue;
146             }
147 
148             if (minQtyMultiple) {
149                 if ((count.intValue() % minQuantity) > 0) {
150                     badItemIds.add(item.getItemId());
151                 }
152             }
153             else {
154                 if (count.intValue() < minQuantity) {
155                     badItemIds.add(item.getItemId());
156                 }
157             }
158         }
159 
160         if (badItemIds.size() > 0) {
161             throw new CartMinQuantityException(StringUtil.merge(
162                 badItemIds.toArray(new Long[badItemIds.size()])));
163         }
164 
165         String[] couponCodesArray = StringUtil.split(couponCodes);
166 
167         for (int i = 0; i < couponCodesArray.length; i++) {
168             try {
169                 ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
170                     couponCodesArray[i]);
171 
172                 if (coupon.getGroupId() != groupId) {
173                     throw new NoSuchCouponException(couponCodesArray[i]);
174                 }
175                 else if (!coupon.isActive()) {
176                     throw new CouponActiveException(couponCodesArray[i]);
177                 }
178                 else if (!coupon.hasValidStartDate()) {
179                     throw new CouponStartDateException(couponCodesArray[i]);
180                 }
181                 else if (!coupon.hasValidEndDate()) {
182                     throw new CouponEndDateException(couponCodesArray[i]);
183                 }
184             }
185             catch (NoSuchCouponException nsce) {
186                 throw new NoSuchCouponException(couponCodesArray[i]);
187             }
188 
189             // Temporarily disable stacking of coupon codes
190 
191             break;
192         }
193 
194         User user = userPersistence.findByPrimaryKey(userId);
195         Date now = new Date();
196 
197         ShoppingCart cart = null;
198 
199         if (user.isDefaultUser()) {
200             cart = shoppingCartPersistence.create(0);
201 
202             cart.setGroupId(groupId);
203             cart.setCompanyId(user.getCompanyId());
204             cart.setUserId(userId);
205             cart.setUserName(user.getFullName());
206             cart.setCreateDate(now);
207         }
208         else {
209             try {
210                 cart = shoppingCartPersistence.findByG_U(groupId, userId);
211             }
212             catch (NoSuchCartException nsce) {
213                 long cartId = counterLocalService.increment();
214 
215                 cart = shoppingCartPersistence.create(cartId);
216 
217                 cart.setGroupId(groupId);
218                 cart.setCompanyId(user.getCompanyId());
219                 cart.setUserId(userId);
220                 cart.setUserName(user.getFullName());
221                 cart.setCreateDate(now);
222             }
223         }
224 
225         cart.setModifiedDate(now);
226         cart.setItemIds(checkItemIds(groupId, itemIds));
227         cart.setCouponCodes(couponCodes);
228         cart.setAltShipping(altShipping);
229         cart.setInsure(insure);
230 
231         if (!user.isDefaultUser()) {
232             shoppingCartPersistence.update(cart, false);
233         }
234 
235         return cart;
236     }
237 
238     protected String checkItemIds(long groupId, String itemIds) {
239         String[] itemIdsArray = StringUtil.split(itemIds);
240 
241         for (int i = 0; i < itemIdsArray.length; i++) {
242             long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
243 
244             ShoppingItem item = null;
245 
246             try {
247                 item = shoppingItemPersistence.findByPrimaryKey(itemId);
248 
249                 ShoppingCategory category = item.getCategory();
250 
251                 if (category.getGroupId() != groupId) {
252                     item = null;
253                 }
254             }
255             catch (Exception e) {
256             }
257 
258             if (item == null) {
259                 itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
260             }
261         }
262 
263         return itemIds;
264     }
265 
266 }