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.publication;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.apache.avalon.framework.service.ServiceException;
024: import org.apache.avalon.framework.service.ServiceManager;
025: import org.apache.avalon.framework.service.ServiceSelector;
026: import org.apache.lenya.cms.repository.Node;
027: import org.apache.lenya.cms.repository.NodeFactory;
028: import org.apache.lenya.cms.repository.RepositoryException;
029: import org.apache.lenya.cms.site.SiteException;
030: import org.apache.lenya.cms.site.SiteManager;
031: import org.apache.lenya.cms.site.SiteNode;
032: import org.apache.lenya.cms.site.SiteStructure;
033:
034: /**
035: * Area implementation.
036: */
037: public class AreaImpl implements Area {
038:
039: private String name;
040: private Publication pub;
041: private DocumentFactory factory;
042: private NodeFactory nodeFactory;
043: private ServiceManager manager;
044:
045: /**
046: * @param manager The service manager.
047: * @param factory The factory.
048: * @param pub The publication.
049: * @param name The area name.
050: */
051: public AreaImpl(ServiceManager manager, DocumentFactory factory,
052: Publication pub, String name) {
053: this .manager = manager;
054: this .factory = factory;
055: this .pub = pub;
056: this .name = name;
057: }
058:
059: public boolean contains(String uuid, String language) {
060: // check site structure first (performance)
061: if (getSite().containsByUuid(uuid, language)) {
062: return true;
063: } else {
064: String sourceUri = DocumentImpl.getSourceURI(pub, name,
065: uuid, language);
066: try {
067: Node node = (Node) getPublication().getSession()
068: .getRepositoryItem(getNodeFactory(), sourceUri);
069: return node.exists();
070: } catch (RepositoryException e) {
071: throw new RuntimeException(e);
072: }
073: }
074: }
075:
076: protected NodeFactory getNodeFactory() {
077: if (this .nodeFactory == null) {
078: try {
079: this .nodeFactory = (NodeFactory) this .manager
080: .lookup(NodeFactory.ROLE);
081: } catch (ServiceException e) {
082: throw new RuntimeException(e);
083: }
084: }
085: return this .nodeFactory;
086: }
087:
088: public Document getDocument(String uuid, String language)
089: throws PublicationException {
090: return this .factory.get(getPublication(), getName(), uuid,
091: language);
092: }
093:
094: public String getName() {
095: return this .name;
096: }
097:
098: public Publication getPublication() {
099: return this .pub;
100: }
101:
102: private SiteStructure site;
103:
104: public SiteStructure getSite() {
105: if (this .site == null) {
106: SiteManager siteManager = null;
107: ServiceSelector selector = null;
108: try {
109: selector = (ServiceSelector) this .manager
110: .lookup(SiteManager.ROLE + "Selector");
111: siteManager = (SiteManager) selector
112: .select(getPublication().getSiteManagerHint());
113: this .site = siteManager.getSiteStructure(this .factory,
114: getPublication(), getName());
115: } catch (Exception e) {
116: throw new RuntimeException(e);
117: } finally {
118: if (selector != null) {
119: if (siteManager != null) {
120: selector.release(siteManager);
121: }
122: this .manager.release(selector);
123: }
124: }
125: }
126: return this .site;
127: }
128:
129: public String toString() {
130: return getPublication().getId() + ":" + getName();
131: }
132:
133: public Document[] getDocuments() {
134: SiteNode[] nodes = getSite().getNodes();
135: List docs = new ArrayList();
136: for (int i = 0; i < nodes.length; i++) {
137: if (nodes[i].getUuid() != null) {
138: String[] langs = nodes[i].getLanguages();
139: for (int l = 0; l < langs.length; l++) {
140: try {
141: docs.add(nodes[i].getLink(langs[l])
142: .getDocument());
143: } catch (SiteException e) {
144: throw new RuntimeException(e);
145: }
146: }
147: }
148: }
149: return (Document[]) docs.toArray(new Document[docs.size()]);
150: }
151:
152: }
|