Search in sources :

Example 1 with Embedded

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

the class DefaultLookupService method lookupEmbeddedDto.

@Override
@SuppressWarnings("unchecked")
public <DTO extends BaseDto> DTO lookupEmbeddedDto(AbstractDto dto, String attributeName) {
    Assert.notNull(dto, "DTO is required.");
    Assert.notNull(dto.getEmbedded(), "DTO does not have embedded DTO map initialized and is required.");
    Assert.hasLength(attributeName, "Singular attribute is required to get embedded DTO.");
    // 
    // from embedded
    DTO embeddedDto = DtoUtils.getEmbedded(dto, attributeName, (DTO) null);
    if (embeddedDto != null) {
        return embeddedDto;
    }
    // try to load by lookup
    try {
        // get target DTO type by embedded annotation - annotation is required
        Field embeddableField = EntityUtils.getFirstFieldInClassHierarchy(dto.getClass(), attributeName);
        if (!embeddableField.isAnnotationPresent(Embedded.class)) {
            throw new IllegalArgumentException(String.format("Dto lookup for dto type [%s] attribute [%s] is not supported. Embedded annotion is missing.", dto.getClass(), attributeName));
        }
        Embedded embedded = embeddableField.getAnnotation(Embedded.class);
        // 
        // get DTO identifier ~ field value
        PropertyDescriptor fieldDescriptor = EntityUtils.getFieldDescriptor(dto, attributeName);
        Object fieldValue = fieldDescriptor.getReadMethod().invoke(dto);
        if (!(fieldValue instanceof Serializable)) {
            throw new IllegalArgumentException(String.format("Dto lookup for dto type [%s] attribute [%s] is not supported.", dto.getClass(), attributeName));
        }
        // 
        return (DTO) lookupDto(embedded.dtoClass(), (Serializable) fieldValue);
    } catch (ReflectiveOperationException | IntrospectionException ex) {
        throw new IllegalArgumentException(String.format("Dto lookup for dto type [%s] attribute [%s] is not supported.", dto.getClass(), attributeName), ex);
    }
}
Also used : Field(java.lang.reflect.Field) Serializable(java.io.Serializable) PropertyDescriptor(java.beans.PropertyDescriptor) IntrospectionException(java.beans.IntrospectionException) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded)

Example 2 with Embedded

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

the class DefaultRequestManager method dtoToMap.

private Map<String, Object> dtoToMap(AbstractDto dto) {
    Map<String, Object> results = new HashMap<>();
    if (dto == null) {
        return results;
    }
    try {
        List<PropertyDescriptor> descriptors = Lists.newArrayList(Introspector.getBeanInfo(dto.getClass()).getPropertyDescriptors());
        List<Field> fields = // 
        Lists.newArrayList(dto.getClass().getDeclaredFields()).stream().filter(// 
        field -> !Requestable.REQUEST_ITEM_FIELD.equals(field.getName())).filter(// 
        field -> !Requestable.REQUEST_FIELD.equals(field.getName())).filter(// 
        field -> !field.isAnnotationPresent(JsonIgnore.class)).collect(// 
        Collectors.toList());
        // Embedded objects
        // 
        fields.stream().filter(// 
        field -> field.isAnnotationPresent(Embedded.class)).forEach(field -> {
            results.put(field.getName(), dto.getEmbedded().get(field.getName()));
        });
        // Others objects
        // 
        fields.stream().filter(// 
        field -> !field.isAnnotationPresent(Embedded.class)).forEach(field -> {
            try {
                PropertyDescriptor fieldDescriptor = // 
                descriptors.stream().filter(// 
                descriptor -> field.getName().equals(descriptor.getName())).findFirst().orElse(// 
                null);
                if (fieldDescriptor != null) {
                    Object value = fieldDescriptor.getReadMethod().invoke(dto);
                    results.put(field.getName(), value);
                }
            } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                throw new CoreException(e);
            }
        });
    } catch (IntrospectionException e) {
        throw new CoreException(e);
    }
    return results;
}
Also used : DtoUtils(eu.bcvsolutions.idm.core.api.utils.DtoUtils) IdmRequestItemDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) Autowired(org.springframework.beans.factory.annotation.Autowired) FormService(eu.bcvsolutions.idm.core.eav.api.service.FormService) SecurityService(eu.bcvsolutions.idm.core.security.api.service.SecurityService) Map(java.util.Map) IdmRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestDto) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) IntrospectionException(java.beans.IntrospectionException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyDescriptor(java.beans.PropertyDescriptor) Builder(eu.bcvsolutions.idm.core.api.dto.OperationResultDto.Builder) Lazy(org.springframework.context.annotation.Lazy) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) RequestOperationType(eu.bcvsolutions.idm.core.api.domain.RequestOperationType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Introspector(java.beans.Introspector) Strings(com.google.common.base.Strings) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) IdmBasePermission(eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission) Lists(com.google.common.collect.Lists) BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) Service(org.springframework.stereotype.Service) WorkflowProcessInstanceService(eu.bcvsolutions.idm.core.workflow.service.WorkflowProcessInstanceService) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) Requestable(eu.bcvsolutions.idm.core.api.domain.Requestable) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) IOException(java.io.IOException) Field(java.lang.reflect.Field) BaseFilter(eu.bcvsolutions.idm.core.api.dto.filter.BaseFilter) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmRequestItemService(eu.bcvsolutions.idm.core.api.service.IdmRequestItemService) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) FormValueService(eu.bcvsolutions.idm.core.eav.api.service.FormValueService) RequestEventType(eu.bcvsolutions.idm.core.model.event.RequestEvent.RequestEventType) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) RequestEvent(eu.bcvsolutions.idm.core.model.event.RequestEvent) RequestState(eu.bcvsolutions.idm.core.api.domain.RequestState) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) CoreGroupPermission(eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Pageable(org.springframework.data.domain.Pageable) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ReadWriteDtoService(eu.bcvsolutions.idm.core.api.service.ReadWriteDtoService) RequestManager(eu.bcvsolutions.idm.core.api.service.RequestManager) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) ExceptionUtils(eu.bcvsolutions.idm.core.api.utils.ExceptionUtils) Modifier(java.lang.reflect.Modifier) Entry(java.util.Map.Entry) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) RoleRequestApprovalProcessor(eu.bcvsolutions.idm.core.model.event.processor.role.RoleRequestApprovalProcessor) PageImpl(org.springframework.data.domain.PageImpl) IdmRequestService(eu.bcvsolutions.idm.core.api.service.IdmRequestService) FormableEntity(eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity) IdmRequestItem(eu.bcvsolutions.idm.core.model.entity.IdmRequestItem) HashMap(java.util.HashMap) MessageFormat(java.text.MessageFormat) ConfidentialStorage(eu.bcvsolutions.idm.core.api.service.ConfidentialStorage) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) RequestFilterPredicate(eu.bcvsolutions.idm.core.api.domain.RequestFilterPredicate) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) Propagation(org.springframework.transaction.annotation.Propagation) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) EntityUtils(eu.bcvsolutions.idm.core.api.utils.EntityUtils) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Niceable(eu.bcvsolutions.idm.core.api.domain.Niceable) IdmRequestItemFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmRequestItemFilter) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) IdmRequestAttributeValueDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestAttributeValueDto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IdmRequestItemChangesDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestItemChangesDto) MethodDescriptor(java.beans.MethodDescriptor) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) IdmRequestItemAttributeDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestItemAttributeDto) Comparator(java.util.Comparator) IdmRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmRequestFilter) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) PropertyDescriptor(java.beans.PropertyDescriptor) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) IntrospectionException(java.beans.IntrospectionException) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded)

