View Javadoc
1   /*
2    * Copyright (C) 2003-2012 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.Comparator;
20  
21  import javax.jcr.Node;
22  
23  /**
24   * Created by The eXo Platform SAS
25   * Author : eXoPlatform
26   *          vinh_nguyen@exoplatform.com
27   * 8 May 2012  
28   */
29  public class NodeSizeComparator implements Comparator<Node> {
30  
31    public static final String ASCENDING_ORDER = "Ascending" ;
32    private String order_ ;
33  
34    public NodeSizeComparator(String pOrder) {
35      order_ = pOrder ;
36    }
37  
38    public int compare(Node node1, Node node2) {
39      try{
40        long sizeNode1 = getSize(node1);
41        long sizeNode2 = getSize(node2);
42        if (sizeNode1 == sizeNode2) return 0;
43        if(order_.equals(ASCENDING_ORDER)) {
44          return sizeNode1 < sizeNode2 ? -1 : 1;
45        }
46        return sizeNode1 < sizeNode2 ? 1 : -1;
47      }catch (Exception e) {
48        return 0;
49      }
50    }
51    
52    private long getSize(Node node) {
53      try {
54        return node.getProperty("jcr:content/jcr:data").getLength();
55      } catch (Exception e) {
56        return 0;
57      }
58    }
59  }