KickJava   Java API By Example, From Geeks To Geeks.

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


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.navigation.NavigationException;
19 import org.outerj.daisy.navigation.NavigationVersionMode;
20 import org.outerj.daisy.repository.*;
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.*;
26
27 public class DocumentNode extends AbstractParentNode implements DocumentRepresentingNode {
28     private final VariantKey variantKey;
29     private String JavaDoc nodeId;
30     private String JavaDoc branch;
31     private String JavaDoc language;
32     private NavigationVersionMode versionMode;
33     private final NodeVisibility nodeVisibility;
34     private boolean docInfoFetched = false;
35     private String JavaDoc label;
36     private boolean dontShow;
37     private CommonNavigationManager.Context context;
38
39     /**
40      *
41      * @param label can be null, in which case the name of the document will serve as label
42      */

43     public DocumentNode(VariantKey variantKey, String JavaDoc nodeId, String JavaDoc label, NodeVisibility nodeVisibility,
44             CommonNavigationManager.Context context,
45             long navigationBranchId, long navigationLanguageId, NavigationVersionMode versionMode) throws RepositoryException {
46         this.variantKey = variantKey;
47         if (nodeId != null)
48             this.nodeId = NavigationUtil.makeNodeIdValid(nodeId);
49         if (this.nodeId == null)
50             this.nodeId = String.valueOf(variantKey.getDocumentId());
51         this.label = label;
52         this.nodeVisibility = nodeVisibility;
53         this.context = context;
54         branch = variantKey.getBranchId() != navigationBranchId ? context.getBranchName(variantKey.getBranchId()) : null;
55         language = variantKey.getLanguageId() != navigationLanguageId ? context.getLanguageName(variantKey.getLanguageId()) : null;
56         this.versionMode = versionMode;
57     }
58
59     public boolean checkId(String JavaDoc id, long branchId, long languageId) {
60         return nodeId.equals(id) && variantKey.getBranchId() == branchId && variantKey.getLanguageId() == languageId;
61     }
62
63     public boolean isExpandable() {
64         return false;
65     }
66
67     public List searchDocument(VariantKey key) throws RepositoryException {
68         if (this.variantKey.equals(key)) {
69             List foundNodePath = new ArrayList(5);
70             foundNodePath.add(this);
71             return foundNodePath;
72         } else {
73             return super.searchDocument(key);
74         }
75     }
76
77     public void populateNodeLookupMap(Map map, String JavaDoc path) throws RepositoryException {
78         loadDocInfo();
79         if (dontShow)
80             return;
81
82         path = path + "/" + nodeId;
83         if (!map.containsKey(variantKey)) {
84             map.put(variantKey, path);
85         }
86         super.populateNodeLookupMap(map, path);
87     }
88
89     public void generateXml(ContentHandler JavaDoc contentHandler, int depth, String JavaDoc path, long userId, long[] roleIds) throws RepositoryException, SAXException JavaDoc {
90         if (!isVisible(userId, roleIds, null, -1))
91             return;
92
93         path = path + "/" + nodeId;
94         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
95         attrs.addAttribute("", "id", "id", "CDATA", nodeId);
96         attrs.addAttribute("", "documentId", "documentId", "CDATA", String.valueOf(variantKey.getDocumentId()));
97         attrs.addAttribute("", "branchId", "branchId", "CDATA", String.valueOf(variantKey.getBranchId()));
98         attrs.addAttribute("", "languageId", "languageId", "CDATA", String.valueOf(variantKey.getLanguageId()));
99         if (branch != null)
100             attrs.addAttribute("", "branch", "branch", "CDATA", branch);
101         if (language != null)
102             attrs.addAttribute("", "language", "language", "CDATA", language);
103         attrs.addAttribute("", "label", "label", "CDATA", label);
104         attrs.addAttribute("", "path", "path", "CDATA", path);
105         List children = getExpandedChildList();
106         attrs.addAttribute("", "hasChildren", "hasChildren", "CATA", String.valueOf(hasVisibleChildren(children, userId, roleIds)));
107         contentHandler.startElement(NAVIGATION_NS, "doc", "doc", attrs);
108
109         if (depth != -1 && depth - 1 > 0)
110             super.generateXml(children, contentHandler, depth - 1, path, userId, roleIds);
111         else if (depth == -1)
112             super.generateXml(children, contentHandler, -1, path, userId, roleIds);
113
114         contentHandler.endElement(NAVIGATION_NS, "doc", "doc");
115     }
116
117     public void generateXml(ContentHandler JavaDoc contentHandler, Node[] activeNodePath, int pos, boolean includeOnlyActivePath,
118             String JavaDoc path, long userId, long[] roleIds) throws RepositoryException, SAXException JavaDoc {
119         if (!isVisible(userId, roleIds, activeNodePath, pos))
120             return;
121         path = path + "/" + nodeId;
122         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
123         attrs.addAttribute("", "id", "id", "CDATA", nodeId);
124         attrs.addAttribute("", "documentId", "documentId", "CDATA", String.valueOf(variantKey.getDocumentId()));
125         attrs.addAttribute("", "branchId", "branchId", "CDATA", String.valueOf(variantKey.getBranchId()));
126         attrs.addAttribute("", "languageId", "languageId", "CDATA", String.valueOf(variantKey.getLanguageId()));
127         if (branch != null)
128             attrs.addAttribute("", "branch", "branch", "CDATA", branch);
129         if (language != null)
130             attrs.addAttribute("", "language", "language", "CDATA", language);
131         attrs.addAttribute("", "label", "label", "CDATA", label);
132         attrs.addAttribute("", "path", "path", "CDATA", path);
133         if (pos < activeNodePath.length && activeNodePath[pos] == this)
134             attrs.addAttribute("", "selected", "selected", "CDATA", "true");
135         if (pos == activeNodePath.length -1 && activeNodePath[pos] == this)
136             attrs.addAttribute("", "active", "active", "CDATA", "true");
137         List children = getExpandedChildList();
138         attrs.addAttribute("", "hasChildren", "hasChildren", "CATA", String.valueOf(hasVisibleChildren(children, userId, roleIds)));
139         contentHandler.startElement(NAVIGATION_NS, "doc", "doc", attrs);
140
141         if ((includeOnlyActivePath && pos < activeNodePath.length && activeNodePath[pos] == this) || (!includeOnlyActivePath))
142             super.generateXml(children, contentHandler, activeNodePath, pos + 1, includeOnlyActivePath, path, userId, roleIds);
143
144         contentHandler.endElement(NAVIGATION_NS, "doc", "doc");
145     }
146
147     public boolean isVisible(long userId, long[] roleIds, Node[] activeNodePath, int activeNodePathPos) throws RepositoryException {
148         loadDocInfo();
149         if (nodeVisibility == NodeVisibility.HIDDEN) {
150             return false;
151         } else if (nodeVisibility == NodeVisibility.WHEN_ACTIVE) {
152             // Note: we can assume the node will only be in the active node path when read-access is OK
153
return activeNodePath != null && activeNodePathPos < activeNodePath.length && activeNodePath[activeNodePathPos] == this;
154         } else if (dontShow) {
155             return false;
156         } else {
157             if (versionMode == NavigationVersionMode.LIVE)
158                 return context.canReadLive(variantKey, userId, roleIds);
159             else
160                 return context.canRead(variantKey, userId, roleIds);
161         }
162     }
163
164     private synchronized void loadDocInfo() throws NavigationException {
165         if (!docInfoFetched) {
166             try {
167                 Document document = context.getRepository().getDocument(variantKey, false);
168
169                 dontShow = document.isRetired();
170                 if (!dontShow) {
171                     Version version;
172                     if (versionMode == NavigationVersionMode.LIVE)
173                         version = document.getLiveVersion();
174                     else if (versionMode == NavigationVersionMode.LAST)
175                         version = document.getLastVersion();
176                     else
177                         throw new NavigationException("Unexepected NavigationVersionMode: " + versionMode);
178
179                     if (version == null) {
180                         dontShow = true;
181                     } else {
182                         if (this.label == null)
183                             label = version.getDocumentName();
184                     }
185                 }
186                 docInfoFetched = true;
187             } catch (Exception JavaDoc e) {
188                 if (e instanceof DocumentNotFoundException || e instanceof DocumentVariantNotFoundException) {
189                     dontShow = true;
190                     docInfoFetched = true;
191                     context.getLogger().warn("A navigation tree references the non-existing " + variantKey);
192                 } else {
193                     throw new NavigationException("Error retrieving info for " + variantKey, e);
194                 }
195             }
196         }
197     }
198
199     public boolean isIdentifiable() {
200         return true;
201     }
202
203     public Document getDocument() throws RepositoryException {
204         return context.getRepository().getDocument(variantKey, false);
205     }
206
207     public VariantKey getVariantKey() {
208         return variantKey;
209     }
210
211     public String JavaDoc getId() {
212         return nodeId;
213     }
214
215     private boolean hasVisibleChildren(List children, long userId, long[] roleIds) throws RepositoryException {
216         Iterator childrenIt = children.iterator();
217         while (childrenIt.hasNext()) {
218             Node child = (Node)childrenIt.next();
219             if (child.isVisible(userId, roleIds, null, -1))
220                 return true;
221         }
222         return false;
223     }
224 }
225
Popular Tags