1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.shopping.model.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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  }