Search in sources :

Example 1 with IdmIdentityFormValue

use of eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleAttributeService method getPredicateForRuleByContract.

/**
 * Return predicate for given rule by contract
 *
 * @param rule
 * @param root
 * @param query
 * @param cb
 * @return
 */
private Predicate getPredicateForRuleByContract(IdmAutomaticRoleAttributeRuleDto rule, Root<IdmIdentityContract> root, CriteriaQuery<?> query, CriteriaBuilder cb, boolean pass) {
    // 
    Metamodel metamodel = entityManager.getMetamodel();
    if (rule.getType() == AutomaticRoleAttributeRuleType.CONTRACT) {
        SingularAttribute<? super IdmIdentityContract, ?> singularAttribute = metamodel.entity(IdmIdentityContract.class).getSingularAttribute(rule.getAttributeName());
        Path<Object> path = root.get(singularAttribute.getName());
        // role will be added and wich roles will be removed.
        return getPredicateWithComparsion(path, castToType(singularAttribute, rule.getValue(), rule.getComparison()), cb, rule.getComparison(), !pass);
    } else if (rule.getType() == AutomaticRoleAttributeRuleType.CONTRACT_EAV) {
        IdmFormAttributeDto formAttributeDto = formAttributeService.get(rule.getFormAttribute());
        AutomaticRoleAttributeRuleComparison comparison = rule.getComparison();
        // Cast given value to specific persistent type
        // For is empty and is not empty comparison is returned null even if value exists
        Object value = getFormValue(rule.getValue(), formAttributeDto, comparison);
        // 
        // For contract form attribute was composed only one subquery
        Subquery<IdmIdentityContractFormValue> subquery = query.subquery(IdmIdentityContractFormValue.class);
        Root<IdmIdentityContractFormValue> subRoot = subquery.from(IdmIdentityContractFormValue.class);
        subquery.select(subRoot);
        // 
        Path<?> path = subRoot.get(getSingularAttributeForEav(formAttributeDto.getPersistentType()));
        // Is empty comparison has specific behavior because form value isn't empty, but value doesn't exist
        if (comparison == AutomaticRoleAttributeRuleComparison.IS_EMPTY) {
            subquery.where(cb.or(// Predicate for check if value exists
            getPredicateForNullFormAttributeIdentityContract(root, query, cb, formAttributeDto), cb.and(cb.equal(subRoot.get(IdmIdentityContractFormValue_.owner), root), cb.equal(subRoot.get(IdmIdentityContractFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, null, cb, rule.getComparison(), null))));
            if (pass) {
                return cb.not(cb.exists(subquery));
            }
            return cb.exists(subquery);
        }
        // 
        subquery.where(cb.and(cb.equal(subRoot.get(IdmIdentityContractFormValue_.owner), root), cb.equal(subRoot.get(IdmIdentityContractFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, value, cb, rule.getComparison(), null)));
        // 
        Predicate existsInEav = getPredicateForConnection(subquery, cb, pass, formAttributeDto.isMultiple());
        // For comparison with not is required also check null values
        if (comparison == AutomaticRoleAttributeRuleComparison.NOT_CONTAINS || comparison == AutomaticRoleAttributeRuleComparison.NOT_END_WITH || comparison == AutomaticRoleAttributeRuleComparison.NOT_EQUALS || comparison == AutomaticRoleAttributeRuleComparison.NOT_START_WITH) {
            if (pass) {
                existsInEav = cb.or(existsInEav, cb.not(getPredicateForNullFormAttributeIdentityContract(root, query, cb, formAttributeDto)));
            } else {
                existsInEav = cb.and(existsInEav, getPredicateForNullFormAttributeIdentityContract(root, query, cb, formAttributeDto));
            }
        }
        // 
        return existsInEav;
    } else if (rule.getType() == AutomaticRoleAttributeRuleType.IDENTITY_EAV) {
        IdmFormAttributeDto formAttributeDto = formAttributeService.get(rule.getFormAttribute());
        AutomaticRoleAttributeRuleComparison comparison = rule.getComparison();
        // Cast given value to specific persistent type
        // For is empty and is not empty comparison is returned null even if value exists
        Object value = getFormValue(rule.getValue(), formAttributeDto, comparison);
        // 
        // Rules for identity form values must contains two subquery identity -> identity eav
        Subquery<IdmIdentity> subquery = query.subquery(IdmIdentity.class);
        Root<IdmIdentity> subRoot = subquery.from(IdmIdentity.class);
        subquery.select(subRoot);
        // 
        Subquery<IdmIdentityFormValue> subQueryIdentityEav = query.subquery(IdmIdentityFormValue.class);
        Root<IdmIdentityFormValue> subRootIdentityEav = subQueryIdentityEav.from(IdmIdentityFormValue.class);
        subQueryIdentityEav.select(subRootIdentityEav);
        // 
        Path<?> path = subRootIdentityEav.get(getSingularAttributeForEav(formAttributeDto.getPersistentType()));
        // Is empty comparison has specific behavior because form value isn't empty, but value doesn't exist
        if (comparison == AutomaticRoleAttributeRuleComparison.IS_EMPTY) {
            subquery.where(cb.and(cb.equal(root.get(IdmIdentityContract_.identity), subRoot), cb.or(cb.exists(subQueryIdentityEav.where(cb.and(cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.owner), subRoot), cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, null, cb, rule.getComparison(), null)))), // Predicate for check if value exists
            getPredicateForNullFormAttributeIdentity(subRoot, subquery, cb, formAttributeDto))));
            // 
            if (pass) {
                return cb.not(cb.exists(subquery));
            }
            return cb.exists(subquery);
        }
        // 
        subQueryIdentityEav.where(cb.and(cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.owner), subRoot), cb.equal(root.get(IdmIdentityContract_.identity), subRoot), cb.equal(subRootIdentityEav.get(IdmIdentityFormValue_.formAttribute).get(AbstractFormValue_.id), formAttributeDto.getId()), getPredicateWithComparsion(path, value, cb, rule.getComparison(), null)));
        Predicate existsInEav = getPredicateForConnection(subQueryIdentityEav, cb, pass, formAttributeDto.isMultiple());
        // For comparison with not is required also check null values
        if (comparison == AutomaticRoleAttributeRuleComparison.NOT_CONTAINS || comparison == AutomaticRoleAttributeRuleComparison.NOT_END_WITH || comparison == AutomaticRoleAttributeRuleComparison.NOT_EQUALS || comparison == AutomaticRoleAttributeRuleComparison.NOT_START_WITH) {
            if (pass) {
                existsInEav = cb.or(existsInEav, cb.not(getPredicateForNullFormAttributeIdentity(subRoot, subquery, cb, formAttributeDto)));
            } else {
                existsInEav = cb.and(existsInEav, getPredicateForNullFormAttributeIdentity(subRoot, subquery, cb, formAttributeDto));
            }
        }
        // 
        subquery.where(cb.and(cb.equal(subRoot.get(IdmIdentity_.id), root.get(IdmIdentityContract_.identity).get(AbstractEntity_.id)), existsInEav));
        // 
        return cb.exists(subquery);
    } else if (rule.getType() == AutomaticRoleAttributeRuleType.IDENTITY) {
        Subquery<IdmIdentity> subquery = query.subquery(IdmIdentity.class);
        Root<IdmIdentity> subRoot = subquery.from(IdmIdentity.class);
        subquery.select(subRoot);
        // 
        SingularAttribute<? super IdmIdentity, ?> singularAttribute = metamodel.entity(IdmIdentity.class).getSingularAttribute(rule.getAttributeName());
        Path<Object> path = subRoot.get(singularAttribute.getName());
        // 
        subquery.where(// correlation attr
        cb.and(// correlation attr
        cb.equal(subRoot.get(IdmIdentity_.id), root.get(IdmIdentityContract_.identity).get(AbstractEntity_.id)), getPredicateWithComparsion(path, castToType(singularAttribute, rule.getValue(), rule.getComparison()), cb, rule.getComparison(), null)));
        // 
        return getPredicateForConnection(subquery, cb, pass, false);
    } else {
        throw new UnsupportedOperationException("Type: " + rule.getType().name() + ", isn't supported for contract rules!");
    }
}
Also used : Path(javax.persistence.criteria.Path) AutomaticRoleAttributeRuleComparison(eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison) Root(javax.persistence.criteria.Root) IdmIdentityFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue) Subquery(javax.persistence.criteria.Subquery) Predicate(javax.persistence.criteria.Predicate) SingularAttribute(javax.persistence.metamodel.SingularAttribute) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) Metamodel(javax.persistence.metamodel.Metamodel) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) IdmIdentityContract(eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract) IdmIdentityContractFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityContractFormValue)

