Search in sources :

Example 1 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManager method createEvent.

private IdmEntityEventDto createEvent(Class<? extends Identifiable> ownerType, UUID ownerId, EntityEvent<? extends Identifiable> originalEvent) {
    Assert.notNull(ownerType);
    Assert.notNull(ownerId, "Change can be published after entity id is assigned at least.");
    // 
    IdmEntityEventDto savedEvent = new IdmEntityEventDto();
    savedEvent.setOwnerId(ownerId);
    savedEvent.setOwnerType(getOwnerType(ownerType));
    savedEvent.setResult(new OperationResultDto.Builder(OperationState.CREATED).build());
    savedEvent.setInstanceId(eventConfiguration.getAsynchronousInstanceId());
    // 
    if (originalEvent != null) {
        savedEvent.setEventType(originalEvent.getType().name());
        savedEvent.getProperties().putAll(originalEvent.getProperties());
        savedEvent.setParent(EntityUtils.toUuid(originalEvent.getProperties().get(EVENT_PROPERTY_EVENT_ID)));
        savedEvent.setExecuteDate((DateTime) originalEvent.getProperties().get(EVENT_PROPERTY_EXECUTE_DATE));
        savedEvent.setPriority((PriorityType) originalEvent.getProperties().get(EVENT_PROPERTY_PRIORITY));
        savedEvent.setParentEventType(originalEvent.getType().name());
        savedEvent.setContent(originalEvent.getContent());
        savedEvent.setOriginalSource(originalEvent.getOriginalSource());
        savedEvent.setClosed(originalEvent.isClosed());
        if (savedEvent.isClosed()) {
            savedEvent.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(new DefaultResultModel(CoreResultCode.EVENT_ALREADY_CLOSED)).build());
        }
        savedEvent.setSuspended(originalEvent.isSuspended());
    } else {
        // notify as default event type
        savedEvent.setEventType(CoreEventType.NOTIFY.name());
    }
    if (savedEvent.getPriority() == null) {
        savedEvent.setPriority(PriorityType.NORMAL);
    }
    // 
    return savedEvent;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)

Example 2 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class DefaultSysProvisioningOperationService method handleFailed.

