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 getItems(long groupId, String itemIds)
79          throws SystemException {
80  
81          Map items = new TreeMap();
82  
83          String[] itemIdsArray = StringUtil.split(itemIds);
84  
85          for (int i = 0; i < itemIdsArray.length; i++) {
86              try {
87                  long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
88                  String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
89  
90                  ShoppingItem item = shoppingItemPersistence.findByPrimaryKey(
91                      itemId);
92  
93                  ShoppingCategory category = item.getCategory();
94  
95                  if (category.getGroupId() == groupId) {
96                      ShoppingCartItem cartItem =
97                          new ShoppingCartItemImpl(item, fields);
98  
99                      Integer count = (Integer)items.get(cartItem);
100 
101                     if (count == null) {
102                         count = new Integer(1);
103                     }
104                     else {
105                         count = new Integer(count.intValue() + 1);
106                     }
107 
108                     items.put(cartItem, count);
109                 }
110             }
111             catch (NoSuchItemException nsie) {
112             }
113         }
114 
115         return items;
116     }
117 
118     public ShoppingCart updateCart(
119             long userId, long groupId, String itemIds, String couponCodes,
120             int altShipping, boolean insure)
121         throws PortalException, SystemException {
122 
123         List badItemIds = new ArrayList();
124 
125         Map items = getItems(groupId, itemIds);
126 
127         boolean minQtyMultiple = GetterUtil.getBoolean(PropsUtil.get(
128             PropsUtil.SHOPPING_CART_MIN_QTY_MULTIPLE));
129 
130         Iterator itr = items.entrySet().iterator();
131 
132         while (itr.hasNext()) {
133             Map.Entry entry = (Map.Entry)itr.next();
134 
135             ShoppingCartItem cartItem = (ShoppingCartItem)entry.getKey();
136             Integer count = (Integer)entry.getValue();
137 
138             ShoppingItem item = cartItem.getItem();
139 
140             int minQuantity = ShoppingUtil.getMinQuantity(item);
141 
142             if (minQuantity <= 0) {
143                 continue;
144             }
145 
146             if (minQtyMultiple) {
147                 if ((count.intValue() % minQuantity) > 0) {
148                     badItemIds.add(new Long(item.getItemId()));
149                 }
150             }
151             else {
152                 if (count.intValue() < minQuantity) {
153                     badItemIds.add(new Long(item.getItemId()));
154                 }
155             }
156         }
157 
158         if (badItemIds.size() > 0) {
159             throw new CartMinQuantityException(
160                 StringUtil.merge((Long[])badItemIds.toArray(new Long[0])));
161         }
162 
163         String[] couponCodesArray = StringUtil.split(couponCodes);
164 
165         for (int i = 0; i < couponCodesArray.length; i++) {
166             try {
167                 ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
168                     couponCodesArray[i]);
169 
170                 if (coupon.getGroupId() != groupId) {
171                     throw new NoSuchCouponException(couponCodesArray[i]);
172                 }
173                 else if (!coupon.isActive()) {
174                     throw new CouponActiveException(couponCodesArray[i]);
175                 }
176                 else if (!coupon.hasValidStartDate()) {
177                     throw new CouponStartDateException(couponCodesArray[i]);
178                 }
179                 else if (!coupon.hasValidEndDate()) {
180                     throw new CouponEndDateException(couponCodesArray[i]);
181                 }
182             }
183             catch (NoSuchCouponException nsce) {
184                 throw new NoSuchCouponException(couponCodesArray[i]);
185             }
186 
187             // Temporarily disable stacking of coupon codes
188 
189             break;
190         }
191 
192         User user = userPersistence.findByPrimaryKey(userId);
193         Date now = new Date();
194 
195         ShoppingCart cart = null;
196 
197         if (user.isDefaultUser()) {
198             cart = shoppingCartPersistence.create(0);
199 
200             cart.setGroupId(groupId);
201             cart.setCompanyId(user.getCompanyId());
202             cart.setUserId(userId);
203             cart.setUserName(user.getFullName());
204             cart.setCreateDate(now);
205         }
206         else {
207             try {
208                 cart = shoppingCartPersistence.findByG_U(groupId, userId);
209             }
210             catch (NoSuchCartException nsce) {
211                 long cartId = counterLocalService.increment();
212 
213                 cart = shoppingCartPersistence.create(cartId);
214 
215                 cart.setGroupId(groupId);
216                 cart.setCompanyId(user.getCompanyId());
217                 cart.setUserId(userId);
218                 cart.setUserName(user.getFullName());
219                 cart.setCreateDate(now);
220             }
221         }
222 
223         cart.setModifiedDate(now);
224         cart.setItemIds(checkItemIds(groupId, itemIds));
225         cart.setCouponCodes(couponCodes);
226         cart.setAltShipping(altShipping);
227         cart.setInsure(insure);
228 
229         if (!user.isDefaultUser()) {
230             shoppingCartPersistence.update(cart);
231         }
232 
233         return cart;
234     }
235 
236     protected String checkItemIds(long groupId, String itemIds) {
237         String[] itemIdsArray = StringUtil.split(itemIds);
238 
239         for (int i = 0; i < itemIdsArray.length; i++) {
240             long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
241 
242             ShoppingItem item = null;
243 
244             try {
245                 item = shoppingItemPersistence.findByPrimaryKey(itemId);
246 
247                 ShoppingCategory category = item.getCategory();
248 
249                 if (category.getGroupId() != groupId) {
250                     item = null;
251                 }
252             }
253             catch (Exception e) {
254             }
255 
256             if (item == null) {
257                 itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
258             }
259         }
260 
261         return itemIds;
262     }
263 
264 }