Search in sources :

Example 1 with StateTransitionNotAlloweException

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

Example 2 with StateTransitionNotAlloweException

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;
}
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 StateTransitionNotAlloweException

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);
}
Also used : Entity(com.qcadoo.model.api.Entity) StateEnum(com.qcadoo.mes.states.StateEnum) StateTransitionNotAlloweException(com.qcadoo.mes.states.exception.StateTransitionNotAlloweException) StateChangeContextImpl(com.qcadoo.mes.states.StateChangeContextImpl) DataDefinition(com.qcadoo.model.api.DataDefinition) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with StateTransitionNotAlloweException

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);
    }
}
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)

Example 5 with StateTransitionNotAlloweException

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();
}
Also used : StateChangeEntityDescriber(com.qcadoo.mes.states.StateChangeEntityDescriber) Entity(com.qcadoo.model.api.Entity) StateEnum(com.qcadoo.mes.states.StateEnum) StateTransitionNotAlloweException(com.qcadoo.mes.states.exception.StateTransitionNotAlloweException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

StateTransitionNotAlloweException (com.qcadoo.mes.states.exception.StateTransitionNotAlloweException)5 StateEnum (com.qcadoo.mes.states.StateEnum)3 Entity (com.qcadoo.model.api.Entity)3 StateChangeEntityDescriber (com.qcadoo.mes.states.StateChangeEntityDescriber)2 AnotherChangeInProgressException (com.qcadoo.mes.states.exception.AnotherChangeInProgressException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 StateChangeContext (com.qcadoo.mes.states.StateChangeContext)1 StateChangeContextImpl (com.qcadoo.mes.states.StateChangeContextImpl)1 StateChangeException (com.qcadoo.mes.states.exception.StateChangeException)1 DataDefinition (com.qcadoo.model.api.DataDefinition)1 EntityRuntimeException (com.qcadoo.model.api.exception.EntityRuntimeException)1 Date (java.util.Date)1