View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (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 General Public License for more details.
13   *
14   * You should have received a copy of the GNU 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  import org.exoplatform.ecm.webui.utils.Utils;
24  import org.exoplatform.services.jcr.ext.audit.AuditHistory;
25  import org.exoplatform.services.jcr.ext.audit.AuditService;
26  import org.exoplatform.services.log.ExoLogger;
27  import org.exoplatform.services.log.Log;
28  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
29  
30  /**
31   * Created by The eXo Platform SARL
32   * Author : Ly Dinh Quang
33   *          quang.ly@exoplatform.com
34   *          xxx5669@gmail.com
35   * Jan 20, 2009
36   */
37  public class StringComparator implements Comparator<Node> {
38  
39    public static final String ASCENDING_ORDER = "Ascending" ;
40    public static final String DESCENDING_ORDER = "Descending" ;
41    private String order_;
42    private String type_ ;
43    private static final Log LOG  = ExoLogger.getLogger(StringComparator.class.getName());
44    public StringComparator(String order, String type) {
45      this.order_ = order ;
46      this.type_ = type;
47    }
48  
49    private String getVersionName(Node node) throws Exception {
50      return node.getBaseVersion().getName() ;
51    }
52  
53    private String versionName(Node node) throws Exception {
54      String returnString = "";
55      returnString = String.valueOf(Utils.isVersionable(node));
56      if (Utils.isVersionable(node) && !getVersionName(node).equals("jcr:rootVersion")) {
57        returnString = "(" + getVersionName(node) + ")" ;
58      }
59      return returnString;
60    }
61  
62    private boolean hasAuditHistory(Node node) throws Exception{
63      AuditService auServ = WCMCoreUtils.getService(AuditService.class);
64      return auServ.hasHistory(node);
65    }
66  
67    private int getNumAuditHistory(Node node) throws Exception{
68      AuditService auServ = WCMCoreUtils.getService(AuditService.class);
69      if (auServ.hasHistory(node)) {
70        AuditHistory auHistory = auServ.getHistory(node);
71        return (auHistory.getAuditRecords()).size();
72      }
73      return 0;
74    }
75  
76    private String getAuditing(Node node) throws Exception {
77      String returnString = "";
78      returnString = String.valueOf(Utils.isAuditable(node));
79      if (Utils.isAuditable(node)&& hasAuditHistory(node)) {
80        returnString = "(" + getNumAuditHistory(node) + ")";
81      }
82      return returnString;
83    }
84  
85    private int compare(String s1, String s2) {
86      if (ASCENDING_ORDER.equals(order_)) {
87        return s1.compareTo(s2) ;
88      }
89      return s2.compareTo(s1) ;
90    }
91  
92    public int compare(Node node1, Node node2) {
93      int returnCompare = 0;
94      try {
95        if (type_.equals("Owner")) {
96          if (node1.hasProperty("exo:owner")  && node2.hasProperty("exo:owner")) {
97            String owner1 = node1.getProperty("exo:owner").getString();
98            String owner2 = node2.getProperty("exo:owner").getString();
99            returnCompare = compare(owner1, owner2);
100         }
101       } else if (type_.equals("Versionable")) {
102         String versionNode1 = versionName(node1);
103         String versionNode2 = versionName(node2);
104         returnCompare = compare(versionNode1, versionNode2);
105       } else if (type_.equals("Auditing")) {
106         String auditing1 = getAuditing(node1);
107         String auditing2 = getAuditing(node2);
108         returnCompare = compare(auditing1, auditing2);
109       }
110     } catch (Exception e) {
111       if (LOG.isErrorEnabled()) {
112         LOG.error("Unexpected error", e);
113       }
114     }
115     return returnCompare;
116   }
117 }