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          }
79          else {
80              String propPath = line.substring(0, line.indexOf("=")).trim();
81              List<String> propPathElements = getPathElements(propPath);
82              folderNode = loadPath(repositoryData, propPathElements.subList(0, propPathElements.size() - 1));
83              String propName = propPathElements.get(propPathElements.size() - 1);
84              String value = line.substring(line.indexOf("=") + 1, line.length()).trim();
85              folderNode.setProperty(propName, value);
86          }
87          return folderNode;
88      }
89  
90      private FolderNode loadPath(RepositoryData repositoryData, List<String> pathElements) {
91          FolderNode folderNode = repositoryData.getRootNode();
92          for (int i = 0; i < pathElements.size(); i++) {
93              folderNode = getChildNode(folderNode, pathElements.get(i));
94          }
95          return folderNode;
96      }
97  
98      private FolderNode getChildNode(FolderNode folderNode, String childNodeName) {
99          List<DocStoreNode> childNodes = folderNode.getChildren();
100         if (childNodes == null) {
101             childNodes = new ArrayList<DocStoreNode>();
102             folderNode.setChildren(childNodes);
103         }
104         FolderNode childNode = null;
105         for (DocStoreNode node : childNodes) {
106             if (node instanceof FolderNode && node.getName().equalsIgnoreCase(childNodeName)) {
107                 childNode = (FolderNode) node;
108                 break;
109             }
110 
111         }
112         if (childNode == null) {
113             childNode = new FolderNode();
114             childNode.setName(childNodeName);
115             childNodes.add(childNode);
116         }
117 
118         return childNode;
119     }
120 
121     private List<String> getPathElements(String line) {
122         String[] nodeList = line.split("/");
123         List<String> pathElements = new ArrayList<String>();
124         for (int i = 0; i < nodeList.length; i++) {
125             if (nodeList[i] != null && nodeList[i].length() > 0) {
126                 pathElements.add(nodeList[i]);
127             }
128         }
129         return pathElements;
130     }
131 
132      private boolean content(String data, boolean status, StringBuilder content, String path) {
133         status = false;
134         if (data.contains(path) && !data.contains("jcr:data")) {
135             status = true;
136             return status;
137         }
138         if (data.contains("jcr:data")) {
139             content.append(data.substring(data.indexOf("=") + 1, data.length()).trim());
140         }
141         else {
142             content.append("\n" + data);
143         }
144         return status;
145     }
146 
147      public String getRepositoryDump(RepositoryData repositoryData) {
148         StringBuffer repositoryDump = new StringBuffer();
149         repositoryDump(repositoryData.getRootNode(), repositoryDump);
150         return repositoryDump.toString();
151     }
152 
153     private void repositoryDump(FolderNode rootNode, StringBuffer repositoryDump) {
154         DocStoreNode docStoreNode = rootNode;
155         repositoryDump.append(rootNode.toString());
156         if (rootNode != null && rootNode.getChildren() != null && rootNode.getChildren().size() > 0) {
157             for (Iterator<DocStoreNode> folderIterator = rootNode.getChildren().iterator(); folderIterator.hasNext();) {
158                 repositoryDump((FolderNode) folderIterator.next(), repositoryDump);
159             }
160         }
161     }
162 }