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.kernel.util.StringUtil;
18  import com.liferay.portal.kernel.util.Validator;
19  import com.liferay.portlet.shopping.model.ShoppingCartItem;
20  import com.liferay.portlet.shopping.model.ShoppingItem;
21  
22  /**
23   * <a href="ShoppingCartItemImpl.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public class ShoppingCartItemImpl implements ShoppingCartItem {
28  
29      public static String[] getFieldsArray(String fields) {
30          return StringUtil.split(fields, "&");
31      }
32  
33      public ShoppingCartItemImpl(ShoppingItem item, String fields) {
34          _item = item;
35          _fields = fields;
36      }
37  
38      public int compareTo(ShoppingCartItem cartItem) {
39          if (cartItem == null) {
40              return -1;
41          }
42  
43          int value = getItem().compareTo(cartItem.getItem());
44  
45          if (value == 0) {
46              value = getFields().compareTo(cartItem.getFields());
47          }
48  
49          return value;
50      }
51  
52      public boolean equals(Object obj) {
53          if (obj == null) {
54              return false;
55          }
56  
57          ShoppingCartItem cartItem = (ShoppingCartItem)obj;
58  
59          if (getItem().equals(cartItem.getItem()) &&
60              getFields().equals(cartItem.getFields())) {
61  
62              return true;
63          }
64          else {
65              return false;
66          }
67      }
68  
69      public String getCartItemId() {
70          long itemId = getItem().getItemId();
71  
72          if (Validator.isNull(_fields)) {
73              return String.valueOf(itemId);
74          }
75          else {
76              return itemId + "|" + _fields;
77          }
78      }
79  
80      public String getFields() {
81          return _fields;
82      }
83  
84      public String[] getFieldsArray() {
85          return getFieldsArray(_fields);
86      }
87  
88      public ShoppingItem getItem() {
89          return _item;
90      }
91  
92      private String _fields;
93      private ShoppingItem _item;
94  
95  }