001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.site.tree2;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.lenya.cms.publication.Area;
028: import org.apache.lenya.cms.publication.Document;
029: import org.apache.lenya.cms.publication.Publication;
030: import org.apache.lenya.cms.repository.Node;
031: import org.apache.lenya.cms.repository.NodeFactory;
032: import org.apache.lenya.cms.repository.RepositoryException;
033: import org.apache.lenya.cms.repository.Session;
034: import org.apache.lenya.cms.site.Link;
035: import org.apache.lenya.cms.site.SiteException;
036: import org.apache.lenya.cms.site.SiteNode;
037: import org.apache.lenya.cms.site.SiteStructure;
038: import org.apache.lenya.cms.site.tree.SiteTree;
039:
040: /**
041: * Site tree implementation which delegates all operations to a shared site tree.
042: */
043: public class DelegatingSiteTree implements SiteStructure, SiteTree {
044:
045: private SiteTreeImpl tree;
046: private Area area;
047: private ServiceManager manager;
048: private Map links = new HashMap();
049: private Map nodes = new HashMap();
050: private List nodeList;
051: private List topLevelNodes;
052: private List preOrder;
053: private String sourceUri;
054:
055: /**
056: * @param manager The service manager.
057: * @param area The area which this tree belongs to.
058: * @param tree The tree to delegate to.
059: */
060: public DelegatingSiteTree(ServiceManager manager, Area area,
061: SiteTreeImpl tree) {
062: this .tree = tree;
063: this .area = area;
064: this .manager = manager;
065: }
066:
067: public Link add(String path, Document doc) throws SiteException {
068: throw new UnsupportedOperationException();
069: }
070:
071: public SiteNode add(String path) throws SiteException {
072: throw new UnsupportedOperationException();
073: }
074:
075: public SiteNode add(String path, String followingSiblingPath)
076: throws SiteException {
077: throw new UnsupportedOperationException();
078: }
079:
080: public boolean contains(String path) {
081: return this .tree.contains(path);
082: }
083:
084: public boolean contains(String path, String language) {
085: return this .tree.contains(path, language);
086: }
087:
088: public boolean containsByUuid(String uuid, String language) {
089: return this .tree.containsByUuid(uuid, language);
090: }
091:
092: public boolean containsInAnyLanguage(String uuid) {
093: return this .tree.containsInAnyLanguage(uuid);
094: }
095:
096: public String getArea() {
097: return this .area.getName();
098: }
099:
100: public Link getByUuid(String uuid, String language)
101: throws SiteException {
102: Link delegate = this .tree.getByUuid(uuid, language);
103: return getLink(delegate);
104: }
105:
106: protected Link getLink(Link delegate) {
107: Link link = (Link) this .links.get(delegate);
108: if (link == null) {
109: link = new DelegatingLink(this .area.getPublication()
110: .getFactory(), getNode(delegate.getNode()),
111: delegate.getLabel(), delegate.getLanguage());
112: }
113: return link;
114: }
115:
116: protected DelegatingNode getNode(SiteNode delegate) {
117: DelegatingNode node = (DelegatingNode) this .nodes.get(delegate);
118: if (node == null) {
119: node = new DelegatingNode(this , delegate);
120: this .nodes.put(delegate, node);
121: }
122: return node;
123: }
124:
125: public SiteNode getNode(String path) throws SiteException {
126: return getNode(this .tree.getNode(path));
127: }
128:
129: public SiteNode[] getNodes() {
130: if (this .nodeList == null) {
131: SiteNode[] delegates = this .tree.getNodes();
132: this .nodeList = new ArrayList();
133: for (int i = 0; i < delegates.length; i++) {
134: this .nodeList.add(getNode(delegates[i]));
135: }
136: }
137: return (SiteNode[]) this .nodeList
138: .toArray(new SiteNode[this .nodeList.size()]);
139: }
140:
141: public Publication getPublication() {
142: return this .area.getPublication();
143: }
144:
145: public SiteNode[] getTopLevelNodes() {
146: if (this .topLevelNodes == null) {
147: SiteNode[] delegates = this .tree.getTopLevelNodes();
148: this .topLevelNodes = new ArrayList();
149: for (int i = 0; i < delegates.length; i++) {
150: this .topLevelNodes.add(getNode(delegates[i]));
151: }
152: }
153: return (SiteNode[]) this .topLevelNodes
154: .toArray(new SiteNode[this .topLevelNodes.size()]);
155: }
156:
157: public Session getSession() {
158: return this .area.getPublication().getSession();
159: }
160:
161: private NodeFactory nodeFactory;
162:
163: protected NodeFactory getNodeFactory() {
164: if (this .nodeFactory == null) {
165: try {
166: this .nodeFactory = (NodeFactory) this .manager
167: .lookup(NodeFactory.ROLE);
168: } catch (ServiceException e) {
169: throw new RuntimeException(
170: "Creating repository node failed: ", e);
171: }
172: }
173: return this .nodeFactory;
174: }
175:
176: public Node getRepositoryNode() {
177: try {
178: return (Node) getSession().getRepositoryItem(
179: getNodeFactory(), getSourceUri());
180: } catch (RepositoryException e) {
181: throw new RuntimeException(
182: "Creating repository node failed: ", e);
183: }
184: }
185:
186: protected String getSourceUri() {
187: if (this .sourceUri == null) {
188: String baseUri = this .area.getPublication().getContentURI(
189: this .area.getName());
190: this .sourceUri = baseUri + "/sitetree.xml";
191: }
192: return this .sourceUri;
193: }
194:
195: public void moveDown(String path) throws SiteException {
196: throw new UnsupportedOperationException();
197: }
198:
199: public void moveUp(String path) throws SiteException {
200: throw new UnsupportedOperationException();
201: }
202:
203: public SiteNode[] preOrder() {
204: if (this .preOrder == null) {
205: SiteNode[] delegates = this .tree.preOrder();
206: this .preOrder = new ArrayList();
207: for (int i = 0; i < delegates.length; i++) {
208: this .preOrder.add(getNode(delegates[i]));
209: }
210: }
211: return (SiteNode[]) this .preOrder
212: .toArray(new SiteNode[this .preOrder.size()]);
213: }
214:
215: public void save() throws RepositoryException {
216: throw new UnsupportedOperationException();
217: }
218:
219: }
|