Activiti series (4) automatic submission of the first step of the process

Keywords: Spring xml encoding Apache

1. Reasons for realizing the first step of automatic submission

Comparing the following two flow charts, we can see that the second chart better reflects the process of application initiation. In addition, the second flow chart can better realize the business logic of the leader approval phase returning to the initiating application phase and modifying the application conditions.


2. implementation

Create a new configuration file activiti-boot.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        "
>
    <!--Technological process activiti Related configuration-->
    <!-- spring Responsible for creating the configuration file for the process engine -->
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="history" value="full"/>
        <!-- data source -->
        <property name="dataSource" ref="dataSource" />
        <!-- Configure transaction manager, unify transactions -->
        <property name="transactionManager" ref="transactionManager" />
        <!-- Set the table creation policy. If there is no table, the table will be created automatically -->
        <property name="databaseSchemaUpdate" value="true" />
        <!-- Whether to start jobExecutor -->
        <property name="jobExecutorActivate" value="false" />

        <!--Global listener -->
        <property name="eventListeners">
            <list>
                <bean class="com.example.demo.listener.AutoCompleteFirstTaskEventListener" />
            </list>
        </property>

    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"   destroy-method="close">
        <property name="driverClassName" value="${spring.datasource.driver-class-name}" />
        <property name="url"  value="${spring.datasource.url}" />
        <property name="username" value="${spring.datasource.username}" />
        <property name="password" value="${spring.datasource.password}" />
    </bean>

</beans>

AutoCompleteFirstTaskEventListener listener

import com.example.demo.cmd.AutoCompleteCmd;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.TaskEntity;

public class AutoCompleteFirstTaskEventListener implements ActivitiEventListener {
    @Override
    public void onEvent(ActivitiEvent activitiEvent) {
        if (!(activitiEvent instanceof ActivitiEntityEventImpl)) {
            return;
        }

        ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) activitiEvent;
        Object entity = activitiEntityEventImpl.getEntity();

        if (!(entity instanceof TaskEntity)) {
            return;
        }

        TaskEntity taskEntity = (TaskEntity) entity;

        try {
            switch (activitiEvent.getType()) {
                case TASK_CREATED:
                    this.onCreate(taskEntity);
                    break;
            }
        } catch (Exception ex) {
        }
    }

    private void onCreate(DelegateTask delegateTask) throws Exception {
        new AutoCompleteCmd(delegateTask.getId(),delegateTask.getVariables(),"Initiation process").execute(Context.getCommandContext());
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }
}

Auto commit implementation class

import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.task.IdentityLinkType;

import java.util.Map;

public class AutoCompleteCmd implements Command<Object> {
    private String taskId;
    private String comment;
    private Map<String,Object> variables;

    public AutoCompleteCmd(String taskId,Map<String,Object> variables,String comment){
        this.taskId = taskId;
        this.variables = variables;
        this.comment = comment;
    }

    @Override
    public Object execute(CommandContext commandContext) {
        TaskEntity taskEntity = commandContext.getTaskEntityManager().findTaskById(taskId);

        if(variables != null){
            taskEntity.setExecutionVariables(variables);
        }

        taskEntity.fireEvent(TaskListener.EVENTNAME_COMPLETE);

        if(Authentication.getAuthenticatedUserId() != null && taskEntity.getProcessInstanceId() != null){
            taskEntity.getProcessInstance().involveUser(Authentication.getAuthenticatedUserId(), IdentityLinkType.PARTICIPANT);
        }

        Context.getCommandContext().getTaskEntityManager().deleteTask(taskEntity,comment,false);

        if(taskEntity.getExecutionId() != null){
            ExecutionEntity executionEntity = taskEntity.getExecution();
            executionEntity.removeTask(taskEntity);
            executionEntity.signal(null,null);
        }

        return null;
    }
}

Enable profile activiti-boot.xml

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations = {"classpath:activiti-boot.xml"})
public class ActivitiConfig {
}

Posted by LAMP on Fri, 03 Apr 2020 06:19:19 -0700