Search in sources :

Example 1 with StateSpec

use of com.icthh.xm.ms.entity.domain.spec.StateSpec in project xm-ms-entity by xm-online.

the class StateKeyValidator method isValid.

@Override
public boolean isValid(XmEntity value, ConstraintValidatorContext context) {
    TypeSpec typeSpec = xmEntitySpecService.findTypeByKey(value.getTypeKey());
    if (typeSpec == null) {
        return true;
    }
    if (isEmpty(typeSpec.getStates()) && value.getStateKey() == null) {
        return true;
    }
    List<StateSpec> stateSpecs = typeSpec.getStates();
    stateSpecs = (stateSpecs != null) ? stateSpecs : Collections.emptyList();
    Set<String> stateKeys = stateSpecs.stream().map(StateSpec::getKey).collect(toSet());
    log.debug("Type specification states {}, checked state {}", stateKeys, value.getStateKey());
    return stateKeys.contains(value.getStateKey());
}
Also used : StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec)

Example 2 with StateSpec

use of com.icthh.xm.ms.entity.domain.spec.StateSpec in project xm-ms-entity by xm-online.

the class XmEntityResourceIntTest method changeStateError.

@Test
@Transactional
public void changeStateError() throws Exception {
    XmEntitySpecService xmEntitySpecService = Mockito.mock(XmEntitySpecService.class);
    StateSpec nextSpec = new StateSpec();
    nextSpec.setKey("NEXT_STATE");
    when(xmEntitySpecService.nextStates(eq(DEFAULT_TYPE_KEY), eq(DEFAULT_STATE_KEY))).thenReturn(Collections.singletonList(nextSpec));
    XmEntity tenant = createEntity(em);
    xmEntityServiceImpl.save(tenant);
    restXmEntityMockMvc.perform(put("/api/xm-entities/{idOrKey}/states/{stateKey}", tenant.getId(), "INVALID_NEXT_STATE").contentType(TestUtil.APPLICATION_JSON_UTF8)).andExpect(status().is4xxClientError());
}
Also used : StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with StateSpec

use of com.icthh.xm.ms.entity.domain.spec.StateSpec in project xm-ms-entity by xm-online.

the class XmEntityServiceImpl method updateState.

@Override
public XmEntity updateState(IdOrKey idOrKey, String stateKey, Map<String, Object> context) {
    XmEntityStateProjection entity = findStateProjectionById(idOrKey).orElseThrow(() -> new EntityNotFoundException("XmEntity with key [" + idOrKey.getKey() + "] not found"));
    List<StateSpec> stateSpecs = xmEntitySpecService.nextStates(entity.getTypeKey(), entity.getStateKey());
    if (stateSpecs.stream().map(StateSpec::getKey).anyMatch(stateKey::equals)) {
        LifecycleLepStrategy lifecycleLepStrategy = lifecycleLepStrategyFactory.getLifecycleLepStrategy();
        return lifecycleLepStrategy.changeState(idOrKey, entity.getTypeKey(), entity.getStateKey(), stateKey, context);
    } else {
        throw new BusinessException(ErrorConstants.ERR_VALIDATION, "Entity " + entity + " can not go from [" + entity.getStateKey() + "] to [" + stateKey + "]");
    }
}
Also used : StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) XmEntityStateProjection(com.icthh.xm.ms.entity.projection.XmEntityStateProjection) EntityNotFoundException(com.icthh.xm.commons.exceptions.EntityNotFoundException) LifecycleLepStrategy(com.icthh.xm.ms.entity.service.LifecycleLepStrategy)

Aggregations

StateSpec (com.icthh.xm.ms.entity.domain.spec.StateSpec)3 BusinessException (com.icthh.xm.commons.exceptions.BusinessException)1 EntityNotFoundException (com.icthh.xm.commons.exceptions.EntityNotFoundException)1 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)1 TypeSpec (com.icthh.xm.ms.entity.domain.spec.TypeSpec)1 XmEntityStateProjection (com.icthh.xm.ms.entity.projection.XmEntityStateProjection)1 LifecycleLepStrategy (com.icthh.xm.ms.entity.service.LifecycleLepStrategy)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Transactional (org.springframework.transaction.annotation.Transactional)1