KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > WorkflowInvoker


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: WorkflowInvoker.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.task;
21
22 import java.util.Map JavaDoc;
23
24 import org.apache.lenya.ac.Identity;
25 import org.apache.lenya.ac.Machine;
26 import org.apache.lenya.ac.Role;
27 import org.apache.lenya.ac.User;
28 import org.apache.lenya.cms.publication.Document;
29 import org.apache.lenya.cms.publication.DocumentBuildException;
30 import org.apache.lenya.cms.publication.Publication;
31 import org.apache.lenya.cms.workflow.WorkflowFactory;
32 import org.apache.lenya.util.NamespaceMap;
33 import org.apache.lenya.workflow.Event;
34 import org.apache.lenya.workflow.Situation;
35 import org.apache.lenya.workflow.SynchronizedWorkflowInstances;
36 import org.apache.log4j.Category;
37
38 public class WorkflowInvoker extends ParameterWrapper {
39
40     private static Category log = Category.getInstance(WorkflowInvoker.class);
41
42     public static final String JavaDoc ROLES = "roles";
43     public static final String JavaDoc USER_ID = "user-id";
44     public static final String JavaDoc MACHINE = "machine";
45     public static final String JavaDoc EVENT = "event";
46
47     public static final String JavaDoc PREFIX = "workflow";
48
49     public static final String JavaDoc EVENT_REQUEST_PARAMETER = "workflow.event";
50     public static final String JavaDoc LENYA_EVENT_REQUEST_PARAMETER = "lenya.event";
51
52     /**
53      * Ctor.
54      *
55      * @param eventName
56      * The event name.
57      * @param identity
58      * The identity.
59      * @param roles
60      * The roles.
61      * @return A namespace map containing the parameters.
62      */

63     public static NamespaceMap extractParameters(
64         String JavaDoc eventName,
65         Identity identity,
66         Role[] roles) {
67         NamespaceMap parameters = new NamespaceMap(PREFIX);
68         log.debug("Extractign workflow invoker parameters.");
69         log.debug(" Event: [" + eventName + "]");
70         parameters.put(EVENT, eventName);
71         setRoles(parameters, roles);
72         setIdentity(parameters, identity);
73         return parameters;
74     }
75
76     /**
77      * Ctor.
78      *
79      * @param parameters
80      * A map containing the prefixed parameters.
81      */

82     public WorkflowInvoker(Map JavaDoc parameters) {
83         super(parameters);
84     }
85
86     /**
87      * Returns the role names.
88      *
89      * @return A string array.
90      */

91     protected String JavaDoc[] getRoleIDs() {
92         String JavaDoc rolesString = get(ROLES);
93         String JavaDoc[] roleIDs = rolesString.split(",");
94         return roleIDs;
95     }
96
97     /**
98      * Sets the roles.
99      *
100      * @param parameters
101      * A workflow invoker namespace map.
102      * @param roles
103      * A role array.
104      */

105     public static void setRoles(NamespaceMap parameters, Role[] roles) {
106
107         String JavaDoc roleString = "";
108         for (int i = 0; i < roles.length; i++) {
109             if (i > 0) {
110                 roleString += ",";
111             }
112             roleString += roles[i].getId();
113         }
114         parameters.put(ROLES, roleString);
115     }
116
117     /**
118      * Sets the identity.
119      *
120      * @param parameters
121      * A workflow invoker namespace map.
122      * @param identity
123      * An identity.
124      */

125     public static void setIdentity(NamespaceMap parameters, Identity identity) {
126
127         String JavaDoc userId = "";
128         User user = identity.getUser();
129         if (user != null) {
130             userId = user.getId();
131         }
132         parameters.put(USER_ID, userId);
133
134         String JavaDoc machineIp = "";
135         Machine machine = identity.getMachine();
136         if (machine != null) {
137             machineIp = machine.getIp();
138         }
139         parameters.put(MACHINE, machineIp);
140     }
141
142     /**
143      * Returns the workflow event name.
144      *
145      * @return A string.
146      */

147     public String JavaDoc getEventName() {
148         return get(EVENT);
149     }
150
151     /**
152      * Returns the user ID.
153      *
154      * @return A string.
155      */

156     public String JavaDoc getUserId() {
157         return get(USER_ID);
158     }
159
160     /**
161      * Returns the machine IP address.
162      *
163      * @return A string.
164      */

165     public String JavaDoc getMachineIp() {
166         return get(MACHINE);
167     }
168
169     private Document document;
170     private boolean doTransition = false;
171
172     /**
173      * Initializes the workflow invoker.
174      *
175      * @param publication
176      * The publication.
177      * @param webappUrl
178      * The webapp URL.
179      * @throws ExecutionException
180      * when something went wrong.
181      */

182     public void setup(Publication publication, String JavaDoc webappUrl) throws ExecutionException {
183         String JavaDoc eventName = getEventName();
184         if (eventName == null) {
185             log.debug("No workflow event.");
186         } else {
187             log.debug("Workflow event: [" + eventName + "]");
188             // check for workflow instance first (task can initialize the workflow history)
189
WorkflowFactory factory = WorkflowFactory.newInstance();
190             try {
191                 document = publication.getDocumentBuilder().buildDocument(publication, webappUrl);
192             } catch (DocumentBuildException e) {
193                 throw new ExecutionException(e);
194             }
195             doTransition = factory.hasWorkflow(document);
196         }
197     }
198
199     /**
200      * Invokes the transition.
201      *
202      * @throws ExecutionException
203      * when something went wrong.
204      */

205     public void invokeTransition() throws ExecutionException {
206         if (doTransition) {
207
208             try {
209                 WorkflowFactory factory = WorkflowFactory.newInstance();
210                 SynchronizedWorkflowInstances instance =
211                     factory.buildSynchronizedInstance(document);
212                 Situation situation =
213                     factory.buildSituation(getRoleIDs(), getUserId(), getMachineIp());
214
215                 Event event = null;
216                 Event[] events = instance.getExecutableEvents(situation);
217
218                 log.debug("Resolved executable events.");
219                 
220                 for (int i = 0; i < events.length; i++) {
221                     if (events[i].getName().equals(getEventName())) {
222                         event = events[i];
223                     }
224                 }
225
226                 assert event != null;
227                 
228                 log.debug("Invoking transition.");
229                 instance.invoke(situation, event);
230                 log.debug("Invoking transition completed.");
231
232             } catch (Exception JavaDoc e) {
233                 throw new ExecutionException(e);
234             }
235         }
236
237     }
238
239     /**
240      * @see org.apache.lenya.cms.task.ParameterWrapper#getPrefix()
241      */

242     public String JavaDoc getPrefix() {
243         return PREFIX;
244     }
245
246     /**
247      * @see org.apache.lenya.cms.task.ParameterWrapper#getRequiredKeys()
248      */

249     protected String JavaDoc[] getRequiredKeys() {
250         String JavaDoc[] keys = {
251         };
252         return keys;
253     }
254
255 }
256
Popular Tags