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.portal.util.comparator;
16  
17  import com.liferay.portal.kernel.util.OrderByComparator;
18  import com.liferay.portal.model.Layout;
19  
20  /**
21   * <a href="LayoutComparator.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   */
25  public class LayoutComparator extends OrderByComparator {
26  
27      public static String ORDER_BY_ASC = "groupId ASC, layoutId ASC";
28  
29      public static String ORDER_BY_DESC = "groupId DESC, layoutId DESC";
30  
31      public static String[] ORDER_BY_FIELDS = {"groupId", "layoutId"};
32  
33      public LayoutComparator() {
34          this(false);
35      }
36  
37      public LayoutComparator(boolean asc) {
38          _asc = asc;
39      }
40  
41      public int compare(Object obj1, Object obj2) {
42          Layout layout1 = (Layout)obj1;
43          Layout layout2 = (Layout)obj2;
44  
45          Long groupId1 = new Long(layout1.getGroupId());
46          Long groupId2 = new Long(layout2.getGroupId());
47  
48          int value = groupId1.compareTo(groupId2);
49  
50          if (value != 0) {
51              if (_asc) {
52                  return value;
53              }
54              else {
55                  return -value;
56              }
57          }
58  
59          Long layoutId1 = new Long(layout1.getLayoutId());
60          Long layoutId2 = new Long(layout2.getLayoutId());
61  
62          value = layoutId1.compareTo(layoutId2);
63  
64          if (_asc) {
65              return value;
66          }
67          else {
68              return -value;
69          }
70      }
71  
72      public String getOrderBy() {
73          if (_asc) {
74              return ORDER_BY_ASC;
75          }
76          else {
77              return ORDER_BY_DESC;
78          }
79      }
80  
81      public String[] getOrderByFields() {
82          return ORDER_BY_FIELDS;
83      }
84  
85      public boolean isAscending() {
86          return _asc;
87      }
88  
89      private boolean _asc;
90  
91  }