/**
 * REQUIRES_NEW => we want to have log in queue / archive all time, even original transaction ends with exception.
 */
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public SysProvisioningOperationDto handleFailed(SysProvisioningOperationDto operation, Exception ex) {
    ResultModel resultModel;
    if (ex instanceof ResultCodeException) {
        resultModel = ((ResultCodeException) ex).getError().getError();
    } else {
        String uid = getByProvisioningOperation(operation).getUid();
        resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_FAILED, ImmutableMap.of("name", uid, "system", getSystem(operation).getName(), "operationType", operation.getOperationType(), "objectClass", operation.getProvisioningContext().getConnectorObject().getObjectClass().getType()));
    }
    LOG.error(resultModel.toString(), ex);
    // 
    operation.increaseAttempt();
    operation.setMaxAttempts(provisioningConfiguration.getRetryMaxAttempts());
    operation.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
    // 
    if (!operation.isDryRun()) {
        operation = save(operation);
        // create archive operation for the current attempt
        provisioningArchiveService.archive(operation);
        // 
        // calculate next attempt
        SysProvisioningOperationDto firstOperation = getFirstOperationByBatchId(operation.getBatch());
        if (firstOperation.equals(operation)) {
            SysProvisioningBatchDto batch = batchService.get(operation.getBatch());
            batch.setNextAttempt(batchService.calculateNextAttempt(operation));
            batch = batchService.save(batch);
        }
        // 
        if (securityService.getCurrentId() != null) {
            // TODO: check account owner
            // TODO: notification is moved into console ... simple LOG instead?
            notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
        }
    }
    return operation;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) SysProvisioningBatchDto(eu.bcvsolutions.idm.acc.dto.SysProvisioningBatchDto) ConfidentialString(eu.bcvsolutions.idm.core.security.api.domain.ConfidentialString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) SysProvisioningOperationDto(eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class DefaultProvisioningExecutor method execute.

/**
 * Next processing is executed outside a transaction
 * => operation states has to be saved in new transactions
 * => rollback on the target system is not possible anyway.
 */
@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public synchronized OperationResult execute(SysProvisioningBatchDto batch) {
    Assert.notNull(batch, "Provisioning batch is required.");
    batch = batchService.get(batch.getId());
    // 
    OperationResult result = null;
    List<SysProvisioningOperationDto> operations = provisioningOperationService.getByTimelineAndBatchId(batch.getId());
    if (operations.isEmpty()) {
        // reset next attempt time if is filled
        if (batch.getNextAttempt() != null) {
            batch.setNextAttempt(null);
            batchService.save(batch);
        }
        return new OperationResult.Builder(OperationState.EXECUTED).build();
    }
    for (SysProvisioningOperationDto provisioningOperation : operations) {
        // operation is already running - not complete or executed manually between
        if (provisioningOperation.getResultState() == OperationState.RUNNING) {
            LOG.debug("Previous operation [{}] still running, next operations will be executed after previous operation ends (with next retry run)", provisioningOperation.getId());
            // 
            ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_IS_IN_QUEUE, ImmutableMap.of("name", provisioningOperation.getSystemEntityUid(), "system", provisioningOperation.getSystem(), "operationType", provisioningOperation.getOperationType(), "objectClass", provisioningOperation.getProvisioningContext().getConnectorObject().getObjectClass()));
            result = new OperationResult.Builder(OperationState.NOT_EXECUTED).setModel(resultModel).build();
            break;
        }
        // It not possible to get operation from embedded, because missing request
        // not run in transaction
        SysProvisioningOperationDto operation = executeInternal(provisioningOperation);
        result = operation.getResult();
        if (OperationState.EXECUTED != result.getState()) {
            // stop processing next requests
            return result;
        }
    }
    // last processed request state (previous requests will be OperationState.EXECUTED)
    return result;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) SysProvisioningOperationDto(eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class ContractSliceDeleteProcessor method createDeleteDirtyState.

/**
 * Create new dirty state for delete of contract slice
 *
 * @param slice
 * @param parameters
 * @return
 */
private IdmEntityStateDto createDeleteDirtyState(IdmContractSliceDto slice, Map<String, Serializable> parameters) {
    Map<String, Object> transformedMarameters = new HashMap<String, Object>();
    transformedMarameters.put("entityId", slice.getId());
    // Mark state for delete the slice
    transformedMarameters.put(ClearDirtyStateForContractSliceTaskExecutor.TO_DELETE, Boolean.TRUE);
    transformedMarameters.putAll(parameters);
    DefaultResultModel resultModel = new DefaultResultModel(CoreResultCode.DIRTY_STATE, transformedMarameters);
    IdmEntityStateDto dirtyState = new IdmEntityStateDto();
    dirtyState.setResult(new OperationResultDto.Builder(OperationState.BLOCKED).setModel(resultModel).build());
    return entityStateManager.saveState(slice, dirtyState);
}
Also used : IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) HashMap(java.util.HashMap) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto)

Example 5 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class DefaultImportManager method executeImportForType.

/**
 * Ensures add new and update existed DTOs by given batch.
 *
 * @param descriptor
 * @param context
 */
