001: package org.emforge.jbpm;
002:
003: import java.util.Collection;
004: import java.util.LinkedList;
005: import java.util.List;
006:
007: import org.apache.commons.collections.Transformer;
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.emforge.projectmanager.ProjectService;
011: import org.emforge.projectmanager.base.MilestoneDO;
012: import org.emforge.projectmanager.base.ProjectDO;
013: import org.emforge.xfer.StepTO;
014: import org.emforge.xfer.VariableTO;
015: import org.jbpm.context.def.VariableAccess;
016: import org.jbpm.taskmgmt.def.TaskController;
017: import org.jbpm.taskmgmt.exe.TaskInstance;
018:
019: import ru.emdev.EmForge.security.UserFactory;
020:
021: /** Trasformer for converting jBPM TaskInstances into StepTO
022: */
023: public class TaskTransformer implements Transformer {
024: protected final Log logger = LogFactory.getLog(getClass());
025:
026: private ProjectService projectService;
027: private UserFactory userFactory;
028:
029: public TaskTransformer(ProjectService projectService,
030: UserFactory userFactory) {
031: this .projectService = projectService;
032: this .userFactory = userFactory;
033: }
034:
035: public StepTO transform(Object i_input) {
036: TaskInstance task = (TaskInstance) i_input;
037:
038: if (task.getToken().getProcessInstance() == null) {
039: logger.error("Cannot get process for task instance #"
040: + task.getId());
041: }
042:
043: StepTO step = new StepTO();
044: step.setId(task.getId());
045: step.setTaskId(task.getToken().getProcessInstance().getId());
046: step.setActor(task.getActorId());
047:
048: if (task.getSwimlaneInstance() != null) {
049: step.setSwimlane(task.getSwimlaneInstance().getName());
050: }
051:
052: String title = null;
053: if (task.hasVariable(BpmVariable.TITLE.getVariable())
054: && task.getVariable(BpmVariable.TITLE.getVariable()) != null) {
055: title = task.getVariable(BpmVariable.TITLE.getVariable())
056: .toString();
057: }
058: step.setTitle(title);
059:
060: step.setName(task.getName());
061:
062: step.setPriority(new PriorityTransformer()
063: .transform(new Integer(task.getPriority())));
064:
065: step.setPlannedStartTime(task.getStart());
066: step.setActualStartTime(task.getCreate());
067: step.setDueDate(task.getDueDate());
068: step.setHasEnded(task.hasEnded());
069: step.setTaskHelp(task.getDescription());
070: step.setWritableVariables(getStepVariables(task));
071:
072: // get Project
073: ProjectDO project = ProcessTransformer.getProject(task
074: .getToken().getProcessInstance());
075: if (project != null) {
076: step.setProjectName(project.getName());
077:
078: // get milestone
079: MilestoneDO milestone = getMilestone(task);
080: if (milestone != null) {
081: step.setMilestoneName(milestone.getName());
082: }
083: }
084: step.setWorkflowName(task.getToken().getProcessInstance()
085: .getProcessDefinition().getName());
086: step.setWorkflowVersion(task.getToken().getProcessInstance()
087: .getProcessDefinition().getVersion());
088: step.setWorkflowIconLink(ProcessDefTransformer
089: .getWorkflowIconLink(task.getToken()
090: .getProcessInstance().getProcessDefinition()));
091: step.setTaskOwner(ProcessTransformer.getOwner(task.getToken()
092: .getProcessInstance()));
093:
094: // set transitions
095: step.setTransitions(TaskDefinitionTransformer
096: .getTransitions(task.getTask()));
097:
098: return step;
099: }
100:
101: /** Create array of writtable Variables for Step
102: *
103: * @param i_step
104: * @return
105: */
106: @SuppressWarnings("unchecked")
107: private VariableTO[] getStepVariables(TaskInstance task) {
108:
109: List<VariableTO> result = new LinkedList<VariableTO>();
110:
111: // get task controller
112: TaskController taskController = task.getTask()
113: .getTaskController();
114: if (taskController != null) {
115: Collection<VariableAccess> variableAccesses = taskController
116: .getVariableAccesses();
117:
118: VariableTransformer variableTransformer = new VariableTransformer(
119: projectService, userFactory, task.getToken()
120: .getProcessInstance(), null);
121:
122: // fill the list of variables
123: for (VariableAccess variableAccess : variableAccesses) {
124: String mappedName = variableAccess.getMappedName();
125:
126: if (variableAccess.isReadable()
127: && variableAccess.isWritable()) {
128: Object value = task.getVariable(mappedName);
129: VariableTO tfp = variableTransformer.transform(
130: variableAccess, value);
131: result.add(tfp);
132: }
133: }
134: }
135:
136: return result.toArray(new VariableTO[result.size()]);
137: }
138:
139: private MilestoneDO getMilestone(TaskInstance task) {
140: MilestoneDO value = (MilestoneDO) task
141: .getVariable(BpmVariable.MILESTONE.getVariable());
142:
143: return value;
144: }
145: }
|