KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > workflow > WorkflowUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.workflow;
14
15 import info.magnolia.cms.security.MgnlUser;
16 import info.magnolia.cms.util.FactoryUtil;
17 import info.magnolia.context.Context;
18 import info.magnolia.context.MgnlContext;
19 import info.magnolia.module.workflow.flows.FlowDefinionException;
20 import info.magnolia.module.workflow.flows.FlowDefinitionManager;
21 import info.magnolia.module.workflow.jcr.JCRPersistedEngine;
22 import info.magnolia.module.workflow.jcr.JCRWorkItemAPI;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import openwfe.org.engine.expressions.FlowExpressionId;
29 import openwfe.org.engine.workitem.InFlowItem;
30 import openwfe.org.engine.workitem.InFlowWorkItem;
31 import openwfe.org.engine.workitem.LaunchItem;
32 import openwfe.org.engine.workitem.StringAttribute;
33 import openwfe.org.worklist.store.StoreException;
34
35 import org.apache.commons.lang.StringUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40 /**
41  * Util to use the mangoila workflow module. Methods to launch and proceed.
42  * @author jackie
43  */

44 public class WorkflowUtil {
45
46     final static public StringAttribute ATTRIBUTE_TRUE = new StringAttribute("true");
47
48     final static public StringAttribute ATTRIBUTE_FALSE = new StringAttribute("false");
49
50     private final static Logger log = LoggerFactory.getLogger(WorkflowUtil.class.getName());
51
52     /**
53      * Where the work items are stored
54      */

55     private static JCRWorkItemAPI storage;
56
57     static {
58         
59         try {
60             storage = new JCRWorkItemAPI();
61         }
62         catch (Exception JavaDoc e) {
63             log.error("can't initialize the workflow util", e);
64         }
65     }
66
67     /**
68      * Util: don't instantiate
69      */

70     private WorkflowUtil() {
71     }
72
73     public static void launchFlow(LaunchItem li) {
74         try {
75             JCRPersistedEngine engine = WorkflowModule.getEngine();
76             // Launch the item
77
engine.launch(li, true);
78         }
79         catch (Exception JavaDoc e) {
80             log.error("Launching flow failed", e);
81         }
82     }
83     
84     /**
85      * Simply launch a flow for the specified node
86      */

87     public static void launchFlow(String JavaDoc repository, String JavaDoc path, String JavaDoc flowName) throws Exception JavaDoc {
88         try {
89             // Get the references
90
LaunchItem li = new LaunchItem();
91
92             // start activation
93
if (repository != null) {
94                 li.addAttribute(Context.ATTRIBUTE_REPOSITORY, new StringAttribute(repository));
95             }
96             if (path != null) {
97                 li.addAttribute(Context.ATTRIBUTE_PATH, new StringAttribute(path));
98             }
99             launchFlow(li, flowName);
100             // Launch the item
101
}
102         catch (Exception JavaDoc e) {
103             log.error("Launching flow " + flowName + " failed", e);
104         }
105     }
106
107     /**
108      * Start a flow
109      * @param li the prepared lunchItem
110      * @param flowName the flow to start
111      * @throws FlowDefinionException
112      */

113     public static void launchFlow(LaunchItem li, String JavaDoc flowName) throws FlowDefinionException {
114         FlowDefinitionManager configurator = WorkflowModule.getFlowDefinitionManager();
115         
116         configurator.configure(li, flowName);
117         
118         launchFlow(li);
119     }
120     
121     /**
122      * @param id
123      */

124     public static void proceed(String JavaDoc id) {
125         proceed(id, WorkflowConstants.ACTION_PROCEED);
126     }
127
128     public static void proceed(String JavaDoc id, String JavaDoc action) {
129         proceed(id, action, null);
130     }
131
132     public static void proceed(String JavaDoc id, String JavaDoc action, String JavaDoc comment) {
133         InFlowWorkItem wi = getWorkItem(id);
134         if (wi == null) {
135             log.error("can't proceed workitem [{}]", id);
136             return;
137         }
138         wi.touch();
139         // remove old exception
140
if(wi.containsAttribute(Context.ATTRIBUTE_EXCEPTION)){
141             wi.removeAttribute(Context.ATTRIBUTE_EXCEPTION);
142         }
143         if(wi.containsAttribute(Context.ATTRIBUTE_MESSAGE)){
144             wi.removeAttribute(Context.ATTRIBUTE_MESSAGE);
145         }
146         wi.getAttributes().puts(WorkflowConstants.ATTRIBUTE_ACTION, action);
147         wi.getAttributes().puts(WorkflowConstants.ATTRIBUTE_USERNAME, MgnlContext.getUser().getName());
148         
149         if (StringUtils.isNotEmpty(comment)) {
150             wi.getAttributes().puts(Context.ATTRIBUTE_COMMENT, comment);
151         }
152         proceed(wi);
153     }
154
155     public static void reject(String JavaDoc id, String JavaDoc comment) {
156         proceed(id, WorkflowConstants.ACTION_REJECT, comment);
157     }
158
159     public static void cancel(String JavaDoc id, String JavaDoc comment) {
160         proceed(id, WorkflowConstants.ACTION_CANCEL, comment);
161     }
162
163     /**
164      * Proceed this item
165      * @param wi
166      */

167     public static void proceed(InFlowWorkItem wi) {
168         try {
169             WorkflowModule.getEngine().reply(wi);
170         }
171         catch (Exception JavaDoc e) {
172             log.error("Error while accessing the workflow engine", e);
173         }
174         finally {
175             removeWorkItem(wi);
176         }
177     }
178
179     /**
180      * @param id identifier for the workitem as stored in the engine
181      * @return <code>InFlowWorkItem</code> corresponding to the workitem
182      */

183     public static InFlowWorkItem getWorkItem(String JavaDoc id) {
184         InFlowWorkItem wi = null;
185         try {
186             wi = storage.retrieveWorkItem(StringUtils.EMPTY, FlowExpressionId.fromParseableString(id));
187         }
188         catch (StoreException e) {
189             log.error("can't get the workitem by expression [" + id + "]", e);
190         }
191         return wi;
192     }
193
194     public static String JavaDoc getPath(String JavaDoc id){
195         return storage.createPathFromId(FlowExpressionId.fromParseableString(id));
196     }
197     
198     /**
199      * get all work items for the user
200      * @param userName
201      * @return
202      * @throws Exception
203      */

204     public static List JavaDoc getWorkItems(String JavaDoc userName) throws Exception JavaDoc {
205         if (log.isDebugEnabled()) {
206             log.debug("enter getWorkItems");
207             log.debug("user name = " + userName);
208         }
209
210         long start = System.currentTimeMillis();
211
212         MgnlUser user = (MgnlUser) MgnlContext.getUser();
213         Collection JavaDoc groups = user.getGroups();
214         Collection JavaDoc roles = user.getRoles();
215         StringBuffer JavaDoc queryString = new StringBuffer JavaDoc();
216         queryString.append("//*[(@assignTo=\"");
217         queryString.append(userName);
218         queryString.append("\") or (@participant=\"user-");
219         queryString.append(userName);
220         queryString.append("\" and not(@assignTo))");
221         for (Iterator JavaDoc iter = groups.iterator(); iter.hasNext();) {
222             Object JavaDoc group = iter.next();
223             queryString.append(" or (@participant=\"group-");
224             queryString.append(group);
225             // FIXME
226
// queryString.append("\" and @assignTo!=\"");
227
// queryString.append(userName);
228
queryString.append("\") ");
229         }
230         for (Iterator JavaDoc iter = roles.iterator(); iter.hasNext();) {
231             Object JavaDoc role = iter.next();
232             // FIXME
233
queryString.append(" or (@participant=\"role-");
234             queryString.append(role);
235             // queryString.append("\" and @assignTo!=\"");
236
// queryString.append(userName);
237
queryString.append("\") ");
238         }
239         queryString.append("]");
240
241         if (log.isDebugEnabled()) {
242             log.info("xpath query string = " + queryString);
243         }
244
245         final List JavaDoc doQuery = storage.doQuery(queryString.toString());
246         long end = System.currentTimeMillis();
247         log.debug("Retrieving workitems done. (Took " + (end - start) + " ms)");
248         return doQuery;
249     }
250
251     public static String JavaDoc getId(InFlowItem wi) {
252         return wi.getId().toParseableString();
253     }
254
255     /**
256      * assign work item to a user, if userName = "", then assignment for the workItem will be deleted
257      */

258     public static void assignWorkItemToUser(String JavaDoc id, String JavaDoc userName) {
259         if (id == null || id.length() == 0) {
260             log.error("can not assign work item, invalid express id " + id);
261             return;
262         }
263
264         if (userName == null) {
265             log.info("User name was null");
266             return;
267         }
268
269         InFlowWorkItem wi = getWorkItem(id);
270         if (wi == null) {
271             log.error("can not assign work item, can not retrieve work tiem by express id " + id);
272             return;
273         }
274         assignWorkItemToUser(wi, userName);
275     }
276
277     /**
278      * assign work item to a user, if userName = "", then assignment for the workItem will be deleted
279      */

280     public static void assignWorkItemToUser(InFlowWorkItem wi, String JavaDoc userName) {
281         if (userName == null) {
282             log.info("User name was null");
283             return;
284         }
285
286         try {
287             wi.addAttribute(WorkflowConstants.ATTRIBUTE_ASSIGN_TO, new StringAttribute(userName));
288             storage.storeWorkItem(StringUtils.EMPTY, wi);
289         }
290         catch (Exception JavaDoc e) {
291             log.error("assign work item to user " + userName + " failed.)", e);
292         }
293
294     }
295
296     /**
297      * return a list of workItem for one usre
298      */

299     public static List JavaDoc getUserInbox(String JavaDoc userName) throws Exception JavaDoc {
300         return getWorkItems(userName);
301     }
302
303     /**
304      * return a list of workItem for one group
305      */

306     public static List JavaDoc getGroupInbox(String JavaDoc GroupName) throws Exception JavaDoc {
307         if (log.isDebugEnabled()) {
308             log.debug("enter getGroupInbox");
309             log.debug("GroupName = " + GroupName);
310         }
311
312         StringBuffer JavaDoc queryString = new StringBuffer JavaDoc();
313         queryString.append("//*[@participant=\"group-");
314         queryString.append(GroupName);
315         queryString.append("\"]");
316
317         if (log.isDebugEnabled()) {
318             log.debug("xpath query string = " + queryString);
319         }
320         return storage.doQuery(queryString.toString());
321
322     }
323
324     /**
325      * return a list of workItem for one role
326      */

327     public static List JavaDoc getRoleInbox(String JavaDoc roleName) throws Exception JavaDoc {
328         if (log.isDebugEnabled()) {
329             log.debug("enter getGroupInbox");
330             log.debug("roleName = " + roleName);
331         }
332
333         StringBuffer JavaDoc queryString = new StringBuffer JavaDoc();
334         queryString.append("//*[@participant=\"group-");
335         queryString.append(roleName);
336         queryString.append("\"]");
337
338         if (log.isDebugEnabled()) {
339             log.debug("xpath query string = " + queryString);
340         }
341         return storage.doQuery(queryString.toString());
342     }
343
344     /**
345      * remove one work item by id
346      */

347     private static void removeWorkItem(InFlowWorkItem wi) {
348         try {
349             storage.removeWorkItem(wi.getId());
350         }
351         catch (StoreException e) {
352             log.error("can't remove workitem", e);
353         }
354     }
355
356 }
Popular Tags