use of com.qcadoo.mes.states.StateChangeEntityDescriber in project mes by qcadoo.
the class AbstractStateChangeAspect method performChangeEntityState.
@Transactional
protected void performChangeEntityState(final StateChangeContext stateChangeContext) {
final StateChangeEntityDescriber describer = stateChangeContext.getDescriber();
if (!StateChangePhaseUtil.canRun(stateChangeContext)) {
if (!stateChangeContext.isOwnerValid()) {
stateChangeContext.setStatus(StateChangeStatus.FAILURE);
stateChangeContext.setField(describer.getDateTimeFieldName(), new Date());
LOGGER.info(String.format("State change : failure. Entity name : %S id : %d.", stateChangeContext.getOwner().getDataDefinition().getName(), stateChangeContext.getOwner().getId()));
}
return;
}
final Entity owner = stateChangeContext.getOwner();
final StateEnum sourceState = stateChangeContext.getStateEnumValue(describer.getSourceStateFieldName());
final StateEnum targetState = stateChangeContext.getStateEnumValue(describer.getTargetStateFieldName());
if (sourceState != null && !sourceState.canChangeTo(targetState)) {
throw new StateTransitionNotAlloweException(sourceState, targetState);
}
boolean ownerIsValid = stateChangeContext.isOwnerValid();
if (ownerIsValid) {
owner.setField(describer.getOwnerStateFieldName(), targetState.getStringValue());
ownerIsValid = owner.getDataDefinition().save(owner).isValid();
}
if (ownerIsValid) {
stateChangeContext.setStatus(SUCCESSFUL);
LOGGER.info(String.format("State change : successful. Entity name : %S id : %d. Target state : %S", owner.getDataDefinition().getName(), owner.getId(), targetState));
} else {
LOGGER.info(String.format("State change : failure. Entity name : %S id : %d. Target state : %S", owner.getDataDefinition().getName(), owner.getId(), targetState));
ValidationMessageHelper.copyErrorsFromEntity(stateChangeContext, owner);
stateChangeContext.setStatus(StateChangeStatus.FAILURE);
}
stateChangeContext.setField(describer.getDateTimeFieldName(), new Date());
stateChangeContext.save();
}
use of com.qcadoo.mes.states.StateChangeEntityDescriber in project mes by qcadoo.
the class RunForStateTransitionAspect method stateChangeMatchAnyTransition.
private boolean stateChangeMatchAnyTransition(final StateChangeContext stateChangeContext, final RunForStateTransition[] transitions) {
final StateChangeEntityDescriber describer = stateChangeContext.getDescriber();
final String givenSource = stateChangeContext.getStateChangeEntity().getStringField(describer.getSourceStateFieldName());
final String givenTarget = stateChangeContext.getStateChangeEntity().getStringField(describer.getTargetStateFieldName());
for (RunForStateTransition transition : transitions) {
final String expectedSource = transition.sourceState();
final String expectedTarget = transition.targetState();
if (matchTransition(expectedSource, givenSource) && matchTransition(expectedTarget, givenTarget)) {
return true;
}
}
return false;
}
Aggregations