Search in sources :

Example 1 with PropertyMapping

use of org.modelmapper.spi.PropertyMapping in project CzechIdMng by bcvsolutions.

the class EntityToUuidConverter method convert.

@Override
public UUID convert(MappingContext<BaseEntity, UUID> context) {
    if (context != null && context.getSource() != null && context.getSource().getId() instanceof UUID) {
        MappingContext<?, ?> parentContext = context.getParent();
        if (parentContext != null && parentContext.getDestination() != null && AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) && parentContext.getSource() != null && BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
            try {
                AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
                BaseEntity entity = (BaseEntity) context.getSource();
                Map<String, BaseDto> embedded = parentDto.getEmbedded();
                PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
                // Find name of field by property mapping
                String field = propertyMapping.getLastDestinationProperty().getName();
                // Find field in DTO class
                Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getDestinationType(), field);
                if (fieldTyp.isAnnotationPresent(Embedded.class)) {
                    Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
                    if (embeddedAnnotation.enabled()) {
                        // If has field Embedded (enabled) annotation, then
                        // we will create new
                        // instance of DTO
                        // 
                        AbstractDto dto = null;
                        // If dto class is abstract get dto from lookup
                        if (Modifier.isAbstract(embeddedAnnotation.dtoClass().getModifiers())) {
                            dto = (AbstractDto) getLookupService().lookupDto(entity.getClass(), entity.getId());
                        } else {
                            dto = embeddedAnnotation.dtoClass().newInstance();
                        }
                        dto.setTrimmed(true);
                        // Separate map entity to new embedded DTO
                        modeler.map(entity, dto);
                        embedded.put(field, dto);
                        // Add filled DTO to embedded map to parent DTO
                        parentDto.setEmbedded(embedded);
                    }
                }
            } catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
                throw new CoreException(e);
            }
        }
        return (UUID) context.getSource().getId();
    }
    return null;
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Example 2 with PropertyMapping

use of org.modelmapper.spi.PropertyMapping in project CzechIdMng by bcvsolutions.

the class UuidToUuidConverter method convert.

@Override
public UUID convert(MappingContext<UUID, UUID> context) {
    if (context != null && context.getSource() != null && context.getSource() instanceof UUID) {
        MappingContext<?, ?> parentContext = context.getParent();
        if (parentContext != null && parentContext.getDestination() != null && AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) && parentContext.getSource() != null && BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
            try {
                AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
                UUID entityId = (UUID) context.getSource();
                Map<String, BaseDto> embedded = parentDto.getEmbedded();
                PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
                // Find name of field by property mapping
                String field = propertyMapping.getLastDestinationProperty().getName();
                // Find field in DTO class
                Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getDestinationType(), field);
                if (fieldTyp.isAnnotationPresent(Embedded.class)) {
                    Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
                    if (embeddedAnnotation.enabled()) {
                        // Load DTO service by dtoClass and get DTO by UUID
                        ReadDtoService<?, ?> lookup = getLookupService().getDtoService(embeddedAnnotation.dtoClass());
                        if (lookup != null) {
                            AbstractDto dto = (AbstractDto) lookup.get(entityId);
                            dto.setTrimmed(true);
                            embedded.put(field, dto);
                            // Add filled DTO to embedded map to parent DTO
                            parentDto.setEmbedded(embedded);
                        }
                    }
                }
            } catch (NoSuchFieldException | SecurityException e) {
                throw new CoreException(e);
            }
        }
    }
    return context == null ? null : (UUID) context.getSource();
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Example 3 with PropertyMapping

use of org.modelmapper.spi.PropertyMapping in project CzechIdMng by bcvsolutions.

the class UuidToEntityConverter method convert.

@Override
public BaseEntity convert(MappingContext<UUID, BaseEntity> context) {
    if (context != null && context.getSource() != null) {
        UUID sourceUUID = context.getSource();
        Class<BaseEntity> entityClass = context.getDestinationType();
        MappingContext<?, ?> parentContext = context.getParent();
        PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
        // Find name of field by property mapping
        String field = propertyMapping.getLastDestinationProperty().getName();
        try {
            // Find field in DTO class
            Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getSourceType(), field);
            if (fieldTyp.isAnnotationPresent(Embedded.class)) {
                Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
                if (embeddedAnnotation.enabled()) {
                    EntityLookup<?> lookup = getLookupService().getEntityLookup(embeddedAnnotation.dtoClass());
                    if (lookup != null) {
                        return lookup.lookup(sourceUUID);
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException e) {
            throw new CoreException(e);
        }
        // We do not have lookup by embedded annotation. We try load service for entity
        EntityLookup<?> lookup = getLookupService().getEntityLookup(entityClass);
        if (lookup != null) {
            return lookup.lookup(sourceUUID);
        }
    }
    return null;
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Aggregations

Embedded (eu.bcvsolutions.idm.core.api.domain.Embedded)3 BaseEntity (eu.bcvsolutions.idm.core.api.entity.BaseEntity)3 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)3 Field (java.lang.reflect.Field)3 UUID (java.util.UUID)3 PropertyMapping (org.modelmapper.spi.PropertyMapping)3 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)2 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)2