Example 3 with Embedded

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

the class UuidToUuidConverter method convert.

@Override
public UUID convert(MappingContext<UUID, UUID> context) {
    if (context.getSource() == null) {
        return null;
    }
    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 = 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.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 4 with Embedded

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

the class EntityToUuidConditionalConverter method convert.

@Override
public UUID convert(MappingContext<BaseEntity, UUID> context) {
    if (context.getSource() == null || !(context.getSource().getId() instanceof UUID)) {
        return null;
    }
    // 
    MappingContext<?, ?> parentContext = context.getParent();
    if (parentContext == null || parentContext.getDestination() == null || !AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) || parentContext.getSource() == null || !BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
        return (UUID) context.getSource().getId();
    }
    // 
    PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
    // Find name of field by property mapping
    String field = propertyMapping.getLastDestinationProperty().getName();
    // dto type
    Class<?> destinationType = parentContext.getDestinationType();
    try {
        AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
        BaseEntity entity = (BaseEntity) context.getSource();
        Map<String, BaseDto> embedded = parentDto.getEmbedded();
        // Find field in DTO class
        // FIXME: Embedded will not be set, if field name is different with getter and setter (fieldType == null when setter has different name).
        Field fieldType = getFirstFieldInClassHierarchy(destinationType, field);
        if (fieldType.isAnnotationPresent(Embedded.class)) {
            Embedded embeddedAnnotation = fieldType.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().getDeclaredConstructor().newInstance();
                }
                dto.setTrimmed(true);
                // Separate map entity to new embedded DTO
                try {
                    dto = getLookupService().toDto(entity, dto, null);
                } catch (Exception ex) {
                    LOG.warn("DTO [{}] cannot be converted into embedded by underlying service, try to convert by default converter.", dto.getClass().getCanonicalName(), ex);
                    modeler.map(entity, dto);
                }
                embedded.put(field, dto);
                // Add filled DTO to embedded map to parent DTO
                parentDto.setEmbedded(embedded);
            }
        }
    } catch (NoSuchFieldException ex) {
        // FIXME: Embedded will not be set, if field name is different with getter and setter (fieldType == null when setter has different name).
        LOG.debug("Field [{}] in dto class [{}] not found, embedded dto cannot be filled. Different dto field name vs. getter and setter name is not supported.", field, destinationType);
    } catch (ReflectiveOperationException ex) {
        throw new CoreException(ex);
    }
    // 
    return (UUID) context.getSource().getId();
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) 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 5 with Embedded

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

the class EntityToUuidConverter method convert.

@Override
public UUID convert(MappingContext<BaseEntity, UUID> context) {
    if (context.getSource() == null || !(context.getSource().getId() instanceof UUID)) {
        return null;
    }
    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().getDeclaredConstructor().newInstance();
                    }
                    dto.setTrimmed(true);
                    // Separate map entity to new embedded DTO.
                    try {
                        dto = getLookupService().toDto(entity, dto, null);
                    } catch (Exception ex) {
                        LOG.warn("DTO [{}] cannot be converted into embedded by underlying service, try to convert by default converter.", dto.getClass().getCanonicalName(), ex);
                        modeler.map(entity, dto);
                    }
                    embedded.put(field, dto);
                    // Add filled DTO to embedded map to parent DTO.
                    parentDto.setEmbedded(embedded);
                }
            }
        } catch (ReflectiveOperationException ex) {
            throw new CoreException(ex);
        }
    }
    return (UUID) context.getSource().getId();
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) 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)

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