Example 2 with IdmIdentityFormValue

use of eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue in project CzechIdMng by bcvsolutions.

the class IdmFormValueDtoMapperIntegrationTest method testMapWithOwner.

@Test
public void testMapWithOwner() {
    IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
    // 
    // create definition one
    IdmFormAttributeDto attributeDefinitionOne = new IdmFormAttributeDto();
    attributeDefinitionOne.setCode(getHelper().createName());
    attributeDefinitionOne.setName(attributeDefinitionOne.getCode());
    attributeDefinitionOne.setPersistentType(PersistentType.TEXT);
    IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class, getHelper().createName(), Lists.newArrayList(attributeDefinitionOne));
    attributeDefinitionOne = formDefinitionOne.getMappedAttributeByCode(attributeDefinitionOne.getCode());
    // 
    IdmFormValueDto value = new IdmFormValueDto(attributeDefinitionOne);
    value.setValue(getHelper().createName());
    // 
    formService.saveValues(identity, formDefinitionOne, Lists.newArrayList(value));
    // 
    IdmFormValueDto savedValue = formService.getValues(identity, formDefinitionOne).get(0);
    Assert.assertEquals(value.getStringValue(), savedValue.getStringValue());
    // 
    IdmIdentityFormValue savedEntity = identityFormValueRepository.findById(savedValue.getId()).get();
    // 
    IdmFormValueFilter<?> context = new IdmFormValueFilter<>();
    context.setAddOwnerDto(true);
    IdmFormValueDto valueDto = lookupService.toDto(savedEntity, null, context);
    // 
    Assert.assertEquals(value.getStringValue(), valueDto.getStringValue());
    BaseDto owner = valueDto.getEmbedded().get(FormValueService.PROPERTY_OWNER);
    Assert.assertNotNull(owner);
    Assert.assertEquals(identity.getId(), owner.getId());
}
Also used : IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) IdmIdentityFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue) IdmFormValueFilter(eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)

