001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            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 deleteShoppingCart(long cartId)
062                    throws PortalException, SystemException {
063    
064                    ShoppingCart cart = shoppingCartPersistence.findByPrimaryKey(
065                            cartId);
066    
067                    deleteShoppingCart(cart);
068            }
069    
070            @Override
071            public void deleteShoppingCart(ShoppingCart cart) throws SystemException {
072                    shoppingCartPersistence.remove(cart);
073            }
074    
075            public void deleteUserCarts(long userId) throws SystemException {
076                    List<ShoppingCart> shoppingCarts = shoppingCartPersistence.findByUserId(
077                            userId);
078    
079                    for (ShoppingCart shoppingCart : shoppingCarts) {
080                            deleteShoppingCart(shoppingCart);
081                    }
082            }
083    
084            public ShoppingCart getCart(long userId, long groupId)
085                    throws PortalException, SystemException {
086    
087                    return shoppingCartPersistence.findByG_U(groupId, userId);
088            }
089    
090            public Map<ShoppingCartItem, Integer> getItems(long groupId, String itemIds)
091                    throws SystemException {
092    
093                    Map<ShoppingCartItem, Integer> items =
094                            new TreeMap<ShoppingCartItem, Integer>();
095    
096                    String[] itemIdsArray = StringUtil.split(itemIds);
097    
098                    for (int i = 0; i < itemIdsArray.length; i++) {
099                            long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
100                            String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
101    
102                            ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
103                                    itemId);
104    
105                            if (item != null) {
106                                    ShoppingCategory category = item.getCategory();
107    
108                                    if (category.getGroupId() == groupId) {
109                                            ShoppingCartItem cartItem = new ShoppingCartItemImpl(
110                                                    item, fields);
111    
112                                            Integer count = items.get(cartItem);
113    
114                                            if (count == null) {
115                                                    count = new Integer(1);
116                                            }
117                                            else {
118                                                    count = new Integer(count.intValue() + 1);
119                                            }
120    
121                                            items.put(cartItem, count);
122                                    }
123                            }
124                    }
125    
126                    return items;
127            }
128    
129            public ShoppingCart updateCart(
130                            long userId, long groupId, String itemIds, String couponCodes,
131                            int altShipping, boolean insure)
132                    throws PortalException, SystemException {
133    
134                    List<Long> badItemIds = new ArrayList<Long>();
135    
136                    Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
137    
138                    boolean minQtyMultiple = GetterUtil.getBoolean(PropsUtil.get(
139                            PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
140    
141                    Iterator<Map.Entry<ShoppingCartItem, Integer>> itr =
142                            items.entrySet().iterator();
143    
144                    while (itr.hasNext()) {
145                            Map.Entry<ShoppingCartItem, Integer> entry = itr.next();
146    
147                            ShoppingCartItem cartItem = entry.getKey();
148                            Integer count = entry.getValue();
149    
150                            ShoppingItem item = cartItem.getItem();
151    
152                            int minQuantity = ShoppingUtil.getMinQuantity(item);
153    
154                            if (minQuantity <= 0) {
155                                    continue;
156                            }
157    
158                            if (minQtyMultiple) {
159                                    if ((count.intValue() % minQuantity) > 0) {
160                                            badItemIds.add(item.getItemId());
161                                    }
162                            }
163                            else {
164                                    if (count.intValue() < minQuantity) {
165                                            badItemIds.add(item.getItemId());
166                                    }
167                            }
168                    }
169    
170                    if (badItemIds.size() > 0) {
171                            throw new CartMinQuantityException(StringUtil.merge(
172                                    badItemIds.toArray(new Long[badItemIds.size()])));
173                    }
174    
175                    String[] couponCodesArray = StringUtil.split(couponCodes);
176    
177                    for (int i = 0; i < couponCodesArray.length; i++) {
178                            try {
179                                    ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
180                                            couponCodesArray[i]);
181    
182                                    if (coupon.getGroupId() != groupId) {
183                                            throw new NoSuchCouponException(couponCodesArray[i]);
184                                    }
185                                    else if (!coupon.isActive()) {
186                                            throw new CouponActiveException(couponCodesArray[i]);
187                                    }
188                                    else if (!coupon.hasValidStartDate()) {
189                                            throw new CouponStartDateException(couponCodesArray[i]);
190                                    }
191                                    else if (!coupon.hasValidEndDate()) {
192                                            throw new CouponEndDateException(couponCodesArray[i]);
193                                    }
194                            }
195                            catch (NoSuchCouponException nsce) {
196                                    throw new NoSuchCouponException(couponCodesArray[i]);
197                            }
198    
199                            // Temporarily disable stacking of coupon codes
200    
201                            break;
202                    }
203    
204                    User user = userPersistence.findByPrimaryKey(userId);
205                    Date now = new Date();
206    
207                    ShoppingCart cart = null;
208    
209                    if (user.isDefaultUser()) {
210                            cart = shoppingCartPersistence.create(0);
211    
212                            cart.setGroupId(groupId);
213                            cart.setCompanyId(user.getCompanyId());
214                            cart.setUserId(userId);
215                            cart.setUserName(user.getFullName());
216                            cart.setCreateDate(now);
217                    }
218                    else {
219                            cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
220    
221                            if (cart == null) {
222                                    long cartId = counterLocalService.increment();
223    
224                                    cart = shoppingCartPersistence.create(cartId);
225    
226                                    cart.setGroupId(groupId);
227                                    cart.setCompanyId(user.getCompanyId());
228                                    cart.setUserId(userId);
229                                    cart.setUserName(user.getFullName());
230                                    cart.setCreateDate(now);
231                            }
232                    }
233    
234                    cart.setModifiedDate(now);
235                    cart.setItemIds(checkItemIds(groupId, itemIds));
236                    cart.setCouponCodes(couponCodes);
237                    cart.setAltShipping(altShipping);
238                    cart.setInsure(insure);
239    
240                    if (!user.isDefaultUser()) {
241                            shoppingCartPersistence.update(cart, false);
242                    }
243    
244                    return cart;
245            }
246    
247            protected String checkItemIds(long groupId, String itemIds) {
248                    String[] itemIdsArray = StringUtil.split(itemIds);
249    
250                    for (int i = 0; i < itemIdsArray.length; i++) {
251                            long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
252    
253                            ShoppingItem item = null;
254    
255                            try {
256                                    item = shoppingItemPersistence.findByPrimaryKey(itemId);
257    
258                                    ShoppingCategory category = item.getCategory();
259    
260                                    if (category.getGroupId() != groupId) {
261                                            item = null;
262                                    }
263                            }
264                            catch (Exception e) {
265                            }
266    
267                            if (item == null) {
268                                    itemIds = StringUtil.remove(itemIds, itemIdsArray[i]);
269                            }
270                    }
271    
272                    return itemIds;
273            }
274    
275    }