Search in sources :

Example 1 with ConceptRoleRequestOperation

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

the class DefaultIdmConceptRoleRequestService method getRoleAttributeValues.

@Override
public IdmFormInstanceDto getRoleAttributeValues(IdmConceptRoleRequestDto dto, boolean checkChanges) {
    Assert.notNull(dto, "DTO is required.");
    UUID roleId = dto.getRole();
    if (roleId != null) {
        IdmRoleDto role = DtoUtils.getEmbedded(dto, IdmConceptRoleRequest_.role, IdmRoleDto.class, null);
        if (role == null) {
            role = roleService.get(roleId);
        }
        // Has role filled attribute definition?
        UUID formDefintion = role.getIdentityRoleAttributeDefinition();
        if (formDefintion != null) {
            IdmFormDefinitionDto formDefinitionDto = roleService.getFormAttributeSubdefinition(role);
            IdmFormInstanceDto conceptFormInstance = null;
            List<IdmFormInstanceDto> eavs = dto.getEavs();
            // Get form instance from given concept first
            if (eavs != null && eavs.size() == 1) {
                conceptFormInstance = eavs.get(0);
                if (conceptFormInstance.getFormDefinition() == null) {
                    conceptFormInstance.setFormDefinition(formDefinitionDto);
                }
            } else {
                conceptFormInstance = formService.getFormInstance(dto, formDefinitionDto);
            }
            if (!checkChanges) {
                // Return only EAV values, without compare changes
                return conceptFormInstance;
            }
            // If exists identity role, then we try to evaluate changes against EAVs in the
            // current identity role.
            ConceptRoleRequestOperation operation = dto.getOperation();
            if (dto.getIdentityRole() != null && ConceptRoleRequestOperation.UPDATE == operation) {
                IdmIdentityRoleDto identityRoleDto = DtoUtils.getEmbedded(dto, IdmConceptRoleRequest_.identityRole, IdmIdentityRoleDto.class, null);
                if (identityRoleDto == null) {
                    identityRoleDto = identityRoleThinService.get(dto.getIdentityRole());
                }
                IdmFormInstanceDto formInstance = formService.getFormInstance(new IdmIdentityRoleDto(identityRoleDto.getId()), formDefinitionDto);
                if (formInstance != null && conceptFormInstance != null) {
                    IdmFormInstanceDto conceptFormInstanceFinal = conceptFormInstance;
                    List<IdmFormValueDto> conceptValues = conceptFormInstanceFinal.getValues();
                    List<IdmFormValueDto> values = formInstance.getValues();
                    conceptValues.forEach(conceptFormValue -> {
                        IdmFormValueDto formValue = // 
                        values.stream().filter(value -> value.getFormAttribute().equals(conceptFormValue.getFormAttribute()) && // 
                        value.getSeq() == conceptFormValue.getSeq()).findFirst().orElse(// 
                        null);
                        // Compile changes
                        Serializable value = formValue != null ? formValue.getValue() : null;
                        Serializable conceptValue = conceptFormValue.getValue();
                        if (!Objects.equals(conceptValue, value)) {
                            conceptFormValue.setChanged(true);
                            conceptFormValue.setOriginalValue(formValue);
                        }
                    });
                    // Find deleted values in a concepts. If will be found, then new instance of
                    // IdmFormValue will be created with the value from original identity-role
                    // attribute.
                    values.forEach(formValue -> {
                        IdmFormValueDto missingConceptFormValue = // 
                        conceptValues.stream().filter(conceptFormValue -> conceptFormValue.getFormAttribute().equals(formValue.getFormAttribute()) && // 
                        conceptFormValue.getSeq() == formValue.getSeq()).findFirst().orElse(// 
                        null);
                        if (missingConceptFormValue == null) {
                            IdmFormAttributeDto formAttributeDto = DtoUtils.getEmbedded(formValue, IdmFormValue_.formAttribute.getName());
                            missingConceptFormValue = new IdmFormValueDto(formAttributeDto);
                            missingConceptFormValue.setChanged(true);
                            missingConceptFormValue.setOriginalValue(formValue);
                            List<IdmFormValueDto> newConceptValues = new ArrayList<IdmFormValueDto>(conceptValues);
                            newConceptValues.add(missingConceptFormValue);
                            conceptFormInstanceFinal.setValues(newConceptValues);
                        }
                    });
                }
            }
            return conceptFormInstance;
        }
    }
    return null;
}
Also used : IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) Serializable(java.io.Serializable) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) ArrayList(java.util.ArrayList) ConceptRoleRequestOperation(eu.bcvsolutions.idm.core.api.domain.ConceptRoleRequestOperation) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) UUID(java.util.UUID) IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)

Aggregations

ConceptRoleRequestOperation (eu.bcvsolutions.idm.core.api.domain.ConceptRoleRequestOperation)1 IdmIdentityRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)1 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)1 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)1 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)1 IdmFormInstanceDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto)1 IdmFormValueDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1