Search in sources :

Example 1 with AnotherChangeInProgressException

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();
    }
}
Also used : AnotherChangeInProgressException(com.qcadoo.mes.states.exception.AnotherChangeInProgressException) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder)

Example 2 with 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;
}
Also used : StateChangeEntityDescriber(com.qcadoo.mes.states.StateChangeEntityDescriber) Entity(com.qcadoo.model.api.Entity) AnotherChangeInProgressException(com.qcadoo.mes.states.exception.AnotherChangeInProgressException) StateTransitionNotAlloweException(com.qcadoo.mes.states.exception.StateTransitionNotAlloweException) AnotherChangeInProgressException(com.qcadoo.mes.states.exception.AnotherChangeInProgressException) EntityRuntimeException(com.qcadoo.model.api.exception.EntityRuntimeException) StateTransitionNotAlloweException(com.qcadoo.mes.states.exception.StateTransitionNotAlloweException) EntityRuntimeException(com.qcadoo.model.api.exception.EntityRuntimeException)

Example 3 with AnotherChangeInProgressException

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();
    }
}
Also used : AnotherChangeInProgressException(com.qcadoo.mes.states.exception.AnotherChangeInProgressException) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder)

Example 4 with 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) {
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) AnotherChangeInProgressException(com.qcadoo.mes.states.exception.AnotherChangeInProgressException) StateChangeTest(com.qcadoo.mes.states.StateChangeTest) Test(org.junit.Test)

Example 5 with AnotherChangeInProgressException

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);
    }
}
Also used : AnotherChangeInProgressException(com.qcadoo.mes.states.exception.AnotherChangeInProgressException) StateTransitionNotAlloweException(com.qcadoo.mes.states.exception.StateTransitionNotAlloweException) StateChangeException(com.qcadoo.mes.states.exception.StateChangeException) StateChangeContext(com.qcadoo.mes.states.StateChangeContext) AnotherChangeInProgressException(com.qcadoo.mes.states.exception.AnotherChangeInProgressException) StateTransitionNotAlloweException(com.qcadoo.mes.states.exception.StateTransitionNotAlloweException) StateChangeException(com.qcadoo.mes.states.exception.StateChangeException)

Aggregations

AnotherChangeInProgressException (com.qcadoo.mes.states.exception.AnotherChangeInProgressException)5 StateTransitionNotAlloweException (com.qcadoo.mes.states.exception.StateTransitionNotAlloweException)2 Entity (com.qcadoo.model.api.Entity)2 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)2 StateChangeContext (com.qcadoo.mes.states.StateChangeContext)1 StateChangeEntityDescriber (com.qcadoo.mes.states.StateChangeEntityDescriber)1 StateChangeTest (com.qcadoo.mes.states.StateChangeTest)1 StateChangeException (com.qcadoo.mes.states.exception.StateChangeException)1 EntityRuntimeException (com.qcadoo.model.api.exception.EntityRuntimeException)1 Test (org.junit.Test)1