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.simple;
019:
020: import org.apache.avalon.framework.service.ServiceException;
021: import org.apache.avalon.framework.service.ServiceManager;
022: import org.apache.avalon.framework.service.Serviceable;
023: import org.apache.lenya.cms.cocoon.source.SourceUtil;
024: import org.apache.lenya.cms.publication.Document;
025: import org.apache.lenya.cms.publication.DocumentException;
026: import org.apache.lenya.cms.publication.DocumentFactory;
027: import org.apache.lenya.cms.publication.DocumentLocator;
028: import org.apache.lenya.cms.publication.Publication;
029: import org.apache.lenya.cms.repository.RepositoryItemFactory;
030: import org.apache.lenya.cms.site.AbstractSiteManager;
031: import org.apache.lenya.cms.site.SiteException;
032: import org.apache.lenya.cms.site.SiteNode;
033: import org.apache.lenya.cms.site.SiteStructure;
034: import org.apache.lenya.transaction.TransactionException;
035:
036: /**
037: * Simple site manager which does not imply structural information. The documents are stored in
038: * collections.
039: *
040: * @version $Id: SimpleSiteManager.java 527520 2007-04-11 15:22:11Z andreas $
041: */
042: public class SimpleSiteManager extends AbstractSiteManager implements
043: Serviceable {
044:
045: private ServiceManager manager;
046:
047: /**
048: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
049: */
050: public void service(ServiceManager manager) throws ServiceException {
051: this .manager = manager;
052: }
053:
054: /**
055: * @see org.apache.lenya.cms.site.SiteManager#requires(org.apache.lenya.cms.publication.DocumentFactory,
056: * org.apache.lenya.cms.site.SiteNode, org.apache.lenya.cms.site.SiteNode)
057: */
058: public boolean requires(DocumentFactory map,
059: SiteNode dependingResource, SiteNode requiredResource)
060: throws SiteException {
061: return false;
062: }
063:
064: /**
065: * @see org.apache.lenya.cms.site.SiteManager#getRequiringResources(org.apache.lenya.cms.publication.DocumentFactory,
066: * org.apache.lenya.cms.site.SiteNode)
067: */
068: public SiteNode[] getRequiringResources(DocumentFactory map,
069: SiteNode resource) throws SiteException {
070: return new SiteNode[0];
071: }
072:
073: public void add(String path, Document document)
074: throws SiteException {
075: getStore(document).add(path, document);
076: }
077:
078: /**
079: * @param document The document.
080: * @return The store of the document.
081: * @throws SiteException if an error occurs.
082: */
083: private DocumentStore getStore(Document document)
084: throws SiteException {
085: Publication publication = document.getPublication();
086: String area = document.getArea();
087: DocumentFactory map = document.getFactory();
088: return getStore(map, publication, area);
089: }
090:
091: /**
092: * @param map The identity map.
093: * @param publication The publication.
094: * @param area The area.
095: * @return A document store.
096: * @throws SiteException if an error occurs.
097: */
098: protected DocumentStore getStore(DocumentFactory map,
099: Publication publication, String area) throws SiteException {
100: String key = getKey(publication, area);
101: DocumentStore store;
102: RepositoryItemFactory factory = new DocumentStoreFactory(
103: this .manager, getLogger());
104: try {
105: store = (DocumentStore) map.getSession().getRepositoryItem(
106: factory, key);
107: } catch (Exception e) {
108: throw new SiteException(e);
109: }
110:
111: return store;
112: }
113:
114: protected String getCollectionUuid(Publication pub) {
115: String sourceUri = pub
116: .getContentURI(Publication.AUTHORING_AREA)
117: + DOCUMENT_PATH;
118: try {
119:
120: if (!SourceUtil.exists(sourceUri, manager)) {
121: throw new RuntimeException("The site configuration ["
122: + sourceUri + "] does not exist!");
123: }
124:
125: org.w3c.dom.Document xml = SourceUtil.readDOM(sourceUri,
126: manager);
127: if (!xml.getDocumentElement().hasAttribute("uuid")) {
128: throw new RuntimeException("The document element of ["
129: + sourceUri
130: + "] doesn't contain a uuid attribute!");
131: }
132: return xml.getDocumentElement().getAttribute("uuid");
133: } catch (Exception e) {
134: throw new RuntimeException(e);
135: }
136: }
137:
138: protected static final String DOCUMENT_PATH = "/site.xml";
139:
140: /**
141: * @param publication The publication.
142: * @param area The area.
143: * @return The key to store sitetree objects in the identity map.
144: */
145: protected String getKey(Publication publication, String area) {
146: return publication.getId() + ":" + area + ":"
147: + getCollectionUuid(publication);
148: }
149:
150: /**
151: * @see org.apache.lenya.cms.site.SiteManager#contains(org.apache.lenya.cms.publication.Document)
152: */
153: public boolean contains(Document resource) throws SiteException {
154:
155: try {
156: DocumentStore store = getStore(resource);
157: if (resource.equals(store)) {
158: return true;
159: }
160: return store.contains(resource);
161: } catch (DocumentException e) {
162: throw new SiteException(e);
163: }
164: }
165:
166: /**
167: * @see org.apache.lenya.cms.site.SiteManager#containsInAnyLanguage(org.apache.lenya.cms.publication.Document)
168: */
169: public boolean containsInAnyLanguage(Document resource)
170: throws SiteException {
171: try {
172: boolean contains = false;
173:
174: String[] languages = resource.getLanguages();
175: for (int i = 0; i < languages.length; i++) {
176: Document doc = resource.getTranslation(languages[i]);
177: DocumentStore store = getStore(doc);
178: contains = contains || store.contains(doc);
179: }
180:
181: return contains;
182: } catch (Exception e) {
183: throw new SiteException(e);
184: }
185: }
186:
187: /**
188: * @see org.apache.lenya.cms.site.SiteManager#copy(org.apache.lenya.cms.publication.Document,
189: * org.apache.lenya.cms.publication.Document)
190: */
191: public void copy(Document sourceDocument,
192: Document destinationDocument) throws SiteException {
193: DocumentStore destinationStore = getStore(destinationDocument);
194: try {
195: if (!destinationStore.contains(destinationDocument)) {
196: destinationStore.add(destinationDocument);
197: }
198: } catch (Exception e) {
199: throw new SiteException(e);
200: }
201: }
202:
203: /**
204: * @see org.apache.lenya.cms.site.SiteManager#setVisibleInNav(org.apache.lenya.cms.publication.Document,
205: * boolean)
206: */
207: public void setVisibleInNav(Document document, boolean visibleInNav)
208: throws SiteException {
209: }
210:
211: /**
212: * @see org.apache.lenya.cms.site.SiteManager#getDocuments(org.apache.lenya.cms.publication.DocumentFactory,
213: * org.apache.lenya.cms.publication.Publication, java.lang.String)
214: */
215: public Document[] getDocuments(DocumentFactory identityMap,
216: Publication publication, String area) throws SiteException {
217: DocumentStore store = getStore(identityMap, publication, area);
218: try {
219: return store.getDocuments();
220: } catch (DocumentException e) {
221: throw new SiteException(e);
222: }
223: }
224:
225: /**
226: * @see org.apache.lenya.cms.site.SiteManager#getSiteStructure(org.apache.lenya.cms.publication.DocumentFactory,
227: * org.apache.lenya.cms.publication.Publication, java.lang.String)
228: */
229: public SiteStructure getSiteStructure(DocumentFactory map,
230: Publication publication, String area) throws SiteException {
231: return getStore(map, publication, area);
232: }
233:
234: /**
235: * @see org.apache.lenya.cms.site.SiteManager#getAvailableLocator(DocumentFactory,
236: * DocumentLocator)
237: */
238: public DocumentLocator getAvailableLocator(DocumentFactory factory,
239: DocumentLocator document) throws SiteException {
240: return document;
241: }
242:
243: public boolean isVisibleInNav(Document document)
244: throws SiteException {
245: return true;
246: }
247:
248: public void set(String path, Document document)
249: throws SiteException {
250: try {
251: getStore(document).setPath(document, path);
252: } catch (TransactionException e) {
253: throw new SiteException(e);
254: }
255: }
256:
257: public DocumentLocator[] getRequiredResources(DocumentFactory map,
258: DocumentLocator locator) throws SiteException {
259: return new DocumentLocator[0];
260: }
261:
262: }
|