package com.qqflow.engine.domain.flow.statemachine; import com.qqflow.engine.domain.flow.enums.ProcessEvent; import com.qqflow.engine.domain.flow.enums.ProcessStatus; import org.springframework.context.annotation.Configuration; import org.springframework.statemachine.config.EnableStateMachineFactory; import org.springframework.statemachine.config.StateMachineConfigurerAdapter; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; import java.util.EnumSet; @Configuration @EnableStateMachineFactory public class ProcessInstanceStateMachineConfig extends StateMachineConfigurerAdapter { @Override public void configure(StateMachineStateConfigurer states) throws Exception { states.withStates() .initial(ProcessStatus.PENDING_RECEIVE) .states(EnumSet.allOf(ProcessStatus.class)); } @Override public void configure(StateMachineTransitionConfigurer transitions) throws Exception { this.configureActiveTransitions(transitions); this.configureTerminalTransitions(transitions); } private void configureActiveTransitions(StateMachineTransitionConfigurer transitions) throws Exception { transitions .withExternal() .source(ProcessStatus.PENDING_RECEIVE).target(ProcessStatus.PENDING).event(ProcessEvent.RECEIVE) .and() .withExternal() .source(ProcessStatus.PENDING).target(ProcessStatus.APPROVED).event(ProcessEvent.APPROVE) .and() .withExternal() .source(ProcessStatus.PENDING).target(ProcessStatus.REJECTED).event(ProcessEvent.REJECT) .and() .withExternal() .source(ProcessStatus.PENDING).target(ProcessStatus.RETURNED).event(ProcessEvent.RETURN) .and() .withExternal() .source(ProcessStatus.RETURNED).target(ProcessStatus.PENDING).event(ProcessEvent.RECEIVE); } private void configureTerminalTransitions(StateMachineTransitionConfigurer transitions) throws Exception { transitions .withExternal() .source(ProcessStatus.APPROVED).target(ProcessStatus.COMPLETED).event(ProcessEvent.COMPLETE) .and() .withExternal() .source(ProcessStatus.PENDING_RECEIVE).target(ProcessStatus.REVOKED).event(ProcessEvent.REVOKE) .and() .withExternal() .source(ProcessStatus.PENDING).target(ProcessStatus.REVOKED).event(ProcessEvent.REVOKE) .and() .withExternal() .source(ProcessStatus.RETURNED).target(ProcessStatus.REVOKED).event(ProcessEvent.REVOKE) .and() .withExternal() .source(ProcessStatus.PENDING_RECEIVE).target(ProcessStatus.TERMINATED).event(ProcessEvent.TERMINATE) .and() .withExternal() .source(ProcessStatus.PENDING).target(ProcessStatus.TERMINATED).event(ProcessEvent.TERMINATE); } }