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:
019: package org.apache.lenya.cms.cocoon.components.modules.input;
020:
021: import java.util.Arrays;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.service.ServiceSelector;
028: import org.apache.cocoon.environment.ObjectModelHelper;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.lenya.cms.publication.DocumentFactory;
031: import org.apache.lenya.cms.publication.DocumentUtil;
032: import org.apache.lenya.cms.publication.PageEnvelope;
033: import org.apache.lenya.cms.publication.Publication;
034: import org.apache.lenya.cms.repository.RepositoryUtil;
035: import org.apache.lenya.cms.repository.Session;
036: import org.apache.lenya.cms.site.SiteManager;
037: import org.apache.lenya.cms.site.tree.SiteTree;
038: import org.apache.lenya.cms.site.tree.TreeSiteManager;
039:
040: /**
041: * Module for sitetree access.
042: *
043: * @version $Id: SitetreeModule.java 159584 2005-03-31 12:49:41Z andreas $
044: */
045: public class SitetreeModule extends AbstractPageEnvelopeModule {
046:
047: /**
048: * <code>AUTHORING_NODE</code> The authoring node
049: */
050: public static final String AUTHORING_NODE = "authoring-node";
051: /**
052: * <code>LIVE_NODE</code> The live node
053: */
054: public static final String LIVE_NODE = "live-node";
055: /**
056: * <code>TRASH_NODE</code> The trash node
057: */
058: public static final String TRASH_NODE = "trash-node";
059: /**
060: * <code>ARCHIVE_NODE</code> The archive node
061: */
062: public static final String ARCHIVE_NODE = "archive-node";
063:
064: protected static final String[] PARAMETER_NAMES = { AUTHORING_NODE,
065: LIVE_NODE, TRASH_NODE, ARCHIVE_NODE };
066:
067: /**
068: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
069: * org.apache.avalon.framework.configuration.Configuration,
070: * java.util.Map)
071: */
072: public Object getAttribute(String name, Configuration modeConf,
073: Map objectModel) throws ConfigurationException {
074:
075: Object value = null;
076: ServiceSelector selector = null;
077: TreeSiteManager _manager = null;
078:
079: try {
080: PageEnvelope envelope = getEnvelope(objectModel, name);
081: Publication publication = envelope.getPublication();
082:
083: selector = (ServiceSelector) this .manager
084: .lookup(SiteManager.ROLE + "Selector");
085: _manager = (TreeSiteManager) selector.select(publication
086: .getSiteManagerHint());
087:
088: Request request = ObjectModelHelper.getRequest(objectModel);
089: Session session = RepositoryUtil.getSession(this .manager,
090: request);
091: DocumentFactory map = DocumentUtil.createDocumentFactory(
092: this .manager, session);
093:
094: if (name.equals(AUTHORING_NODE)) {
095: SiteTree authoringTree = _manager.getTree(map,
096: publication, Publication.AUTHORING_AREA);
097: value = authoringTree.getNode(envelope.getDocument()
098: .getPath());
099: }
100:
101: if (name.equals(LIVE_NODE)) {
102: SiteTree liveTree = _manager.getTree(map, publication,
103: Publication.LIVE_AREA);
104: value = liveTree.getNode(envelope.getDocument()
105: .getPath());
106: }
107:
108: if (name.equals(TRASH_NODE)) {
109: SiteTree trashTree = _manager.getTree(map, publication,
110: Publication.TRASH_AREA);
111: value = trashTree.getNode(envelope.getDocument()
112: .getPath());
113: }
114:
115: if (name.equals(ARCHIVE_NODE)) {
116: SiteTree archiveTree = _manager.getTree(map,
117: publication, Publication.ARCHIVE_AREA);
118: value = archiveTree.getNode(envelope.getDocument()
119: .getPath());
120: }
121: } catch (Exception e) {
122: throw new ConfigurationException("Obtaining value for ["
123: + name + "] failed: ", e);
124: } finally {
125: if (selector != null) {
126: if (_manager != null) {
127: selector.release(_manager);
128: }
129: this .manager.release(selector);
130: }
131: }
132:
133: return value;
134: }
135:
136: /**
137: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
138: * java.util.Map)
139: */
140: public Iterator getAttributeNames(Configuration modeConf,
141: Map objectModel) throws ConfigurationException {
142: return Arrays.asList(PARAMETER_NAMES).iterator();
143: }
144:
145: /**
146: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
147: * org.apache.avalon.framework.configuration.Configuration,
148: * java.util.Map)
149: */
150: public Object[] getAttributeValues(String name,
151: Configuration modeConf, Map objectModel)
152: throws ConfigurationException {
153: Object[] objects = { getAttribute(name, modeConf, objectModel) };
154: return objects;
155: }
156:
157: }
|