use of com.qcadoo.mes.states.exception.AnotherChangeInProgressException in project mes by qcadoo.
the class StateExecutorService method checkForUnfinishedStateChange.
private void checkForUnfinishedStateChange(final StateChangeEntityDescriber describer, final Entity owner) {
final String ownerFieldName = describer.getOwnerFieldName();
final String statusFieldName = describer.getStatusFieldName();
final Set<String> unfinishedStatuses = Sets.newHashSet(IN_PROGRESS.getStringValue(), PAUSED.getStringValue());
final SearchCriteriaBuilder searchCriteria = describer.getDataDefinition().find();
searchCriteria.createAlias(ownerFieldName, ownerFieldName);
searchCriteria.add(SearchRestrictions.eq(ownerFieldName + ".id", owner.getId()));
searchCriteria.add(SearchRestrictions.in(statusFieldName, unfinishedStatuses));
if (searchCriteria.list().getTotalNumberOfEntities() > 0) {
throw new AnotherChangeInProgressException();
}
}
use of com.qcadoo.mes.states.exception.AnotherChangeInProgressException 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.AnotherChangeInProgressException in project mes by qcadoo.
the class StateChangeContextBuilderImpl method checkForUnfinishedStateChange.
/**
* Checks if given owner entity have not any unfinished state change request.
*
* @param owner
* state change's owner entity
* @throws AnotherChangeInProgressException
* if at least one unfinished state change request for given owner entity is found.
*/
protected void checkForUnfinishedStateChange(final StateChangeEntityDescriber describer, final Entity owner) {
final String ownerFieldName = describer.getOwnerFieldName();
final String statusFieldName = describer.getStatusFieldName();
final Set<String> unfinishedStatuses = Sets.newHashSet(IN_PROGRESS.getStringValue(), PAUSED.getStringValue());
final SearchCriteriaBuilder searchCriteria = describer.getDataDefinition().find();
searchCriteria.createAlias(ownerFieldName, ownerFieldName);
searchCriteria.add(SearchRestrictions.eq(ownerFieldName + ".id", owner.getId()));
searchCriteria.add(SearchRestrictions.in(statusFieldName, unfinishedStatuses));
if (searchCriteria.list().getTotalNumberOfEntities() > 0) {
throw new AnotherChangeInProgressException();
}
}
use of com.qcadoo.mes.states.exception.AnotherChangeInProgressException in project mes by qcadoo.
the class StateChangeContextBuilderTest method shouldThrowExceptionIfActiveStateChangeEntityAlreadyExists.
@Test
public final void shouldThrowExceptionIfActiveStateChangeEntityAlreadyExists() {
// given
final Entity existingActiveStateChangeEntity = mock(Entity.class);
given(stateChangeDD.create()).willReturn(stateChangeEntity);
stubSearchCriteria(Lists.<Entity>newArrayList(existingActiveStateChangeEntity));
// when
try {
stateChangeContextBuilder.build(describer, owner, TestState.ACCEPTED.getStringValue());
Assert.fail();
} catch (AnotherChangeInProgressException e) {
}
}
use of com.qcadoo.mes.states.exception.AnotherChangeInProgressException 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);
}
}
Aggregations