package com.chinaztt.mes.plan.state.masterproductionschedule;
|
|
import com.chinaztt.mes.plan.entity.MasterProductionSchedule;
|
import com.chinaztt.mes.plan.state.masterproductionschedule.constant.MasterProductionScheduleEvents;
|
import com.chinaztt.mes.plan.state.masterproductionschedule.constant.MasterProductionScheduleStates;
|
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.statemachine.StateMachineContext;
|
import org.springframework.statemachine.StateMachinePersist;
|
import org.springframework.statemachine.config.EnableStateMachineFactory;
|
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
|
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
import org.springframework.statemachine.persist.DefaultStateMachinePersister;
|
import org.springframework.statemachine.persist.StateMachinePersister;
|
import org.springframework.statemachine.support.DefaultStateMachineContext;
|
|
import java.util.EnumSet;
|
|
/**
|
* @author ZTT
|
*/
|
@Configuration
|
@EnableStateMachineFactory(name = "masterproductionscheduleStateMachineFactory")
|
public class MasterProductionScheduleStateMachineConfig extends EnumStateMachineConfigurerAdapter<MasterProductionScheduleStates, MasterProductionScheduleEvents> {
|
public final static String MACHINE_ID = "masterproductionscheduleStateMachine";
|
|
@Override
|
public void configure(StateMachineStateConfigurer<MasterProductionScheduleStates, MasterProductionScheduleEvents> states) throws Exception {
|
states.withStates()
|
.initial(MasterProductionScheduleStates.PENDING)
|
.states(EnumSet.allOf(MasterProductionScheduleStates.class));
|
}
|
|
@Override
|
public void configure(StateMachineTransitionConfigurer<MasterProductionScheduleStates, MasterProductionScheduleEvents> transitions) throws Exception {
|
transitions
|
.withExternal()
|
.source(MasterProductionScheduleStates.PENDING).target(MasterProductionScheduleStates.PROCESSED)
|
.event(MasterProductionScheduleEvents.PROCESSED)
|
.and()
|
.withExternal()
|
.source(MasterProductionScheduleStates.PROCESSED).target(MasterProductionScheduleStates.PENDING)
|
.event(MasterProductionScheduleEvents.PENDING)
|
.and()
|
.withExternal()
|
.source(MasterProductionScheduleStates.PROCESSED).target(MasterProductionScheduleStates.CANCELED)
|
.event(MasterProductionScheduleEvents.CANCELED)
|
.and()
|
.withExternal()
|
.source(MasterProductionScheduleStates.PENDING).target(MasterProductionScheduleStates.CANCELED)
|
.event(MasterProductionScheduleEvents.CANCELED);
|
}
|
|
@Override
|
public void configure(StateMachineConfigurationConfigurer<MasterProductionScheduleStates, MasterProductionScheduleEvents> config)
|
throws Exception {
|
config.withConfiguration().machineId(MACHINE_ID);
|
}
|
|
/**
|
* 持久化配置
|
*
|
* @return
|
*/
|
@Bean(name = "MasterProductionSchedulePersister")
|
public StateMachinePersister<MasterProductionScheduleStates, MasterProductionScheduleEvents, MasterProductionSchedule> persister() {
|
return new DefaultStateMachinePersister<>(new StateMachinePersist<MasterProductionScheduleStates, MasterProductionScheduleEvents, MasterProductionSchedule>() {
|
@Override
|
public void write(StateMachineContext<MasterProductionScheduleStates, MasterProductionScheduleEvents> stateMachineContext, MasterProductionSchedule masterProductionSchedule) throws Exception {
|
|
}
|
|
@Override
|
public StateMachineContext<MasterProductionScheduleStates, MasterProductionScheduleEvents> read(MasterProductionSchedule masterProductionSchedule) throws Exception {
|
StateMachineContext<MasterProductionScheduleStates, MasterProductionScheduleEvents> result = new DefaultStateMachineContext<MasterProductionScheduleStates, MasterProductionScheduleEvents>(MasterProductionScheduleStates.getEnum(masterProductionSchedule.getState()),
|
null, null, null, null, MACHINE_ID);
|
return result;
|
}
|
});
|
}
|
}
|