Search in sources :

Example 1 with EmbeddedDto

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

the class DefaultImportManager method makeAdvancedPairing.

/**
 * Make advanced paring
 *
 * @param dto
 * @param context
 * @param dtoClass
 * @return
 */
private BaseDto makeAdvancedPairing(BaseDto dto, ImportContext context, Class<? extends BaseDto> dtoClass) {
    ExportDescriptorDto descriptor = this.getDescriptor(context, dtoClass);
    for (String advancedParingField : descriptor.getAdvancedParingFields()) {
        try {
            Class<? extends AbstractDto> dtoClassField = this.getDtoClassFromField(dtoClass, advancedParingField);
            if (dtoClassField == null) {
                throw new ResultCodeException(CoreResultCode.IMPORT_FIELD_EMBEDDED_ANNOTATION_MISSING, ImmutableMap.of("field", advancedParingField));
            }
            UUID id = this.getFieldUUIDValue(advancedParingField, dto, dtoClass);
            if (id == null) {
                // ID is null -> no relation exists -> continues
                continue;
            }
            // Get service for DTO in field.
            Class<? extends BaseDto> serviceDtoClass = dtoClassField;
            if (dtoClassField.isAnnotationPresent(Inheritable.class)) {
                serviceDtoClass = dto.getClass().getAnnotation(Inheritable.class).dtoService();
            }
            ReadWriteDtoService<BaseDto, ?> dtoServiceForField = getDtoService(serviceDtoClass);
            BaseDto currentFieldDto = dtoServiceForField.get(id);
            if (currentFieldDto != null) {
                // DTO exists -> no change is necessary.
                continue;
            }
            // DTO not found by ID, we will try find it by code.
            if (dto instanceof AbstractDto) {
                Path dtoPath = Paths.get(context.getTempDirectory().toString(), dtoClass.getSimpleName(), MessageFormat.format("{0}.{1}", dto.getId().toString(), ExportManager.EXTENSION_JSON));
                EmbeddedDto embeddedDto = (EmbeddedDto) this.convertFileToDto(dtoPath.toFile(), EmbeddedDto.class, context);
                JsonNode batchFieldDtoAsString = embeddedDto.getEmbedded().get(advancedParingField);
                if (batchFieldDtoAsString == null) {
                    if (descriptor.isOptional()) {
                        return null;
                    } else {
                        Assert.notNull(batchFieldDtoAsString, MessageFormat.format("Embedded map must contains DTO for advanced paring field [{0}]", advancedParingField));
                    }
                }
                BaseDto batchFieldDto;
                try {
                    batchFieldDto = this.convertStringToDto(batchFieldDtoAsString.toString(), dtoClassField, context);
                    String code = null;
                    if (batchFieldDto instanceof Codeable) {
                        code = ((Codeable) batchFieldDto).getCode();
                        if (Strings.isNotEmpty(code)) {
                            currentFieldDto = lookupService.lookupDto(serviceDtoClass, code);
                        }
                    } else if (batchFieldDto != null) {
                        // Find target DTO by example source DTO (typically by more then one filter field).
                        code = batchFieldDto.toString();
                        EmbeddedDto fieldEmbeddedDto = (EmbeddedDto) this.convertStringToDto(batchFieldDtoAsString.toString(), EmbeddedDto.class, context);
                        currentFieldDto = this.findByExample(batchFieldDto, fieldEmbeddedDto, context);
                    }
                    if (currentFieldDto != null) {
                        // DTO for given code exists -> replace ID by this new in given DTO.
                        // 
                        new PropertyDescriptor(advancedParingField, dtoClass).getWriteMethod().invoke(dto, currentFieldDto.getId());
                        // Save old and new ID for next DTOs.
                        context.getReplacedIDs().put(id, (UUID) currentFieldDto.getId());
                        continue;
                    } else {
                        // If is DTO set as optional, we will only skip this DTO.
                        if (descriptor.isOptional()) {
                            throw new ResultCodeException(CoreResultCode.IMPORT_ADVANCED_PARING_NOT_FOUND_OPTIONAL, ImmutableMap.of("field", advancedParingField, "dto", dto.toString(), "notFoundDto", batchFieldDto.toString(), "code", code));
                        }
                        throw new ResultCodeException(CoreResultCode.IMPORT_ADVANCED_PARING_FAILED_NOT_FOUND, ImmutableMap.of("field", advancedParingField, "dto", dto.toString(), "notFoundDto", batchFieldDto.toString(), "code", code));
                    }
                } catch (IOException e) {
                    throw new ResultCodeException(CoreResultCode.IMPORT_CONVERT_TO_DTO_FAILED, ImmutableMap.of("file", "Converted from String.", "dto", dtoClass), e);
                }
            }
            // (skip this DTO).
            if (descriptor.isOptional()) {
                return null;
            }
        } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
            throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_REFLECTION_FAILED, e);
        }
    }
    return dto;
}
Also used : Path(java.nio.file.Path) Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) PropertyDescriptor(java.beans.PropertyDescriptor) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IntrospectionException(java.beans.IntrospectionException) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExportDescriptorDto(eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto) EmbeddedDto(eu.bcvsolutions.idm.core.api.dto.EmbeddedDto) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) UUID(java.util.UUID)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Codeable (eu.bcvsolutions.idm.core.api.domain.Codeable)1 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)1 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)1 EmbeddedDto (eu.bcvsolutions.idm.core.api.dto.EmbeddedDto)1 ExportDescriptorDto (eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1 UUID (java.util.UUID)1