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