1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.model.impl;
21  
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portlet.shopping.model.ShoppingCartItem;
25  import com.liferay.portlet.shopping.model.ShoppingItem;
26  
27  /**
28   * <a href="ShoppingCartItemImpl.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   *
32   */
33  public class ShoppingCartItemImpl implements ShoppingCartItem {
34  
35      public static String[] getFieldsArray(String fields) {
36          return StringUtil.split(fields, "&");
37      }
38  
39      public ShoppingCartItemImpl(ShoppingItem item, String fields) {
40          _item = item;
41          _fields = fields;
42      }
43  
44      public String getCartItemId() {
45          long itemId = getItem().getItemId();
46  
47          if (Validator.isNull(_fields)) {
48              return String.valueOf(itemId);
49          }
50          else {
51              return itemId + "|" + _fields;
52          }
53      }
54  
55      public ShoppingItem getItem() {
56          return _item;
57      }
58  
59      public String getFields() {
60          return _fields;
61      }
62  
63      public String[] getFieldsArray() {
64          return getFieldsArray(_fields);
65      }
66  
67      public int compareTo(ShoppingCartItem cartItem) {
68          if (cartItem == null) {
69              return -1;
70          }
71  
72          int value = getItem().compareTo(cartItem.getItem());
73  
74          if (value == 0) {
75              value = getFields().compareTo(cartItem.getFields());
76          }
77  
78          return value;
79      }
80  
81      public boolean equals(Object obj) {
82          if (obj == null) {
83              return false;
84          }
85  
86          ShoppingCartItem cartItem = (ShoppingCartItem)obj;
87  
88          if (getItem().equals(cartItem.getItem()) &&
89              getFields().equals(cartItem.getFields())) {
90  
91              return true;
92          }
93          else {
94              return false;
95          }
96      }
97  
98      private ShoppingItem _item;
99      private String _fields;
100 
101 }