Example 3 with IdmIdentityFormValue

use of eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue in project CzechIdMng by bcvsolutions.

the class IdmFormValueDtoMapperIntegrationTest method testMapWithDefaultMapper.

@Test
public void testMapWithDefaultMapper() {
    IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
    // 
    // create definition one
    IdmFormAttributeDto attributeDefinitionOne = new IdmFormAttributeDto();
    attributeDefinitionOne.setCode(getHelper().createName());
    attributeDefinitionOne.setName(attributeDefinitionOne.getCode());
    attributeDefinitionOne.setPersistentType(PersistentType.TEXT);
    IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class, getHelper().createName(), Lists.newArrayList(attributeDefinitionOne));
    attributeDefinitionOne = formDefinitionOne.getMappedAttributeByCode(attributeDefinitionOne.getCode());
    // 
    IdmFormValueDto value = new IdmFormValueDto(attributeDefinitionOne);
    value.setValue(getHelper().createName());
    // 
    formService.saveValues(identity, formDefinitionOne, Lists.newArrayList(value));
    // 
    IdmFormValueDto savedValue = formService.getValues(identity, formDefinitionOne).get(0);
    Assert.assertEquals(value.getStringValue(), savedValue.getStringValue());
    // 
    IdmIdentityFormValue savedEntity = identityFormValueRepository.findById(savedValue.getId()).get();
    // 
    DefaultDtoMapper mapper = new DefaultDtoMapper(modelMapper, IdmFormValueDto.class);
    IdmFormValueFilter<?> context = new IdmFormValueFilter<>();
    context.setAddOwnerDto(true);
    IdmFormValueDto valueDto = (IdmFormValueDto) mapper.map(savedEntity, null, context);
    // 
    Assert.assertEquals(value.getStringValue(), valueDto.getStringValue());
    BaseDto owner = valueDto.getEmbedded().get(FormValueService.PROPERTY_OWNER);
    Assert.assertNull(owner);
    // 
    valueDto = (IdmFormValueDto) mapper.map(savedEntity, new IdmFormValueDto(), null);
    // 
    Assert.assertEquals(value.getStringValue(), valueDto.getStringValue());
    owner = valueDto.getEmbedded().get(FormValueService.PROPERTY_OWNER);
    Assert.assertNull(owner);
    // 
    Assert.assertNull(mapper.map(null, null, null));
}
Also used : IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) IdmIdentityFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue) IdmFormValueFilter(eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) DefaultDtoMapper(eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)

Aggregations

IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)3 IdmIdentityFormValue (eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue)3 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)2 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)2 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)2 IdmFormValueDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto)2 IdmFormValueFilter (eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter)2 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)2 Test (org.junit.Test)2 AutomaticRoleAttributeRuleComparison (eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison)1 DefaultDtoMapper (eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper)1 IdmIdentity (eu.bcvsolutions.idm.core.model.entity.IdmIdentity)1 IdmIdentityContract (eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)1 IdmIdentityContractFormValue (eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityContractFormValue)1 Path (javax.persistence.criteria.Path)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1 Subquery (javax.persistence.criteria.Subquery)1 Metamodel (javax.persistence.metamodel.Metamodel)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1