use of com.qcadoo.mes.states.exception.StateTransitionNotAlloweException in project mes by qcadoo.
the class StateExecutorService method saveStateChangeContext.
private Entity saveStateChangeContext(final Entity entity, Entity stateChangeEntity, final StateChangeEntityDescriber describer, final String _sourceState, final String _targetState, final StateChangeStatus status) {
final StateEnum sourceState = describer.parseStateEnum(_sourceState);
final StateEnum targetState = describer.parseStateEnum(_targetState);
if (Objects.nonNull(sourceState) && !sourceState.canChangeTo(targetState)) {
throw new StateTransitionNotAlloweException(sourceState, targetState);
}
checkForUnfinishedStateChange(describer, entity);
stateChangeEntity = saveStateChangeEntity(stateChangeEntity, status);
return stateChangeEntity;
}
use of com.qcadoo.mes.states.exception.StateTransitionNotAlloweException in project mes by qcadoo.
the class StateExecutorService method changeState.
public <M extends StateService> Entity changeState(final Class<M> serviceMarker, Entity entity, final String userName, final String targetState) {
List<M> services = lookupChangeStateServices(serviceMarker);
StateChangeEntityDescriber describer = services.stream().findFirst().get().getChangeEntityDescriber();
String sourceState = entity.getStringField(describer.getOwnerStateFieldName());
Entity stateChangeEntity = buildStateChangeEntity(describer, entity, userName, sourceState, targetState);
try {
stateChangeEntity = saveStateChangeContext(entity, stateChangeEntity, describer, sourceState, targetState, StateChangeStatus.IN_PROGRESS);
List<Entity> stateChanges = Lists.newArrayList();
stateChanges.addAll(entity.getHasManyField(describer.getOwnerStateChangesFieldName()));
stateChanges.add(stateChangeEntity);
entity.setField(describer.getOwnerStateChangesFieldName(), stateChanges);
entity = performChangeState(services, entity, stateChangeEntity, describer);
if (entity.isValid()) {
copyMessages(entity);
saveStateChangeEntity(stateChangeEntity, StateChangeStatus.SUCCESSFUL);
message("states.messages.change.successful", ComponentState.MessageType.SUCCESS);
LOG.info(String.format("Change state successful. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), stateChangeEntity.getStringField(describer.getTargetStateFieldName())));
} else {
saveStateChangeEntity(stateChangeEntity, StateChangeStatus.FAILURE);
entity = rollbackStateChange(entity, sourceState);
message("states.messages.change.failure", ComponentState.MessageType.FAILURE);
LOG.info(String.format("Change state failure. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), stateChangeEntity.getStringField(describer.getTargetStateFieldName())));
}
} catch (EntityRuntimeException entityException) {
copyMessages(entityException.getEntity(), entity);
entity = rollbackStateChange(entity, sourceState);
saveStateChangeEntity(stateChangeEntity, StateChangeStatus.FAILURE);
message("states.messages.change.failure", ComponentState.MessageType.FAILURE);
return entity;
} catch (AnotherChangeInProgressException e) {
entity = rollbackStateChange(entity, sourceState);
saveStateChangeEntity(stateChangeEntity, StateChangeStatus.FAILURE);
message("states.messages.change.failure", ComponentState.MessageType.FAILURE);
message("states.messages.change.failure.anotherChangeInProgress", ComponentState.MessageType.FAILURE);
LOG.info(String.format("Another state change in progress. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), targetState));
} catch (StateTransitionNotAlloweException e) {
entity = rollbackStateChange(entity, sourceState);
saveStateChangeEntity(stateChangeEntity, StateChangeStatus.FAILURE);
message("states.messages.change.failure", ComponentState.MessageType.FAILURE);
message("states.messages.change.failure.transitionNotAllowed", ComponentState.MessageType.FAILURE);
LOG.info(String.format("State change - transition not allowed. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), targetState));
} catch (Exception exception) {
entity = rollbackStateChange(entity, sourceState);
saveStateChangeEntity(stateChangeEntity, StateChangeStatus.FAILURE);
message("states.messages.change.failure", ComponentState.MessageType.FAILURE);
message("states.messages.change.failure.internalServerError", ComponentState.MessageType.FAILURE);
LOG.info(String.format("State change exception. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), targetState));
LOG.warn("Can't perform state change", exception);
}
return entity;
}
use of com.qcadoo.mes.states.exception.StateTransitionNotAlloweException in project mes by qcadoo.
the class StateChangeContextBuilderImpl method build.
@Override
@Transactional
public StateChangeContext build(final StateChangeEntityDescriber describer, final Entity owner, final String targetStateString) {
final Entity persistedOwner = owner.getDataDefinition().save(owner);
final DataDefinition stateChangeDataDefinition = describer.getDataDefinition();
final StateEnum sourceState = describer.parseStateEnum(owner.getStringField(describer.getOwnerStateFieldName()));
final StateEnum targetState = describer.parseStateEnum(targetStateString);
if (sourceState != null && !sourceState.canChangeTo(targetState)) {
throw new StateTransitionNotAlloweException(sourceState, targetState);
}
final Entity stateChangeEntity = stateChangeEntityBuilder.build(describer, persistedOwner, targetState);
checkForUnfinishedStateChange(describer, persistedOwner);
return new StateChangeContextImpl(stateChangeDataDefinition.save(stateChangeEntity), describer, messageService);
}
use of com.qcadoo.mes.states.exception.StateTransitionNotAlloweException in project mes by qcadoo.
the class AbstractStateChangeViewClient method changeState.
@Override
public final void changeState(final ViewContextHolder viewContext, final String targetState, final Entity entity) {
try {
LOG.info(String.format("Change state. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), targetState));
StateChangeContext stateChangeContext = stateChangeContextBuilder.build(getStateChangeService().getChangeEntityDescriber(), entity, targetState);
getStateChangeService().changeState(stateChangeContext);
viewClientUtil.refreshComponent(viewContext);
showMessages(viewContext, stateChangeContext);
stateChangeContext = null;
} catch (AnotherChangeInProgressException e) {
viewContext.getMessagesConsumer().addMessage("states.messages.change.failure.anotherChangeInProgress", MessageType.FAILURE);
LOG.info(String.format("Another state change in progress. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), targetState));
} catch (StateTransitionNotAlloweException e) {
viewContext.getMessagesConsumer().addMessage("states.messages.change.failure.transitionNotAllowed", MessageType.FAILURE);
LOG.info(String.format("State change - transition not allowed. Entity name : %S id : %d. Target state : %S", entity.getDataDefinition().getName(), entity.getId(), targetState));
} catch (Exception e) {
throw new StateChangeException(e);
}
}
use of com.qcadoo.mes.states.exception.StateTransitionNotAlloweException 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();
}
Aggregations