001    /**
002     * Copyright (c) 2000-2010 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.model.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.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portlet.shopping.NoSuchCouponException;
023    import com.liferay.portlet.shopping.model.ShoppingCart;
024    import com.liferay.portlet.shopping.model.ShoppingCartItem;
025    import com.liferay.portlet.shopping.model.ShoppingCoupon;
026    import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
027    import com.liferay.portlet.shopping.service.ShoppingCouponLocalServiceUtil;
028    
029    import java.util.Map;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ShoppingCartImpl
035            extends ShoppingCartModelImpl implements ShoppingCart {
036    
037            public ShoppingCartImpl() {
038            }
039    
040            public void addItemId(long itemId, String fields) {
041                    setItemIds(StringUtil.add(
042                            getItemIds(), itemId + fields, StringPool.COMMA, true));
043            }
044    
045            public ShoppingCoupon getCoupon() throws PortalException, SystemException {
046                    ShoppingCoupon coupon = null;
047    
048                    if (Validator.isNotNull(getCouponCodes())) {
049                            String code = StringUtil.split(getCouponCodes())[0];
050    
051                            try {
052                                    coupon = ShoppingCouponLocalServiceUtil.getCoupon(code);
053                            }
054                            catch (NoSuchCouponException nsce) {
055                            }
056                    }
057    
058                    return coupon;
059            }
060    
061            public Map<ShoppingCartItem, Integer> getItems() throws SystemException {
062                    return ShoppingCartLocalServiceUtil.getItems(
063                            getGroupId(), getItemIds());
064            }
065    
066            public int getItemsSize() {
067                    return StringUtil.split(getItemIds()).length;
068            }
069    
070    }