1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.shopping.model.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portlet.shopping.NoSuchCouponException;
23  import com.liferay.portlet.shopping.model.ShoppingCart;
24  import com.liferay.portlet.shopping.model.ShoppingCartItem;
25  import com.liferay.portlet.shopping.model.ShoppingCoupon;
26  import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
27  import com.liferay.portlet.shopping.service.ShoppingCouponLocalServiceUtil;
28  
29  import java.util.Map;
30  
31  /**
32   * <a href="ShoppingCartImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class ShoppingCartImpl
37      extends ShoppingCartModelImpl implements ShoppingCart {
38  
39      public ShoppingCartImpl() {
40      }
41  
42      public void addItemId(long itemId, String fields) {
43          setItemIds(StringUtil.add(
44              getItemIds(), itemId + fields, StringPool.COMMA, true));
45      }
46  
47      public ShoppingCoupon getCoupon() throws PortalException, SystemException {
48          ShoppingCoupon coupon = null;
49  
50          if (Validator.isNotNull(getCouponCodes())) {
51              String code = StringUtil.split(getCouponCodes())[0];
52  
53              try {
54                  coupon = ShoppingCouponLocalServiceUtil.getCoupon(code);
55              }
56              catch (NoSuchCouponException nsce) {
57              }
58          }
59  
60          return coupon;
61      }
62  
63      public Map<ShoppingCartItem, Integer> getItems() throws SystemException {
64          return ShoppingCartLocalServiceUtil.getItems(
65              getGroupId(), getItemIds());
66      }
67  
68      public int getItemsSize() {
69          return StringUtil.split(getItemIds()).length;
70      }
71  
72  }