return result;
}
public DataHandler provideForm(FormAuthorityRef ref)
{
Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
try
{
TaskService taskService = processEngine.getTaskService();
Task task = taskService.getTask(ref.getReferenceId());
// access the processdefition
TaskImpl cast = ((TaskImpl) task);
ExecutionImpl processInstance = cast.getProcessInstance();
String processInstanceId = processInstance.getId();
String processId = processInstance.getProcessDefinition().getId();
RepositoryService repoService = processEngine.getRepositoryService();
ProcessDefinitionQuery query = repoService.createProcessDefinitionQuery();
query.processDefinitionId(processId);
ProcessDefinition procDef = query.uniqueResult();
// check if a template exists
String name = task.getFormResourceName();
InputStream template = repoService.getResourceAsStream(
procDef.getDeploymentId(), name
);
// merge template with process variables
if(template==null)
throw new IllegalArgumentException("Task form resource '"+name+"' doesn't exist.");
Map<String, Object> processContext = new HashMap<String, Object>(); // empty default
ExecutionService execService = processEngine.getExecutionService();
Set<String> varNames = execService.getVariableNames(processInstanceId);
if(varNames!=null)
processContext = execService.getVariables(processInstanceId, varNames);
// plugin context
StringBuilder action = getBaseUrl();
action.append("/form/task/");
action.append( ref.getReferenceId() );
action.append("/complete");
Map<String, Object> renderContext = new HashMap<String,Object>();
// form directive
FormDirective formDirective = new FormDirective();
formDirective.setAction( action.toString() );
renderContext.put(FORM_DIRECTIVE_KEY, formDirective);
// outcome directive
// TODO: Fix when https://jira.jboss.org/jira/browse/JBPM-2220 is done
OutcomeDirective outcomeDirective = new OutcomeDirective();
List<Transition> transitions =
((ExecutionImpl) processInstance).getActivity().getOutgoingTransitions();
for(Transition t : transitions)
{
String outcomeName = t.getName()!=null ? t.getName() : "to_"+t.getDestination().getName();
outcomeDirective.getValues().add(outcomeName);
}
renderContext.put(OUTCOME_DIRECTIVE_NAME, outcomeDirective);
// process variables
renderContext.putAll(processContext);
DataHandler result = processTemplate(name, template, renderContext);
return result;
}
finally
{
env.close();
}
}