KickJava   Java API By Example, From Geeks To Geeks.

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


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.commands.CommandsManager;
16 import info.magnolia.context.Context;
17 import info.magnolia.context.MgnlContext;
18 import info.magnolia.module.workflow.jcr.JCRWorkItemAPI;
19 import openwfe.org.embed.impl.engine.AbstractEmbeddedParticipant;
20 import openwfe.org.engine.workitem.CancelItem;
21 import openwfe.org.engine.workitem.InFlowWorkItem;
22 import openwfe.org.engine.workitem.WorkItem;
23
24 import org.apache.commons.chain.Command;
25 import org.apache.commons.lang.StringUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 /**
31  * The Magnolia-specific workflow participant.
32  * @author Jackie Ju
33  * @author Philipp Bracher
34  * @author Nicolas Modrzyk
35  * @author John Mettraux
36  */

37 public class MgnlParticipant extends AbstractEmbeddedParticipant {
38
39     /**
40      * Logger
41      */

42     private static Logger log = LoggerFactory.getLogger(AbstractEmbeddedParticipant.class);
43
44     JCRWorkItemAPI storage = null;
45
46     public MgnlParticipant() throws Exception JavaDoc {
47         super();
48         this.storage = new JCRWorkItemAPI();
49         log.debug("storage = {}", this.storage);
50     }
51
52     public MgnlParticipant(String JavaDoc name) throws Exception JavaDoc {
53         super(name);
54         this.storage = new JCRWorkItemAPI();
55         log.debug("storage = {}", this.storage);
56     }
57
58     public void cancel(CancelItem cancelItem) throws Exception JavaDoc {
59         if (log.isDebugEnabled()) {
60             if (cancelItem.getId() != null) {
61                 log.debug("Cancelling {}", cancelItem.getId().toParseableString());
62             }
63
64         }
65         String JavaDoc parName = cancelItem.getParticipantName();
66         if (!parName.startsWith(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND)) {
67             //
68
// remove workitem from inbox
69
this.storage.removeWorkItem(cancelItem.getId());
70         }
71         // else {
72
// // ignore
73
// }
74
}
75
76     /**
77      * @see openwfe.org.embed.engine.EmbeddedParticipant#consume(openwfe.org.engine.workitem.WorkItem)
78      */

79     public void consume(WorkItem wi) throws Exception JavaDoc {
80
81         // get participant name
82
log.debug("enter consume()..");
83
84         if (wi == null) {
85             log.error("work item is null");
86             return;
87         }
88         String JavaDoc parName = ((InFlowWorkItem) (wi)).getParticipantName();
89
90         log.debug("participant name = {}", parName);
91
92         if (parName.startsWith(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND)) // handle commands
93
{
94             log.info("consume command {}...", parName);
95
96             Context originalContext = null;
97             if (MgnlContext.hasInstance()) {
98                 originalContext = MgnlContext.getInstance();
99             }
100
101             try {
102                 String JavaDoc name = StringUtils.removeStart(parName, WorkflowConstants.PARTICIPANT_PREFIX_COMMAND);
103                 Command c = CommandsManager.getInstance().getCommand(name);
104                 if (c != null) {
105                     log.info("Command has been found through the magnolia catalog: {}", c.getClass().getName());
106
107                     // set parameters in the context
108
// precise what we're talking about here: this is forced to be a System Context :
109
// since we are processing within the workflow enviroment
110
Context context = new WorkItemContext(MgnlContext.getSystemContext(), wi);
111
112                     // remember to reset it after execution
113
MgnlContext.setInstance(context);
114
115                     // execute
116
c.execute(context);
117
118                     WorkflowModule.getEngine().reply((InFlowWorkItem) wi);
119
120                 }
121                 else {
122                     // not found, do in the old ways
123
log.error("No command has been found through the magnolia catalog for name: {}", parName);
124                 }
125
126                 log.info("consume command {} end", parName);
127             }
128             catch (Exception JavaDoc e) {
129                 log.error("consume command failed", e);
130             }
131             finally {
132                 MgnlContext.setInstance(originalContext);
133             }
134         }
135         else {
136             log.debug("storage = {}", this.storage);
137
138             this.storage.storeWorkItem(StringUtils.EMPTY, (InFlowWorkItem) wi);
139         }
140
141         log.debug("leave consume()..");
142
143     }
144
145 }
146
Popular Tags