use of org.kie.api.task.model.Content in project jbpm by kiegroup.
the class RemoveTaskDataCommand method execute.
@SuppressWarnings("unchecked")
@Override
public Void execute(Context cntxt) {
TaskContext context = (TaskContext) cntxt;
TaskEventSupport taskEventSupport = context.getTaskEventSupport();
TaskPersistenceContext persistenceContext = context.getPersistenceContext();
Task task = persistenceContext.findTask(taskId);
// security check
if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context)) {
throw new PermissionDeniedException("User " + userId + " is not business admin of task " + taskId);
}
long contentId = task.getTaskData().getDocumentContentId();
if (!input) {
contentId = task.getTaskData().getOutputContentId();
}
Content outputContent = persistenceContext.findContent(contentId);
Map<String, Object> initialContent = new HashMap<>();
Map<String, Object> mergedContent = new HashMap<>();
if (outputContent != null) {
ContentMarshallerContext mcontext = context.getTaskContentService().getMarshallerContext(task);
Object unmarshalledObject = ContentMarshallerHelper.unmarshall(outputContent.getContent(), mcontext.getEnvironment(), mcontext.getClassloader());
if (unmarshalledObject != null && unmarshalledObject instanceof Map) {
mergedContent.putAll(((Map<String, Object>) unmarshalledObject));
// set initial content for the sake of listeners
initialContent.putAll(mergedContent);
variableNames.forEach(name -> mergedContent.remove(name));
}
ContentData outputContentData = ContentMarshallerHelper.marshal(task, mergedContent, mcontext.getEnvironment());
((InternalContent) outputContent).setContent(outputContentData.getContent());
persistenceContext.persistContent(outputContent);
}
if (input) {
taskEventSupport.fireBeforeTaskInputVariablesChanged(task, context, initialContent);
((InternalTaskData) task.getTaskData()).setTaskInputVariables(mergedContent);
taskEventSupport.fireAfterTaskInputVariablesChanged(task, context, mergedContent);
} else {
taskEventSupport.fireBeforeTaskOutputVariablesChanged(task, context, initialContent);
((InternalTaskData) task.getTaskData()).setTaskOutputVariables(mergedContent);
taskEventSupport.fireAfterTaskOutputVariablesChanged(task, context, mergedContent);
}
return null;
}
use of org.kie.api.task.model.Content in project jbpm by kiegroup.
the class UpdateTaskCommand method execute.
@SuppressWarnings("unchecked")
@Override
public Void execute(Context cntxt) {
TaskContext context = (TaskContext) cntxt;
TaskEventSupport taskEventSupport = context.getTaskEventSupport();
TaskPersistenceContext persistenceContext = context.getPersistenceContext();
Task task = persistenceContext.findTask(taskId);
// security check
if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context) && !isOwner(userId, task.getPeopleAssignments().getPotentialOwners(), task.getTaskData().getActualOwner(), context)) {
throw new PermissionDeniedException("User " + userId + " is not business admin or potential owner of task " + taskId);
}
taskEventSupport.fireBeforeTaskUpdated(task, context);
// process task meta data
if (userTask.getFormName() != null) {
((InternalTask) task).setFormName(userTask.getFormName());
}
if (userTask.getName() != null) {
((InternalTask) task).setName(userTask.getName());
}
if (userTask.getDescription() != null) {
((InternalTask) task).setDescription(userTask.getDescription());
}
if (userTask.getPriority() != null) {
((InternalTask) task).setPriority(userTask.getPriority());
}
if (userTask.getDueDate() != null) {
((InternalTaskData) task.getTaskData()).setExpirationTime(userTask.getDueDate());
}
// process task inputs
long inputContentId = task.getTaskData().getDocumentContentId();
Content inputContent = persistenceContext.findContent(inputContentId);
Map<String, Object> mergedContent = inputs;
if (inputs != null) {
if (inputContent == null) {
ContentMarshallerContext mcontext = context.getTaskContentService().getMarshallerContext(task);
ContentData outputContentData = ContentMarshallerHelper.marshal(task, inputs, mcontext.getEnvironment());
Content content = TaskModelProvider.getFactory().newContent();
((InternalContent) content).setContent(outputContentData.getContent());
persistenceContext.persistContent(content);
((InternalTaskData) task.getTaskData()).setOutput(content.getId(), outputContentData);
} else {
ContentMarshallerContext mcontext = context.getTaskContentService().getMarshallerContext(task);
Object unmarshalledObject = ContentMarshallerHelper.unmarshall(inputContent.getContent(), mcontext.getEnvironment(), mcontext.getClassloader());
if (unmarshalledObject != null && unmarshalledObject instanceof Map) {
((Map<String, Object>) unmarshalledObject).putAll(inputs);
mergedContent = ((Map<String, Object>) unmarshalledObject);
}
ContentData outputContentData = ContentMarshallerHelper.marshal(task, unmarshalledObject, mcontext.getEnvironment());
((InternalContent) inputContent).setContent(outputContentData.getContent());
persistenceContext.persistContent(inputContent);
}
((InternalTaskData) task.getTaskData()).setTaskInputVariables(mergedContent);
}
if (outputs != null) {
// process task outputs
context.getTaskContentService().addOutputContent(taskId, outputs);
}
persistenceContext.updateTask(task);
// finally trigger event support after the updates
taskEventSupport.fireAfterTaskUpdated(task, context);
return null;
}
use of org.kie.api.task.model.Content in project jbpm by kiegroup.
the class FormProviderServiceImpl method getFormDisplayTask.
@Override
@SuppressWarnings("unchecked")
public String getFormDisplayTask(long taskId) {
Task task = taskService.getTaskById(taskId);
if (task == null) {
return "";
}
String name = task.getName();
final String deploymentId = task.getTaskData().getDeploymentId();
final String processId = task.getTaskData().getProcessId();
ProcessDefinition processDesc = null;
if (deploymentId != null && processId != null) {
processDesc = dataService.getProcessesByDeploymentIdProcessId(deploymentId, processId);
}
Map<String, Object> renderContext = new HashMap<String, Object>();
ContentMarshallerContext marshallerContext = getMarshallerContext(task);
// read task variables
Object input = null;
long inputContentId = task.getTaskData().getDocumentContentId();
if (inputContentId != -1) {
Content content = taskService.getContentById(inputContentId);
input = ContentMarshallerHelper.unmarshall(content.getContent(), marshallerContext.getEnvironment(), marshallerContext.getClassloader());
}
if (input == null) {
input = new HashMap<String, Object>();
}
Object output = null;
long outputContentId = task.getTaskData().getOutputContentId();
if (outputContentId != -1) {
Content content = taskService.getContentById(outputContentId);
output = ContentMarshallerHelper.unmarshall(content.getContent(), marshallerContext.getEnvironment(), marshallerContext.getClassloader());
}
if (output == null) {
output = new HashMap<String, Object>();
}
// prepare task variables for rendering
Map<String, Object> finalOutput = new HashMap<String, Object>();
if (processId != null && !processId.equals("")) {
// If task has an associated process let's merge the outputs
Map<String, String> taskOutputMappings = bpmn2Service.getTaskOutputMappings(deploymentId, processId, task.getName());
if (taskOutputMappings == null) {
taskOutputMappings = new HashMap<String, String>();
}
// process mappings with the value that can be stored in the output Content
for (String key : taskOutputMappings.keySet()) {
Object value = ((Map<String, Object>) output).get(key);
if (value == null) {
value = "";
}
finalOutput.put(key, value);
}
} else if (output instanceof Map && !((Map) output).isEmpty()) {
// If the task doesn't belongs to any project BUT it has outputs let's add them directly to the rendering context.
finalOutput.putAll((Map<String, Object>) output);
}
// merge template with process variables
renderContext.put("task", task);
renderContext.put("marshallerContext", marshallerContext);
// add all inputs as direct entries
if (input instanceof Map) {
renderContext.put("inputs", input);
for (Map.Entry<String, Object> inputVar : ((Map<String, Object>) input).entrySet()) {
renderContext.put(inputVar.getKey(), inputVar.getValue());
}
} else {
renderContext.put("input", input);
}
// add all outputs as direct entries
renderContext.put("outputs", finalOutput);
for (Map.Entry<String, Object> outputVar : ((Map<String, Object>) finalOutput).entrySet()) {
renderContext.put(outputVar.getKey(), outputVar.getValue());
}
// find form
for (FormProvider provider : providers) {
String template = provider.render(name, task, processDesc, renderContext);
if (!StringUtils.isEmpty(template)) {
return template;
}
}
logger.warn("Unable to find form to render for task '{}' on process '{}'", name, processDesc == null ? "" : processDesc.getName());
return "";
}
use of org.kie.api.task.model.Content in project jbpm by kiegroup.
the class HumanTaskResolver method run.
@Override
public void run() {
System.out.println(pid + " running tasks");
// "sales-rep" reviews request
TaskService taskService1 = getTaskService();
List<TaskSummary> tasks1 = taskService1.getTasksAssignedAsPotentialOwner("sales", "en-UK");
TaskSummary task1 = selectTaskForProcessInstance(tasks1);
System.out.println("Sales-rep executing task " + task1.getName() + "(" + task1.getId() + ": " + task1.getDescription() + ")");
taskService1.claim(task1.getId(), "sales-rep");
taskService1.start(task1.getId(), "sales-rep");
Map<String, Object> results = new HashMap<String, Object>();
results.put("comment", "Agreed, existing laptop needs replacing");
results.put("outcome", "Accept");
taskService1.complete(task1.getId(), "sales-rep", results);
TaskService taskService2 = getTaskService();
// "krisv" approves result
List<TaskSummary> tasks2 = taskService2.getTasksAssignedAsPotentialOwner("krisv", "en-UK");
TaskSummary task2 = selectTaskForProcessInstance(tasks2);
System.out.println("krisv executing task " + task2.getName() + "(" + task2.getId() + ": " + task2.getDescription() + ")");
taskService2.start(task2.getId(), "krisv");
results = new HashMap<String, Object>();
results.put("outcome", "Agree");
taskService2.complete(task2.getId(), "krisv", results);
TaskService taskService3 = getTaskService();
// "john" as manager reviews request
List<TaskSummary> tasks3 = taskService3.getTasksAssignedAsPotentialOwner("john", "en-UK");
TaskSummary task3 = selectTaskForProcessInstance(tasks3);
System.out.println("john executing task " + task3.getName() + "(" + task3.getId() + ": " + task3.getDescription() + ")");
taskService3.claim(task3.getId(), "john");
taskService3.start(task3.getId(), "john");
results = new HashMap<String, Object>();
results.put("outcome", "Agree");
taskService3.complete(task3.getId(), "john", results);
TaskService taskService4 = getTaskService();
// "sales-rep" gets notification
List<TaskSummary> tasks4 = taskService4.getTasksAssignedAsPotentialOwner("sales-rep", "en-UK");
TaskSummary task4 = selectTaskForProcessInstance(tasks4);
System.out.println("sales-rep executing task " + task4.getName() + "(" + task4.getId() + ": " + task4.getDescription() + ")");
taskService4.start(task4.getId(), "sales-rep");
Task task = taskService4.getTaskById(task4.getId());
Content content = taskService4.getContentById(task.getTaskData().getDocumentContentId());
Object result = ContentMarshallerHelper.unmarshall(content.getContent(), null);
Assert.assertNotNull(result);
taskService4.complete(task4.getId(), "sales-rep", null);
System.out.println("Process instance completed");
runtime.close();
latch.countDown();
}
use of org.kie.api.task.model.Content in project jbpm by kiegroup.
the class NonManagedTaskEventListener method processTaskState.
public void processTaskState(Task task) {
long workItemId = task.getTaskData().getWorkItemId();
if (task.getTaskData().getStatus() == Status.Completed) {
String userId = task.getTaskData().getActualOwner().getId();
Map<String, Object> results = new HashMap<String, Object>();
long contentId = task.getTaskData().getOutputContentId();
if (contentId != -1) {
Content content = taskService.getContentById(contentId);
Object result = ContentMarshallerHelper.unmarshall(content.getContent(), ksession.getEnvironment(), ksession.getClass().getClassLoader());
results.put("Result", result);
if (result instanceof Map) {
Map<?, ?> map = (Map<?, ?>) result;
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (entry.getKey() instanceof String) {
results.put((String) entry.getKey(), entry.getValue());
}
}
}
results.put("ActorId", userId);
ksession.getWorkItemManager().completeWorkItem(task.getTaskData().getWorkItemId(), results);
} else {
results.put("ActorId", userId);
ksession.getWorkItemManager().completeWorkItem(workItemId, results);
}
} else {
ksession.getWorkItemManager().abortWorkItem(workItemId);
}
}
Aggregations