org.kuali.ole.RepositoryBrowser Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011 The Kuali Foundation.
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ecl2.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kuali.ole;
import org.kuali.ole.docstore.model.enums.DocFormat;
import org.kuali.ole.docstore.model.enums.DocType;
import org.kuali.ole.docstore.model.repopojo.FolderNode;
import org.kuali.ole.docstore.model.repopojo.RepositoryData;
import org.kuali.ole.docstore.process.ProcessParameters;
import org.kuali.ole.logger.DocStoreLogger;
import org.kuali.ole.logger.MetricsLogger;
import org.kuali.ole.pojo.OleException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: peris
* Date: 5/3/11
* Time: 10:59 AM
* To change this template use File | Settings | File Templates.
*/
public class RepositoryBrowser {
private DocStoreLogger docStoreLogger = new DocStoreLogger(this.getClass().getName());
private MetricsLogger metricsLogger;
protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
public void browseRepositoryContent() throws OleException {
Session session = RepositoryManager.getRepositoryManager()
.getSession("repositoryBrowser", "browseRepositoryContent");
try {
Node root = session.getRootNode();
List oleDocStoreDatas = browseDataSetup();
for (Iterator iterator = oleDocStoreDatas.iterator(); iterator.hasNext(); ) {
OleDocStoreData oleDocStoreData = iterator.next();
if (root.hasNode(oleDocStoreData.getCategory())) {
Node categoryNode = root.getNode(oleDocStoreData.getCategory());
NodeIterator nodes = categoryNode.getNodes();
while (nodes.hasNext()) {
Node node = nodes.nextNode();
NodeIterator childNodes = node.getNodes();
while (childNodes.hasNext()) {
Node childOfChildNode = childNodes.nextNode();
NodeIterator fileNodes = childOfChildNode.getNodes();
while (fileNodes.hasNext()) {
Node nextNode = fileNodes.nextNode();
if (nextNode.hasNode("jcr:content")) {
}
}
}
}
}
}
} catch (RepositoryException e) {
throw new OleException(e.getMessage());
} finally {
RepositoryManager.getRepositoryManager().logout(session);
}
}
public List getFilesCount() throws OleException {
List oleDocStoreDatas;
getMetricsLogger().startRecording();
Session session = RepositoryManager.getRepositoryManager().getSession("repositoryBrowser", "fileCount");
Map> formatLevelMap = new HashMap>();
try {
Node root = session.getRootNode();
oleDocStoreDatas = browseDataSetup();
for (Iterator iterator = oleDocStoreDatas.iterator(); iterator.hasNext(); ) {
OleDocStoreData oleDocStoreData = iterator.next();
if (root.hasNode(oleDocStoreData.getCategory())) {
Node categoryNode = root.getNode(oleDocStoreData.getCategory());
Map> typeFormatMap = oleDocStoreData.getTypeFormatMap();
Set types = typeFormatMap.keySet();
for (Iterator catIterator = types.iterator(); catIterator.hasNext(); ) {
String type = catIterator.next();
if (categoryNode.hasNode(type)) {
Node typeNode = categoryNode.getNode(type);
List formats = typeFormatMap.get(type);
Map countMap = new HashMap();
if (formats != null && formats.size() > 0) {
for (Iterator typIterator = formats.iterator(); typIterator.hasNext(); ) {
String format = typIterator.next();
if (format.equalsIgnoreCase(DocFormat.MARC.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL1);
formatLevelMap.put(DocFormat.MARC.getCode(), levelNodes);
} else if (format.equalsIgnoreCase(DocFormat.DUBLIN_CORE.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL1);
formatLevelMap.put(DocFormat.DUBLIN_CORE.getCode(), levelNodes);
} else if (format.equalsIgnoreCase(DocFormat.DUBLIN_UNQUALIFIED.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL1);
formatLevelMap.put(DocFormat.DUBLIN_UNQUALIFIED.getCode(), levelNodes);
} else if (format.equalsIgnoreCase("oleml") && type.equalsIgnoreCase("instance")) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL1);
formatLevelMap.put("oleml", levelNodes);
}
if (typeNode.hasNode(format)) {
Node formatNode = typeNode.getNode(format);
List folderL1list = formatLevelMap.get(format);
if (folderL1list != null && folderL1list.size() > 0) {
for (Iterator forIterator = folderL1list.iterator(); forIterator
.hasNext(); ) {
String levelL1 = forIterator.next();
if (format.equalsIgnoreCase(DocFormat.MARC.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL2);
formatLevelMap.put(ProcessParameters.NODE_LEVEL1, levelNodes);
} else if (format.equalsIgnoreCase(DocFormat.DUBLIN_CORE.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL2);
formatLevelMap.put(ProcessParameters.NODE_LEVEL1, levelNodes);
} else if (format
.equalsIgnoreCase(DocFormat.DUBLIN_UNQUALIFIED.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL2);
formatLevelMap.put(ProcessParameters.NODE_LEVEL1, levelNodes);
} else if (format.equalsIgnoreCase("oleml") && type
.equalsIgnoreCase("instance")) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL2);
formatLevelMap.put(ProcessParameters.NODE_LEVEL1, levelNodes);
}
// addFormatCount(folder12Node, file, format, countMap, type,oleDocStoreData);
if (formatNode.hasNode(levelL1)) {
Node levelL1Node = formatNode.getNode(levelL1);
List folder12List = formatLevelMap.get(levelL1);
if (folder12List != null && folder12List.size() > 0) {
for (Iterator levelL1Iterator = folder12List
.iterator(); levelL1Iterator.hasNext(); ) {
String levelL2 = levelL1Iterator.next();
if (format.equalsIgnoreCase(DocFormat.MARC.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL3);
formatLevelMap
.put(ProcessParameters.NODE_LEVEL2, levelNodes);
} else if (format.equalsIgnoreCase(
DocFormat.DUBLIN_CORE.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL3);
formatLevelMap
.put(ProcessParameters.NODE_LEVEL2, levelNodes);
} else if (format.equalsIgnoreCase(
DocFormat.DUBLIN_UNQUALIFIED.getCode())) {
List levelNodes = new ArrayList();
levelNodes.add(ProcessParameters.NODE_LEVEL3);
formatLevelMap
.put(ProcessParameters.NODE_LEVEL2, levelNodes);
} else {
addFormatCount(levelL1Node, levelL2, format, countMap,
type, oleDocStoreData);
}
if (levelL1Node.hasNode(levelL2)) {
Node folder12Node = levelL1Node.getNode(levelL2);
List folderL3List = formatLevelMap.get(levelL2);
if (folderL3List != null && folderL3List.size() > 0) {
for (Iterator levelL2Iterator = folderL3List
.iterator(); levelL2Iterator.hasNext(); ) {
String file = levelL2Iterator.next();
addFormatCount(folder12Node, file, format,
countMap, type, oleDocStoreData);
}
}
}
}
}
}
}
}
}
if (!type.equalsIgnoreCase(DocType.INSTANCE.getCode())
&& !format.equalsIgnoreCase(DocFormat.MARC.getCode()) && !format
.contains("dublin")) {
addFormatCount(typeNode, format, format, countMap, type, oleDocStoreData);
}
}
}
}
}
}
}
getMetricsLogger().endRecording();
getMetricsLogger().printTimes("Getting Files count took:: ");
} catch (RepositoryException e) {
throw new OleException(e.getMessage());
} finally {
RepositoryManager.getRepositoryManager().logout(session);
}
return oleDocStoreDatas;
}
private void addFormatCount(Node levelL1Node, String levelL2, String format, Map countMap,
String type, OleDocStoreData oleDocStoreData) throws RepositoryException {
Long count = 0L;
NodeIterator L2Nodes = levelL1Node.getNodes(levelL2);
Map> typeFormatMapWithNodeCount = oleDocStoreData.getTypeFormatMapWithNodeCount();
while (L2Nodes.hasNext()) {
Node fileNode = (Node) L2Nodes.next();
NodeIterator filesNodes = fileNode.getNodes();
if (countMap.get(format) != null) {
count = countMap.get(format);
}
count = count + filesNodes.getSize();
countMap.put(format, count);
}
typeFormatMapWithNodeCount.put(type, countMap);
}
/* public Map getDocumentCount() throws OleException, RepositoryException {
Map documentCountMap = new HashMap();
RepositoryManager oleRepositoryManager = RepositoryManager.getRepositoryManager();
Node node = oleRepositoryManager.getSession().getRootNode();
getDocumentCount(node, documentCountMap);
return documentCountMap;
}
private void getDocumentCount(Node node, Map documentCountMap) throws RepositoryException {
if (node.getName().equals("jcr:system")) {
return;
}
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
if (property.getName().equals("jcr:primaryType") && property.getString().equalsIgnoreCase("olefile")) {
String formatKey = property.getPath().substring(1, property.getPath().lastIndexOf("/"));
formatKey = formatKey.substring(0, formatKey.lastIndexOf("/"));
if (formatKey.contains("[")) {
formatKey = formatKey.substring(0, formatKey.lastIndexOf("["));
}
if (formatKey != null) {
if (documentCountMap.containsKey(formatKey)) {
int count = documentCountMap.get(formatKey);
documentCountMap.put(formatKey, ++count);
}
else {
documentCountMap.put(formatKey, 1);
}
}
}
}
NodeIterator nodes = node.getNodes();
while (nodes.hasNext()) {
getDocumentCount(nodes.nextNode(), documentCountMap);
} `
}*/
public String getRepositoryDump() throws OleException, RepositoryException {
StringBuffer repositoryDump = new StringBuffer();
RepositoryManager oleRepositoryManager = RepositoryManager.getRepositoryManager();
Session session = oleRepositoryManager.getSession("repositoryBrowser", "getRepositoryDump");
repositoryDump(session.getRootNode(), repositoryDump);
oleRepositoryManager.logout(session);
return repositoryDump.toString();
}
private void repositoryDump(Node node, StringBuffer repositoryDump) throws RepositoryException {
repositoryDump.append(node.getPath() + "\n");
if (node.getName().equals("jcr:system")) {
return;
}
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
if (property.getDefinition().isMultiple()) {
Value[] values = property.getValues();
for (int i = 0; i < values.length; i++) {
repositoryDump.append(property.getPath() + " = " + values[i].getString() + "\n");
}
} else {
repositoryDump.append(property.getPath() + " = " + property.getString() + "\n");
}
}
NodeIterator nodes = node.getNodes();
while (nodes.hasNext()) {
repositoryDump(nodes.nextNode(), repositoryDump);
}
}
public String getRepositoryRangeDump(String category, String type, String format, int fromIndex, int count)
throws OleException, RepositoryException {
StringBuffer repositoryDump = new StringBuffer();
RepositoryManager oleRepositoryManager = RepositoryManager.getRepositoryManager();
Node rootnode = oleRepositoryManager.getSession("repositoryBrowser", "getRepositoryRangeDump").getRootNode();
repositoryRangeDump(rootnode, repositoryDump, category, type, format, fromIndex, count);
return repositoryDump.toString();
}
private void repositoryRangeDump(Node rootnode, StringBuffer repositoryDump, String category, String type,
String format, int fromIndex, int count) throws RepositoryException {
int tempCount = 0;
Node categoryNode = rootnode.getNode(category);
Node typeNode = categoryNode.getNode(type);
Node formatNode = typeNode.getNode(format);
repositoryDump.append(categoryNode.toString().replace("node ", "") + "\n");
getNodeDump(categoryNode, repositoryDump);
repositoryDump.append(typeNode.toString().replace("node ", "") + "\n");
getNodeDump(typeNode, repositoryDump);
repositoryDump.append(formatNode.toString().replace("node ", "") + "\n");
getNodeDump(formatNode, repositoryDump);
for (Iterator formatiterator = formatNode.getNodes(); formatiterator.hasNext(); ) {
Node fileNode = formatiterator.next();
PropertyIterator properties = fileNode.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
if (property.getName().equals("jcr:primaryType") && property.getString().equalsIgnoreCase(
ProcessParameters.FILE_OLE)) {
tempCount++;
if (tempCount >= fromIndex && tempCount < fromIndex + count) {
repositoryDump.append(fileNode.toString().replace("node ", "") + "\n");
getNodeDump(fileNode, repositoryDump);
NodeIterator nodes = fileNode.getNodes();
while (nodes.hasNext()) {
getNodeDump(nodes.nextNode(), repositoryDump);
}
}
}
}
}
}
public void getNodeDump(Node node, StringBuffer repositoryDump) throws RepositoryException {
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
if (property.getDefinition().isMultiple()) {
Value[] values = property.getValues();
for (int i = 0; i < values.length; i++) {
repositoryDump.append(property.getPath() + " = " + values[i].getString() + "\n");
}
} else {
repositoryDump.append(property.getPath() + " = " + property.getString() + "\n");
}
}
}
public List browseDataSetup() {
List oleDocStoreDatas = new ArrayList();
InputStream in = RepositoryBrowser.class.getResourceAsStream("docstore-category.properties");
Properties categories = getPropertyValues(in);
Set> entries = categories.entrySet();
Map.Entry