View Javadoc
1   package org.kuali.ole.docstore.repository;
2   
3   import org.kuali.ole.RepositoryManager;
4   import org.kuali.ole.docstore.process.ProcessParameters;
5   import org.kuali.ole.pojo.OleException;
6   
7   import javax.jcr.Node;
8   import javax.jcr.NodeIterator;
9   import javax.jcr.RepositoryException;
10  import javax.jcr.Session;
11  import java.util.Iterator;
12  import java.util.LinkedHashMap;
13  import java.util.Map;
14  
15  /**
16   * Created by IntelliJ IDEA.
17   * User: SG7940
18   * Date: 10/11/12
19   * Time: 12:59 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class NodeCountManager {
23  
24      private static NodeCountManager nodeCountManager;
25      private Map<String, Long> nodeCountMap = null;
26      private RepositoryManager repositoryManager;
27  
28      private NodeCountManager() {
29  
30      }
31  
32      public static NodeCountManager getNodeCountManager() {
33          if (null == nodeCountManager) {
34              nodeCountManager = new NodeCountManager();
35          }
36          return nodeCountManager;
37      }
38  
39      public Map generateNodeCountMap() throws OleException, RepositoryException {
40          nodeCountMap = new LinkedHashMap<String, Long>();
41          RepositoryManager oleRepositoryManager = getRepositoryManager();
42          Session session = oleRepositoryManager.getSession("repositoryBrowser", "generateNodeCount");
43          computeNodeCountMap(session.getRootNode(), nodeCountMap);
44          oleRepositoryManager.logout(session);
45          return nodeCountMap;
46      }
47  
48      private RepositoryManager getRepositoryManager() throws OleException {
49          if (null == repositoryManager) {
50              repositoryManager = RepositoryManager.getRepositoryManager();
51          }
52          return repositoryManager;
53      }
54  
55      private void computeNodeCountMap(Node node, Map<String, Long> nodeCountMap) throws RepositoryException {
56          Long size = null;
57          if (node != null) {
58              if (!node.hasProperty("nodeType") && !node.getName().equalsIgnoreCase("") && node.getName()
59                      .equalsIgnoreCase(
60                              "jcr:system")) {
61                  return;
62              } else {
63                  if (isLastFolderNodeByName(node)) {
64                      size = new Long(node.getNodes().getSize());
65                      nodeCountMap.put(node.getPath(), size);
66                  } else {
67                      nodeCountMap.put(node.getPath(), 0l);
68                      long count = 0;
69                      long longValue = 0;
70                      for (Iterator<NodeIterator> it = node.getNodes(); it.hasNext(); ) {
71                          Node childNode = (Node) it.next();
72                          computeNodeCountMap(childNode, nodeCountMap);
73                          if (nodeCountMap.containsKey(childNode.getPath())) {
74                              longValue = nodeCountMap.get(childNode.getPath());
75                              count = count + longValue;
76                          }
77                      }
78                      size = new Long(count);
79                      if (node.getPath() != null) {
80                          nodeCountMap.put(node.getPath(), size);
81                      }
82                  }
83              }
84          }
85      }
86  
87      public boolean isLastFolderNodeByName(Node node) throws RepositoryException {
88          Iterator it = node.getNodes();
89          while (it.hasNext()) {
90              Node childNode = (Node) it.next();
91              if (childNode.getName().equals(ProcessParameters.NODE_INSTANCE)) {
92                  return true;
93              } else if (childNode.getName().endsWith("File")) {
94                  return true;
95              }
96          }
97          return false;
98      }
99  
100     @Deprecated
101     public void updateNodeCount(Node node, long count) throws RepositoryException, OleException {
102         if (node.getName().length() == 0 || node.getName().equals("jcr:system")) {
103         } else {
104             long size = 0;
105             String path = node.getPath();
106             if (nodeCountMap != null) {
107                 if (nodeCountMap.containsKey(path)) {
108                     size = nodeCountMap.get(node.getPath());
109                 }
110                 nodeCountMap.put(path, size + count);
111                 updateNodeCount(node.getParent(), count);
112             }
113         }
114     }
115 
116 
117     public Map getNodeCountMap() {
118         return nodeCountMap;
119     }
120 
121     public void setRepositoryManager(RepositoryManager repositoryManager) {
122         this.repositoryManager = repositoryManager;
123     }
124 }