Activiti Summary (ii) Common apis

1. Query all defined processes

Query the defined process through the repositoryService. CreateProcessDefinitionQuery () to create a process definition query, query, can set a lot of query conditions.

Here is an example query:

public
 List getAllProcess(a) {
        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
        // Add sort criteria
        query.orderByProcessDefinitionVersion().desc();

// // Add paging query
        query.listPage(0.10);
        List list = query.list();
        for (ProcessDefinition pd : list) {
            System.out.println(pd.getKey());
        }
        return list;
    }
Copy the code

There are only two query criteria in the above code, see the Activiti documentation for more query methods

As is evident from the documentation, defined queries support chained queries (all queries in Activiti support chained queries)

Create a process instance

Create process instance use runtimeService startProcessInstanceByKey method, this method is overridden a lot of time, support a variety of ways to create a process instance. Refer to the Activiti documentation for details.

// Set the process creator
        identityService.setAuthenticatedUserId(userId);
        // Create the process
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, vars);
        // Return the process instance ID
        return processInstance.getId();
Copy the code
  • Create process instance can use identityService. Before setting process authors setAuthenticatedUserId method.

  • Once the instance is created, it is automatically streamed to the first task of the process.

  • There are several ways to create an instance. You can create an instance based on a key definition or a process ID. By default, creating a process based on a process key uses the latest version of the process.

  • After a process is redefined, the created process is not affected

3. Query tasks according to task completers

To query a task, create a task query using the createTaskQuery method, and then set the query criteria. See activiti for more details on the API.

/** * Query tasks by task completer *@paramAssignee */
    @Override
    public void queryTasks(String assignee) {
        TaskQuery taskQuery = taskService.createTaskQuery();
        // Add a query condition transactor
        taskQuery.taskAssignee(assignee);
        // Query all
        List list = taskQuery.list();

        for (Task task : list) {
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId()).singleResult();
            System.out.println("Task ID:" + task.getId() + "----- Task name:" + task.getName() + "------ Mission finisher." +
                    task.getAssignee() + "------ Task owner (assigner)" + task.getOwner() + "------- Process creator"+ processInstance.getStartUserId()); task.getProcessVariables(); }}Copy the code

4. Claim and complete the task

The claim and complete functions.

/** * Claim task *@paramTaskId indicates the taskId *@paramAssignee task claimant */
    @Override
    public void claimTask(String taskId, String assignee) {
        taskService.claim(taskId, assignee);
    }

    /** * Complete the task *@paramTaskId indicates the taskId *@paramVaris Adds the process control variable */
    @Override
    public void completeTask(String taskId, HashMap varis) {
        taskService.complete(taskId, varis);
    }
Copy the code
  • useclaimMethod, it should be noted that tasks that have been set by Assignee are invoked againclaimMethod will report an error.

Query the ongoing process instance

Apis associated with querying ongoing processes are usually invoked through a RuntimeService. To query the process instance, create the process query as usual, and then set the search criteria. You can refer to the official documentation for detailed use of the API.

 /** * Query ongoing process instances *@paramParams query parameters: userId, processInstanceId, and other fields */
    @Override
    public void queryProcess(HashMap params) {

        // Get the process instance query
        ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();

        for(String key : params.keySet()){

            if (key.equals("userId")) {// Query by user ID
                processInstanceQuery.involvedUser(params.get(key).toString());/ / user id
            }
            else if(key.equals("processInstanceId")){
                processInstanceQuery.processInstanceId(params.get(key).toString());/ / user id
            }
        }

        List processInstances = processInstanceQuery.list();
        for (ProcessInstance processInstance : processInstances) {
            System.out.println("Process instance ID:" + processInstance.getId() + "-- -- -- -- -- - >" +
                    "Process definition ID:" + processInstance.getProcessDefinitionId() + "-- -- -- -- -- - >" +
                    "Process start time :" + processInstance.getStartTime() + "-- -- -- -- -- - >" +
                    "Process Creator :" + processInstance.getStartUserId() + "-- -- -- -- -- - >" +
                    "Current Task:"+ processInstance.getActivityId() ); }}Copy the code

6. Historical Query process and Historical query Task

  1. Querying historical process-related apis is usually done byHistoryServiceTo invoke. To query the historical process instance, create the process query as usual, and then set the search criteria. You can refer to the official documentation for detailed use of the API.
/** * Query history process *@paramParams Query parameters: include fields such as userId */
    @Override
    public void queryHistoricProcess(HashMap params) {
        HistoricProcessInstanceQuery processInstanceQuery = historyService.createHistoricProcessInstanceQuery();

        for(String key : params.keySet()){

            if (key.equals("userId")) {// Query by user ID
                processInstanceQuery.involvedUser(params.get(key).toString());/ / user id
            }
        }
        processInstanceQuery.finished();// The finished process
        List processInstances = processInstanceQuery.list();
        for (HistoricProcessInstance processInstance : processInstances) {
            System.out.println("Process instance ID:" + processInstance.getId() + "-- -- -- -- -- - >" +
                    "Process definition ID:" + processInstance.getProcessDefinitionId() + "-- -- -- -- -- - >" +
                    "Process start time :" + processInstance.getStartTime() + "-- -- -- -- -- - >" +
                    "Process end time :" + processInstance.getEndTime() + "-- -- -- -- -- - >" +
                    "Process Creator :"+ processInstance.getStartUserId()); }}Copy the code
  • By default, the historical process query queries all processes, both running and finished.
  1. The process of querying historical tasks is similar to that of querying historical tasks
/** * Query historical tasks by task Assignee (or query tasks by task Owner) */
    @Override
    public void queryHistoricTasks(String assignee, String owner) {
        // Create the history task query
        HistoricTaskInstanceQuery historicTaskInstancesQuery = historyService.createHistoricTaskInstanceQuery();
        // Query by task completer
        historicTaskInstancesQuery.taskAssignee(assignee);
        // Query by task owner
        historicTaskInstancesQuery.taskOwner(owner);
        List  historicTaskInstances = historicTaskInstancesQuery.list();

        for(HistoricTaskInstance pi:historicTaskInstances){
            System.out.println("Task instance ID:" + pi.getId() + "-- -- -- -- -- - >" +
                    "Task Definition ID" + pi.getProcessDefinitionId() + "-- -- -- -- -- - >" +
                    "Task name :"+pi.getName() + "-- -- -- -- -- - >" +
                    "Agent :"+pi.getAssignee() + "-- -- -- -- -- - >" +
                    "Task Owner:"+ pi.getOwner() ); }}Copy the code

Undo a process

The undo process actually deletes an unfinished process, as follows

    /** * Undo a process based on the process instance ID *@paramProcessInstanceId processInstanceId *@paramTaskId Uses the task instance ID to undo a process * if the incoming process instance ID is empty@paramReason Reason for process deletion */
    public void deleteProcess(String processInstanceId, String taskId, String reason){
        if(processInstanceId ! =null){
            runtimeService.deleteProcessInstance(processInstanceId, reason);
        }
        else{
            // Obtain the task instance based on the task instance ID
            Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
            // Obtain the process instance ID of the task based on the task instanceruntimeService.deleteProcessInstance(task.getProcessInstanceId(), reason); }}Copy the code