Activiti系列(四)实现流程第一步自动提交

1.实现第一步自动提交的理由

对比下面两张流程图可以发现,第二张图更好的体现出了发起申请的这个环节。并且,第二张流程图可以更好的实现组长审批环节退回至发起申请环节,修改申请条件等业务逻辑。

Activiti系列(四)实现流程第一步自动提交_第1张图片
Activiti系列(四)实现流程第一步自动提交_第2张图片

2.实现

新建配置文件activiti-boot.xml


<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
        "
>
    
    
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="history" value="full"/>
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="transactionManager" ref="transactionManager" />
        
        <property name="databaseSchemaUpdate" value="true" />
        
        <property name="jobExecutorActivate" value="false" />

		
        <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 监听器

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 {
	    //如果是流程的第一步,则自动提交
	    PvmActivity targetActivity = findFirstActivity(delegateTask.getProcessDefinitionId());

        if (!targetActivity.getId().equals(delegateTask.getExecution().getCurrentActivityId())) {
            return;
        }
        
        new AutoCompleteCmd(delegateTask.getId(),delegateTask.getVariables(),"发起流程").execute(Context.getCommandContext());
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }
    
	public RepositoryService getRepositoryService(){
        return SpringContextHolder.getBean(RepositoryService.class);
    }

    public PvmActivity findFirstActivity(String processDefinitionId) {
        ProcessDefinitionEntity processDefinitionEntity  = (ProcessDefinitionEntity)
                ((RepositoryServiceImpl)getRepositoryService())
                        .getDeployedProcessDefinition(processDefinitionId);

        ActivityImpl startActivity = processDefinitionEntity.getInitial();

        PvmTransition pvmTransition = startActivity.getOutgoingTransitions().get(0);
        PvmActivity targetActivity = pvmTransition.getDestination();

        if (!"userTask".equals(targetActivity.getProperty("type"))) {
            return null;
        }
        return targetActivity;
    }
}

自动提交实现类

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;
    }
}

启用配置文件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 {
}

你可能感兴趣的:(activiti工作流)