Search in sources :

Example 1 with RoleCompositionEvent

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

the class DuplicateRoleCompositionProcessor method process.

@Override
@SuppressWarnings("unchecked")
public EventResult<IdmRoleDto> process(EntityEvent<IdmRoleDto> event) {
    IdmRoleDto cloned = event.getContent();
    IdmRoleDto originalSource = event.getOriginalSource();
    // 
    Map<String, Serializable> props = resolveProperties(event);
    Set<UUID> processedRoles = (Set<UUID>) props.get(RoleEvent.PROPERTY_PROCESSED_ROLES);
    processedRoles.add(cloned.getId());
    // 
    // find and clone business role composition
    // clone roles recursively
    Set<String> processedSubRoles = new HashSet<>();
    Map<String, IdmRoleCompositionDto> currentSubRoles = new HashMap<>();
    roleCompositionService.findDirectSubRoles(cloned.getId()).forEach(composition -> {
        IdmRoleDto subRole = DtoUtils.getEmbedded(composition, IdmRoleComposition_.sub);
        currentSubRoles.put(subRole.getCode(), composition);
    });
    // 
    roleCompositionService.findDirectSubRoles(originalSource.getId()).stream().filter(composition -> {
        return includeComposition(event, composition);
    }).forEach(composition -> {
        // find sub role on the target environment
        IdmRoleDto subRole = DtoUtils.getEmbedded(composition, IdmRoleComposition_.sub);
        IdmRoleDto targetRole = roleService.getByBaseCodeAndEnvironment(subRole.getBaseCode(), cloned.getEnvironment());
        // 
        if (targetRole != null || duplicateRecursively(event, subRole, targetRole)) {
            if (targetRole == null) {
                // new clone
                targetRole = prepareRole(subRole.getBaseCode(), cloned.getEnvironment());
            }
            if (targetRole != null && subRole.getId().equals(targetRole.getId())) {
                LOG.debug("Role [{}] is duplicated on the same environment - skipping recursion for the same roles", targetRole.getCode());
            } else if (targetRole != null && processedRoles.contains(targetRole.getId())) {
                LOG.debug("Role [{}] was already processed by other business role composition - cycle, skipping", targetRole.getCode());
            } else {
                // 
                // clone / update
                EntityEvent<IdmRoleDto> subEvent = new RoleEvent(RoleEventType.DUPLICATE, targetRole, props);
                // original source is the cloned role
                subEvent.setOriginalSource(subRole);
                // we want to be sync
                subEvent.setPriority(PriorityType.IMMEDIATE);
                EventContext<IdmRoleDto> resultSubRole = roleService.publish(subEvent, event);
                targetRole = resultSubRole.getContent();
            }
            // 
            // create the composition (or check composition exists)
            // find exists
            processedSubRoles.add(targetRole.getCode());
            if (!currentSubRoles.containsKey(targetRole.getCode())) {
                IdmRoleCompositionDto cloneComposition = new IdmRoleCompositionDto(cloned.getId(), targetRole.getId());
                EntityEvent<IdmRoleCompositionDto> createCompositionEvent = new RoleCompositionEvent(RoleCompositionEventType.CREATE, cloneComposition);
                // we want to be sync
                createCompositionEvent.setPriority(PriorityType.IMMEDIATE);
                roleCompositionService.publish(createCompositionEvent, event);
            }
        }
    });
    // 
    // remove unprocessed sub roles, which was removed in surce role
    currentSubRoles.entrySet().stream().filter(entry -> {
        return !processedSubRoles.contains(entry.getKey());
    }).filter(entry -> {
        return includeComposition(event, entry.getValue());
    }).forEach(entry -> {
        // dirty flag role composition only - will be processed after parent action ends
        IdmEntityStateDto stateDeleted = new IdmEntityStateDto();
        stateDeleted.setEvent(event.getId());
        stateDeleted.setSuperOwnerId(cloned.getId());
        stateDeleted.setResult(new OperationResultDto.Builder(OperationState.RUNNING).setModel(new DefaultResultModel(CoreResultCode.DELETED)).build());
        entityStateManager.saveState(entry.getValue(), stateDeleted);
    });
    // 
    return new DefaultEventResult<>(event, this);
}
Also used : DtoUtils(eu.bcvsolutions.idm.core.api.utils.DtoUtils) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) IdmRoleCompositionDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleCompositionDto) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) CoreEventProcessor(eu.bcvsolutions.idm.core.api.event.CoreEventProcessor) RoleEvent(eu.bcvsolutions.idm.core.model.event.RoleEvent) PersistentType(eu.bcvsolutions.idm.core.eav.api.domain.PersistentType) HashSet(java.util.HashSet) EntityStateManager(eu.bcvsolutions.idm.core.api.service.EntityStateManager) Lists(com.google.common.collect.Lists) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) Map(java.util.Map) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) EventResult(eu.bcvsolutions.idm.core.api.event.EventResult) RoleEventType(eu.bcvsolutions.idm.core.model.event.RoleEvent.RoleEventType) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) Description(org.springframework.context.annotation.Description) RoleCompositionEventType(eu.bcvsolutions.idm.core.model.event.RoleCompositionEvent.RoleCompositionEventType) RoleProcessor(eu.bcvsolutions.idm.core.api.event.processor.RoleProcessor) IdmRoleComposition_(eu.bcvsolutions.idm.core.model.entity.IdmRoleComposition_) IdmRoleService(eu.bcvsolutions.idm.core.api.service.IdmRoleService) Set(java.util.Set) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) IdmRoleCompositionService(eu.bcvsolutions.idm.core.api.service.IdmRoleCompositionService) UUID(java.util.UUID) RoleCompositionEvent(eu.bcvsolutions.idm.core.model.event.RoleCompositionEvent) EventContext(eu.bcvsolutions.idm.core.api.event.EventContext) PriorityType(eu.bcvsolutions.idm.core.api.domain.PriorityType) Serializable(java.io.Serializable) IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) List(java.util.List) Component(org.springframework.stereotype.Component) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) Serializable(java.io.Serializable) HashSet(java.util.HashSet) Set(java.util.Set) RoleCompositionEvent(eu.bcvsolutions.idm.core.model.event.RoleCompositionEvent) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) HashMap(java.util.HashMap) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) RoleEvent(eu.bcvsolutions.idm.core.model.event.RoleEvent) EventContext(eu.bcvsolutions.idm.core.api.event.EventContext) IdmRoleCompositionDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleCompositionDto) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) UUID(java.util.UUID) HashSet(java.util.HashSet)

Aggregations

Lists (com.google.common.collect.Lists)1 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)1 PriorityType (eu.bcvsolutions.idm.core.api.domain.PriorityType)1 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)1 IdmEntityStateDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto)1 IdmRoleCompositionDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleCompositionDto)1 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)1 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)1 CoreEventProcessor (eu.bcvsolutions.idm.core.api.event.CoreEventProcessor)1 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)1 EntityEvent (eu.bcvsolutions.idm.core.api.event.EntityEvent)1 EventContext (eu.bcvsolutions.idm.core.api.event.EventContext)1 EventResult (eu.bcvsolutions.idm.core.api.event.EventResult)1 RoleProcessor (eu.bcvsolutions.idm.core.api.event.processor.RoleProcessor)1 EntityStateManager (eu.bcvsolutions.idm.core.api.service.EntityStateManager)1 IdmRoleCompositionService (eu.bcvsolutions.idm.core.api.service.IdmRoleCompositionService)1 IdmRoleService (eu.bcvsolutions.idm.core.api.service.IdmRoleService)1 DtoUtils (eu.bcvsolutions.idm.core.api.utils.DtoUtils)1 PersistentType (eu.bcvsolutions.idm.core.eav.api.domain.PersistentType)1