use of org.alfresco.rest.workflow.api.model.Task in project alfresco-remote-api by Alfresco.
the class TasksImpl method update.
@Override
public Task update(String taskId, Task task, Parameters parameters) {
TaskStateTransition taskAction = null;
List<String> selectedProperties = parameters.getSelectedProperties();
if (selectedProperties.contains("state")) {
taskAction = TaskStateTransition.getTaskActionFromString(task.getState());
}
// Fetch the task unfiltered, we check authorization below
TaskQuery query = activitiProcessEngine.getTaskService().createTaskQuery().taskId(taskId);
org.activiti.engine.task.Task taskInstance = query.singleResult();
if (taskInstance == null) {
// Check if task exists in history, to be able to return appropriate error when trying to update an
// existing completed task vs. an unexisting task vs. unauthorized
boolean taskHasExisted = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery().taskId(taskId).count() > 0;
if (taskHasExisted) {
throw new UnsupportedResourceOperationException("Task with id: " + taskId + " cannot be updated, it's completed");
} else {
throw new EntityNotFoundException(taskId);
}
} else {
String user = AuthenticationUtil.getRunAsUser();
// Check if user is either assignee, owner or admin
boolean authorized = authorityService.isAdminAuthority(user) || user.equals(taskInstance.getOwner()) || user.equals(taskInstance.getAssignee());
Set<String> candidateGroups = new HashSet<String>();
if (!authorized) {
// Check if user is initiator of the process this task is involved with
List<IdentityLink> linksForTask = activitiProcessEngine.getTaskService().getIdentityLinksForTask(taskId);
// the identity-links, there is no reason why we should check candidate using a DB-query
for (IdentityLink link : linksForTask) {
if (user.equals(link.getUserId()) && IdentityLinkType.STARTER.equals(link.getType())) {
authorized = true;
break;
}
// MNT-13276
if ((taskInstance.getAssignee() == null) && (link.getGroupId() != null) && link.getType().equals(IdentityLinkType.CANDIDATE)) {
Set<String> userGroups = authorityService.getAuthoritiesForUser(user);
if (userGroups.contains(link.getGroupId())) {
authorized = true;
break;
}
}
if (taskAction == TaskStateTransition.CLAIMED && link.getGroupId() != null && link.getType().equals(IdentityLinkType.CANDIDATE)) {
candidateGroups.add(link.getGroupId());
}
if (taskAction == TaskStateTransition.CLAIMED && link.getUserId() != null && link.getType().equals(IdentityLinkType.CANDIDATE) && user.equals(link.getUserId())) {
// User is a direct candidate for the task, authorized to claim
authorized = true;
break;
}
}
}
// When claiming, a limited update (set assignee through claim) is allowed
if (!authorized && taskAction == TaskStateTransition.CLAIMED) {
Set<String> userGroups = authorityService.getAuthoritiesForUser(user);
for (String group : candidateGroups) {
if (userGroups.contains(group)) {
authorized = true;
break;
}
}
}
if (!authorized) {
// None of the above conditions are met, not authorized to update task
throw new PermissionDeniedException();
}
}
// Update fields if no action is required
if (taskAction == null) {
// Only update task in Activiti API if actual properties are changed
if (updateTaskProperties(selectedProperties, task, taskInstance)) {
activitiProcessEngine.getTaskService().saveTask(taskInstance);
}
} else {
// Perform actions associated to state transition
if (taskAction != null) {
// look for variables submitted with task action
Map<String, Object> globalVariables = new HashMap<String, Object>();
Map<String, Object> localVariables = new HashMap<String, Object>();
if (selectedProperties.contains("variables") && task.getVariables() != null && task.getVariables().size() > 0) {
for (TaskVariable taskVariable : task.getVariables()) {
taskVariable = convertToTypedVariable(taskVariable, taskInstance);
if (taskVariable.getVariableScope() == VariableScope.GLOBAL) {
globalVariables.put(taskVariable.getName(), taskVariable.getValue());
} else {
localVariables.put(taskVariable.getName(), taskVariable.getValue());
}
}
}
switch(taskAction) {
case CLAIMED:
try {
activitiProcessEngine.getTaskService().claim(taskId, AuthenticationUtil.getRunAsUser());
} catch (ActivitiTaskAlreadyClaimedException atace) {
throw new ConstraintViolatedException("The task is already claimed by another user.");
}
break;
case COMPLETED:
if (localVariables.size() > 0) {
activitiProcessEngine.getTaskService().setVariablesLocal(taskId, localVariables);
}
setOutcome(taskId);
if (globalVariables.size() > 0) {
activitiProcessEngine.getTaskService().complete(taskId, globalVariables);
} else {
activitiProcessEngine.getTaskService().complete(taskId);
}
break;
case DELEGATED:
if (selectedProperties.contains("assignee") && task.getAssignee() != null) {
if (taskInstance.getAssignee() == null || !taskInstance.getAssignee().equals(AuthenticationUtil.getRunAsUser())) {
// Alter assignee before delegating to preserve trail of who actually delegated
activitiProcessEngine.getTaskService().setAssignee(taskId, AuthenticationUtil.getRunAsUser());
}
activitiProcessEngine.getTaskService().delegateTask(taskId, task.getAssignee());
} else {
throw new InvalidArgumentException("When delegating a task, assignee should be selected and provided in the request.");
}
break;
case RESOLVED:
if (localVariables.size() > 0) {
activitiProcessEngine.getTaskService().setVariablesLocal(taskId, localVariables);
}
setOutcome(taskId);
if (globalVariables.size() > 0) {
activitiProcessEngine.getTaskService().resolveTask(taskId, globalVariables);
} else {
activitiProcessEngine.getTaskService().resolveTask(taskId);
}
break;
case UNCLAIMED:
activitiProcessEngine.getTaskService().setAssignee(taskId, null);
break;
}
}
}
Task responseTask = new Task(activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery().taskId(taskId).singleResult());
// if the task is not ended the task state might be pending or resolved
if (responseTask.getEndedAt() == null) {
try {
org.activiti.engine.task.Task runningTask = activitiProcessEngine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
if (runningTask != null) {
if (runningTask.getDelegationState() == DelegationState.PENDING) {
responseTask.setState(TaskStateTransition.DELEGATED.name().toLowerCase());
} else if (runningTask.getDelegationState() == DelegationState.RESOLVED) {
responseTask.setState(TaskStateTransition.RESOLVED.name().toLowerCase());
}
}
} catch (Exception e) {
// ignore the exception
}
}
return responseTask;
}
use of org.alfresco.rest.workflow.api.model.Task in project alfresco-remote-api by Alfresco.
the class TasksImpl method getTasks.
@Override
public CollectionWithPagingInfo<Task> getTasks(Parameters parameters) {
Paging paging = parameters.getPaging();
MapBasedQueryWalker propertyWalker = new MapBasedQueryWalker(TASK_COLLECTION_EQUALS_QUERY_PROPERTIES, TASK_COLLECTION_MATCHES_QUERY_PROPERTIES);
propertyWalker.setSupportedGreaterThanParameters(TASK_COLLECTION_GREATERTHAN_QUERY_PROPERTIES);
propertyWalker.setSupportedGreaterThanOrEqualParameters(TASK_COLLECTION_GREATERTHANOREQUAL_QUERY_PROPERTIES);
propertyWalker.setSupportedLessThanParameters(TASK_COLLECTION_LESSTHAN_QUERY_PROPERTIES);
propertyWalker.setSupportedLessThanOrEqualParameters(TASK_COLLECTION_LESSTHANOREQUAL_QUERY_PROPERTIES);
propertyWalker.enableVariablesSupport(namespaceService, dictionaryService);
if (parameters.getQuery() != null) {
QueryHelper.walk(parameters.getQuery(), propertyWalker);
}
String status = propertyWalker.getProperty("status", WhereClauseParser.EQUALS);
String assignee = propertyWalker.getProperty("assignee", WhereClauseParser.EQUALS);
String assigneeLike = propertyWalker.getProperty("assignee", WhereClauseParser.MATCHES);
String owner = propertyWalker.getProperty("owner", WhereClauseParser.EQUALS);
String ownerLike = propertyWalker.getProperty("owner", WhereClauseParser.MATCHES);
String candidateUser = propertyWalker.getProperty("candidateUser", WhereClauseParser.EQUALS);
String candidateGroup = propertyWalker.getProperty("candidateGroup", WhereClauseParser.EQUALS);
String name = propertyWalker.getProperty("name", WhereClauseParser.EQUALS);
String nameLike = propertyWalker.getProperty("name", WhereClauseParser.MATCHES);
String description = propertyWalker.getProperty("description", WhereClauseParser.EQUALS);
String descriptionLike = propertyWalker.getProperty("description", WhereClauseParser.MATCHES);
Integer priority = propertyWalker.getProperty("priority", WhereClauseParser.EQUALS, Integer.class);
Integer priorityGreaterThanOrEquals = propertyWalker.getProperty("priority", WhereClauseParser.GREATERTHANOREQUALS, Integer.class);
Integer priorityLessThanOrEquals = propertyWalker.getProperty("priority", WhereClauseParser.LESSTHANOREQUALS, Integer.class);
String processInstanceId = propertyWalker.getProperty("processId", WhereClauseParser.EQUALS);
String processInstanceBusinessKey = propertyWalker.getProperty("processBusinessKey", WhereClauseParser.EQUALS);
String processInstanceBusinessKeyLike = propertyWalker.getProperty("processBusinessKey", WhereClauseParser.MATCHES);
String activityDefinitionId = propertyWalker.getProperty("activityDefinitionId", WhereClauseParser.EQUALS);
String activityDefinitionIdLike = propertyWalker.getProperty("activityDefinitionId", WhereClauseParser.MATCHES);
String processDefinitionId = propertyWalker.getProperty("processDefinitionId", WhereClauseParser.EQUALS);
String processDefinitionKey = propertyWalker.getProperty("processDefinitionKey", WhereClauseParser.EQUALS);
String processDefinitionKeyLike = propertyWalker.getProperty("processDefinitionKey", WhereClauseParser.MATCHES);
String processDefinitionName = propertyWalker.getProperty("processDefinitionName", WhereClauseParser.EQUALS);
String processDefinitionNameLike = propertyWalker.getProperty("processDefinitionName", WhereClauseParser.MATCHES);
Date startedAt = propertyWalker.getProperty("startedAt", WhereClauseParser.EQUALS, Date.class);
Date startedAtGreaterThan = propertyWalker.getProperty("startedAt", WhereClauseParser.GREATERTHAN, Date.class);
Date startedAtLessThan = propertyWalker.getProperty("startedAt", WhereClauseParser.LESSTHAN, Date.class);
Date endedAt = propertyWalker.getProperty("endedAt", WhereClauseParser.EQUALS, Date.class);
Date endedAtGreaterThan = propertyWalker.getProperty("endedAt", WhereClauseParser.GREATERTHAN, Date.class);
Date endedAtLessThan = propertyWalker.getProperty("endedAt", WhereClauseParser.LESSTHAN, Date.class);
Date dueAt = propertyWalker.getProperty("dueAt", WhereClauseParser.EQUALS, Date.class);
Date dueAtGreaterThan = propertyWalker.getProperty("dueAt", WhereClauseParser.GREATERTHAN, Date.class);
Date dueAtLessThan = propertyWalker.getProperty("dueAt", WhereClauseParser.LESSTHAN, Date.class);
Boolean includeProcessVariables = propertyWalker.getProperty("includeProcessVariables", WhereClauseParser.EQUALS, Boolean.class);
Boolean includeTaskVariables = propertyWalker.getProperty("includeTaskVariables", WhereClauseParser.EQUALS, Boolean.class);
List<SortColumn> sortList = parameters.getSorting();
SortColumn sortColumn = null;
if (sortList != null && sortList.size() > 0) {
if (sortList.size() != 1) {
throw new InvalidArgumentException("Only one order by parameter is supported");
}
sortColumn = sortList.get(0);
}
List<Task> page = null;
int totalCount = 0;
if (status == null || STATUS_ACTIVE.equals(status)) {
TaskQuery query = activitiProcessEngine.getTaskService().createTaskQuery();
if (assignee != null)
query.taskAssignee(assignee);
if (assigneeLike != null)
query.taskAssigneeLike(assigneeLike);
if (owner != null)
query.taskOwner(owner);
if (ownerLike != null)
query.taskOwner(ownerLike);
if (candidateUser != null) {
Set<String> parents = authorityService.getContainingAuthorities(AuthorityType.GROUP, candidateUser, false);
if (parents != null) {
List<String> authorities = new ArrayList<String>();
authorities.addAll(parents);
// there's a limitation in at least Oracle for using an IN statement with more than 1000 items
if (parents.size() > 1000) {
authorities = authorities.subList(0, 1000);
}
if (authorities.size() > 0) {
query.taskCandidateGroupIn(authorities);
} else {
query.taskCandidateUser(candidateUser);
}
}
}
if (candidateGroup != null)
query.taskCandidateGroup(candidateGroup);
if (name != null)
query.taskName(name);
if (nameLike != null)
query.taskNameLike(nameLike);
if (description != null)
query.taskDescription(description);
if (descriptionLike != null)
query.taskDescriptionLike(descriptionLike);
if (priority != null)
query.taskPriority(priority);
if (priorityGreaterThanOrEquals != null)
query.taskMinPriority(priorityGreaterThanOrEquals);
if (priorityLessThanOrEquals != null)
query.taskMaxPriority(priorityLessThanOrEquals);
if (processInstanceId != null)
query.processInstanceId(processInstanceId);
if (processInstanceBusinessKey != null)
query.processInstanceBusinessKey(processInstanceBusinessKey);
if (processInstanceBusinessKeyLike != null)
query.processInstanceBusinessKeyLike(processInstanceBusinessKeyLike);
if (activityDefinitionId != null)
query.taskDefinitionKey(activityDefinitionId);
if (activityDefinitionIdLike != null)
query.taskDefinitionKey(activityDefinitionIdLike);
if (processDefinitionId != null)
query.processDefinitionId(processDefinitionId);
if (processDefinitionKey != null)
query.processDefinitionKey(processDefinitionKey);
if (processDefinitionKeyLike != null)
query.processDefinitionKeyLike(processDefinitionKeyLike);
if (processDefinitionName != null)
query.processDefinitionName(processDefinitionName);
if (processDefinitionNameLike != null)
query.processDefinitionNameLike(processDefinitionNameLike);
if (dueAt != null)
query.dueDate(dueAt);
if (dueAtGreaterThan != null)
query.dueAfter(dueAtGreaterThan);
if (dueAtLessThan != null)
query.dueBefore(dueAtLessThan);
if (startedAt != null)
query.taskCreatedOn(startedAt);
if (startedAtGreaterThan != null)
query.taskCreatedAfter(startedAtGreaterThan);
if (startedAtLessThan != null)
query.taskCreatedBefore(startedAtLessThan);
if (includeProcessVariables != null && includeProcessVariables) {
query.includeProcessVariables();
}
if (includeTaskVariables != null && includeTaskVariables) {
query.includeTaskLocalVariables();
}
// use the limit set in alfresco-global.properties
query.limitTaskVariables(taskVariablesLimit);
List<QueryVariableHolder> variableProperties = propertyWalker.getVariableProperties();
setQueryUsingVariables(query, variableProperties);
// Add tenant-filtering
if (tenantService.isEnabled()) {
query.processVariableValueEquals(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
}
// Add involvement filtering if user is not admin
if (processInstanceId == null && !authorityService.isAdminAuthority(AuthenticationUtil.getRunAsUser()) && candidateUser == null && candidateGroup == null) {
query.taskInvolvedUser(AuthenticationUtil.getRunAsUser());
}
setSorting(query, sortColumn);
List<org.activiti.engine.task.Task> tasks = query.listPage(paging.getSkipCount(), paging.getMaxItems());
totalCount = (int) query.count();
page = new ArrayList<Task>(tasks.size());
Map<String, TypeDefinition> definitionTypeMap = new HashMap<String, TypeDefinition>();
for (org.activiti.engine.task.Task taskInstance : tasks) {
Task task = new Task(taskInstance);
task.setFormResourceKey(getFormResourceKey(taskInstance));
if ((includeProcessVariables != null && includeProcessVariables) || (includeTaskVariables != null && includeTaskVariables)) {
addVariables(task, includeProcessVariables, includeTaskVariables, taskInstance.getProcessVariables(), taskInstance.getTaskLocalVariables(), definitionTypeMap);
}
page.add(task);
}
} else if (STATUS_COMPLETED.equals(status) || STATUS_ANY.equals(status)) {
// Candidate user and group is only supported with STATUS_ACTIVE
if (candidateUser != null) {
throw new InvalidArgumentException("Filtering on candidateUser is only allowed in combination with status-parameter 'active'");
}
if (candidateGroup != null) {
throw new InvalidArgumentException("Filtering on candidateGroup is only allowed in combination with status-parameter 'active'");
}
HistoricTaskInstanceQuery query = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery();
if (STATUS_COMPLETED.equals(status))
query.finished();
if (assignee != null)
query.taskAssignee(assignee);
if (assigneeLike != null)
query.taskAssigneeLike(assigneeLike);
if (owner != null)
query.taskOwner(owner);
if (ownerLike != null)
query.taskOwnerLike(ownerLike);
if (name != null)
query.taskName(name);
if (nameLike != null)
query.taskNameLike(nameLike);
if (description != null)
query.taskDescription(description);
if (descriptionLike != null)
query.taskDescriptionLike(descriptionLike);
if (priority != null)
query.taskPriority(priority);
if (priorityGreaterThanOrEquals != null)
query.taskMinPriority(priorityGreaterThanOrEquals);
if (priorityLessThanOrEquals != null)
query.taskMaxPriority(priorityLessThanOrEquals);
if (processInstanceId != null)
query.processInstanceId(processInstanceId);
if (processInstanceBusinessKey != null)
query.processInstanceBusinessKey(processInstanceBusinessKey);
if (processInstanceBusinessKeyLike != null)
query.processInstanceBusinessKeyLike(processInstanceBusinessKeyLike);
if (activityDefinitionId != null)
query.taskDefinitionKey(activityDefinitionId);
if (activityDefinitionIdLike != null)
query.taskDefinitionKey(activityDefinitionIdLike);
if (processDefinitionId != null)
query.processDefinitionId(processDefinitionId);
if (processDefinitionKey != null)
query.processDefinitionKey(processDefinitionKey);
if (processDefinitionKeyLike != null)
query.processDefinitionKeyLike(processDefinitionKeyLike);
if (processDefinitionName != null)
query.processDefinitionName(processDefinitionName);
if (processDefinitionNameLike != null)
query.processDefinitionNameLike(processDefinitionNameLike);
if (dueAt != null)
query.taskDueDate(dueAt);
if (dueAtGreaterThan != null)
query.taskDueAfter(dueAtGreaterThan);
if (dueAtLessThan != null)
query.taskDueBefore(dueAtLessThan);
if (startedAt != null)
query.taskCreatedOn(startedAt);
if (startedAtGreaterThan != null)
query.taskCreatedAfter(startedAtGreaterThan);
if (startedAtLessThan != null)
query.taskCreatedBefore(startedAtLessThan);
if (endedAt != null)
query.taskCompletedOn(endedAt);
if (endedAtGreaterThan != null)
query.taskCompletedAfter(endedAtGreaterThan);
if (endedAtLessThan != null)
query.taskCompletedBefore(endedAtLessThan);
if (includeProcessVariables != null && includeProcessVariables) {
query.includeProcessVariables();
}
if (includeTaskVariables != null && includeTaskVariables) {
query.includeTaskLocalVariables();
}
List<QueryVariableHolder> variableProperties = propertyWalker.getVariableProperties();
setQueryUsingVariables(query, variableProperties);
// Add tenant filtering
if (tenantService.isEnabled()) {
query.processVariableValueEquals(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
}
// Add involvement filtering if user is not admin
if (processInstanceId == null && !authorityService.isAdminAuthority(AuthenticationUtil.getRunAsUser())) {
query.taskInvolvedUser(AuthenticationUtil.getRunAsUser());
}
setSorting(query, sortColumn);
List<HistoricTaskInstance> tasks = query.listPage(paging.getSkipCount(), paging.getMaxItems());
totalCount = (int) query.count();
page = new ArrayList<Task>(tasks.size());
Map<String, TypeDefinition> definitionTypeMap = new HashMap<String, TypeDefinition>();
for (HistoricTaskInstance taskInstance : tasks) {
Task task = new Task(taskInstance);
if ((includeProcessVariables != null && includeProcessVariables) || (includeTaskVariables != null && includeTaskVariables)) {
addVariables(task, includeProcessVariables, includeTaskVariables, taskInstance.getProcessVariables(), taskInstance.getTaskLocalVariables(), definitionTypeMap);
}
page.add(task);
}
} else {
throw new InvalidArgumentException("Invalid status parameter: " + status);
}
return CollectionWithPagingInfo.asPaged(paging, page, (page.size() + paging.getSkipCount()) < totalCount, totalCount);
}
use of org.alfresco.rest.workflow.api.model.Task in project alfresco-remote-api by Alfresco.
the class TasksImpl method getTasks.
@Override
public CollectionWithPagingInfo<Task> getTasks(String processId, Parameters parameters) {
Paging paging = parameters.getPaging();
String status = parameters.getParameter("status");
List<SortColumn> sortList = parameters.getSorting();
SortColumn sortColumn = null;
if (sortList != null && sortList.size() > 0) {
if (sortList.size() != 1) {
throw new InvalidArgumentException("Only one order by parameter is supported");
}
sortColumn = sortList.get(0);
}
validateIfUserAllowedToWorkWithProcess(processId);
List<Task> page = null;
int totalCount = 0;
if (status == null || STATUS_ACTIVE.equals(status)) {
TaskQuery query = activitiProcessEngine.getTaskService().createTaskQuery();
query.processInstanceId(processId);
setSorting(query, sortColumn);
List<org.activiti.engine.task.Task> tasks = query.listPage(paging.getSkipCount(), paging.getMaxItems());
totalCount = (int) query.count();
page = new ArrayList<Task>(tasks.size());
for (org.activiti.engine.task.Task taskInstance : tasks) {
Task task = new Task(taskInstance);
task.setFormResourceKey(getFormResourceKey(taskInstance));
page.add(task);
}
} else if (STATUS_COMPLETED.equals(status) || STATUS_ANY.equals(status)) {
HistoricTaskInstanceQuery query = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery();
if (STATUS_COMPLETED.equals(status))
query.finished();
query.processInstanceId(processId);
// Add tenant filtering
if (tenantService.isEnabled()) {
query.processVariableValueEquals(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
}
setSorting(query, sortColumn);
List<HistoricTaskInstance> tasks = query.listPage(paging.getSkipCount(), paging.getMaxItems());
totalCount = (int) query.count();
page = new ArrayList<Task>(tasks.size());
for (HistoricTaskInstance taskInstance : tasks) {
Task task = new Task(taskInstance);
page.add(task);
}
} else {
throw new InvalidArgumentException("Invalid status parameter: " + status);
}
return CollectionWithPagingInfo.asPaged(paging, page, (page.size() + paging.getSkipCount()) < totalCount, totalCount);
}
Aggregations