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