View Javadoc
1   /*
2    * Copyright (C) 2003-2013 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.comparator;
18  
19  import java.util.Calendar;
20  import java.util.Comparator;
21  
22  import javax.jcr.Node;
23  
24  import org.exoplatform.services.wcm.core.NodetypeConstant;
25  
26  /**
27   * Created by The eXo Platform SAS
28   * Author : eXoPlatform
29   *          exo@exoplatform.com
30   * Jan 9, 2013  
31   */
32  public class DateComparator implements Comparator<Node>{
33  
34    public static final String ASCENDING_ORDER = "Ascending" ;
35    public static final String DESCENDING_ORDER = "Descending" ;
36    private String order_ ;
37  
38    public DateComparator(String pOrder) {
39      order_ = pOrder ;
40    }
41  
42    public int compare(Node node1, Node node2) {
43      try{
44        Calendar date1 = node1.hasProperty(NodetypeConstant.EXO_DATE_MODIFIED) ? 
45                            node1.getProperty(NodetypeConstant.EXO_DATE_MODIFIED).getDate() : 
46                              node1.getProperty(NodetypeConstant.EXO_DATE_CREATED).getDate();
47        Calendar date2 = node2.hasProperty(NodetypeConstant.EXO_DATE_MODIFIED) ? 
48                            node2.getProperty(NodetypeConstant.EXO_DATE_MODIFIED).getDate() : 
49                              node2.getProperty(NodetypeConstant.EXO_DATE_CREATED).getDate();
50        
51        if(order_.equals(ASCENDING_ORDER)) {
52          return date1.compareTo(date2) ;
53        }
54        return date2.compareTo(date1) ;
55      }catch (Exception e) {
56        return 0;
57      }
58    }
59  
60  }