1 16 package org.outerj.daisy.navigation.impl; 17 18 import org.outerj.daisy.repository.VariantKey; 19 import org.outerj.daisy.repository.RepositoryException; 20 import org.outerj.daisy.navigation.NavigationVersionMode; 21 import org.xml.sax.ContentHandler ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.helpers.AttributesImpl ; 24 25 import java.util.List ; 26 import java.util.Collections ; 27 import java.util.Map ; 28 import java.util.ArrayList ; 29 30 33 public class DocumentFromQueryNode implements Node, DocumentRepresentingNode { 34 private final VariantKey variantKey; 35 private final String idAsString; 36 private final String branch; 37 private final String language; 38 private final String label; 39 private final NodeVisibility nodeVisibility; 40 private final CommonNavigationManager.Context context; 41 private final NavigationVersionMode versionMode; 42 43 public DocumentFromQueryNode(VariantKey variantKey, String label, NodeVisibility nodeVisibility, 44 CommonNavigationManager.Context context, 45 long navigationBranchId, long navigationLanguageId, NavigationVersionMode versionMode) throws RepositoryException { 46 this.variantKey = variantKey; 47 this.idAsString = String.valueOf(variantKey.getDocumentId()); 48 this.label = label; 49 this.nodeVisibility = nodeVisibility; 50 this.context = context; 51 branch = variantKey.getBranchId() != navigationBranchId ? context.getBranchName(variantKey.getBranchId()) : null; 52 language = variantKey.getLanguageId() != navigationLanguageId ? context.getLanguageName(variantKey.getLanguageId()) : null; 53 this.versionMode = versionMode; 54 } 55 56 public VariantKey getVariantKey() { 57 return variantKey; 58 } 59 60 public void searchPath(String [] path, int pos, long branchId, long languageId, Node[] foundPath) throws RepositoryException { 61 } 63 64 public List searchDocument(VariantKey key) throws RepositoryException { 65 if (this.variantKey.equals(key)) { 66 List foundNodePath = new ArrayList (5); 67 foundNodePath.add(this); 68 return foundNodePath; 69 } else { 70 return null; 71 } 72 } 73 74 public void populateNodeLookupMap(Map map, String path) throws RepositoryException { 75 if (!map.containsKey(variantKey)) { 76 map.put(variantKey, path + "/" + idAsString); 77 } 78 } 79 80 public boolean checkId(String id, long branchId, long languageId) { 81 return idAsString.equals(id) && variantKey.getBranchId() == branchId && variantKey.getLanguageId() == languageId; 82 } 83 84 public boolean isExpandable() throws RepositoryException { 85 return false; 86 } 87 88 public List getExpandedChildList() throws RepositoryException { 89 return Collections.EMPTY_LIST; 90 } 91 92 public void generateXml(ContentHandler contentHandler, Node[] activeNodePath, int pos, boolean includeOnlyActivePath, 93 String path, long userId, long[] roleIds) throws RepositoryException, SAXException { 94 if (!isVisible(userId, roleIds, activeNodePath, pos)) 95 return; 96 path = path + "/" + idAsString; 97 AttributesImpl attrs = new AttributesImpl (); 98 attrs.addAttribute("", "id", "id", "CDATA", idAsString); 99 attrs.addAttribute("", "documentId", "documentId", "CDATA", String.valueOf(variantKey.getDocumentId())); 100 attrs.addAttribute("", "branchId", "branchId", "CDATA", String.valueOf(variantKey.getBranchId())); 101 attrs.addAttribute("", "languageId", "languageId", "CDATA", String.valueOf(variantKey.getLanguageId())); 102 if (branch != null) 103 attrs.addAttribute("", "branch", "branch", "CDATA", branch); 104 if (language != null) 105 attrs.addAttribute("", "language", "language", "CDATA", language); 106 attrs.addAttribute("", "label", "label", "CDATA", label); 107 attrs.addAttribute("", "path", "path", "CDATA", path); 108 if (pos < activeNodePath.length && activeNodePath[pos] == this) 109 attrs.addAttribute("", "selected", "selected", "CDATA", "true"); 110 if (pos == activeNodePath.length -1 && activeNodePath[pos] == this) 111 attrs.addAttribute("", "active", "active", "CDATA", "true"); 112 contentHandler.startElement(NAVIGATION_NS, "doc", "doc", attrs); 113 contentHandler.endElement(NAVIGATION_NS, "doc", "doc"); 114 } 115 116 public void generateXml(ContentHandler contentHandler, int depth, String path, long userId, long[] roleIds) throws RepositoryException, SAXException { 117 if (!isVisible(userId, roleIds, null, -1)) 118 return; 119 path = path + "/" + idAsString; 120 AttributesImpl attrs = new AttributesImpl (); 121 attrs.addAttribute("", "id", "id", "CDATA", idAsString); 122 attrs.addAttribute("", "documentId", "documentId", "CDATA", String.valueOf(variantKey.getDocumentId())); 123 attrs.addAttribute("", "branchId", "branchId", "CDATA", String.valueOf(variantKey.getBranchId())); 124 attrs.addAttribute("", "languageId", "languageId", "CDATA", String.valueOf(variantKey.getLanguageId())); 125 if (branch != null) 126 attrs.addAttribute("", "branch", "branch", "CDATA", branch); 127 if (language != null) 128 attrs.addAttribute("", "language", "language", "CDATA", language); 129 attrs.addAttribute("", "label", "label", "CDATA", label); 130 attrs.addAttribute("", "path", "path", "CDATA", path); 131 contentHandler.startElement(NAVIGATION_NS, "doc", "doc", attrs); 132 contentHandler.endElement(NAVIGATION_NS, "doc", "doc"); 133 } 134 135 public boolean isVisible(long userId, long[] roleIds, Node[] activeNodePath, int activeNodePathPos) throws RepositoryException { 136 if (nodeVisibility == NodeVisibility.HIDDEN) { 137 return false; 138 } else if (nodeVisibility == NodeVisibility.WHEN_ACTIVE) { 139 return activeNodePath != null && activeNodePathPos < activeNodePath.length && activeNodePath[activeNodePathPos] == this; 141 } else { 142 if (versionMode == NavigationVersionMode.LIVE) 143 return context.canReadLive(variantKey, userId, roleIds); 144 else 145 return context.canRead(variantKey, userId, roleIds); 146 } 147 } 148 149 public boolean isIdentifiable() { 150 return true; 151 } 152 153 public String getId() { 154 return idAsString; 155 } 156 } 157 | Popular Tags |