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);
}
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));
}
Aggregations