Java Code Examples for org.activiti.engine.history.HistoricTaskInstanceQuery#singleResult()

The following examples show how to use org.activiti.engine.history.HistoricTaskInstanceQuery#singleResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HistoricTaskAndVariablesQueryTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment
public void testOrQueryMultipleVariableValues() {
  if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
    Map<String, Object> startMap = new HashMap<String, Object>();
    startMap.put("processVar", true);
    startMap.put("anotherProcessVar", 123);
    runtimeService.startProcessInstanceByKey("oneTaskProcess", startMap);

    startMap.put("anotherProcessVar", 999);
    runtimeService.startProcessInstanceByKey("oneTaskProcess", startMap);

    HistoricTaskInstanceQuery query0 = historyService.createHistoricTaskInstanceQuery().includeProcessVariables().or();
    for (int i = 0; i < 20; i++) {
      query0 = query0.processVariableValueEquals("anotherProcessVar", i);
    }
    query0 = query0.endOr();
    assertNull(query0.singleResult());

    HistoricTaskInstanceQuery query1 = historyService.createHistoricTaskInstanceQuery().includeProcessVariables().or().processVariableValueEquals("anotherProcessVar", 123);
    for (int i = 0; i < 20; i++) {
      query1 = query1.processVariableValueEquals("anotherProcessVar", i);
    }
    query1 = query1.endOr();
    HistoricTaskInstance task = query1.singleResult();
    assertEquals(2, task.getProcessVariables().size());
    assertEquals(true, task.getProcessVariables().get("processVar"));
    assertEquals(123, task.getProcessVariables().get("anotherProcessVar"));
  }
}
 
Example 2
Source File: ActivitiUtil.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param localId String
 * @return HistoricTaskInstance
 */
public HistoricTaskInstance getHistoricTaskInstance(String localId)
{
    HistoricTaskInstanceQuery taskQuery =  historyService.createHistoricTaskInstanceQuery()
        .taskId(localId);
    if(!deployWorkflowsInTenant) {
    	taskQuery.processVariableValueEquals(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
    }
    return taskQuery.singleResult();
}
 
Example 3
Source File: HistoricTaskInstanceVariableDataResource.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public RestVariable getVariableFromRequest(boolean includeBinary, String taskId, String variableName, String scope, HttpServletRequest request) {
  RestVariableScope variableScope = RestVariable.getScopeFromString(scope);
  HistoricTaskInstanceQuery taskQuery = historyService.createHistoricTaskInstanceQuery().taskId(taskId);

  if (variableScope != null) {
    if (variableScope == RestVariableScope.GLOBAL) {
      taskQuery.includeProcessVariables();
    } else {
      taskQuery.includeTaskLocalVariables();
    }
  } else {
    taskQuery.includeTaskLocalVariables().includeProcessVariables();
  }

  HistoricTaskInstance taskObject = taskQuery.singleResult();

  if (taskObject == null) {
    throw new ActivitiObjectNotFoundException("Historic task instance '" + taskId + "' couldn't be found.", HistoricTaskInstanceEntity.class);
  }

  Object value = null;
  if (variableScope != null) {
    if (variableScope == RestVariableScope.GLOBAL) {
      value = taskObject.getProcessVariables().get(variableName);
    } else {
      value = taskObject.getTaskLocalVariables().get(variableName);
    }
  } else {
    // look for local task variables first
    if (taskObject.getTaskLocalVariables().containsKey(variableName)) {
      value = taskObject.getTaskLocalVariables().get(variableName);
    } else {
      value = taskObject.getProcessVariables().get(variableName);
    }
  }

  if (value == null) {
    throw new ActivitiObjectNotFoundException("Historic task instance '" + taskId + "' variable value for " + variableName + " couldn't be found.", VariableInstanceEntity.class);
  } else {
    return restResponseFactory.createRestVariable(variableName, value, null, taskId, RestResponseFactory.VARIABLE_HISTORY_TASK, includeBinary);
  }
}
 
Example 4
Source File: TasksImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Get a valid {@link HistoricTaskInstance} based on the given task id. Checks if current logged
 * in user is assignee/owner/involved with the task. In case true was passed for "validIfClaimable", 
 * the task is also valid if the current logged in user is a candidate for claiming the task.
 *  
 * @throws EntityNotFoundException when the task was not found
 * @throws PermissionDeniedException when the current logged in user isn't allowed to access task.
 */
protected HistoricTaskInstance getValidHistoricTask(String taskId)
{
    HistoricTaskInstanceQuery query = activitiProcessEngine.getHistoryService()
        .createHistoricTaskInstanceQuery()
        .taskId(taskId);
    
    if (authorityService.isAdminAuthority(AuthenticationUtil.getRunAsUser())) 
    {
        // Admin is allowed to read all tasks in the current tenant
        if (tenantService.isEnabled()) 
        {
            query.processVariableValueEquals(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
        }
    }
    else
    {
        // If non-admin user, involvement in the task is required (either owner, assignee or externally involved).
        query.taskInvolvedUser(AuthenticationUtil.getRunAsUser());
    }
    
    HistoricTaskInstance taskInstance = query.singleResult();
    
    if (taskInstance == null) 
    {
        // Either the task doesn't exist or the user is not involved directly. We can differentiate by
        // checking if the task exists without applying the additional filtering
        taskInstance =  activitiProcessEngine.getHistoryService()
            .createHistoricTaskInstanceQuery()
            .taskId(taskId)
            .singleResult();
        
        if (taskInstance == null) 
        {
            // Full error message will be "Task with id: 'id' was not found" 
            throw new EntityNotFoundException(taskId); 
        }
        else
        {
            boolean isTaskClaimable = false;
            if (taskInstance.getEndTime() == null) 
            {
                // Task is not yet finished, so potentially claimable. If user is part of a "candidateGroup", the task is accessible to the
                // user regardless of not being involved/owner/assignee
                isTaskClaimable = activitiProcessEngine.getTaskService()
                        .createTaskQuery()
                        .taskCandidateGroupIn(new ArrayList<String>(authorityService.getAuthoritiesForUser(AuthenticationUtil.getRunAsUser())))
                        .taskId(taskId)
                        .count() == 1;
            }
            
            if (isTaskClaimable == false)
            {
                throw new PermissionDeniedException();
            }
        }
    }
    return taskInstance;
}