1 package org.jbpm.webapp.bean; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 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 taskFormParameters; 29 List availableTransitions; 30 List 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 this.taskFormParameters = (List ) JsfHelper.getSessionAttribute("taskFormParameters"); 44 } 45 46 public void initialize(TaskInstance taskInstance) { 47 this.taskInstance = taskInstance; 48 this.taskInstanceId = taskInstance.getId(); 49 50 this.taskFormParameters = taskInstance.getTaskFormParameters(); 52 JsfHelper.setSessionAttribute("taskFormParameters",taskFormParameters); 54 55 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 (); 66 Iterator 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 save() { 81 log.debug("saving the task parameters "+taskFormParameters); 82 83 Map submitParameters = new HashMap (); 86 Iterator 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 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 JsfHelper.removeSessionAttribute("taskFormParameters"); 108 109 graphSession.saveProcessInstance(taskInstance.getTaskMgmtInstance().getProcessInstance()); 110 111 return "home"; 112 } 113 114 public String saveAndClose() { 115 save(); 117 118 String 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 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 msg = "New tasks have been assigned to: "; 144 Iterator 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 getTaskFormParameters() { 172 return taskFormParameters; 173 } 174 public List getAvailableTransitions() { 175 return availableTransitions; 176 } 177 public void setAvailableTransitions(List availableTransitions) { 178 this.availableTransitions = availableTransitions; 179 } 180 public List 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 |