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: FlowHelperImpl.java 564507 2007-08-10 08:33:31Z andreas $ */
020:
021: package org.apache.lenya.cms.cocoon.flow;
022:
023: import java.util.Enumeration;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.Serviceable;
030: import org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon;
031: import org.apache.cocoon.environment.Request;
032: import org.apache.lenya.ac.AccessControlException;
033: import org.apache.lenya.cms.publication.Document;
034: import org.apache.lenya.cms.publication.DocumentFactory;
035: import org.apache.lenya.cms.publication.DocumentUtil;
036: import org.apache.lenya.cms.publication.PageEnvelope;
037: import org.apache.lenya.cms.publication.PageEnvelopeException;
038: import org.apache.lenya.cms.publication.PageEnvelopeFactory;
039: import org.apache.lenya.cms.publication.Publication;
040: import org.apache.lenya.cms.publication.PublicationUtil;
041: import org.apache.lenya.cms.publication.util.DocumentHelper;
042: import org.apache.lenya.cms.rc.FileReservedCheckInException;
043: import org.apache.lenya.cms.repository.Node;
044: import org.apache.lenya.cms.repository.RepositoryUtil;
045: import org.apache.lenya.cms.repository.Session;
046: import org.apache.lenya.cms.workflow.WorkflowUtil;
047: import org.apache.lenya.workflow.WorkflowException;
048:
049: /**
050: * Flowscript utility class. The FOM_Cocoon object is not passed in the constructor to avoid errors.
051: * This way, not the initial, but the current FOM_Cocoon object is used by the methods.
052: */
053: public class FlowHelperImpl extends AbstractLogEnabled implements
054: FlowHelper, Serviceable {
055:
056: /**
057: * Ctor.
058: */
059: public FlowHelperImpl() {
060: // do nothing
061: }
062:
063: /**
064: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#getPageEnvelope(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon)
065: */
066: public PageEnvelope getPageEnvelope(FOM_Cocoon cocoon)
067: throws PageEnvelopeException {
068: Request request = getRequest(cocoon);
069: try {
070: Session session = RepositoryUtil.getSession(this .manager,
071: request);
072: DocumentFactory map = DocumentUtil.createDocumentFactory(
073: this .manager, session);
074: PageEnvelopeFactory factory = PageEnvelopeFactory
075: .getInstance();
076: Publication publication = PublicationUtil.getPublication(
077: this .manager, request);
078: return factory.getPageEnvelope(map,
079: cocoon.getObjectModel(), publication);
080: } catch (Exception e) {
081: throw new PageEnvelopeException(e);
082: }
083: }
084:
085: /**
086: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#getRequestURI(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon)
087: */
088: public String getRequestURI(FOM_Cocoon cocoon) {
089: return cocoon.getRequest().getRequestURI();
090: }
091:
092: /**
093: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#getRequest(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon)
094: */
095: public Request getRequest(FOM_Cocoon cocoon) {
096: return cocoon.getRequest();
097: }
098:
099: /**
100: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#getObjectModel(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon)
101: */
102: public Map getObjectModel(FOM_Cocoon cocoon) {
103: return cocoon.getObjectModel();
104: }
105:
106: /**
107: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#getDocumentHelper(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon)
108: */
109: public DocumentHelper getDocumentHelper(FOM_Cocoon cocoon) {
110: return new DocumentHelper(this .manager, cocoon.getObjectModel());
111: }
112:
113: /**
114: * <code>SEPARATOR</code> The separator
115: */
116: public static final String SEPARATOR = ":";
117:
118: /**
119: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#getImageParameterValue(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon,
120: * java.lang.String)
121: */
122: public String getImageParameterValue(FOM_Cocoon cocoon,
123: String parameterName) {
124:
125: getLogger().debug(
126: "Resolving parameter value for name [" + parameterName
127: + "]");
128:
129: Request request = cocoon.getRequest();
130: String value = request.getParameter(parameterName);
131:
132: if (value == null) {
133: String prefix = parameterName + SEPARATOR;
134: Enumeration e = request.getParameterNames();
135: while (e.hasMoreElements() && value == null) {
136: String name = (String) e.nextElement();
137: if (name.startsWith(prefix)) {
138: getLogger().debug(
139: "Complete parameter name: [" + name + "]");
140: value = name.substring(prefix.length(), name
141: .length() - 2);
142: getLogger()
143: .debug("Resolved value: [" + value + "]");
144: }
145: }
146: }
147:
148: return value;
149: }
150:
151: /**
152: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#triggerWorkflow(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon,
153: * java.lang.String)
154: */
155: public void triggerWorkflow(FOM_Cocoon cocoon, String event)
156: throws WorkflowException, PageEnvelopeException,
157: AccessControlException {
158: Document document = getPageEnvelope(cocoon).getDocument();
159: Request request = getRequest(cocoon);
160: try {
161: Session session = RepositoryUtil.getSession(this .manager,
162: request);
163: WorkflowUtil.invoke(this .manager, session, getLogger(),
164: document, event);
165: } catch (Exception e) {
166: // TODO Auto-generated catch block
167: e.printStackTrace();
168: }
169: }
170:
171: /**
172: * @see org.apache.lenya.cms.cocoon.flow.FlowHelper#reservedCheckIn(org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon,
173: * boolean)
174: */
175: public void reservedCheckIn(FOM_Cocoon cocoon, boolean backup)
176: throws FileReservedCheckInException, Exception {
177: final PageEnvelope pageEnvelope = getPageEnvelope(cocoon);
178: Node node = pageEnvelope.getDocument().getRepositoryNode();
179: node.checkin();
180: }
181:
182: private ServiceManager manager;
183:
184: /**
185: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
186: */
187: public void service(ServiceManager manager) throws ServiceException {
188: this.manager = manager;
189: }
190: }
|