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