Search in sources :

Example 1 with AutomaticRoleAttributeRuleComparison

use of eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleAttributeRuleService method save.

@Override
@Transactional
public IdmAutomaticRoleAttributeRuleDto save(IdmAutomaticRoleAttributeRuleDto dto, BasePermission... permission) {
    // Numeric attribute can be only EAV (for now, even external code is string)
    boolean isAttributeNumeric = false;
    AutomaticRoleAttributeRuleComparison comparison = dto.getComparison();
    AutomaticRoleAttributeRuleType type = dto.getType();
    // Boolean attribute is allowed only with equals and not equals comparison.
    boolean isEqualsOrNotEquals = comparison == AutomaticRoleAttributeRuleComparison.EQUALS || comparison == AutomaticRoleAttributeRuleComparison.NOT_EQUALS;
    // now isn't possible do equals with string_value (clob), so it is necessary to use only short text
    if ((AutomaticRoleAttributeRuleType.CONTRACT_EAV == type || AutomaticRoleAttributeRuleType.IDENTITY_EAV == type) && dto.getFormAttribute() != null) {
        initFormAttributeService();
        IdmFormAttributeDto formAttribute = formAttributeService.get(dto.getFormAttribute());
        if (formAttribute == null) {
            throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("attribute", dto.getFormAttribute()));
        }
        PersistentType formAttributePersistenType = formAttribute.getPersistentType();
        if (formAttributePersistenType == PersistentType.TEXT) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_PERSISTENT_TYPE_TEXT);
        }
        if (formAttribute.isMultiple() && (comparison != AutomaticRoleAttributeRuleComparison.EQUALS && comparison != AutomaticRoleAttributeRuleComparison.IS_EMPTY && comparison != AutomaticRoleAttributeRuleComparison.IS_NOT_EMPTY)) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_WITH_MULTIPLE_ATTIBUTE, ImmutableMap.of("comparison", comparison.name()));
        }
        if (formAttributePersistenType == PersistentType.BOOLEAN && !isEqualsOrNotEquals) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
        }
        // Numeric value now can be only EAV
        isAttributeNumeric = formAttributePersistenType == PersistentType.INT || formAttributePersistenType == PersistentType.DOUBLE || formAttributePersistenType == PersistentType.LONG;
    }
    if ((comparison == AutomaticRoleAttributeRuleComparison.GREATER_THAN_OR_EQUAL || comparison == AutomaticRoleAttributeRuleComparison.LESS_THAN_OR_EQUAL) && !isAttributeNumeric) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_COMPARSION_IS_ONLY_FOR_NUMERIC_ATTRIBUTE, ImmutableMap.of("comparison", comparison.name()));
    }
    String attributeName = dto.getAttributeName();
    // check if is filled all necessary attribute
    if ((type == AutomaticRoleAttributeRuleType.CONTRACT || type == AutomaticRoleAttributeRuleType.IDENTITY) && StringUtils.isEmpty(attributeName)) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_ATTRIBUTE_EMPTY, ImmutableMap.of("automaticRoleId", dto.getId(), "attribute", IdmAutomaticRoleAttributeRule_.attributeName.getName()));
    }
    // 
    if (!isEqualsOrNotEquals) {
        if (type == AutomaticRoleAttributeRuleType.CONTRACT) {
            if (attributeName.equals(IdmIdentityContract_.main.getName())) {
                throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
            }
            if (attributeName.equals(IdmIdentityContract_.externe.getName())) {
                throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
            }
        }
        if (type == AutomaticRoleAttributeRuleType.IDENTITY && attributeName.equals(IdmIdentity_.disabled.getName())) {
            if (attributeName.equals(IdmIdentity_.disabled.getName())) {
                throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
            }
        }
    }
    // 
    if ((dto.getType() == AutomaticRoleAttributeRuleType.IDENTITY_EAV || dto.getType() == AutomaticRoleAttributeRuleType.CONTRACT_EAV) && dto.getAutomaticRoleAttribute() == null) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_ATTRIBUTE_EMPTY, ImmutableMap.of("automaticRoleId", dto.getId(), "attribute", IdmAutomaticRoleAttributeRule_.automaticRoleAttribute.getName()));
    }
    if (comparison == AutomaticRoleAttributeRuleComparison.EQUALS && dto.getValue() == null) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_ATTRIBUTE_EMPTY, ImmutableMap.of("attribute", IdmAutomaticRoleAttributeRule_.value.getName()));
    }
    // throw new event
    if (isNew(dto)) {
        return entityEventManager.process(new AutomaticRoleAttributeRuleEvent(AutomaticRoleAttributeRuleEventType.CREATE, dto)).getContent();
    }
    return entityEventManager.process(new AutomaticRoleAttributeRuleEvent(AutomaticRoleAttributeRuleEventType.UPDATE, dto)).getContent();
}
Also used : AutomaticRoleAttributeRuleComparison(eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison) AutomaticRoleAttributeRuleEvent(eu.bcvsolutions.idm.core.model.event.AutomaticRoleAttributeRuleEvent) PersistentType(eu.bcvsolutions.idm.core.eav.api.domain.PersistentType) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AutomaticRoleAttributeRuleType(eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleType) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with AutomaticRoleAttributeRuleComparison

