Search in sources :

Example 1 with DefaultDtoMapper

use of eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper in project CzechIdMng by bcvsolutions.

the class DefaultLookupService method toDto.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <DTO extends BaseDto> DTO toDto(BaseEntity entity, DTO dto, DataFilter context) {
    Assert.notNull(entity, "Entity is required.");
    // 
    Class<? extends BaseDto> dtoClass;
    if (dto != null) {
        dtoClass = dto.getClass();
    } else {
        Class<? extends BaseEntity> entityClass = entity.getClass();
        if (entity instanceof HibernateProxy) {
            entityClass = (Class<? extends BaseEntity>) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation().getClass();
        }
        dtoClass = getDtoClass(entityClass);
    }
    if (dtoClass == null) {
        LOG.error("DTO class for entity type [{}] cannot be resolved. Entity cannot be mapped to DTO.", entity.getClass());
        // 
        return null;
    }
    // 
    DtoMapper mapper = (DtoMapper) getDtoMappers().getPluginFor(dtoClass);
    if (mapper == null) {
        LOG.debug("DTO for entity type [{}] will be mapped by default mapper.", entity.getClass());
        // 
        mapper = new DefaultDtoMapper(this.context.getBean(ModelMapper.class), dtoClass);
    }
    return (DTO) mapper.map(entity, dto, context);
}
Also used : DefaultDtoMapper(eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper) DtoMapper(eu.bcvsolutions.idm.core.api.rest.lookup.DtoMapper) HibernateProxy(org.hibernate.proxy.HibernateProxy) DefaultDtoMapper(eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper)

Example 2 with DefaultDtoMapper

use of eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper in project CzechIdMng by bcvsolutions.

the class IdmFormValueDtoMapperIntegrationTest method testMapWithDefaultMapper.

@Test
public void testMapWithDefaultMapper() {
    IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
    // 
    // create definition one
    IdmFormAttributeDto attributeDefinitionOne = new IdmFormAttributeDto();
    attributeDefinitionOne.setCode(getHelper().createName());
    attributeDefinitionOne.setName(attributeDefinitionOne.getCode());
    attributeDefinitionOne.setPersistentType(PersistentType.TEXT);
    IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class, getHelper().createName(), Lists.newArrayList(attributeDefinitionOne));
    attributeDefinitionOne = formDefinitionOne.getMappedAttributeByCode(attributeDefinitionOne.getCode());
    // 
    IdmFormValueDto value = new IdmFormValueDto(attributeDefinitionOne);
    value.setValue(getHelper().createName());
    // 
    formService.saveValues(identity, formDefinitionOne, Lists.newArrayList(value));
    // 
    IdmFormValueDto savedValue = formService.getValues(identity, formDefinitionOne).get(0);
    Assert.assertEquals(value.getStringValue(), savedValue.getStringValue());
    // 
    IdmIdentityFormValue savedEntity = identityFormValueRepository.findById(savedValue.getId()).get();
    // 
    DefaultDtoMapper mapper = new DefaultDtoMapper(modelMapper, IdmFormValueDto.class);
    IdmFormValueFilter<?> context = new IdmFormValueFilter<>();
    context.setAddOwnerDto(true);
    IdmFormValueDto valueDto = (IdmFormValueDto) mapper.map(savedEntity, null, context);
    // 
    Assert.assertEquals(value.getStringValue(), valueDto.getStringValue());
    BaseDto owner = valueDto.getEmbedded().get(FormValueService.PROPERTY_OWNER);
    Assert.assertNull(owner);
    // 
    valueDto = (IdmFormValueDto) mapper.map(savedEntity, new IdmFormValueDto(), null);
    // 
    Assert.assertEquals(value.getStringValue(), valueDto.getStringValue());
    owner = valueDto.getEmbedded().get(FormValueService.PROPERTY_OWNER);
    Assert.assertNull(owner);
    // 
    Assert.assertNull(mapper.map(null, null, null));
}
Also used : IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) IdmIdentityFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue) IdmFormValueFilter(eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) DefaultDtoMapper(eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)

Aggregations

DefaultDtoMapper (eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper)2 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)1 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)1 DtoMapper (eu.bcvsolutions.idm.core.api.rest.lookup.DtoMapper)1 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)1 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)1 IdmFormValueDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto)1 IdmFormValueFilter (eu.bcvsolutions.idm.core.eav.api.dto.filter.IdmFormValueFilter)1 IdmIdentityFormValue (eu.bcvsolutions.idm.core.model.entity.eav.IdmIdentityFormValue)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1 HibernateProxy (org.hibernate.proxy.HibernateProxy)1 Test (org.junit.Test)1