KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > webapp > bean > TaskBean


1 package org.jbpm.webapp.bean;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.faces.model.SelectItem;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.jbpm.db.GraphSession;
14 import org.jbpm.db.JbpmSession;
15 import org.jbpm.db.TaskMgmtSession;
16 import org.jbpm.graph.def.Transition;
17 import org.jbpm.graph.exe.ProcessInstance;
18 import org.jbpm.logging.exe.LoggingInstance;
19 import org.jbpm.taskmgmt.def.TaskController;
20 import org.jbpm.taskmgmt.exe.TaskFormParameter;
21 import org.jbpm.taskmgmt.exe.TaskInstance;
22 import org.jbpm.taskmgmt.log.TaskAssignLog;
23 import org.jbpm.webapp.context.Context;
24
25 public class TaskBean {
26
27   UserBean userBean = null;
28   List JavaDoc taskFormParameters;
29   List JavaDoc availableTransitions;
30   List JavaDoc availableTransitionItems;
31   TaskInstance taskInstance;
32   long taskInstanceId;
33   
34   GraphSession graphSession;
35   TaskMgmtSession taskMgmtSession;
36   
37   public TaskBean() {
38     JbpmSession jbpmSession = Context.getPersistenceContext().getJbpmSession();
39     this.graphSession = jbpmSession.getGraphSession();
40     this.taskMgmtSession = jbpmSession.getTaskMgmtSession();
41
42     // get the parameters from the session
43
this.taskFormParameters = (List JavaDoc) JsfHelper.getSessionAttribute("taskFormParameters");
44   }
45
46   public void initialize(TaskInstance taskInstance) {
47     this.taskInstance = taskInstance;
48     this.taskInstanceId = taskInstance.getId();
49     
50     // set the parameters
51
this.taskFormParameters = taskInstance.getTaskFormParameters();
52     // store the parameters in the session
53
JsfHelper.setSessionAttribute("taskFormParameters",taskFormParameters);
54     
55     // get the available transitions
56
availableTransitions = null;
57     
58     availableTransitions = taskInstance.getAvailableTransitions();
59     if ( (availableTransitions!=null)
60          && (availableTransitions.size()<=1)
61        ) {
62       availableTransitions = null;
63       availableTransitionItems = null;
64     } else {
65       availableTransitionItems = new ArrayList JavaDoc();
66       Iterator JavaDoc iter = availableTransitions.iterator();
67       while (iter.hasNext()) {
68         Transition transition = (Transition) iter.next();
69         SelectItem transitionItem = new SelectItem();
70         transitionItem.setValue(transition.getName());
71         transitionItem.setLabel(transition.getName());
72         transitionItem.setDisabled(false);
73         availableTransitionItems.add(transitionItem);
74       }
75     }
76     
77     log.debug("initialized availableTransitions "+availableTransitions);
78   }
79
80   public String JavaDoc save() {
81     log.debug("saving the task parameters "+taskFormParameters);
82     
83     // collect the parameter values from the values that were updated in the
84
// parameters by jsf.
85
Map JavaDoc submitParameters = new HashMap JavaDoc();
86     Iterator JavaDoc iter = taskFormParameters.iterator();
87     while (iter.hasNext()) {
88       TaskFormParameter taskFormParameter = (TaskFormParameter) iter.next();
89       
90       if ( (taskFormParameter.isWritable())
91            && (taskFormParameter.getValue()!=null)
92          ) {
93         log.debug("submitting ["+taskFormParameter.getLabel()+"]="+taskFormParameter.getValue());
94         submitParameters.put(taskFormParameter.getLabel(), taskFormParameter.getValue());
95       } else {
96         log.debug("ignoring unwritable ["+taskFormParameter.getLabel()+"]");
97       }
98     }
99
100     // submit the parameters in the jbpm task controller
101
TaskInstance taskInstance = taskMgmtSession.loadTaskInstance(taskInstanceId);
102     TaskController taskController = taskInstance.getTask().getTaskController();
103     log.debug("submitting task parameters: "+submitParameters);
104     taskController.submitParameters(submitParameters, taskInstance);
105
106     // remove the parameters from the session
107
JsfHelper.removeSessionAttribute("taskFormParameters");
108     
109     graphSession.saveProcessInstance(taskInstance.getTaskMgmtInstance().getProcessInstance());
110
111     return "home";
112   }
113
114   public String JavaDoc saveAndClose() {
115     // save
116
save();
117
118     // close the task instance
119
String JavaDoc transitionButton = JsfHelper.getParameter("taskform:transitionButton");
120     log.debug("Submitted button:" + transitionButton);
121     TaskInstance taskInstance = taskMgmtSession.loadTaskInstance(taskInstanceId);
122     if ("Save and Close Task".equals(transitionButton)) {
123       taskInstance.end();
124     } else {
125       taskInstance.end(transitionButton);
126     }
127     
128     ProcessInstance processInstance = taskInstance.getTaskMgmtInstance().getProcessInstance();
129     if (processInstance.hasEnded()) {
130       JsfHelper.addMessage("The process has finished.");
131     }
132
133     LoggingInstance loggingInstance = processInstance.getLoggingInstance();
134     List JavaDoc assignmentLogs = loggingInstance.getLogs(TaskAssignLog.class);
135     
136     log.debug("assignmentlogs: "+assignmentLogs);
137     
138     if (assignmentLogs.size()==1) {
139       TaskAssignLog taskAssignLog = (TaskAssignLog) assignmentLogs.get(0);
140       JsfHelper.addMessage("A new task has been assigned to '"+taskAssignLog.getTaskNewActorId()+"'");
141       
142     } else if (assignmentLogs.size()>1) {
143       String JavaDoc msg = "New tasks have been assigned to: ";
144       Iterator JavaDoc iter = assignmentLogs.iterator();
145       while (iter.hasNext()) {
146         TaskAssignLog taskAssignLog = (TaskAssignLog) iter.next();
147         msg += taskAssignLog.getActorId();
148         if (iter.hasNext()) msg +=", ";
149       }
150       msg +=".";
151       JsfHelper.addMessage(msg);
152     }
153
154     graphSession.saveProcessInstance(taskInstance.getTaskMgmtInstance().getProcessInstance());
155
156     return "home";
157   }
158   
159   public long getTaskInstanceId() {
160     return taskInstanceId;
161   }
162   public void setTaskInstanceId(long taskInstanceId) {
163     this.taskInstanceId = taskInstanceId;
164   }
165   public UserBean getUserBean() {
166     return userBean;
167   }
168   public void setUserBean(UserBean userBean) {
169     this.userBean = userBean;
170   }
171   public List JavaDoc getTaskFormParameters() {
172     return taskFormParameters;
173   }
174   public List JavaDoc getAvailableTransitions() {
175     return availableTransitions;
176   }
177   public void setAvailableTransitions(List JavaDoc availableTransitions) {
178     this.availableTransitions = availableTransitions;
179   }
180   public List JavaDoc getAvailableTransitionItems() {
181     return availableTransitionItems;
182   }
183   public TaskInstance getTaskInstance() {
184     return taskInstance;
185   }
186   
187   private static final Log log = LogFactory.getLog(TaskBean.class);
188   
189 }
190
Popular Tags