1 package org.jbpm.graph.node; 2 3 import java.util.HashSet ; 4 import java.util.Iterator ; 5 import java.util.Set ; 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 subProcessName = null; 25 transient protected String subProcessVersion = null; 26 protected ProcessDefinition subProcessDefinition = null; 27 protected Set variableAccesses = null; 28 29 31 public static final String [] supportedEventTypes = new String [] { 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 [] getSupportedEventTypes() { 35 return supportedEventTypes; 36 } 37 38 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 JbpmSession jbpmSession = JbpmSession.getCurrentJbpmSession(); 52 if (jbpmSession != null) { 53 54 if (subProcessName != null) { 56 57 if (subProcessVersion != null) { 59 60 try { 61 int version = Integer.parseInt(subProcessVersion); 62 subProcessDefinition = jbpmSession.getGraphSession().findProcessDefinition(subProcessName, version); 64 65 } catch (NumberFormatException e) { 66 jpdlReader.addWarning("version in process-state was not a number: " + processStateElement.asXML()); 67 } 68 69 } else { 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 (jpdlReader.readVariableAccesses(processStateElement)); 80 } 81 82 public void execute(ExecutionContext executionContext) { 83 Token superProcessToken = executionContext.getToken(); 84 85 ProcessInstance subProcessInstance = new ProcessInstance(subProcessDefinition); 87 superProcessToken.setSubProcessInstance(subProcessInstance); 89 subProcessInstance.setSuperProcessToken(superProcessToken); 90 91 fireEvent(Event.EVENTTYPE_SUBPROCESS_CREATED, executionContext); 93 94 if ((variableAccesses != null) && (!variableAccesses.isEmpty())) { 96 97 ContextInstance superContextInstance = executionContext.getContextInstance(); 98 ContextInstance subContextInstance = subProcessInstance.getContextInstance(); 99 100 Iterator iter = variableAccesses.iterator(); 102 while (iter.hasNext()) { 103 VariableAccess variableAccess = (VariableAccess) iter.next(); 104 if (variableAccess.isReadable()) { 106 String variableName = variableAccess.getVariableName(); 109 Object value = superContextInstance.getVariable(variableName, superProcessToken); 110 String mappedName = variableAccess.getMappedName(); 111 subContextInstance.setVariable(mappedName, value); 112 } 113 } 114 } 115 116 subProcessInstance.signal(); 118 } 119 120 public void notifySubProcessEnd(ProcessInstance subProcessInstance) { 121 Token superProcessToken = subProcessInstance.getSuperProcessToken(); 122 ExecutionContext executionContext = new ExecutionContext(superProcessToken); 123 124 if ((variableAccesses != null) && (!variableAccesses.isEmpty())) { 126 127 ContextInstance superContextInstance = executionContext.getContextInstance(); 128 ContextInstance subContextInstance = subProcessInstance.getContextInstance(); 129 130 Iterator iter = variableAccesses.iterator(); 132 while (iter.hasNext()) { 133 VariableAccess variableAccess = (VariableAccess) iter.next(); 134 if (variableAccess.isWritable()) { 136 String mappedName = variableAccess.getMappedName(); 139 Object value = subContextInstance.getVariable(mappedName); 140 String variableName = variableAccess.getVariableName(); 141 superContextInstance.setVariable(variableName, value, superProcessToken); 142 } 143 } 144 } 145 146 fireEvent(Event.EVENTTYPE_SUBPROCESS_END, executionContext); 148 149 superProcessToken.setSubProcessInstance(null); 151 152 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 |