KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > taskmgmt > def > TaskController


1 package org.jbpm.taskmgmt.def;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
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 /**
18  * is a controller for one task. this object either delegates to a custom
19  * {@link org.jbpm.taskmgmt.def.TaskControllerHandler} or it is configured
20  * with {@link org.jbpm.context.def.VariableAccess}s to perform the default
21  * behaviour of the controller functionality for a task.
22  */

23 public class TaskController implements Serializable JavaDoc {
24
25   private static final long serialVersionUID = 1L;
26   
27   private long id = 0;
28
29   /**
30    * allows the user to specify a custom task controller handler. if this is
31    * specified, the other member variables are ignored. so either a
32    * taskControllerDelegation is specified or the variable- and signalMappings
33    * are specified, but not both.
34    */

35   private Delegation taskControllerDelegation = null;
36
37   /**
38    * maps process variable names (java.lang.String) to VariableAccess objects.
39    */

40   private List JavaDoc variableAccesses = null;
41   
42   public TaskController() {
43   }
44   
45   /**
46    * extract the list of TaskFormParameter's from the process variables.
47    */

48   public List JavaDoc getTaskFormParameters(TaskInstance taskInstance) {
49     List JavaDoc 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 JavaDoc();
57
58       Token token = taskInstance.getToken();
59       ProcessInstance processInstance = token.getProcessInstance();
60       ContextInstance contextInstance = (ContextInstance) processInstance.getInstance(ContextInstance.class);
61
62       Iterator JavaDoc iter = variableAccesses.iterator();
63       while (iter.hasNext()) {
64         VariableAccess variableAccess = (VariableAccess) iter.next();
65         if (variableAccess.isReadable()) {
66           Object JavaDoc value = contextInstance.getVariable(variableAccess.getVariableName(), token);
67           taskFormParameters.add(new TaskFormParameter(variableAccess, value));
68         }
69       }
70     }
71     return taskFormParameters;
72   }
73
74   /**
75    * update the process variables from the the task-parameter input.
76    */

77   public void submitParameters(Map JavaDoc 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       // loop over all the variableAccesses elements
89
String JavaDoc missingParameters = null;
90       Iterator JavaDoc iter = variableAccesses.iterator();
91       while (iter.hasNext()) {
92         VariableAccess variableAccess = (VariableAccess) iter.next();
93         String JavaDoc parameterName = variableAccess.getMappedName();
94
95         // first check if the required variables are present
96
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         // then do the update
107
if ( (variableAccess.isWritable())
108              && (parameters.get(parameterName)!=null)
109            ) {
110           contextInstance.setVariable(variableAccess.getVariableName(), parameters.get(parameterName), token);
111         }
112       }
113
114       // if there are missing, required parameters, throw an IllegalArgumentException
115
if (missingParameters!=null) {
116         throw new IllegalArgumentException JavaDoc("missing parameters: "+missingParameters);
117       }
118     }
119   }
120   
121   public boolean hasVariableAccess(String JavaDoc variableName) {
122     if (variableAccesses!=null) {
123       Iterator JavaDoc 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 JavaDoc 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 JavaDoc variableAccesses) {
148     this.variableAccesses = variableAccesses;
149   }
150 }
151
Popular Tags