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!");
}
}
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());
}
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));
}
Aggregations