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