001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.util.PropsUtil;
024    import com.liferay.portlet.shopping.CartMinQuantityException;
025    import com.liferay.portlet.shopping.CouponActiveException;
026    import com.liferay.portlet.shopping.CouponEndDateException;
027    import com.liferay.portlet.shopping.CouponStartDateException;
028    import com.liferay.portlet.shopping.NoSuchCouponException;
029    import com.liferay.portlet.shopping.model.ShoppingCart;
030    import com.liferay.portlet.shopping.model.ShoppingCartItem;
031    import com.liferay.portlet.shopping.model.ShoppingCategory;
032    import com.liferay.portlet.shopping.model.ShoppingCoupon;
033    import com.liferay.portlet.shopping.model.ShoppingItem;
034    import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
035    import com.liferay.portlet.shopping.service.base.ShoppingCartLocalServiceBaseImpl;
036    import com.liferay.portlet.shopping.util.ShoppingUtil;
037    
038    import java.util.ArrayList;
039    import java.util.Date;
040    import java.util.Iterator;
041    import java.util.List;
042    import java.util.Map;
043    import java.util.TreeMap;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class ShoppingCartLocalServiceImpl
049            extends ShoppingCartLocalServiceBaseImpl {
050    
051            @Override
052            public void deleteGroupCarts(long groupId) throws SystemException {
053                    List<ShoppingCart> carts = shoppingCartPersistence.findByGroupId(
054                            groupId);
055    
056                    for (ShoppingCart cart : carts) {
057                            deleteShoppingCart(cart);
058                    }
059            }
060    
061            @Override
062            public void deleteUserCarts(long userId) throws SystemException {
063                    List<ShoppingCart> shoppingCarts = shoppingCartPersistence.findByUserId(
064                            userId);
065    
066                    for (ShoppingCart shoppingCart : shoppingCarts) {
067                            deleteShoppingCart(shoppingCart);
068                    }
069            }
070    
071            @Override
072            public ShoppingCart getCart(long userId, long groupId)
073                    throws PortalException, SystemException {
074    
075                    return shoppingCartPersistence.findByG_U(groupId, userId);
076            }
077    
078            @Override
079            public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
080                    throws SystemException {
081    
082                    Map<ShoppingCartItem, Integer> items =
083                            new TreeMap<ShoppingCartItem, Integer>();
084    
085                    String[] itemIdsArray = StringUtil.split(itemIds);
086    
087                    for (int i = 0; i < itemIdsArray.length; i++) {
088                            long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
089                            String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
090    
091                            ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
092                                    itemId);
093    
094                            if (item != null) {
095                                    ShoppingCategory category = item.getCategory();
096    
097                                    if (category.getGroupId() == groupId) {
098                                            ShoppingCartItem cartItem = new ShoppingCartItemImpl(
099                                                    item, fields);
100    
101                                            Integer count = items.get(cartItem);
102    
103                                            if (count == null) {
104                                                    count = new Integer(1);
105                                            }
106                                            else {
107                                                    count = new Integer(count.intValue() + 1);
108                                            }
109    
110                                            items.put(cartItem, count);
111                                    }
112                            }
113                    }
114    
115                    return items;
116            }
117    
118            @Override
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(
129                            PropsUtil.get(PropsKeys.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(
162                                    StringUtil.merge(
163                                            badItemIds.toArray(new Long[badItemIds.size()])));
164                    }
165    
166                    String[] couponCodesArray = StringUtil.split(couponCodes);
167    
168                    for (int i = 0; i < couponCodesArray.length; i++) {
169                            try {
170                                    ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
171                                            couponCodesArray[i]);
172    
173                                    if (coupon.getGroupId() != groupId) {
174                                            throw new NoSuchCouponException(couponCodesArray[i]);
175                                    }
176                                    else if (!coupon.isActive()) {
177                                            throw new CouponActiveException(couponCodesArray[i]);
178                                    }
179                                    else if (!coupon.hasValidStartDate()) {
180                                            throw new CouponStartDateException(couponCodesArray[i]);
181                                    }
182                                    else if (!coupon.hasValidEndDate()) {
183                                            throw new CouponEndDateException(couponCodesArray[i]);
184                                    }
185                            }
186                            catch (NoSuchCouponException nsce) {
187                                    throw new NoSuchCouponException(couponCodesArray[i]);
188                            }
189    
190                            // Temporarily disable stacking of coupon codes
191    
192                            break;
193                    }
194    
195                    User user = userPersistence.findByPrimaryKey(userId);
196                    Date now = new Date();
197    
198                    ShoppingCart cart = null;
199    
200                    if (user.isDefaultUser()) {
201                            cart = shoppingCartPersistence.create(0);
202    
203                            cart.setGroupId(groupId);
204                            cart.setCompanyId(user.getCompanyId());
205                            cart.setUserId(userId);
206                            cart.setUserName(user.getFullName());
207                            cart.setCreateDate(now);
208                    }
209                    else {
210                            cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
211    
212                            if (cart == null) {
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    }