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