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.component.explorer.sidebar;
18  
19  import org.exoplatform.services.cms.link.NodeLinkAware;
20  import org.exoplatform.services.jcr.impl.core.NodeImpl;
21  import org.exoplatform.services.jcr.util.Text;
22  import org.exoplatform.services.log.ExoLogger;
23  import org.exoplatform.services.log.Log;
24  import org.exoplatform.services.wcm.core.NodeLocation;
25  
26  import javax.jcr.Node;
27  import javax.jcr.RepositoryException;
28  import java.util.ArrayList;
29  import java.util.List;
30  /**
31   * Created by The eXo Platform SARL
32   * Author : Tran The Trong
33   *          trongtt@gmail.com
34   * Sep 29, 2006
35   * 5:37:31 PM
36   */
37  public class TreeNode {
38    private static final Log LOG  = ExoLogger.getLogger(TreeNode.class.getName());
39    
40    //TODO Need use this class for BC TreeNode
41    private boolean isExpanded_ ;
42    private String path_;
43    private String prefix;
44    private NodeLocation node_ ;
45    private NodeLinkAware node;
46    private String name_;
47    private List<TreeNode> children_ = new ArrayList<TreeNode>() ;
48  
49    private long childrenSize;
50  
51    public TreeNode(Node node) throws RepositoryException {
52      this(node, node.getPath());
53    }
54  
55    private TreeNode(Node node, String path) {
56      if (node instanceof NodeLinkAware) {
57        this.node = (NodeLinkAware)node;
58        try {
59          this.childrenSize = this.node.getNodesLazily().getSize();
60        } catch (RepositoryException e) {
61          this.childrenSize = 0;
62        }
63      } else {
64        node_ = NodeLocation.getNodeLocationByNode(node);
65        try {
66          this.childrenSize = ((NodeImpl) node).getNodesLazily().getSize();
67        } catch (RepositoryException e) {
68          this.childrenSize = 0;
69        }
70      }
71  
72      name_ = getName(node);
73      isExpanded_ = false ;
74      path_ = path;
75      prefix =  path_.equals("/") ? "" : path_;
76    }
77  
78    public boolean isExpanded() { return isExpanded_; }
79    public void setExpanded(boolean isExpanded) { isExpanded_ = isExpanded; }
80  
81    public String getName() throws RepositoryException {
82      return name_;
83    }
84  
85    private String getName(Node node) {
86      StringBuilder buffer = new StringBuilder(128);
87      try {
88        buffer.append(node.getName());
89        int index = node.getIndex();
90        if (index > 1) {
91          buffer.append('[');
92          buffer.append(index);
93          buffer.append(']');
94        }
95      } catch (RepositoryException e) {
96        if (LOG.isWarnEnabled()) {
97          LOG.warn(e.getMessage());
98        }
99      }
100     return buffer.toString();
101   }
102 
103   public String getPath() { return path_; }
104   public String getNodePath() throws RepositoryException { 
105     return node != null ? node.getPath() : node_.getPath(); 
106   }
107 
108   public Node getNode() { 
109     return node != null ? node : NodeLocation.getNodeByLocation(node_); 
110   }
111   public void setNode(Node node) {
112     if (node instanceof NodeLinkAware) {
113       this.node = (NodeLinkAware)node;
114     } else {
115       node_ = NodeLocation.getNodeLocationByNode(node);
116     }
117   }
118   public String getNodePath4ID() {
119     String tmp = Text.escape(path_);
120     return tmp.replace('%', '_');
121   }
122   public List<TreeNode> getChildren() { return children_ ; }
123   public int getChildrenSize() { 
124     return (int) childrenSize;
125     }
126 
127   public TreeNode getChildByName(String name) throws RepositoryException {
128     for(TreeNode child : children_) {
129       if(child.getName().equals(name)) return child ;
130     }
131     Node tempNode = null;
132     if(this.getNode().hasNode(name)) {
133       tempNode = this.getNode().getNode(name);
134     }
135     if (tempNode == null) {
136       return null;
137     }
138     TreeNode tempTreeNode = new TreeNode(tempNode, prefix + "/" + getName(tempNode));
139     return tempTreeNode;
140   }
141 
142   public void setChildren(List<Node> children) throws Exception {
143     setExpanded(true) ;
144     for(Node child : children) {
145       children_.add(new TreeNode(child, prefix  + "/" + getName(child))) ;
146     }
147   }
148 
149 }