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: /* $Id: DocumentHelper.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.publication.util;
022:
023: import java.util.Arrays;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.cocoon.ProcessingException;
029: import org.apache.cocoon.environment.ObjectModelHelper;
030: import org.apache.cocoon.environment.Request;
031: import org.apache.lenya.cms.publication.Document;
032: import org.apache.lenya.cms.publication.DocumentBuildException;
033: import org.apache.lenya.cms.publication.DocumentException;
034: import org.apache.lenya.cms.publication.DocumentFactory;
035: import org.apache.lenya.cms.publication.DocumentLocator;
036: import org.apache.lenya.cms.publication.DocumentUtil;
037: import org.apache.lenya.cms.publication.Publication;
038: import org.apache.lenya.cms.publication.PublicationException;
039: import org.apache.lenya.cms.publication.PublicationUtil;
040: import org.apache.lenya.cms.publication.URLInformation;
041: import org.apache.lenya.cms.repository.RepositoryException;
042: import org.apache.lenya.cms.repository.RepositoryUtil;
043: import org.apache.lenya.cms.repository.Session;
044: import org.apache.lenya.util.ServletHelper;
045:
046: /**
047: * Helper class to handle documents from XSP.
048: */
049: public class DocumentHelper {
050:
051: private Map objectModel;
052: private DocumentFactory identityMap;
053: private Publication publication;
054:
055: /**
056: * Ctor.
057: * @param manager The service manager.
058: * @param _objectModel The Cocoon object model.
059: */
060: public DocumentHelper(ServiceManager manager, Map _objectModel) {
061: this .objectModel = _objectModel;
062: try {
063: this .publication = PublicationUtil.getPublication(manager,
064: _objectModel);
065: } catch (PublicationException e) {
066: throw new RuntimeException(e);
067: }
068: Request request = ObjectModelHelper.getRequest(_objectModel);
069: Session session;
070: try {
071: session = RepositoryUtil.getSession(manager, request);
072: } catch (RepositoryException e) {
073: throw new RuntimeException(e);
074: }
075: this .identityMap = DocumentUtil.createDocumentFactory(manager,
076: session);
077: }
078:
079: /**
080: * Creates a document URL. <br/>If the document ID is null, the current document ID is used.
081: * <br/>If the document area is null, the current area is used. <br/>If the language is null,
082: * the current language is used.
083: * @param uuid The target document UUID.
084: * @param documentArea The target area.
085: * @param language The target language.
086: * @return A string.
087: * @throws ProcessingException if something went wrong.
088: */
089: public String getDocumentUrl(String uuid, String documentArea,
090: String language) throws ProcessingException {
091:
092: String url = null;
093:
094: try {
095: Request request = ObjectModelHelper
096: .getRequest(this .objectModel);
097: String webappUrl = ServletHelper.getWebappURI(request);
098: Document envDocument = this .identityMap
099: .getFromURL(webappUrl);
100: if (uuid == null) {
101: uuid = envDocument.getUUID();
102: }
103:
104: if (documentArea == null) {
105: URLInformation info = new URLInformation(webappUrl);
106: String completeArea = info.getCompleteArea();
107: documentArea = completeArea;
108: }
109:
110: if (language == null) {
111: language = envDocument.getLanguage();
112: }
113:
114: Document document = this .identityMap.get(this .publication,
115: documentArea, uuid, language);
116: url = document.getCanonicalWebappURL();
117:
118: String contextPath = request.getContextPath();
119: if (contextPath == null) {
120: contextPath = "";
121: }
122:
123: url = contextPath + url;
124: } catch (final DocumentBuildException e) {
125: throw new ProcessingException(e);
126: }
127:
128: return url;
129:
130: }
131:
132: /**
133: * Returns the complete URL of the parent document. If the document is a top-level document, the
134: * /index document is chosen. If the parent does not exist in the appropriate language, the
135: * default language is chosen.
136: * @return A string.
137: * @throws ProcessingException when something went wrong.
138: */
139: public String getCompleteParentUrl() throws ProcessingException {
140:
141: String parentUrl;
142: String contextPath;
143: try {
144: Request request = ObjectModelHelper
145: .getRequest(this .objectModel);
146: String webappUrl = ServletHelper.getWebappURI(request);
147: Document document = this .identityMap.getFromURL(webappUrl);
148:
149: contextPath = request.getContextPath();
150:
151: DocumentLocator parentLocator = document.getLocator()
152: .getParent("/index");
153: Document parent = this .identityMap.get(parentLocator);
154: parentUrl = parent.getCanonicalWebappURL();
155: } catch (final DocumentBuildException e) {
156: throw new ProcessingException(e);
157: }
158: if (contextPath == null) {
159: contextPath = "";
160: }
161:
162: return contextPath + parentUrl;
163: }
164:
165: /**
166: * Returns an existing language version of a document. If the document exists in the default
167: * language, the default language version is returned. Otherwise, a random language version is
168: * returned. If no language version exists, a DocumentException is thrown.
169: *
170: * @param document The document.
171: * @return A document.
172: * @throws DocumentException when an error occurs.
173: */
174: public static Document getExistingLanguageVersion(Document document)
175: throws DocumentException {
176: return getExistingLanguageVersion(document, document
177: .getPublication().getDefaultLanguage());
178: }
179:
180: /**
181: * Returns an existing language version of a document. If the document exists in the preferred
182: * language, this version is returned. Otherwise, if the document exists in the default
183: * language, the default language version is returned. Otherwise, a random language version is
184: * returned. If no language version exists, a DocumentException is thrown.
185: *
186: * @param document The document.
187: * @param preferredLanguage The preferred language.
188: * @return A document.
189: * @throws DocumentException when an error occurs.
190: */
191: public static Document getExistingLanguageVersion(
192: final Document document, String preferredLanguage)
193: throws DocumentException {
194:
195: Publication publication = document.getPublication();
196:
197: String[] languages = document.getLanguages();
198:
199: if (languages.length == 0) {
200: throw new DocumentException("The document [" + document
201: + "] does not exist in any language!");
202: }
203:
204: List languageList = Arrays.asList(languages);
205:
206: String existingLanguage = null;
207:
208: if (languageList.contains(preferredLanguage)) {
209: existingLanguage = preferredLanguage;
210: } else if (languageList.contains(publication
211: .getDefaultLanguage())) {
212: existingLanguage = publication.getDefaultLanguage();
213: } else {
214: existingLanguage = languages[0];
215: }
216:
217: Document existingVersion = null;
218: try {
219: existingVersion = document.getTranslation(existingLanguage);
220: } catch (DocumentException e) {
221: throw new DocumentException(e);
222: }
223:
224: return existingVersion;
225: }
226:
227: }
|