Search in sources :

Example 6 with Embedded

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

the class DefaultRequestManager method addEmbedded.

/**
 * Loads and adds DTOs by embedded annotation
 *
 * @param dto
 * @param requestId
 *
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException
 * @throws IntrospectionException
 * @throws InstantiationException
 */
private void addEmbedded(AbstractDto dto, UUID requestId) throws ReflectiveOperationException, IllegalArgumentException, IntrospectionException {
    Assert.notNull(dto, "DTO is required!");
    Field[] fields = dto.getClass().getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(Embedded.class)) {
            Embedded embeddedAnnotation = field.getAnnotation(Embedded.class);
            if (embeddedAnnotation.enabled()) {
                // If DTO class is abstract then continue
                if (Modifier.isAbstract(embeddedAnnotation.dtoClass().getModifiers())) {
                    continue;
                }
                Object value = EntityUtils.getEntityValue(dto, field.getName());
                if (value instanceof UUID) {
                    // Create mock instance of embedded DTO only with ID
                    UUID id = (UUID) value;
                    AbstractDto embeddedDto = null;
                    if (Requestable.class.isAssignableFrom(embeddedAnnotation.dtoClass())) {
                        embeddedDto = embeddedAnnotation.dtoClass().getDeclaredConstructor().newInstance();
                        embeddedDto.setId(id);
                        Requestable originalEmbeddedDto = this.getDtoService((Requestable) embeddedDto).get(embeddedDto.getId());
                        if (originalEmbeddedDto != null) {
                            // Call standard method for load request's DTO with original DTO
                            embeddedDto = (AbstractDto) this.get(requestId, originalEmbeddedDto);
                        } else {
                            // Call standard method for load request's DTO with mock DTO (only with ID)
                            embeddedDto = (AbstractDto) this.get(requestId, (Requestable) embeddedDto);
                        }
                    } else {
                        // If embedded DTO is not Requestable, then standard service is using
                        embeddedDto = (AbstractDto) lookupService.getDtoService(embeddedAnnotation.dtoClass()).get(id);
                    }
                    if (embeddedDto == null) {
                        continue;
                    }
                    embeddedDto.setTrimmed(true);
                    dto.getEmbedded().put(field.getName(), embeddedDto);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Requestable(eu.bcvsolutions.idm.core.api.domain.Requestable) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Example 7 with Embedded

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

the class UuidToEntityConverter method convert.

@Override
public BaseEntity convert(MappingContext<UUID, BaseEntity> context) {
    Class<BaseEntity> entityClass = context.getDestinationType();
    UUID sourceUuid = context.getSource();
    // 
    if (sourceUuid != null) {
        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)

Example 8 with Embedded

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

the class UuidToEntityConditionalConverter method convert.

@Override
public BaseEntity convert(MappingContext<UUID, BaseEntity> context) {
    Class<BaseEntity> entityClass = context.getDestinationType();
    UUID sourceUuid = context.getSource();
    // 
    if (sourceUuid == null) {
        return null;
    }
    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)8 Field (java.lang.reflect.Field)8 UUID (java.util.UUID)7 BaseEntity (eu.bcvsolutions.idm.core.api.entity.BaseEntity)6 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)6 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)5 PropertyMapping (org.modelmapper.spi.PropertyMapping)5 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)4 Requestable (eu.bcvsolutions.idm.core.api.domain.Requestable)2 IntrospectionException (java.beans.IntrospectionException)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 Serializable (java.io.Serializable)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Codeable (eu.bcvsolutions.idm.core.api.domain.Codeable)1