Search in sources :

Example 1 with DtoMapper

use of eu.bcvsolutions.idm.core.api.rest.lookup.DtoMapper 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)

Aggregations

DefaultDtoMapper (eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoMapper)1 DtoMapper (eu.bcvsolutions.idm.core.api.rest.lookup.DtoMapper)1 HibernateProxy (org.hibernate.proxy.HibernateProxy)1