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.List;
041    import java.util.Map;
042    import java.util.TreeMap;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class ShoppingCartLocalServiceImpl
048            extends ShoppingCartLocalServiceBaseImpl {
049    
050            @Override
051            public void deleteGroupCarts(long groupId) throws SystemException {
052                    List<ShoppingCart> carts = shoppingCartPersistence.findByGroupId(
053                            groupId);
054    
055                    for (ShoppingCart cart : carts) {
056                            deleteShoppingCart(cart);
057                    }
058            }
059    
060            @Override
061            public void deleteUserCarts(long userId) throws SystemException {
062                    List<ShoppingCart> shoppingCarts = shoppingCartPersistence.findByUserId(
063                            userId);
064    
065                    for (ShoppingCart shoppingCart : shoppingCarts) {
066                            deleteShoppingCart(shoppingCart);
067                    }
068            }
069    
070            @Override
071            public ShoppingCart getCart(long userId, long groupId)
072                    throws PortalException, SystemException {
073    
074                    return shoppingCartPersistence.findByG_U(groupId, userId);
075            }
076    
077            @Override
078            public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
079                    throws SystemException {
080    
081                    Map<ShoppingCartItem, Integer> items =
082                            new TreeMap<ShoppingCartItem, Integer>();
083    
084                    String[] itemIdsArray = StringUtil.split(itemIds);
085    
086                    for (int i = 0; i < itemIdsArray.length; i++) {
087                            long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
088                            String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
089    
090                            ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
091                                    itemId);
092    
093                            if (item != null) {
094                                    ShoppingCategory category = item.getCategory();
095    
096                                    if (category.getGroupId() == groupId) {
097                                            ShoppingCartItem cartItem = new ShoppingCartItemImpl(
098                                                    item, fields);
099    
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                    }
113    
114                    return items;
115            }
116    
117            @Override
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<Long> badItemIds = new ArrayList<Long>();
124    
125                    Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
126    
127                    boolean minQtyMultiple = GetterUtil.getBoolean(
128                            PropsUtil.get(PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
129    
130                    for (Map.Entry<ShoppingCartItem, Integer> entry : items.entrySet()) {
131                            ShoppingCartItem cartItem = entry.getKey();
132                            Integer count = entry.getValue();
133    
134                            ShoppingItem item = cartItem.getItem();
135    
136                            int minQuantity = ShoppingUtil.getMinQuantity(item);
137    
138                            if (minQuantity <= 0) {
139                                    continue;
140                            }
141    
142                            if (minQtyMultiple) {
143                                    if ((count.intValue() % minQuantity) > 0) {
144                                            badItemIds.add(item.getItemId());
145                                    }
146                            }
147                            else {
148                                    if (count.intValue() < minQuantity) {
149                                            badItemIds.add(item.getItemId());
150                                    }
151                            }
152                    }
153    
154                    if (badItemIds.size() > 0) {
155                            throw new CartMinQuantityException(
156                                    StringUtil.merge(
157                                            badItemIds.toArray(new Long[badItemIds.size()])));
158                    }
159    
160                    String[] couponCodesArray = StringUtil.split(couponCodes);
161    
162                    for (int i = 0; i < couponCodesArray.length; i++) {
163                            try {
164                                    ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
165                                            couponCodesArray[i]);
166    
167                                    if (coupon.getGroupId() != groupId) {
168                                            throw new NoSuchCouponException(couponCodesArray[i]);
169                                    }
170                                    else if (!coupon.isActive()) {
171                                            throw new CouponActiveException(couponCodesArray[i]);
172                                    }
173                                    else if (!coupon.hasValidStartDate()) {
174                                            throw new CouponStartDateException(couponCodesArray[i]);
175                                    }
176                                    else if (!coupon.hasValidEndDate()) {
177                                            throw new CouponEndDateException(couponCodesArray[i]);
178                                    }
179                            }
180                            catch (NoSuchCouponException nsce) {
181                                    throw new NoSuchCouponException(couponCodesArray[i]);
182                            }
183    
184                            // Temporarily disable stacking of coupon codes
185    
186                            break;
187                    }
188    
189                    User user = userPersistence.findByPrimaryKey(userId);
190                    Date now = new Date();
191    
192                    ShoppingCart cart = null;
193    
194                    if (user.isDefaultUser()) {
195                            cart = shoppingCartPersistence.create(0);
196    
197                            cart.setGroupId(groupId);
198                            cart.setCompanyId(user.getCompanyId());
199                            cart.setUserId(userId);
200                            cart.setUserName(user.getFullName());
201                            cart.setCreateDate(now);
202                    }
203                    else {
204                            cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
205    
206                            if (cart == null) {
207                                    long cartId = counterLocalService.increment();
208    
209                                    cart = shoppingCartPersistence.create(cartId);
210    
211                                    cart.setGroupId(groupId);
212                                    cart.setCompanyId(user.getCompanyId());
213                                    cart.setUserId(userId);
214                                    cart.setUserName(user.getFullName());
215                                    cart.setCreateDate(now);
216                            }
217                    }
218    
219                    cart.setModifiedDate(now);
220                    cart.setItemIds(checkItemIds(groupId, itemIds));
221                    cart.setCouponCodes(couponCodes);
222                    cart.setAltShipping(altShipping);
223                    cart.setInsure(insure);
224    
225                    if (!user.isDefaultUser()) {
226                            shoppingCartPersistence.update(cart);
227                    }
228    
229                    return cart;
230            }
231    
232            protected String checkItemIds(long groupId, String itemIds) {
233                    String[] itemIdsArray = StringUtil.split(itemIds);
234    
235                    for (int i = 0; i < itemIdsArray.length; i++) {
236                            long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
237    
238                            ShoppingItem item = null;
239    
240                            try {
241                                    item = shoppingItemPersistence.findByPrimaryKey(itemId);
242    
243                                    ShoppingCategory category = item.getCategory();
244    
245                                    if (category.getGroupId() != groupId) {
246                                            item = null;
247                                    }
248                            }
249                            catch (Exception e) {
250                            }
251    
252                            if (item == null) {
253                                    itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
254                            }
255                    }
256    
257                    return itemIds;
258            }
259    
260    }