KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > flow > FlowHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: FlowHelper.java 151165 2005-02-03 15:34:19Z michi $ */
19
20 package org.apache.lenya.cms.cocoon.flow;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon;
28 import org.apache.cocoon.environment.ObjectModelHelper;
29 import org.apache.cocoon.environment.Request;
30 import org.apache.cocoon.environment.Session;
31 import org.apache.lenya.ac.AccessControlException;
32 import org.apache.lenya.ac.Identity;
33 import org.apache.lenya.ac.Machine;
34 import org.apache.lenya.ac.Role;
35 import org.apache.lenya.ac.User;
36 import org.apache.lenya.ac.impl.PolicyAuthorizer;
37 import org.apache.lenya.cms.publication.DocumentHelper;
38 import org.apache.lenya.cms.publication.PageEnvelope;
39 import org.apache.lenya.cms.publication.PageEnvelopeException;
40 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
41 import org.apache.lenya.cms.publication.Publication;
42 import org.apache.lenya.cms.rc.FileReservedCheckInException;
43 import org.apache.lenya.cms.rc.RCEnvironment;
44 import org.apache.lenya.cms.rc.RevisionController;
45 import org.apache.lenya.cms.workflow.WorkflowDocument;
46 import org.apache.lenya.cms.workflow.WorkflowFactory;
47 import org.apache.lenya.workflow.Situation;
48 import org.apache.lenya.workflow.WorkflowException;
49 import org.apache.log4j.Category;
50
51 /**
52  * Flowscript utility class.
53  * The FOM_Cocoon object is not passed in the constructor to avoid
54  * errors. This way, not the initial, but the current FOM_Cocoon
55  * object is used by the methods.
56  */