use of eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison 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 3 with AutomaticRoleAttributeRuleComparison

use of eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleAttributeRuleRequestService method saveInternal.

@Override
@Transactional
public IdmAutomaticRoleAttributeRuleRequestDto saveInternal(IdmAutomaticRoleAttributeRuleRequestDto dto) {
    // Numeric attribute can be only EAV (for now, even external code is string)
    boolean isAttributeNumeric = false;
    AutomaticRoleAttributeRuleComparison comparison = dto.getComparison();
    AutomaticRoleAttributeRuleType type = dto.getType();
    // Boolean attribute is allowed only with equals and not equals comparison.
    boolean isEqualsOrNotEquals = comparison == AutomaticRoleAttributeRuleComparison.EQUALS || comparison == AutomaticRoleAttributeRuleComparison.NOT_EQUALS;
    // use only short text
    if ((AutomaticRoleAttributeRuleType.CONTRACT_EAV == type || AutomaticRoleAttributeRuleType.IDENTITY_EAV == type) && dto.getFormAttribute() != null) {
        IdmFormAttributeDto formAttribute = formAttributeService.get(dto.getFormAttribute());
        if (formAttribute == null) {
            throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("attribute", dto.getFormAttribute()));
        }
        PersistentType formAttributePersistenType = formAttribute.getPersistentType();
        if (formAttributePersistenType == PersistentType.TEXT) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_PERSISTENT_TYPE_TEXT);
        }
        if (formAttribute.isMultiple() && (comparison != AutomaticRoleAttributeRuleComparison.EQUALS && comparison != AutomaticRoleAttributeRuleComparison.IS_EMPTY && comparison != AutomaticRoleAttributeRuleComparison.IS_NOT_EMPTY)) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_WITH_MULTIPLE_ATTIBUTE, ImmutableMap.of("comparison", comparison.name()));
        }
        if (formAttributePersistenType == PersistentType.BOOLEAN && !isEqualsOrNotEquals) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
        }
        // Numeric value now can be only EAV
        isAttributeNumeric = formAttributePersistenType == PersistentType.INT || formAttributePersistenType == PersistentType.DOUBLE || formAttributePersistenType == PersistentType.LONG;
    }
    if ((comparison == AutomaticRoleAttributeRuleComparison.GREATER_THAN_OR_EQUAL || comparison == AutomaticRoleAttributeRuleComparison.LESS_THAN_OR_EQUAL) && !isAttributeNumeric) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_COMPARSION_IS_ONLY_FOR_NUMERIC_ATTRIBUTE, ImmutableMap.of("comparison", comparison.name()));
    }
    // 
    String attributeName = dto.getAttributeName();
    // check if is filled all necessary attribute
    if ((type == AutomaticRoleAttributeRuleType.CONTRACT || type == AutomaticRoleAttributeRuleType.IDENTITY) && StringUtils.isEmpty(attributeName)) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_ATTRIBUTE_EMPTY, ImmutableMap.of("automaticRoleId", dto.getId(), "attribute", IdmAutomaticRoleAttributeRule_.attributeName.getName()));
    }
    // 
    if (!isEqualsOrNotEquals) {
        if (type == AutomaticRoleAttributeRuleType.CONTRACT) {
            if (attributeName.equals(IdmIdentityContract_.main.getName())) {
                throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
            }
            if (attributeName.equals(IdmIdentityContract_.externe.getName())) {
                throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
            }
        }
        if (type == AutomaticRoleAttributeRuleType.IDENTITY && attributeName.equals(IdmIdentity_.disabled.getName())) {
            if (attributeName.equals(IdmIdentity_.disabled.getName())) {
                throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_INVALID_COMPARSION_BOOLEAN, ImmutableMap.of("comparison", comparison.name()));
            }
        }
    }
    // 
    if (comparison == AutomaticRoleAttributeRuleComparison.EQUALS && dto.getValue() == null) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_RULE_ATTRIBUTE_EMPTY, ImmutableMap.of("attribute", IdmAutomaticRoleAttributeRule_.value.getName()));
    }
    return super.saveInternal(dto);
}
Also used : AutomaticRoleAttributeRuleComparison(eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison) PersistentType(eu.bcvsolutions.idm.core.eav.api.domain.PersistentType) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AutomaticRoleAttributeRuleType(eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleType) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AutomaticRoleAttributeRuleComparison (eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison)3 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)3 AutomaticRoleAttributeRuleType (eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleType)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 PersistentType (eu.bcvsolutions.idm.core.eav.api.domain.PersistentType)2 Transactional (org.springframework.transaction.annotation.Transactional)2 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 IdmIdentityFormValue (eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue)1 AutomaticRoleAttributeRuleEvent (eu.bcvsolutions.idm.core.model.event.AutomaticRoleAttributeRuleEvent)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