KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > node > ProcessState


1 package org.jbpm.graph.node;
2
3 import java.util.HashSet JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import org.dom4j.Element;
8 import org.jbpm.context.def.VariableAccess;
9 import org.jbpm.context.exe.ContextInstance;
10 import org.jbpm.db.JbpmSession;
11 import org.jbpm.graph.def.Event;
12 import org.jbpm.graph.def.Node;
13 import org.jbpm.graph.def.ProcessDefinition;
14 import org.jbpm.graph.exe.ExecutionContext;
15 import org.jbpm.graph.exe.ProcessInstance;
16 import org.jbpm.graph.exe.Token;
17 import org.jbpm.jpdl.xml.JpdlXmlReader;
18 import org.jbpm.jpdl.xml.Parsable;
19
20 public class ProcessState extends Node implements Parsable {
21
22   private static final long serialVersionUID = 1L;
23
24   transient protected String JavaDoc subProcessName = null;
25   transient protected String JavaDoc subProcessVersion = null;
26   protected ProcessDefinition subProcessDefinition = null;
27   protected Set JavaDoc variableAccesses = null;
28
29   // event types //////////////////////////////////////////////////////////////
30

31   public static final String JavaDoc[] supportedEventTypes = new String JavaDoc[] { Event.EVENTTYPE_SUBPROCESS_CREATED, Event.EVENTTYPE_SUBPROCESS_END,
32       Event.EVENTTYPE_NODE_ENTER, Event.EVENTTYPE_NODE_LEAVE, Event.EVENTTYPE_BEFORE_SIGNAL, Event.EVENTTYPE_AFTER_SIGNAL };
33
34   public String JavaDoc[] getSupportedEventTypes() {
35     return supportedEventTypes;
36   }
37
38   // xml //////////////////////////////////////////////////////////////////////
39

40   public void read(Element processStateElement, JpdlXmlReader jpdlReader) {
41     Element subProcessElement = processStateElement.element("sub-process");
42     if (subProcessElement != null) {
43       subProcessName = subProcessElement.attributeValue("name");
44       subProcessVersion = subProcessElement.attributeValue("version");
45     }
46
47     // if this parsing is done in the context of a process deployment, there is
48
// a database connection to look up the subprocess.
49
// when there is no jbpmSession, the definition will be left null... the
50
// testcase can set it as appropriate.
51
JbpmSession jbpmSession = JbpmSession.getCurrentJbpmSession();
52     if (jbpmSession != null) {
53       
54       // now, we must be able to find the sub-process
55
if (subProcessName != null) {
56         
57         // if the name and the version are specified
58
if (subProcessVersion != null) {
59           
60           try {
61             int version = Integer.parseInt(subProcessVersion);
62             // select that exact process definition as the subprocess definition
63
subProcessDefinition = jbpmSession.getGraphSession().findProcessDefinition(subProcessName, version);
64
65           } catch (NumberFormatException JavaDoc e) {
66             jpdlReader.addWarning("version in process-state was not a number: " + processStateElement.asXML());
67           }
68           
69         } else { // if only the name is specified
70
// select the latest version of that process as the subprocess
71
// definition
72
subProcessDefinition = jbpmSession.getGraphSession().findLatestProcessDefinition(subProcessName);
73         }
74       } else {
75         jpdlReader.addWarning("no sub-process name specified in process-state " + processStateElement.asXML());
76       }
77     }
78
79     this.variableAccesses = new HashSet JavaDoc(jpdlReader.readVariableAccesses(processStateElement));
80   }
81
82   public void execute(ExecutionContext executionContext) {
83     Token superProcessToken = executionContext.getToken();
84
85     // create the subprocess
86
ProcessInstance subProcessInstance = new ProcessInstance(subProcessDefinition);
87     // bind the subprocess to the super-process-token
88
superProcessToken.setSubProcessInstance(subProcessInstance);
89     subProcessInstance.setSuperProcessToken(superProcessToken);
90
91     // fire the subprocess created event
92
fireEvent(Event.EVENTTYPE_SUBPROCESS_CREATED, executionContext);
93
94     // feed the readable variables
95
if ((variableAccesses != null) && (!variableAccesses.isEmpty())) {
96
97       ContextInstance superContextInstance = executionContext.getContextInstance();
98       ContextInstance subContextInstance = subProcessInstance.getContextInstance();
99
100       // loop over all the variable accesses
101
Iterator JavaDoc iter = variableAccesses.iterator();
102       while (iter.hasNext()) {
103         VariableAccess variableAccess = (VariableAccess) iter.next();
104         // if this variable access is readable
105
if (variableAccess.isReadable()) {
106           // the variable is copied from the super process variable name
107
// to the sub process mapped name
108
String JavaDoc variableName = variableAccess.getVariableName();
109           Object JavaDoc value = superContextInstance.getVariable(variableName, superProcessToken);
110           String JavaDoc mappedName = variableAccess.getMappedName();
111           subContextInstance.setVariable(mappedName, value);
112         }
113       }
114     }
115
116     // send the signal to start the subprocess
117
subProcessInstance.signal();
118   }
119
120   public void notifySubProcessEnd(ProcessInstance subProcessInstance) {
121     Token superProcessToken = subProcessInstance.getSuperProcessToken();
122     ExecutionContext executionContext = new ExecutionContext(superProcessToken);
123
124     // feed the readable variables
125
if ((variableAccesses != null) && (!variableAccesses.isEmpty())) {
126
127       ContextInstance superContextInstance = executionContext.getContextInstance();
128       ContextInstance subContextInstance = subProcessInstance.getContextInstance();
129
130       // loop over all the variable accesses
131
Iterator JavaDoc iter = variableAccesses.iterator();
132       while (iter.hasNext()) {
133         VariableAccess variableAccess = (VariableAccess) iter.next();
134         // if this variable access is writable
135
if (variableAccess.isWritable()) {
136           // the variable is copied from the sub process mapped name
137
// to the super process variable name
138
String JavaDoc mappedName = variableAccess.getMappedName();
139           Object JavaDoc value = subContextInstance.getVariable(mappedName);
140           String JavaDoc variableName = variableAccess.getVariableName();
141           superContextInstance.setVariable(variableName, value, superProcessToken);
142         }
143       }
144     }
145
146     // fire the subprocess ended event
147
fireEvent(Event.EVENTTYPE_SUBPROCESS_END, executionContext);
148
149     // remove the subprocess reference
150
superProcessToken.setSubProcessInstance(null);
151
152     // call the subProcessEndAction
153
super.leave(executionContext, getDefaultLeavingTransition());
154   }
155
156   public ProcessDefinition getSubProcessDefinition() {
157     return subProcessDefinition;
158   }
159   public void setSubProcessDefinition(ProcessDefinition subProcessDefinition) {
160     this.subProcessDefinition = subProcessDefinition;
161   }
162 }
163
Popular Tags