001: package org.libresource.kernel.comparator.util;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.net.URI;
006: import java.util.ArrayList;
007: import java.util.Collections;
008: import java.util.HashMap;
009:
010: import javax.naming.NamingException;
011:
012: import org.libresource.LibresourceResourceValue;
013: import org.libresource.kernel.comparator.FactoryComparator;
014: import org.libresource.kernel.interfaces.NodeLocal;
015: import org.libresource.kernel.util.NodeUtil;
016:
017: public class TreeNode {
018: private LibresourceResourceValue value;
019: private ArrayList<TreeNode> children;
020:
021: public TreeNode(LibresourceResourceValue value) {
022: this .value = value;
023: children = new ArrayList<TreeNode>();
024: }
025:
026: public void addChild(LibresourceResourceValue child) {
027: children.add(new TreeNode(child));
028: }
029:
030: public void addChild(TreeNode child) {
031: children.add(child);
032: }
033:
034: public LibresourceResourceValue getValue() {
035: return value;
036: }
037:
038: public ArrayList<TreeNode> getChildren() {
039: return children;
040: }
041:
042: public void print(Writer writer, String groupBy, String sort,
043: boolean showACL) throws IOException {
044: // Print node
045: writer.write("<li><a href=\"");
046: writer.write(value.getUri().getPath().substring(1));
047: writer.write("\">");
048: writer.write(value.getShortResourceName());
049: writer.write("</a>");
050: if (showACL) {
051: writer.write("<table border=\"1\">");
052: try {
053: NodeLocal uriNode = NodeUtil.getLocalHome().getByPath(
054: value.getUri().getPath());
055: HashMap<URI, String[]> acls = (HashMap<URI, String[]>) uriNode
056: .getAcls();
057: for (URI owner : acls.keySet()) {
058: writer.write("<tr>");
059: writer.write("<td valign=\"center\"><b>");
060: writer.write(owner.getPath());
061: writer.write("</b></td>");
062: writer.write("<td>");
063: for (String right : acls.get(owner)) {
064: writer.write(right);
065: writer.write("<br/>");
066: }
067: writer.write("</td>");
068: writer.write("</tr>");
069: }
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073: writer.write("</table>");
074: }
075: writer.write("</li>");
076: if (children.size() > 0) {
077: // Manage children
078: writer.write("<ul>");
079: // Sort before
080: Collections.sort(children, FactoryComparator.getComparator(
081: groupBy, sort));
082: // local var for groupby
083: String currentGroupBy = null;
084: for (TreeNode currentNode : children) {
085: if (groupBy != null
086: && (currentGroupBy == null || !currentGroupBy
087: .equals(ListStructure
088: .getInfo(
089: currentNode.getValue(),
090: groupBy)))) {
091: if (currentGroupBy != null) {
092: writer.write("</ul>");
093: }
094: currentGroupBy = ListStructure.getInfo(currentNode
095: .getValue(), groupBy);
096: writer.write("<li><b>");
097: writer.write(currentGroupBy);
098: writer.write("</b></li><ul>");
099: }
100: currentNode.print(writer, groupBy, sort, showACL);
101: }
102: if (groupBy != null) {
103: writer.write("</ul>");
104: }
105: writer.write("</ul>");
106: }
107: }
108: }
|