1 package org.jbpm.taskmgmt.def; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.jbpm.context.def.VariableAccess; 10 import org.jbpm.context.exe.ContextInstance; 11 import org.jbpm.graph.exe.ProcessInstance; 12 import org.jbpm.graph.exe.Token; 13 import org.jbpm.instantiation.Delegation; 14 import org.jbpm.taskmgmt.exe.TaskFormParameter; 15 import org.jbpm.taskmgmt.exe.TaskInstance; 16 17 23 public class TaskController implements Serializable { 24 25 private static final long serialVersionUID = 1L; 26 27 private long id = 0; 28 29 35 private Delegation taskControllerDelegation = null; 36 37 40 private List variableAccesses = null; 41 42 public TaskController() { 43 } 44 45 48 public List getTaskFormParameters(TaskInstance taskInstance) { 49 List taskFormParameters = null; 50 51 if (taskControllerDelegation != null) { 52 TaskControllerHandler taskControllerHandler = (TaskControllerHandler) taskControllerDelegation.instantiate(); 53 taskFormParameters = taskControllerHandler.getTaskFormParameters(taskInstance); 54 55 } else { 56 taskFormParameters = new ArrayList (); 57 58 Token token = taskInstance.getToken(); 59 ProcessInstance processInstance = token.getProcessInstance(); 60 ContextInstance contextInstance = (ContextInstance) processInstance.getInstance(ContextInstance.class); 61 62 Iterator iter = variableAccesses.iterator(); 63 while (iter.hasNext()) { 64 VariableAccess variableAccess = (VariableAccess) iter.next(); 65 if (variableAccess.isReadable()) { 66 Object value = contextInstance.getVariable(variableAccess.getVariableName(), token); 67 taskFormParameters.add(new TaskFormParameter(variableAccess, value)); 68 } 69 } 70 } 71 return taskFormParameters; 72 } 73 74 77 public void submitParameters(Map parameters, TaskInstance taskInstance) { 78 if (taskControllerDelegation != null) { 79 TaskControllerHandler taskControllerHandler = (TaskControllerHandler) taskControllerDelegation.instantiate(); 80 taskControllerHandler.submitParameters(parameters, taskInstance); 81 82 } else { 83 84 Token token = taskInstance.getToken(); 85 ProcessInstance processInstance = token.getProcessInstance(); 86 ContextInstance contextInstance = (ContextInstance) processInstance.getInstance(ContextInstance.class); 87 88 String missingParameters = null; 90 Iterator iter = variableAccesses.iterator(); 91 while (iter.hasNext()) { 92 VariableAccess variableAccess = (VariableAccess) iter.next(); 93 String parameterName = variableAccess.getMappedName(); 94 95 if ( (variableAccess.isRequired()) 97 && (parameters.get(parameterName)==null) 98 ) { 99 if (missingParameters==null) { 100 missingParameters = parameterName; 101 } else { 102 missingParameters += ", "+parameterName; 103 } 104 } 105 106 if ( (variableAccess.isWritable()) 108 && (parameters.get(parameterName)!=null) 109 ) { 110 contextInstance.setVariable(variableAccess.getVariableName(), parameters.get(parameterName), token); 111 } 112 } 113 114 if (missingParameters!=null) { 116 throw new IllegalArgumentException ("missing parameters: "+missingParameters); 117 } 118 } 119 } 120 121 public boolean hasVariableAccess(String variableName) { 122 if (variableAccesses!=null) { 123 Iterator iter = variableAccesses.iterator(); 124 while (iter.hasNext()) { 125 VariableAccess variableAccess = (VariableAccess) iter.next(); 126 if (variableAccess.getVariableName().equals(variableName)) { 127 return true; 128 } 129 } 130 } 131 return false; 132 } 133 134 public List getVariableAccesses() { 135 return variableAccesses; 136 } 137 public Delegation getTaskControllerDelegation() { 138 return taskControllerDelegation; 139 } 140 public void setTaskControllerDelegation(Delegation taskControllerDelegation) { 141 this.taskControllerDelegation = taskControllerDelegation; 142 } 143 public long getId() { 144 return id; 145 } 146 147 public void setVariableAccesses(List variableAccesses) { 148 this.variableAccesses = variableAccesses; 149 } 150 } 151 | Popular Tags |