57 public class FlowHelper {
58
59     private static final Category log = Category.getInstance(FlowHelper.class);
60
61     /**
62      * Ctor.
63      */

64     public FlowHelper() {
65     }
66
67     /**
68      * Returns the current workflow situation.
69      * @param cocoon The FOM_Cocoon object.
70      * @return A situation.
71      * @throws AccessControlException when something went wrong.
72      */

73     public Situation getSituation(FOM_Cocoon cocoon) throws AccessControlException {
74         Request request = ObjectModelHelper.getRequest(cocoon.getObjectModel());
75         Session session = request.getSession();
76         Identity identity = (Identity) session.getAttribute(Identity.class.getName());
77
78         String JavaDoc userId = "";
79         String JavaDoc ipAddress = "";
80
81         User user = identity.getUser();
82         if (user != null) {
83             userId = user.getId();
84         }
85
86         Machine machine = identity.getMachine();
87         if (machine != null) {
88             ipAddress = machine.getIp();
89         }
90
91         Role[] roles = PolicyAuthorizer.getRoles(request);
92         String JavaDoc[] roleIds = new String JavaDoc[roles.length];
93         for (int i = 0; i < roles.length; i++) {
94             roleIds[i] = roles[i].getId();
95         }
96
97         WorkflowFactory factory = WorkflowFactory.newInstance();
98         Situation situation = factory.buildSituation(roleIds, userId, ipAddress);
99         return situation;
100     }
101
102     /**
103      * Returns the current page envelope.
104      * @param cocoon The FOM_Cocoon object.
105      * @return A page envelope.
106      * @throws PageEnvelopeException when something went wrong.
107      */

108     public PageEnvelope getPageEnvelope(FOM_Cocoon cocoon) throws PageEnvelopeException {
109         PageEnvelopeFactory factory = PageEnvelopeFactory.getInstance();
110         return factory.getPageEnvelope(cocoon.getObjectModel());
111     }
112
113     /**
114      * Returns the request URI of the current request.
115      * @param cocoon The FOM_Cocoon object.
116      * @return A string.
117      */

118     public String JavaDoc getRequestURI(FOM_Cocoon cocoon) {
119         return cocoon.getRequest().getRequestURI();
120     }
121     
122     /**
123      * Returns the request object of the current request.
124      * @param cocoon The FOM_Cocoon object.
125      * @return A request object.
126      */

127     public Request getRequest(FOM_Cocoon cocoon) {
128         return cocoon.getRequest();
129     }
130
131     /**
132      * Returns the Cocoon Object Model
133      * @param cocoon The Flow Object Model of Cocoon
134      * @return The object model
135      */

136     public Map JavaDoc getObjectModel(FOM_Cocoon cocoon) {
137         return cocoon.getObjectModel();
138     }
139     
140     /**
141      * Returns a DocumentHelper instance.
142      * @param cocoon The Flow Object Model of Cocoon
143      * @return The document helper
144      * @see DocumentHelper
145      */

146     public DocumentHelper getDocumentHelper(FOM_Cocoon cocoon) {
147         return new DocumentHelper(cocoon.getObjectModel());
148     }
149     
150     public static final String JavaDoc SEPARATOR = ":";
151
152     /**
153      * Resolves the request parameter value for a specific name.
154      * The parameter names are encoded as <code>{name}:{value}.{axis}</code>.
155      * This is a workaround for the &lt;input type="image"/&gt;
156      * bug in Internet Explorer.
157      * @param cocoon The FOM_Cocoon object.
158      * @param parameterName The request parameter name.
159      * @return A string.
160      */

161     public String JavaDoc getImageParameterValue(FOM_Cocoon cocoon, String JavaDoc parameterName) {
162
163         log.debug("Resolving parameter value for name [" + parameterName + "]");
164
165         Request request = cocoon.getRequest();
166         String JavaDoc value = request.getParameter(parameterName);
167
168         if (value == null) {
169             String JavaDoc prefix = parameterName + SEPARATOR;
170             Enumeration JavaDoc e = request.getParameterNames();
171             while (e.hasMoreElements() && value == null) {
172                 String JavaDoc name = (String JavaDoc) e.nextElement();
173                 if (name.startsWith(prefix)) {
174                     log.debug("Complete parameter name: [" + name + "]");
175                     value = name.substring(prefix.length(), name.length() - 2);
176                     log.debug("Resolved value: [" + value + "]");
177                 }
178             }
179         }
180
181         return value;
182     }
183     
184     /**
185      * Trigger a workflow event for the document associated with the current PageEnvelope.
186      * @param cocoon The Cocoon Flow Object Model
187      * @param event The name of the workflow event to trigger.
188      * @throws WorkflowException If an workflow error occurs
189      * @throws PageEnvelopeException Page envelope can not operate properly.
190      * @throws AccessControlException If an access control violation occurs.
191      */

192     public void triggerWorkflow(FOM_Cocoon cocoon, String JavaDoc event)
193     throws WorkflowException, PageEnvelopeException, AccessControlException {
194         final WorkflowDocument wf = (WorkflowDocument)WorkflowFactory.newInstance().buildInstance(getPageEnvelope(cocoon).getDocument());
195         wf.invoke(getSituation(cocoon), event);
196     }
197     
198     /**
199      * Get a RevisionController instance.
200      * @param cocoon The Cocoon Flow Object Model
201      * @throws PageEnvelopeException Page envelope can not operate properly.
202      * @throws IOException If an IOException occurs.
203      * @see PageEnvelope
204      * @see RevisionController
205      */

206     public RevisionController getRevisionController(FOM_Cocoon cocoon)
207     throws PageEnvelopeException, IOException JavaDoc {
208         final Publication publication = getPageEnvelope(cocoon).getPublication();
209         final String JavaDoc publicationPath = publication.getDirectory().getAbsolutePath();
210         final RCEnvironment rcEnvironment = RCEnvironment.getInstance(publication.getServletContext().getAbsolutePath());
211         String JavaDoc rcmlDirectory = rcEnvironment.getRCMLDirectory();
212         rcmlDirectory = publicationPath + File.separator + rcmlDirectory;
213         String JavaDoc backupDirectory = rcEnvironment.getBackupDirectory();
214         backupDirectory = publicationPath + File.separator + backupDirectory;
215
216         return new RevisionController(rcmlDirectory, backupDirectory, publicationPath);
217     }
218     
219     /**
220      * Checkis in the current document from the PageEnvelope context.
221      * @param cocoon The Cocoon Flow Object Model
222      * @param backup Wether a new revision should be created.
223      * @throws FileReservedCheckInException
224      * @throws Exception
225      * @see RevisionController#reservedCheckIn(String, String, boolean)
226      */

227     public void reservedCheckIn(FOM_Cocoon cocoon, boolean backup)
228     throws FileReservedCheckInException, Exception JavaDoc
229     {
230         final Identity identity = (Identity) ObjectModelHelper.getRequest(cocoon.getObjectModel()).getSession().getAttribute(Identity.class.getName());
231         final PageEnvelope pageEnvelope = getPageEnvelope(cocoon);
232         final Publication publication = getPageEnvelope(cocoon).getPublication();
233         final String JavaDoc filename = pageEnvelope.getDocument().getFile().getAbsolutePath().substring(publication.getDirectory().getAbsolutePath().length());
234         getRevisionController(cocoon).reservedCheckIn(filename, identity.getUser().getId(), backup);
235     }
236 }
237
Popular Tags