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="WorkflowInstanceStartDateComparator.java.html"><b><i>View Source</i>
24   * </b></a>
25   *
26   * @author Shuyang Zhou
27   */
28  public class WorkflowInstanceStartDateComparator extends OrderByComparator {
29  
30      public static String ORDER_BY_ASC = "startDate ASC, workflowInstanceId ASC";
31  
32      public static String ORDER_BY_DESC =
33          "startDate DESC, workflowInstanceId DESC";
34  
35      public static String[] ORDER_BY_FIELDS = {
36          "startDate", "workflowInstanceId"
37      };
38  
39      public WorkflowInstanceStartDateComparator() {
40          this(false);
41      }
42  
43      public WorkflowInstanceStartDateComparator(boolean asc) {
44          _asc = asc;
45      }
46  
47      public int compare(Object obj1, Object obj2) {
48          WorkflowInstance workflowInstance1 = (WorkflowInstance)obj1;
49          WorkflowInstance workflowInstance2 = (WorkflowInstance)obj2;
50  
51          Date startDate1 = workflowInstance1.getStartDate();
52          Date startDate2 = workflowInstance2.getStartDate();
53  
54          int value = startDate1.compareTo(startDate2);
55  
56          if (value == 0) {
57              Long workflowInstanceId1 =
58                  workflowInstance1.getWorkflowInstanceId();
59              Long workflowInstanceId2 =
60                  workflowInstance2.getWorkflowInstanceId();
61  
62              value = workflowInstanceId1.compareTo(workflowInstanceId2);
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  }