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.CollectionUtils;
008: import org.apache.commons.collections.Transformer;
009: import org.apache.commons.lang.StringUtils;
010: import org.emforge.projectmanager.ProjectService;
011: import org.emforge.xfer.StepTO;
012: import org.emforge.xfer.TransitionTO;
013: import org.emforge.xfer.VariableTO;
014: import org.jbpm.context.def.VariableAccess;
015: import org.jbpm.graph.def.Node;
016: import org.jbpm.taskmgmt.def.Task;
017: import org.jbpm.taskmgmt.def.TaskController;
018:
019: import ru.emdev.EmForge.security.UserFactory;
020:
021: /** Transformer, used for converting jBPM Task Definition into StepTO
022: *
023: */
024: class TaskDefinitionTransformer implements Transformer {
025: private ProjectService projectService;
026: private UserFactory userFactory;
027: private String projectName;
028:
029: public TaskDefinitionTransformer(ProjectService projectService,
030: UserFactory userFactory, String projectName) {
031: this .projectService = projectService;
032: this .userFactory = userFactory;
033: this .projectName = projectName;
034: }
035:
036: public StepTO transform(Object i_input) {
037: Task task = (Task) i_input;
038: StepTO step = new StepTO();
039:
040: // only few fields will be filled - since it is not for task instance - but for task definition
041: step.setName(task.getName());
042: step.setPriority(new PriorityTransformer()
043: .transform(new Integer(task.getPriority())));
044: step.setTaskHelp(task.getDescription());
045: step.setProjectName(projectName);
046:
047: // convert variables
048: List<VariableAccess> vars = getWritableVariables(task);
049: List<VariableTO> writableVariables = new LinkedList<VariableTO>();
050: VariableTransformer varTransformer = new VariableTransformer(
051: projectService, userFactory, null, projectName);
052: CollectionUtils
053: .collect(vars, varTransformer, writableVariables);
054:
055: step.setWritableVariables(writableVariables
056: .toArray(new VariableTO[writableVariables.size()]));
057:
058: step.setWorkflowName(task.getProcessDefinition().getName());
059: step.setWorkflowVersion(task.getProcessDefinition()
060: .getVersion());
061:
062: // convert transitions
063: step.setTransitions(getTransitions(task));
064:
065: return step;
066: }
067:
068: @SuppressWarnings("unchecked")
069: static protected TransitionTO[] getTransitions(Task task) {
070: Node node = task.getTaskNode();
071:
072: // task may be stored not in task node - but in start state.
073: if (node == null) {
074: node = task.getStartState();
075: }
076:
077: Collection<org.jbpm.graph.def.Transition> jbpmTransitions = node
078: .getLeavingTransitionsList();
079: TransitionTO[] transitions = new TransitionTO[jbpmTransitions
080: .size()];
081: int i = 0;
082:
083: for (org.jbpm.graph.def.Transition jbpmTransition : jbpmTransitions) {
084: String label = jbpmTransition.getName();
085: if (StringUtils.isEmpty(label)) {
086: label = jbpmTransition.getFrom().getName()
087: + " (go to \""
088: + jbpmTransition.getTo().getName() + "\")";
089: }
090:
091: transitions[i++] = new TransitionTO(jbpmTransition
092: .getName(), label);
093: }
094:
095: return transitions;
096: }
097:
098: @SuppressWarnings("unchecked")
099: static protected List<VariableAccess> getWritableVariables(
100: Task i_task) {
101: List<VariableAccess> writableVariables = new LinkedList<VariableAccess>();
102:
103: TaskController jbpmTaskController = i_task.getTaskController();
104: if (jbpmTaskController != null) {
105: Collection<VariableAccess> variableAccesses = jbpmTaskController
106: .getVariableAccesses();
107:
108: for (VariableAccess var : variableAccesses) {
109: if (var.isReadable() && var.isWritable()) {
110: writableVariables.add(var);
111: }
112: }
113: }
114:
115: return writableVariables;
116: }
117: }
|