use of com.qcadoo.mes.states.StateChangeEntityDescriber 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.StateChangeEntityDescriber in project mes by qcadoo.
the class StateChangeSamplesClientImpl method performChange.
private Entity performChange(final StateChangeService stateChangeService, final Entity entity, final String targetState) {
final StateChangeEntityDescriber describer = stateChangeService.getChangeEntityDescriber();
final StateChangeContext stateChangeContext = stateChangeContextBuilder.build(describer, entity, targetState);
stateChangeService.changeState(stateChangeContext);
checkResults(stateChangeContext);
Entity resultEntity = null;
if (entity.getId() == null) {
resultEntity = entity.getDataDefinition().save(entity);
} else {
resultEntity = entity.getDataDefinition().get(entity.getId());
}
return resultEntity;
}
use of com.qcadoo.mes.states.StateChangeEntityDescriber in project mes by qcadoo.
the class StateExecutorService method buildInitial.
public <M extends StateService> void buildInitial(final Class<M> serviceMarker, final Entity entity, final String initialState) {
List<M> services = lookupChangeStateServices(serviceMarker);
StateChangeEntityDescriber describer = services.get(0).getChangeEntityDescriber();
Entity stateChangeEntity = buildStateChangeEntity(describer, entity, StringUtils.EMPTY, null, initialState);
stateChangeEntity = saveStateChangeEntity(stateChangeEntity, StateChangeStatus.SUCCESSFUL);
entity.setField(describer.getOwnerStateFieldName(), initialState);
entity.setField(describer.getOwnerStateChangesFieldName(), Lists.newArrayList(stateChangeEntity));
}
use of com.qcadoo.mes.states.StateChangeEntityDescriber in project mes by qcadoo.
the class StateChangeSamplesClientImpl method resumeStateChange.
@Override
public void resumeStateChange(final Entity entity, final Entity stateChangeEntity) {
entity.getDataDefinition().save(entity);
final StateChangeService stateChangeService = stateChangeServiceResolver.get(entity.getDataDefinition());
if (stateChangeService != null) {
final StateChangeEntityDescriber describer = stateChangeService.getChangeEntityDescriber();
final StateChangeContext stateChangeContext = stateChangeContextBuilder.build(describer, stateChangeEntity);
stateChangeContext.setStatus(StateChangeStatus.IN_PROGRESS);
stateChangeService.changeState(stateChangeContext);
checkResults(stateChangeContext);
}
}
use of com.qcadoo.mes.states.StateChangeEntityDescriber in project mes by qcadoo.
the class AbstractStateChangeAspect method performStateChange.
@Transactional
private void performStateChange(final StateChangeContext stateChangeContext) {
final StateChangeEntityDescriber describer = stateChangeContext.getDescriber();
describer.checkFields();
for (int phase = stateChangeContext.getPhase() + 1; phase <= getNumOfPhases(); phase++) {
if (StateChangePhaseUtil.canRun(stateChangeContext)) {
stateChangeContext.setPhase(phase);
changeStatePhase(stateChangeContext, phase);
}
}
final Entity owner = stateChangeContext.getOwner();
stateChangeContext.setOwner(owner);
performChangeEntityState(stateChangeContext);
}
Aggregations