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.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            @Override
039            public void addItemId(long itemId, String fields) {
040                    setItemIds(
041                            StringUtil.add(
042                                    getItemIds(), itemId + fields, StringPool.COMMA, true));
043            }
044    
045            @Override
046            public ShoppingCoupon getCoupon() throws PortalException, SystemException {
047                    ShoppingCoupon coupon = null;
048    
049                    if (Validator.isNotNull(getCouponCodes())) {
050                            String code = StringUtil.split(getCouponCodes())[0];
051    
052                            try {
053                                    coupon = ShoppingCouponLocalServiceUtil.getCoupon(code);
054                            }
055                            catch (NoSuchCouponException nsce) {
056                            }
057                    }
058    
059                    return coupon;
060            }
061    
062            @Override
063            public Map<ShoppingCartItem, Integer> getItems() throws SystemException {
064                    return ShoppingCartLocalServiceUtil.getItems(
065                            getGroupId(), getItemIds());
066            }
067    
068            @Override
069            public int getItemsSize() {
070                    return StringUtil.split(getItemIds()).length;
071            }
072    
073    }