View Javadoc
1   package org.kuali.ole.docstore.model.repopojo;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   /**
8    * Class for representing the data in repository
9    * User: Pranitha
10   * Date: 2/28/12
11   * Time: 2:44 PM
12   * To change this template use File | Settings | File Templates.
13   */
14  public class RepositoryData {
15  
16      private FolderNode rootNode = new FolderNode();
17  
18      public RepositoryData() {
19          rootNode.setPath("/");
20          rootNode.setName("/");
21      }
22  
23  
24      public FolderNode getRootNode() {
25          return rootNode;
26      }
27  
28      public void setRootNode(FolderNode rootNode) {
29          this.rootNode = rootNode;
30      }
31  
32  
33      public RepositoryData loadDump(String dump) {
34          RepositoryData repositoryData = new RepositoryData();
35          loadDump(dump, repositoryData);
36          return repositoryData;
37      }
38  
39      public void loadDump(String dump, RepositoryData repositoryData) {
40          String splitDump[];
41          StringBuilder content = new StringBuilder();
42          String key = null;
43          String value = null;
44          String path = null;
45          boolean status = true;
46          splitDump = dump.split("\\n");
47          for (int i = 0; i < splitDump.length; i++) {
48              //set jcr:data key
49              if (splitDump[i].contains("jcr:data")) {
50                  content = new StringBuilder();
51                  key = splitDump[i].substring(0, splitDump[i].indexOf("=")).trim();
52                  path = key.substring(0, key.lastIndexOf("/"));
53                  status = content(splitDump[i], status, content, path);
54              }
55              // set jcr:data value
56              if (!status && !splitDump[i].contains("jcr:data")) {
57                  status = content(splitDump[i], status, content, path);
58                  value = content.toString();
59                  List<String> propPathElements = getPathElements(key);
60                  FolderNode folderNode = loadPath(repositoryData,
61                          propPathElements.subList(0, propPathElements.size() - 1));
62                  String propName = propPathElements.get(propPathElements.size() - 1);
63                  folderNode.setProperty(propName, value);
64              }
65              if (status) {
66                  loadLine(repositoryData, splitDump[i]);
67              }
68          }
69      }
70  
71      public FolderNode loadLine(RepositoryData repositoryData, String line) {
72          FolderNode folderNode = new FolderNode();
73          if (!line.contains("=")) {
74              String[] nodeList = line.split("/");
75              List<String> pathElements = getPathElements(line);
76              folderNode = loadPath(repositoryData, pathElements);
77              folderNode.setPath(line);
78          } else {
79              String propPath = line.substring(0, line.indexOf("=")).trim();
80              List<String> propPathElements = getPathElements(propPath);
81              folderNode = loadPath(repositoryData, propPathElements.subList(0, propPathElements.size() - 1));
82              String propName = propPathElements.get(propPathElements.size() - 1);
83              String value = line.substring(line.indexOf("=") + 1, line.length()).trim();
84              folderNode.setProperty(propName, value);
85          }
86          return folderNode;
87      }
88  
89      private FolderNode loadPath(RepositoryData repositoryData, List<String> pathElements) {
90          FolderNode folderNode = repositoryData.getRootNode();
91          for (int i = 0; i < pathElements.size(); i++) {
92              folderNode = getChildNode(folderNode, pathElements.get(i));
93          }
94          return folderNode;
95      }
96  
97      private FolderNode getChildNode(FolderNode folderNode, String childNodeName) {
98          List<DocStoreNode> childNodes = folderNode.getChildren();
99          if (childNodes == null) {
100             childNodes = new ArrayList<DocStoreNode>();
101             folderNode.setChildren(childNodes);
102         }
103         FolderNode childNode = null;
104         for (DocStoreNode node : childNodes) {
105             if (node instanceof FolderNode && node.getName().equalsIgnoreCase(childNodeName)) {
106                 childNode = (FolderNode) node;
107                 break;
108             }
109 
110         }
111         if (childNode == null) {
112             childNode = new FolderNode();
113             childNode.setName(childNodeName);
114             childNodes.add(childNode);
115         }
116 
117         return childNode;
118     }
119 
120     private List<String> getPathElements(String line) {
121         String[] nodeList = line.split("/");
122         List<String> pathElements = new ArrayList<String>();
123         for (int i = 0; i < nodeList.length; i++) {
124             if (nodeList[i] != null && nodeList[i].length() > 0) {
125                 pathElements.add(nodeList[i]);
126             }
127         }
128         return pathElements;
129     }
130 
131     private boolean content(String data, boolean status, StringBuilder content, String path) {
132         status = false;
133         if (data.contains(path) && !data.contains("jcr:data")) {
134             status = true;
135             return status;
136         }
137         if (data.contains("jcr:data")) {
138             content.append(data.substring(data.indexOf("=") + 1, data.length()).trim());
139         } else {
140             content.append("\n" + data);
141         }
142         return status;
143     }
144 
145     public String getRepositoryDump(RepositoryData repositoryData) {
146         StringBuffer repositoryDump = new StringBuffer();
147         repositoryDump(repositoryData.getRootNode(), repositoryDump);
148         return repositoryDump.toString();
149     }
150 
151     private void repositoryDump(FolderNode rootNode, StringBuffer repositoryDump) {
152         DocStoreNode docStoreNode = rootNode;
153         repositoryDump.append(rootNode.toString());
154         if (rootNode != null && rootNode.getChildren() != null && rootNode.getChildren().size() > 0) {
155             for (Iterator<DocStoreNode> folderIterator = rootNode.getChildren().iterator(); folderIterator.hasNext(); ) {
156                 repositoryDump((FolderNode) folderIterator.next(), repositoryDump);
157             }
158         }
159     }
160 }