Search in sources :

Example 1 with StateChangeEntityDescriber

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;
}
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 2 with StateChangeEntityDescriber

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;
}
Also used : StateChangeEntityDescriber(com.qcadoo.mes.states.StateChangeEntityDescriber) Entity(com.qcadoo.model.api.Entity) StateChangeContext(com.qcadoo.mes.states.StateChangeContext)

Example 3 with StateChangeEntityDescriber

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));
}
Also used : StateChangeEntityDescriber(com.qcadoo.mes.states.StateChangeEntityDescriber) Entity(com.qcadoo.model.api.Entity)

Example 4 with StateChangeEntityDescriber

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);
    }
}
Also used : StateChangeEntityDescriber(com.qcadoo.mes.states.StateChangeEntityDescriber) StateChangeService(com.qcadoo.mes.states.service.StateChangeService) StateChangeContext(com.qcadoo.mes.states.StateChangeContext)

Example 5 with StateChangeEntityDescriber

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

Aggregations

StateChangeEntityDescriber (com.qcadoo.mes.states.StateChangeEntityDescriber)7 Entity (com.qcadoo.model.api.Entity)5 StateChangeContext (com.qcadoo.mes.states.StateChangeContext)2 StateTransitionNotAlloweException (com.qcadoo.mes.states.exception.StateTransitionNotAlloweException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 StateEnum (com.qcadoo.mes.states.StateEnum)1 RunForStateTransition (com.qcadoo.mes.states.annotation.RunForStateTransition)1 AnotherChangeInProgressException (com.qcadoo.mes.states.exception.AnotherChangeInProgressException)1 StateChangeService (com.qcadoo.mes.states.service.StateChangeService)1 EntityRuntimeException (com.qcadoo.model.api.exception.EntityRuntimeException)1 Date (java.util.Date)1