private void executeImportForType(ExportDescriptorDto descriptor, ImportContext context) {
    Class<? extends BaseDto> dtoClass = descriptor.getDtoClass();
    Path dtoTypePath = Paths.get(context.getTempDirectory().toString(), dtoClass.getSimpleName());
    try {
        List<BaseDto> dtos;
        try (Stream<Path> paths = Files.walk(dtoTypePath)) {
            dtos = // 
            paths.filter(// 
            Files::isRegularFile).map(path -> {
                BaseDto dto = convertFileToDto(path.toFile(), dtoClass, context);
                Assert.notNull(dto, "DTO cannot be null after conversion from the batch!");
                return dto;
            }).collect(Collectors.toList());
        }
        if (dtos.isEmpty()) {
            return;
        }
        // Sorts all DTOs for this type (maybe it is tree).
        dtos = sortsDTOs(dtoClass, dtos);
        int i = 0;
        for (BaseDto dto : dtos) {
            // Flush Hibernate in batch - performance improving
            if (i % 20 == 0 && i > 0) {
                // Call hard hibernate session flush and clear
                if (getHibernateSession().isOpen()) {
                    getHibernateSession().flush();
                    getHibernateSession().clear();
                }
            }
            i++;
            // Increase counter and update state of import LRT.
            context.getImportTaskExecutor().increaseCounter();
            context.getImportTaskExecutor().updateState();
            BaseDto parentDto = getParentDtoFromBatch(dto, context);
            if (parentDto == null) {
                parentDto = dto;
            }
            BaseDto originalDto = dto;
            try {
                dto = makeAdvancedPairing(dto, context, dtoClass);
                if (dto == null) {
                    // If DTO after advanced pairing is null, then was not found and is optional ->
                    // skip.
                    IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), originalDto, RequestOperationType.ADD, (UUID) parentDto.getId());
                    ResultModel resultModel = new DefaultResultModel(CoreResultCode.IMPORT_DTO_SKIPPED, ImmutableMap.of("dto", originalDto.toString()));
                    dtoLog.setResult(new OperationResultDto.Builder(OperationState.CANCELED).setModel(resultModel).build());
                    importLogService.saveDistinct(dtoLog);
                    continue;
                }
            } catch (ResultCodeException ex) {
                if (context.isDryRun() && ex.getError() != null && ex.getError().getError() != null && CoreResultCode.IMPORT_ADVANCED_PARING_FAILED_NOT_FOUND.name().equals(ex.getError().getError().getStatusEnum())) {
                    // Not found DTO we will mark as skipped in dry run mode.
                    IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), originalDto, RequestOperationType.ADD, (UUID) parentDto.getId());
                    dtoLog.setResult(new OperationResultDto.Builder(OperationState.EXCEPTION).setException(ex).build());
                    importLogService.saveDistinct(dtoLog);
                    continue;
                } else if (ex.getError() != null && ex.getError().getError() != null && CoreResultCode.IMPORT_ADVANCED_PARING_NOT_FOUND_OPTIONAL.name().equals(ex.getError().getError().getStatusEnum())) {
                    // Not found DTO, but optional, we will mark as skipped.
                    IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), originalDto, RequestOperationType.ADD, (UUID) parentDto.getId());
                    dtoLog.setResult(new OperationResultDto.Builder(OperationState.CANCELED).setException(ex).build());
                    importLogService.saveDistinct(dtoLog);
                    continue;
                }
                throw ex;
            }
            Class<? extends BaseDto> serviceDtoClass = dtoClass;
            if (dto instanceof IdmFormInstanceDto) {
                // Form instance is very special here (doesn't have entity in DB).
                IdmFormInstanceDto formInstance = (IdmFormInstanceDto) dto;
                IdmFormDefinitionDto definition = formInstance.getFormDefinition();
                Assert.notNull(definition, "Definition cannot be null for import!");
                CoreEvent<IdmFormInstanceDto> event = new CoreEvent<>(CoreEventType.UPDATE, formInstance);
                // Check if owner exist (UPDATE/ADD)
                @SuppressWarnings("unchecked") Class<? extends BaseDto> ownerType = (Class<? extends BaseDto>) ((IdmFormInstanceDto) dto).getOwnerType();
                UUID ownerId = UUID.fromString((String) ((IdmFormInstanceDto) dto).getOwnerId());
                BaseDto ownerDto = this.getDtoService(ownerType).get(ownerId);
                IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, ownerDto != null ? RequestOperationType.UPDATE : RequestOperationType.ADD, ownerId);
                if (!context.isDryRun()) {
                    formService.publish(event);
                    dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
                } else {
                    dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(// 
                    new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
                }
                importLogService.saveDistinct(dtoLog);
                continue;
            }
            if (dto.getClass().isAnnotationPresent(Inheritable.class)) {
                serviceDtoClass = dto.getClass().getAnnotation(Inheritable.class).dtoService();
            }
            ReadWriteDtoService<BaseDto, ?> dtoService = getDtoService(serviceDtoClass);
            BaseDto currentDto = dtoService.get(dto.getId());
            if (currentDto != null) {
                // DTO with same ID already exists -> update.
                IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, RequestOperationType.UPDATE, (UUID) parentDto.getId());
                // Resolve excluded fields
                dto = this.excludeFields(dto, currentDto, context);
                if (!context.isDryRun()) {
                    dtoService.save(dto);
                    dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
                } else {
                    dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(// 
                    new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
                }
                importLogService.saveDistinct(dtoLog);
                continue;
            }
            if (dto instanceof Codeable) {
                // We try to find exists DTO by code.
                currentDto = lookupService.lookupDto(serviceDtoClass, ((Codeable) dto).getCode());
            }
            // Find target DTO by example source DTO (typically by more then one filter field).
            currentDto = findByExample(dto, null, context);
            if (dto instanceof IdmFormDefinitionDto) {
                IdmFormDefinitionDto definition = (IdmFormDefinitionDto) dto;
                // We try to find exists definition by code and type (IdmFormDefinitionDto is
                // not Codeable).
                currentDto = formService.getDefinition(definition.getType(), definition.getCode());
            }
            if (dto instanceof IdmFormAttributeDto) {
                IdmFormAttributeDto attribute = (IdmFormAttributeDto) dto;
                IdmFormDefinitionDto definition = formService.getDefinition(attribute.getFormDefinition());
                if (definition != null) {
                    // We try to find exists attribute definition by code and form definition.
                    currentDto = formService.getAttribute(definition, attribute.getCode());
                } else {
                    currentDto = null;
                }
            }
            if (currentDto != null) {
                // We found current DTO in IdM.
                // Save old and new ID for next DTOs.
                context.getReplacedIDs().put((UUID) dto.getId(), (UUID) currentDto.getId());
                // We have to change the ID in import DTO.
                dto.setId(currentDto.getId());
                // Update current DTO by batch DTO.
                IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, RequestOperationType.UPDATE, (UUID) parentDto.getId());
                if (!context.isDryRun()) {
                    // Resolve excluded fields
                    dto = this.excludeFields(dto, currentDto, context);
                    // Save a DTO.
                    dtoService.save(dto);
                    dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
                } else {
                    dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(// 
                    new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
                }
                importLogService.saveDistinct(dtoLog);
            } else {
                IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), dto, RequestOperationType.ADD, (UUID) parentDto.getId());
                // No current DTO was found -> create.
                if (!context.isDryRun()) {
                    // Resolve excluded fields
                    dto = this.excludeFields(dto, null, context);
                    // Save new DTO.
                    dtoService.save(dto);
                    dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
                } else {
                    dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(// 
                    new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
                }
                importLogService.saveDistinct(dtoLog);
            }
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_IO_FAILED, e);
    }
}
Also used : Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) IdmImportLogDto(eu.bcvsolutions.idm.core.api.dto.IdmImportLogDto) UUID(java.util.UUID) Path(java.nio.file.Path) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IOException(java.io.IOException) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent)

Aggregations

DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)104 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)52 UUID (java.util.UUID)48 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)40 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)35 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)25 IdmEntityStateDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto)24 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)21 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)19 HashMap (java.util.HashMap)19 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)18 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)16 IdmRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)16 Autowired (org.springframework.beans.factory.annotation.Autowired)16 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)15 List (java.util.List)15 IdmConceptRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto)14 Set (java.util.Set)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)13