标签:startup ssi 移动 builder 项目 init 修改 code send
1 import org.springframework.beans.factory.support.StaticListableBeanFactory; 2 import org.springframework.core.task.SyncTaskExecutor; 3 import org.springframework.messaging.Message; 4 import org.springframework.statemachine.StateMachine; 5 import org.springframework.statemachine.config.StateMachineBuilder; 6 import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; 7 import org.springframework.statemachine.listener.StateMachineListenerAdapter; 8 import org.springframework.statemachine.transition.Transition; 9 import org.springframework.util.Assert; 10 11 import java.util.*; 12 13 /** 14 * @author shenjixiaodao 15 */ 16 public class Review { 17 private Long reviewId; 18 private Users user; 19 private String featureCode; 20 private Assets asset; 21 private State state; 22 23 public enum State{ 24 Meeting("项目沟通会"), RiskManager("风控经理审批"), 25 ProdManager("产品经理审批"), Review("评审"), 26 Pass("评审通过"), Reject("否决"), Discussing("待议"); 27 private String desc; 28 State(String desc) { 29 this.desc = desc; 30 } 31 public String getDesc(){return this.desc;} 32 } 33 34 private StateMachine<State, FeatureEvent> machine = null; 35 /** 36 * 触发状态转移动作 37 * @return true:允许状态转移 38 */ 39 public boolean fire(){ 40 return this.fireEvent(FeatureEvent.codeOf(featureCode)); 41 } 42 private boolean fireEvent(FeatureEvent event){ 43 try { 44 if(machine == null) { 45 machine = buildSyncMachine(); 46 machine.addStateListener(new StateMachineListenerAdapter<State, FeatureEvent>() { 47 @Override 48 public void eventNotAccepted(Message<FeatureEvent> msg) { 49 FeatureEvent event = msg.getPayload(); 50 StringBuilder appender = new StringBuilder(); 51 appender.append("【").append(event.featureName()).append("】只能将资产从") 52 .append(event.reviewState()).append("修改为").append(event.nextState()); 53 throw new IllegalArgumentException(appender.toString()); 54 } 55 56 @Override 57 public void transitionEnded(Transition<State, FeatureEvent> transition) { 58 FeatureEvent event = transition.getTrigger().getEvent(); 59 state = event.nextState(); 60 } 61 }); 62 } 63 return machine.sendEvent(event); 64 } catch (Exception e) { 65 throw new RuntimeException(e); 66 } 67 } 68 69 public Review(Integer userId, Integer assetId, String featureCode) { 70 this.user = new Users(); 71 this.user.setId(userId); 72 this.asset = new Assets(); 73 this.asset.setId(assetId); 74 this.featureCode = featureCode; 75 } 76 77 public static Review initial(Integer assetId){ 78 Review review = new Review(); 79 review.asset = new Assets(); 80 review.asset.setId(assetId); 81 review.state = State.Publish; 82 return review; 83 } 84 85 public void setReviewId(Long reviewId) { 86 this.reviewId = reviewId; 87 for(Attachment attachment:attachments) 88 attachment.setReviewId(reviewId); 89 } 90 91 public State state() { 92 return state; 93 } 94 95 public Review state(State state) { 96 this.state = state; 97 return this; 98 } 99 100 Review() { 101 //for ORM 102 } 103 /** 104 * 构建线程同步状态机 105 */ 106 private StateMachine<State, FeatureEvent> buildSyncMachine() throws Exception { 107 Assert.notNull(state,"状态不能为空"); 108 StateMachineBuilder.Builder<State, FeatureEvent> builder = StateMachineBuilder.builder(); 109 builder.configureConfiguration().withConfiguration() 110 .taskExecutor(new SyncTaskExecutor()) 111 .beanFactory(new StaticListableBeanFactory()) 112 .autoStartup(true); 113 //配置状态 114 builder.configureStates() 115 .withStates() 116 .initial(state) 117 .states(EnumSet.allOf(State.class)); 118 //配置状态转移 119 StateMachineTransitionConfigurer<State, FeatureEvent> transition = builder.configureTransitions(); 120 for(FeatureEvent event:FeatureEvent.values()) { 121 transition = transition.withExternal() 122 .source(event.reviewState()) 123 .target(event.nextState()) 124 .event(event) 125 .and(); 126 } 127 return builder.build(); 128 } 129 130 }
1 import org.springframework.beans.factory.annotation.Autowired; 2 import org.springframework.stereotype.Service; 3 import org.springframework.transaction.annotation.Transactional; 4 import java.util.List; 5 6 /** 7 * @author shenjixiaodao 8 */ 9 @Service 10 public class ReviewServiceImpl implements ReviewService { 11 12 @Autowired 13 private ReviewRepository repository; 14 15 @Transactional 16 public void review(Review review) { 17 //// FIXME: 2017/7/10 检查资产是否存在 18 Review lastReview = repository.findLastUpdated(review.asset().getId()); 19 boolean accept = review.state(lastReview.state()).fire(); 20 if(!accept) return; 21 repository.save(review); 22 } 23 24 }
标签:startup ssi 移动 builder 项目 init 修改 code send
原文地址:http://www.cnblogs.com/shenjixiaodao/p/7158668.html