state machine, session status update
This commit is contained in:
parent
9402218596
commit
4948d43e94
5 changed files with 72 additions and 47 deletions
|
|
@ -19,4 +19,5 @@ public enum DataEnum {
|
|||
paymentInfo,
|
||||
sdfTables,
|
||||
againReviseSuccess, //Boolean
|
||||
ssnImdg
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package ru.spcex.clearing.session.state.action;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.statemachine.StateContext;
|
||||
|
|
@ -8,6 +10,10 @@ import ru.clearing.classes.statics.data.misc.Session;
|
|||
import ru.spcex.clearing.session.stage.TaskType;
|
||||
import ru.spcex.clearing.session.state.DataEnum;
|
||||
import ru.spcex.clearing.session.state.SsnEvent;
|
||||
import ru.spcex.clearing.util.StateMachineUtil;
|
||||
import static ru.spcex.clearing.util.StateMachineUtil.excp;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.utils.log.ExceptionUtils;
|
||||
|
||||
public abstract class AbstractSessionActionForOkErrorHandling implements Action<TaskType, SsnEvent> {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
|
|
@ -25,6 +31,7 @@ public abstract class AbstractSessionActionForOkErrorHandling implements Action<
|
|||
} else {
|
||||
log.info("{} action started", getClass().getSimpleName());
|
||||
}
|
||||
updateSessionStatus(context);
|
||||
actualExecute(context);
|
||||
} catch (Exception e) {
|
||||
context.getStateMachine().setStateMachineError(e);
|
||||
|
|
@ -32,5 +39,52 @@ public abstract class AbstractSessionActionForOkErrorHandling implements Action<
|
|||
}
|
||||
}
|
||||
|
||||
public void updateSessionStatus(StateContext<TaskType, SsnEvent> ctx) {
|
||||
String ssnId = Optional
|
||||
.ofNullable(ctx.getExtendedState().get(DataEnum.sessionId, Long.class))
|
||||
.map(l -> "id=" + l)
|
||||
.orElse("[unknown id]");
|
||||
String mId = StateMachineUtil.getId(ctx);
|
||||
try {
|
||||
TaskType source = ctx.getSource() != null ? ctx.getSource().getId() : null;
|
||||
TaskType target = ctx.getTarget() != null ? ctx.getTarget().getId() : null;
|
||||
if (target == null) {
|
||||
log.info("{} {} target null, not updating session status", mId, ssnId);
|
||||
return;
|
||||
}
|
||||
Session session = ctx.getExtendedState().get(DataEnum.session, Session.class);
|
||||
if (session == null && target == TaskType.StartRevise) {
|
||||
return;
|
||||
} else if (session == null) {
|
||||
throw excp(ctx, "%s %s source=%s/target=%s and session is not inside the extended state"
|
||||
.formatted(
|
||||
mId, ssnId,
|
||||
source,
|
||||
target
|
||||
));
|
||||
} else if (target.isPseudoStatus()) {
|
||||
return;
|
||||
} else if (target.equalsByKey(session.getSessionStatus())) {
|
||||
log.debug("{} {} session status already is {}", mId, ssnId, target);
|
||||
return;
|
||||
}
|
||||
log.info("{} {} updating session status for transition: source={}/target={}...",
|
||||
mId, ssnId,
|
||||
source,
|
||||
target);
|
||||
@SuppressWarnings("unchecked")
|
||||
Imdg<Session> ssnImdg = ctx.getExtendedState().get(DataEnum.ssnImdg, Imdg.class);
|
||||
String sessionStatus = target.getKey();
|
||||
session.setSessionStatus(sessionStatus);
|
||||
session.setUpdated(Instant.now());
|
||||
ssnImdg.update(session);
|
||||
} catch (Exception e) {
|
||||
if (!ctx.getStateMachine().hasStateMachineError()) {
|
||||
ctx.getStateMachine().setStateMachineError(e);
|
||||
}
|
||||
log.error("{} {}", mId, ExceptionUtils.getStackTrace(e));
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void actualExecute(StateContext<TaskType, SsnEvent> context);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -148,6 +148,7 @@ public class SessionStateMachineWrapper {
|
|||
.doWithAllRegions(
|
||||
access -> access.addStateMachineInterceptor(statusChangingInterceptor)
|
||||
);
|
||||
stateMachine.getExtendedState().getVariables().put(DataEnum.ssnImdg, ssnImdg);
|
||||
return stateMachine;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package ru.spcex.clearing.session.state.interceptor;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -68,48 +69,16 @@ public class SessionStatusChangingInterceptor extends StateMachineInterceptorAda
|
|||
|
||||
@Override
|
||||
public StateContext<TaskType, SsnEvent> preTransition(StateContext<TaskType, SsnEvent> ctx) {
|
||||
String id = StateMachineUtil.getId(ctx);
|
||||
try {
|
||||
State<TaskType, SsnEvent> source = ctx.getSource();
|
||||
State<TaskType, SsnEvent> target = ctx.getTarget();
|
||||
if (target == null) {
|
||||
throw excp(
|
||||
ctx,
|
||||
id + " transition target cannot be empty"
|
||||
);
|
||||
}
|
||||
log.info("{} preTransition interceptor: source={}/target={}",
|
||||
id,
|
||||
source != null ? source.getId() : "",
|
||||
target.getId());
|
||||
String smId = StateMachineUtil.getId(ctx);
|
||||
String ssnId = Optional
|
||||
.ofNullable(ctx.getExtendedState().get(DataEnum.sessionId, Long.class))
|
||||
.map(l -> "id=" + l)
|
||||
.orElse("[unknown id]");
|
||||
|
||||
Session session = ctx.getExtendedState().get(DataEnum.session, Session.class);
|
||||
|
||||
if (session == null && target.getId() == TaskType.StartRevise) {
|
||||
return ctx;
|
||||
} else if (session == null) {
|
||||
throw excp(ctx, "%s source=%s/target=%s and session is not inside the extended state"
|
||||
.formatted(
|
||||
id,
|
||||
source != null ? source.getId() : "unknown",
|
||||
target.getId()
|
||||
));
|
||||
} else if (target.getId().isPseudoStatus()) {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
String sessionStatus = target.getId().getKey();
|
||||
session.setSessionStatus(sessionStatus);
|
||||
session.setUpdated(Instant.now());
|
||||
ssnImdg.update(session);
|
||||
return ctx;
|
||||
} catch (Exception e) {
|
||||
if (!ctx.getStateMachine().hasStateMachineError()) {
|
||||
ctx.getStateMachine().setStateMachineError(e);
|
||||
}
|
||||
log.error("{} {}", id, ExceptionUtils.getStackTrace(e));
|
||||
return null;
|
||||
}
|
||||
String source = ctx.getSource() != null ? String.valueOf(ctx.getSource().getId()) : "[unknown source]";
|
||||
String target = ctx.getTarget() != null ? String.valueOf(ctx.getTarget().getId()) : "[unknown target]";
|
||||
log.info("{} {} preTransition interceptor: source={}/target={}", smId, ssnId, source, target);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -136,10 +105,4 @@ public class SessionStatusChangingInterceptor extends StateMachineInterceptorAda
|
|||
|
||||
return exception;
|
||||
}
|
||||
|
||||
private IllegalStateException excp(StateContext<TaskType, SsnEvent> ctx, String msg) {
|
||||
IllegalStateException excp = new IllegalStateException(msg);
|
||||
ctx.getStateMachine().setStateMachineError(excp);
|
||||
return excp;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,4 +37,10 @@ public class StateMachineUtil {
|
|||
return flag != null && flag;
|
||||
};
|
||||
}
|
||||
|
||||
public static <T1, T2> IllegalStateException excp(StateContext<T1, T2> ctx, String msg) {
|
||||
IllegalStateException excp = new IllegalStateException(msg);
|
||||
ctx.getStateMachine().setStateMachineError(excp);
|
||||
return excp;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue