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.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  }