Java tutorial
/** * The Clican-Pluto software suit is Copyright 2009, Clican Company and individual contributors, and is licensed under the GNU LGPL. * * @author wei.zhang * */ package com.clican.pluto.fsm.engine.state; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.mvel2.MVEL; import org.springframework.transaction.annotation.Transactional; import com.clican.pluto.fsm.engine.IState; import com.clican.pluto.fsm.enumeration.EventType; import com.clican.pluto.fsm.enumeration.Parameters; import com.clican.pluto.fsm.enumeration.Status; import com.clican.pluto.fsm.listener.TaskListener; import com.clican.pluto.fsm.model.Event; import com.clican.pluto.fsm.model.Session; import com.clican.pluto.fsm.model.State; import com.clican.pluto.fsm.model.Task; import com.clican.pluto.fsm.model.Variable; /** * ? * <p> * DefaultState????assignees????????????Scope * Context?? * * @author wei.zhang * */ public class TaskStateImpl extends DefaultStateImpl { private String assignees; protected String taskName; protected String taskType; private List<TaskListener> taskListeners; public void setAssignees(String assignees) { this.assignees = assignees; } public void setTaskName(String taskName) { this.taskName = taskName; } public void setTaskType(String taskType) { this.taskType = taskType; } public void setTaskListeners(List<TaskListener> taskListeners) { this.taskListeners = taskListeners; } @Transactional public void onStart(Session session, IState previousState, Event event) { super.onStart(session, previousState, event); State state = this.getLatestState(event.getState().getSession()); if (Status.convert(state.getStatus()) != Status.ACTIVE) { return; } this.assignTasks(state, event); } /** * ?? * * @param state * @param event * @return */ protected List<Task> assignTasks(State state, Event event) { List<Task> tasks = new ArrayList<Task>(); Calendar current = Calendar.getInstance(); Session session = state.getSession(); Set<Variable> variableSet = session.getVariableSet(); Map<String, Object> vars = new HashMap<String, Object>(); for (Variable var : variableSet) { vars.put(var.getName(), var.getValue()); } variableSet = event.getVariableSet(); for (Variable var : variableSet) { vars.put(var.getName(), var.getValue()); } String assigneesValue = (String) MVEL.eval(assignees, vars); String[] assignees = assigneesValue.split(","); String taskNameValue; if (StringUtils.isEmpty(taskName)) { taskNameValue = getName(); } else { taskNameValue = (String) MVEL.eval(taskName, vars); } String taskTypeValue; if (StringUtils.isEmpty(taskType)) { taskTypeValue = getName(); } else { taskTypeValue = (String) MVEL.eval(taskType, vars); } for (String assignee : assignees) { Task task = newTask(state, assignee.toString(), current.getTime(), taskNameValue, taskTypeValue); tasks.add(task); } if (tasks.size() == 0) { throw new RuntimeException("There is no task assigned"); } return tasks; } /** * * * @param state * ?? * @param assignee * ? * @param assignTime * ? * @param taskNameValue * ?? * @param taskTypeValue * * @return */ protected Task newTask(State state, String assignee, Date assignTime, String taskNameValue, String taskTypeValue) { Task task = new Task(); task.setAssignee(assignee); task.setAssignTime(assignTime); task.setName(taskNameValue); task.setType(taskTypeValue); task.setState(state); task.setEndTime(new Date()); try { if (taskListeners != null) { for (TaskListener taskListener : taskListeners) { taskListener.beforeAssignTask(task); } } taskDao.save(task); if (state.getTaskSet() == null) { state.setTaskSet(new HashSet<Task>()); } state.getTaskSet().add(task); stateDao.save(state); return task; } finally { if (taskListeners != null) { for (TaskListener taskListener : taskListeners) { taskListener.afterAssignTask(task); } } } } /** * ??task???taskDao???taskEvent???? * * @param task * @param event */ protected void handleTask(Task task, Event event) { Calendar current = Calendar.getInstance(); task.setCompleteTime(current.getTime()); taskDao.save(task); } @Transactional public void handle(Event event) { propagateVariables(event); EventType eventType = EventType.convert(event.getEventType()); if (eventType == EventType.JOB) { Set<Task> taskSet = event.getState().getTaskSet(); for (Task task : taskSet) { if (task.getCompleteTime() == null) { // ??Task setTaskVariable(task, event.getVariableSet()); try { if (taskListeners != null) { for (TaskListener taskListener : taskListeners) { taskListener.beforeHandleTask(task, event); } } handleTask(task, event); } finally { if (taskListeners != null) { for (TaskListener taskListener : taskListeners) { taskListener.afterHandleTask(task, event); } } } } } } else if (eventType == EventType.TASK) { Long taskId = (Long) this.getVariableValue(Parameters.TASK_ID.getParameter(), event, false); Task task = taskDao.findTaskById(taskId); // ??Task setTaskVariable(task, event.getVariableSet()); try { if (taskListeners != null) { for (TaskListener taskListener : taskListeners) { taskListener.beforeHandleTask(task, event); } } handleTask(task, event); } finally { if (taskListeners != null) { for (TaskListener taskListener : taskListeners) { taskListener.afterHandleTask(task, event); } } } } else { throw new RuntimeException("Unsupported Event [" + event + "]"); } List<Task> notCompletedTask = taskDao.findActiveTasksByState(event.getState()); if (notCompletedTask.size() == 0) { onEnd(event); } } private void setTaskVariable(Task task, Set<Variable> varialbleSet) { for (Variable va : varialbleSet) { taskDao.setVariable(task, va.getName(), va.getValue()); } } protected void recordLastVoteResultAndSuggestion(Long sessionId, String voteResult, String suggestion, String lastAuditor) { Session session = sessionDao.findSessionById(sessionId); sessionDao.setVariable(session, Parameters.VOTE_RESULT.getParameter(), voteResult); sessionDao.setVariable(session, Parameters.AUDIT_SUGGESTION.getParameter(), suggestion); sessionDao.setVariable(session, "lastAuditor", lastAuditor); } } // $Id$