Search in sources :

Example 6 with StateChangeEntityDescriber

use of com.qcadoo.mes.states.StateChangeEntityDescriber 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)

Example 7 with StateChangeEntityDescriber

use of com.qcadoo.mes.states.StateChangeEntityDescriber in project mes by qcadoo.

the class RunForStateTransitionAspect method stateChangeMatchAnyTransition.

private boolean stateChangeMatchAnyTransition(final StateChangeContext stateChangeContext, final RunForStateTransition[] transitions) {
    final StateChangeEntityDescriber describer = stateChangeContext.getDescriber();
    final String givenSource = stateChangeContext.getStateChangeEntity().getStringField(describer.getSourceStateFieldName());
    final String givenTarget = stateChangeContext.getStateChangeEntity().getStringField(describer.getTargetStateFieldName());
    for (RunForStateTransition transition : transitions) {
        final String expectedSource = transition.sourceState();
        final String expectedTarget = transition.targetState();
        if (matchTransition(expectedSource, givenSource) && matchTransition(expectedTarget, givenTarget)) {
            return true;
        }
    }
    return false;
}
Also used : StateChangeEntityDescriber(com.qcadoo.mes.states.StateChangeEntityDescriber) RunForStateTransition(com.qcadoo.mes.states.annotation.RunForStateTransition)

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