Get EAV for users inherited from the organization structure

This script can be used as a transformation script to the system - in the provisioning mapping. We have an attribute, which should be computed for the users based on their position in the organization structure. The value is computed by the prime work position of the user and if the position doesn't specify the value, then its parents in the organization structure (recursively) may specify the value.

Example use case: users' mailboxes are sorted into databases specified by their departments. Admin sets the databases to selected departments (in their EAV) and all users in their child departments "inherit" the value.

import eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto;
import eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto;
import eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto;
 
org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger("customGetInheritedEavFromWorkPosition");
 
String TREE_EAV_CODE = "codeOfTheEav";
String DEFAULT_VALUE = "DefaultValue";
 
IdmIdentityContractDto contractDto = identityContractService.getPrimeContract(entity.getId());
 
if (contractDto == null) {
    LOG.warn("User {} doesn't have any contract, using default value {}.", entity.getUsername(), DEFAULT_VALUE);
    return DEFAULT_VALUE;
}
 
UUID treeNodeId = contractDto.getWorkPosition();
 
if (treeNodeId == null) {
    LOG.warn("User {} doesn't have any work position, using default value {}.", entity.getUsername(), DEFAULT_VALUE);
    return DEFAULT_VALUE;
}
 
while (treeNodeId != null) {
    IdmTreeNodeDto treeNodeDto = treeNodeService.get(treeNodeId);
    if (treeNodeDto == null) {
        LOG.error("User {}; organization {} was not found.", entity.getUsername(), treeNodeId);
        break;
    }
    List<IdmFormValueDto> formValueDto = formService.getValues(treeNodeId, IdmTreeNodeDto.class, TREE_EAV_CODE);
    if (!formValueDto.isEmpty()) {
        String attrValue = formValueDto.get(0).getShortTextValue();
        LOG.info("User {}; value {} was specified for the organization {}.", entity.getUsername(), attrValue, treeNodeDto.getName());
        return attrValue;
    }
    treeNodeId = treeNodeDto.getParent();
}
 
LOG.info("No value was found for user {}, using default value {}", entity.getUsername(), DEFAULT_VALUE);
return DEFAULT_VALUE;

Service

formService
identityContractService
treeNodeService

Class

eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto
eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto
eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto
  • by apeterova