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