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.kernel.workflow.comparator;
16  
17  import com.liferay.portal.kernel.util.OrderByComparator;
18  import com.liferay.portal.kernel.workflow.WorkflowDefinition;
19  
20  /**
21   * <a href="WorkflowDefinitionNameComparator.java.html"><b><i>View Source</i>
22   * </b></a>
23   *
24   * @author Shuyang Zhou
25   */
26  public class WorkflowDefinitionNameComparator extends OrderByComparator {
27  
28      public static String ORDER_BY_ASC = "name ASC, version ASC";
29  
30      public static String ORDER_BY_DESC = "name DESC, version DESC";
31  
32      public static String[] ORDER_BY_FIELDS = {"name", "version"};
33  
34      public WorkflowDefinitionNameComparator() {
35          this(false);
36      }
37  
38      public WorkflowDefinitionNameComparator(boolean asc) {
39          _asc = asc;
40      }
41  
42      public int compare(Object obj1, Object obj2) {
43          WorkflowDefinition workflowDefinition1 = (WorkflowDefinition)obj1;
44          WorkflowDefinition workflowDefinition2 = (WorkflowDefinition)obj2;
45  
46          String name1 = workflowDefinition1.getName();
47          String name2 = workflowDefinition2.getName();
48  
49          int value = name1.compareTo(name2);
50  
51          if (value == 0) {
52              Integer version1 = workflowDefinition1.getVersion();
53              Integer version2 = workflowDefinition2.getVersion();
54  
55              value = version1.compareTo(version2);
56          }
57  
58          if (_asc) {
59              return value;
60          }
61          else {
62              return -value;
63          }
64      }
65  
66      public String getOrderBy() {
67          if (_asc) {
68              return ORDER_BY_ASC;
69          }
70          else {
71              return ORDER_BY_DESC;
72          }
73      }
74  
75      public String[] getOrderByFields() {
76          return ORDER_BY_FIELDS;
77      }
78  
79      public boolean isAscending() {
80          return _asc;
81      }
82  
83      private boolean _asc;
84  
85  }