Search in sources :

Example 11 with AbstractDto

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

the class RequestManagerTest method testDeleteRequestIntegrity.

@Test
public void testDeleteRequestIntegrity() {
    // Log as admin, but not user 'admin' (we don't want to skip WF)
    IdmIdentityDto adminUser = getHelper().createIdentity();
    loginAsAdmin(adminUser.getUsername());
    // Create role
    IdmRoleDto role = getHelper().createRole();
    // Create request
    IdmRequestDto request = requestManager.createRequest(role);
    Assert.assertNotNull(request);
    // Create guarantee
    IdmIdentityDto guarantee = getHelper().createIdentity();
    IdmRoleGuaranteeDto roleGuarantee = new IdmRoleGuaranteeDto();
    roleGuarantee.setRole(role.getId());
    roleGuarantee.setGuarantee(guarantee.getId());
    Requestable requestablePost = requestManager.post(request.getId(), roleGuarantee);
    IdmRequestItemDto changeRequestItem = DtoUtils.getEmbedded((AbstractDto) requestablePost, Requestable.REQUEST_ITEM_FIELD, IdmRequestItemDto.class);
    Assert.assertEquals(RequestOperationType.ADD, changeRequestItem.getOperation());
    Assert.assertTrue(requestablePost instanceof IdmRoleGuaranteeDto);
    // Change role (without save)
    role.setDescription(getHelper().createName());
    role.setPriority(1000);
    // Create request item
    Requestable requestable = requestManager.post(request.getId(), role);
    Assert.assertNotNull(requestable);
    Assert.assertNotNull(requestable.getRequestItem());
    changeRequestItem = DtoUtils.getEmbedded((AbstractDto) requestable, Requestable.REQUEST_ITEM_FIELD, IdmRequestItemDto.class);
    Assert.assertEquals(RequestOperationType.UPDATE, changeRequestItem.getOperation());
    Assert.assertTrue(requestable instanceof IdmRoleDto);
    // Request should be in concept state
    request = requestService.get(request.getId());
    Assert.assertEquals(RequestState.CONCEPT, request.getState());
    // Two items should be created
    Assert.assertEquals(2, requestManager.findRequestItems(request.getId(), null).size());
    // Delete the request
    requestService.delete(request);
    IdmRequestDto requestDeleted = requestService.get(request.getId());
    Assert.assertNull(requestDeleted);
    // All items should be deleted
    Assert.assertEquals(0, requestManager.findRequestItems(request.getId(), null).size());
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmRequestItemDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto) IdmRoleGuaranteeDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleGuaranteeDto) Requestable(eu.bcvsolutions.idm.core.api.domain.Requestable) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) IdmRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Example 12 with AbstractDto

use of eu.bcvsolutions.idm.core.api.dto.AbstractDto 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 13 with AbstractDto

use of eu.bcvsolutions.idm.core.api.dto.AbstractDto 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 14 with AbstractDto

use of eu.bcvsolutions.idm.core.api.dto.AbstractDto 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)

Example 15 with AbstractDto

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

the class IdmTokenController method toFilter.

@Override
protected IdmTokenFilter toFilter(MultiValueMap<String, Object> parameters) {
    IdmTokenFilter filter = new IdmTokenFilter(parameters, getParameterConverter());
    // owner decorator
    String ownerId = getParameterConverter().toString(parameters, IdmEntityEventFilter.PARAMETER_OWNER_ID);
    UUID ownerUuid = null;
    String ownerType = filter.getOwnerType();
    if (StringUtils.isNotEmpty(ownerType) && StringUtils.isNotEmpty(ownerId)) {
        // try to find entity owner by Codeable identifier
        AbstractDto owner = manager.findOwner(ownerType, ownerId);
        if (owner != null) {
            ownerUuid = owner.getId();
        } else {
            LOG.debug("Entity type [{}] with identifier [{}] does not found, raw ownerId will be used as uuid.", ownerType, ownerId);
            // Better exception for FE.
            try {
                DtoUtils.toUuid(ownerId);
            } catch (ClassCastException ex) {
                throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", ownerId), ex);
            }
        }
    }
    if (ownerUuid == null) {
        ownerUuid = getParameterConverter().toUuid(parameters, IdmEntityEventFilter.PARAMETER_OWNER_ID);
    }
    filter.setOwnerId(ownerUuid);
    // 
    return filter;
}
Also used : IdmTokenFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmTokenFilter) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) UUID(java.util.UUID)

Aggregations

AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)54 UUID (java.util.UUID)28 Test (org.junit.Test)16 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)15 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)13 HashMap (java.util.HashMap)12 List (java.util.List)11 Autowired (org.springframework.beans.factory.annotation.Autowired)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Assert (org.springframework.util.Assert)11 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)10 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)10 Map (java.util.Map)10 Service (org.springframework.stereotype.Service)10 Embedded (eu.bcvsolutions.idm.core.api.domain.Embedded)9 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)9 IntrospectionException (java.beans.IntrospectionException)9 Field (java.lang.reflect.Field)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ImmutableMap (com.google.common.collect.ImmutableMap)8