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