001package org.kuali.ole.docstore.model.repopojo; 002 003import java.util.ArrayList; 004import java.util.Iterator; 005import java.util.List; 006 007/** 008 * Class for representing the data in repository 009 * User: Pranitha 010 * Date: 2/28/12 011 * Time: 2:44 PM 012 * To change this template use File | Settings | File Templates. 013 */ 014public class RepositoryData { 015 016 private FolderNode rootNode = new FolderNode(); 017 018 public RepositoryData() { 019 rootNode.setPath("/"); 020 rootNode.setName("/"); 021 } 022 023 024 public FolderNode getRootNode() { 025 return rootNode; 026 } 027 028 public void setRootNode(FolderNode rootNode) { 029 this.rootNode = rootNode; 030 } 031 032 033 public RepositoryData loadDump(String dump) { 034 RepositoryData repositoryData = new RepositoryData(); 035 loadDump(dump, repositoryData); 036 return repositoryData; 037 } 038 039 public void loadDump(String dump, RepositoryData repositoryData) { 040 String splitDump[]; 041 StringBuilder content = new StringBuilder(); 042 String key = null; 043 String value = null; 044 String path = null; 045 boolean status = true; 046 splitDump = dump.split("\\n"); 047 for (int i = 0; i < splitDump.length; i++) { 048 //set jcr:data key 049 if (splitDump[i].contains("jcr:data")) { 050 content = new StringBuilder(); 051 key = splitDump[i].substring(0, splitDump[i].indexOf("=")).trim(); 052 path = key.substring(0, key.lastIndexOf("/")); 053 status = content(splitDump[i], status, content, path); 054 } 055 // set jcr:data value 056 if (!status && !splitDump[i].contains("jcr:data")) { 057 status = content(splitDump[i], status, content, path); 058 value = content.toString(); 059 List<String> propPathElements = getPathElements(key); 060 FolderNode folderNode = loadPath(repositoryData, 061 propPathElements.subList(0, propPathElements.size() - 1)); 062 String propName = propPathElements.get(propPathElements.size() - 1); 063 folderNode.setProperty(propName, value); 064 } 065 if (status) { 066 loadLine(repositoryData, splitDump[i]); 067 } 068 } 069 } 070 071 public FolderNode loadLine(RepositoryData repositoryData, String line) { 072 FolderNode folderNode = new FolderNode(); 073 if (!line.contains("=")) { 074 String[] nodeList = line.split("/"); 075 List<String> pathElements = getPathElements(line); 076 folderNode = loadPath(repositoryData, pathElements); 077 folderNode.setPath(line); 078 } else { 079 String propPath = line.substring(0, line.indexOf("=")).trim(); 080 List<String> propPathElements = getPathElements(propPath); 081 folderNode = loadPath(repositoryData, propPathElements.subList(0, propPathElements.size() - 1)); 082 String propName = propPathElements.get(propPathElements.size() - 1); 083 String value = line.substring(line.indexOf("=") + 1, line.length()).trim(); 084 folderNode.setProperty(propName, value); 085 } 086 return folderNode; 087 } 088 089 private FolderNode loadPath(RepositoryData repositoryData, List<String> pathElements) { 090 FolderNode folderNode = repositoryData.getRootNode(); 091 for (int i = 0; i < pathElements.size(); i++) { 092 folderNode = getChildNode(folderNode, pathElements.get(i)); 093 } 094 return folderNode; 095 } 096 097 private FolderNode getChildNode(FolderNode folderNode, String childNodeName) { 098 List<DocStoreNode> childNodes = folderNode.getChildren(); 099 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}