KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > acting > WorkflowInvokerAction


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: WorkflowInvokerAction.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.cocoon.acting.AbstractAction;
27 import org.apache.cocoon.environment.Redirector;
28 import org.apache.cocoon.environment.SourceResolver;
29 import org.apache.lenya.cms.cocoon.workflow.WorkflowHelper;
30 import org.apache.lenya.cms.publication.Document;
31 import org.apache.lenya.cms.publication.DocumentBuilder;
32 import org.apache.lenya.cms.publication.PageEnvelope;
33 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
34 import org.apache.lenya.cms.publication.Publication;
35 import org.apache.lenya.cms.workflow.WorkflowFactory;
36 import org.apache.lenya.workflow.Event;
37 import org.apache.lenya.workflow.Situation;
38 import org.apache.lenya.workflow.SynchronizedWorkflowInstances;
39
40 /**
41  * Action to invoke a workflow transition independently from the request document URL.
42  * Parameters:
43  * <ul>
44  * <li><strong>area:</strong> The area.</li>
45  * <li><strong>document-id:</strong> The document id.</li>
46  * <li><strong>language:</strong> The language.</li>
47  * <li><strong>event:</strong> The event to invoke.</li>
48  * </ul>
49  */

50 public class WorkflowInvokerAction extends AbstractAction {
51
52     public static final String JavaDoc AREA = "area";
53     public static final String JavaDoc DOCUMENT_ID = "document-id";
54     public static final String JavaDoc LANGUAGE = "language";
55     public static final String JavaDoc EVENT = "event";
56
57     /**
58      * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector, org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
59      */

60     public Map JavaDoc act(
61         Redirector redirector,
62         SourceResolver resolver,
63         Map JavaDoc objectModel,
64         String JavaDoc source,
65         Parameters parameters)
66         throws Exception JavaDoc {
67
68         String JavaDoc area = parameters.getParameter(AREA);
69         String JavaDoc documentId = parameters.getParameter(DOCUMENT_ID);
70         String JavaDoc language = parameters.getParameter(LANGUAGE);
71         String JavaDoc eventName = parameters.getParameter(EVENT);
72
73         if (getLogger().isDebugEnabled()) {
74             getLogger().debug(getClass().getName() + " invoked.");
75             getLogger().debug(" Area: [" + area + "]");
76             getLogger().debug(" Document ID: [" + documentId + "]");
77             getLogger().debug(" Language: [" + language + "]");
78             getLogger().debug(" Event: [" + eventName + "]");
79         }
80
81         PageEnvelope envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
82         Publication publication = envelope.getPublication();
83         DocumentBuilder builder = publication.getDocumentBuilder();
84         String JavaDoc url = builder.buildCanonicalUrl(publication, area, documentId, language);
85         Document document = builder.buildDocument(publication, url);
86
87         WorkflowFactory factory = WorkflowFactory.newInstance();
88
89         if (factory.hasWorkflow(document)) {
90
91             if (getLogger().isDebugEnabled()) {
92                 getLogger().debug(" Invoking workflow event");
93             }
94
95             SynchronizedWorkflowInstances instance = factory.buildSynchronizedInstance(document);
96             Situation situation = WorkflowHelper.buildSituation(objectModel);
97             Event[] events = instance.getExecutableEvents(situation);
98             Event event = null;
99
100             for (int i = 0; i < events.length; i++) {
101                 if (events[i].getName().equals(eventName)) {
102                     event = events[i];
103                 }
104             }
105
106             assert event != null;
107             instance.invoke(situation, event);
108         } else {
109             if (getLogger().isDebugEnabled()) {
110                 getLogger().debug(" Document has no workflow.");
111             }
112         }
113
114         return Collections.EMPTY_MAP;
115     }
116
117 }
118
Popular Tags