KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > navigation > impl > DocumentFromQueryNode


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.helpers.AttributesImpl JavaDoc;
24
25 import java.util.List JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * A Node representing a Document as result from a Query.
32  */

33 public class DocumentFromQueryNode implements Node, DocumentRepresentingNode {
34     private final VariantKey variantKey;
35     private final String JavaDoc idAsString;
36     private final String JavaDoc branch;
37     private final String JavaDoc language;
38     private final String JavaDoc label;
39     private final NodeVisibility nodeVisibility;
40     private final CommonNavigationManager.Context context;
41     private final NavigationVersionMode versionMode;
42
43     public DocumentFromQueryNode(VariantKey variantKey, String JavaDoc 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 JavaDoc[] path, int pos, long branchId, long languageId, Node[] foundPath) throws RepositoryException {
61         // empty on purpose
62
}
63
64     public List JavaDoc searchDocument(VariantKey key) throws RepositoryException {
65         if (this.variantKey.equals(key)) {
66             List JavaDoc foundNodePath = new ArrayList JavaDoc(5);
67             foundNodePath.add(this);
68             return foundNodePath;
69         } else {
70             return null;
71         }
72     }
73
74     public void populateNodeLookupMap(Map JavaDoc map, String JavaDoc path) throws RepositoryException {
75         if (!map.containsKey(variantKey)) {
76             map.put(variantKey, path + "/" + idAsString);
77         }
78     }
79
80     public boolean checkId(String JavaDoc 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 JavaDoc getExpandedChildList() throws RepositoryException {
89         return Collections.EMPTY_LIST;
90     }
91
92     public void generateXml(ContentHandler JavaDoc contentHandler, Node[] activeNodePath, int pos, boolean includeOnlyActivePath,
93             String JavaDoc path, long userId, long[] roleIds) throws RepositoryException, SAXException JavaDoc {
94         if (!isVisible(userId, roleIds, activeNodePath, pos))
95             return;
96         path = path + "/" + idAsString;
97         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc contentHandler, int depth, String JavaDoc path, long userId, long[] roleIds) throws RepositoryException, SAXException JavaDoc {
117         if (!isVisible(userId, roleIds, null, -1))
118             return;
119         path = path + "/" + idAsString;
120         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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             // Note: we can assume the node will only be in the active node path when read-access is OK
140
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 JavaDoc getId() {
154         return idAsString;
155     }
156 }
157
Popular Tags