Search in sources :

Example 1 with AutomaticRoleAttributeRuleEvent

use of eu.bcvsolutions.idm.core.model.event.AutomaticRoleAttributeRuleEvent 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 AutomaticRoleAttributeRuleEvent

use of eu.bcvsolutions.idm.core.model.event.AutomaticRoleAttributeRuleEvent in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleAttributeRuleService method deleteRuleWithSkipCheckLastRule.

@Override
public void deleteRuleWithSkipCheckLastRule(IdmAutomaticRoleAttributeRuleDto dto) {
    AutomaticRoleAttributeRuleEvent automaticRoleAttributeRuleEvent = new AutomaticRoleAttributeRuleEvent(AutomaticRoleAttributeRuleEventType.DELETE, dto);
    automaticRoleAttributeRuleEvent.getProperties().put(AutomaticRoleAttributeRuleDeleteProcessor.SKIP_CHECK_LAST_RULE, Boolean.TRUE);
    entityEventManager.process(automaticRoleAttributeRuleEvent);
}
Also used : AutomaticRoleAttributeRuleEvent(eu.bcvsolutions.idm.core.model.event.AutomaticRoleAttributeRuleEvent)

Aggregations

AutomaticRoleAttributeRuleEvent (eu.bcvsolutions.idm.core.model.event.AutomaticRoleAttributeRuleEvent)2 AutomaticRoleAttributeRuleComparison (eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleComparison)1 AutomaticRoleAttributeRuleType (eu.bcvsolutions.idm.core.api.domain.AutomaticRoleAttributeRuleType)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 PersistentType (eu.bcvsolutions.idm.core.eav.api.domain.PersistentType)1 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)1 Transactional (org.springframework.transaction.annotation.Transactional)1