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.usecase;
020:
021: import org.apache.lenya.cms.publication.Document;
022: import org.apache.lenya.cms.publication.DocumentBuildException;
023: import org.apache.lenya.cms.publication.DocumentFactory;
024:
025: /**
026: * <p>
027: * Extends AbstractUsecase with document helper methods.
028: * </p>
029: * <p>
030: * Some parameters are initialized by default:
031: * </p>
032: * <ul>
033: * <li><code>document</code>- the document</li>
034: * </ul>
035: */
036: public class DocumentUsecase extends AbstractUsecase {
037:
038: protected static final String DOCUMENT = "document";
039: protected static final String TARGET_DOCUMENT = "private.targetDocument";
040:
041: /**
042: * Ctor.
043: */
044: public DocumentUsecase() {
045: super ();
046: }
047:
048: /**
049: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
050: */
051: protected void doCheckPreconditions() throws Exception {
052: super .doCheckPreconditions();
053: if (getSourceDocument() == null) {
054: addErrorMessage("This usecase can only be invoked on documents!");
055: }
056: }
057:
058: /*
059: public void setParameter(String name, Object value) {
060: if (name.equals(SOURCE_URL)) {
061: setSourceURL((String) value);
062: }
063: else {
064: super.setParameter(name, value);
065: }
066: }
067: */
068:
069: /**
070: * @see org.apache.lenya.cms.usecase.Usecase#setSourceURL(java.lang.String)
071: public void setSourceURL(String url) {
072: try {
073: DocumentFactory factory = getDocumentFactory();
074: if (factory.isDocument(url)) {
075: Document document = factory.getFromURL(url);
076: setParameter(DOCUMENT, document);
077: }
078: } catch (Exception e) {
079: throw new RuntimeException(e);
080: }
081: super.setParameter(SOURCE_URL, url);
082: }
083: */
084:
085: /**
086: * Returns the source document.
087: * @return A document.
088: */
089: protected Document getSourceDocument() {
090: Document doc = (Document) getParameter(DOCUMENT);
091: if (doc == null
092: || doc.getFactory().getSession() != getSession()) {
093: try {
094: DocumentFactory factory = getDocumentFactory();
095: String sourceUrl = getParameterAsString(SOURCE_URL);
096: if (factory.isDocument(sourceUrl)) {
097: doc = factory.getFromURL(sourceUrl);
098: setParameter(DOCUMENT, doc);
099: }
100: } catch (DocumentBuildException e) {
101: throw new RuntimeException(e);
102: }
103: }
104: return doc;
105: }
106:
107: /**
108: * Sets the target document for the case that the usecase execution
109: * succeeded (see {@link #getTargetDocument(boolean)}).
110: * @param document A document.
111: */
112: protected void setTargetDocument(Document document) {
113: setParameter(TARGET_DOCUMENT, document);
114: }
115:
116: /**
117: * Returns the document to be redirected to after the usecase has been
118: * completed. If the parameter <code>success</code> is false, the source
119: * document is returned (override this method to change this behaviour).
120: * @param success If the usecase was successfully completed.
121: * @return A document.
122: */
123: protected Document getTargetDocument(boolean success) {
124: if (success) {
125: return (Document) getParameter(TARGET_DOCUMENT,
126: getSourceDocument());
127: } else {
128: return getSourceDocument();
129: }
130: }
131:
132: /**
133: * If {@link #setTargetDocument(Document)}was not called, the URL of the
134: * source document ( {@link #getSourceDocument()}) is returned.
135: * @see org.apache.lenya.cms.usecase.Usecase#getTargetURL(boolean)
136: */
137: public String getTargetURL(boolean success) {
138: Document document = getTargetDocument(success);
139: String documentUrl = document.getCanonicalDocumentURL();
140: String url = "/" + document.getPublication().getId() + "/"
141: + document.getArea() + documentUrl;
142: return url + getExitQueryString();
143: }
144:
145: /**
146: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getSourceURL()
147: */
148: public String getSourceURL() {
149: Document doc = getSourceDocument();
150: if (doc == null || !doc.hasLink()) {
151: return super .getSourceURL();
152: }
153: return doc.getCanonicalWebappURL();
154: }
155:
156: /**
157: * @see org.apache.lenya.cms.usecase.AbstractUsecase#setDefaultTargetURL(java.lang.String)
158: */
159: protected void setDefaultTargetURL(String url) {
160: try {
161: Document target = getDocumentFactory().getFromURL(url);
162: setParameter(TARGET_DOCUMENT, target);
163: } catch (DocumentBuildException e) {
164: throw new RuntimeException(e);
165: }
166: }
167:
168: /**
169: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
170: protected void initParameters() {
171: super.initParameters();
172:
173: setParameter(DOCUMENT, getSourceDocument());
174: }
175: */
176: }
|