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