All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.beanmapper.spring.converter.IdToEntityBeanConverter Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
package io.beanmapper.spring.converter;

import java.io.Serializable;

import io.beanmapper.BeanMapper;
import io.beanmapper.core.BeanFieldMatch;
import io.beanmapper.core.converter.BeanConverter;

import org.springframework.context.ApplicationContext;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.support.Repositories;

public class IdToEntityBeanConverter implements BeanConverter {
    
    private final Repositories repositories;
    
    public IdToEntityBeanConverter(ApplicationContext applicationContext) {
        this.repositories = new Repositories(applicationContext);
    }
    
    @Override
    public Object convert(BeanMapper beanMapper, Object source, Class targetClass, BeanFieldMatch beanFieldMatch) {
        if (source == null) {
            return null;
        }

        CrudRepository repository = (CrudRepository) repositories.getRepositoryFor(targetClass);
        return repository.findOne((Serializable) source);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean match(Class sourceClass, Class targetClass) {
        if (targetClass.isAssignableFrom(sourceClass)) {
            return false;
        }
        if (!repositories.hasRepositoryFor(targetClass)) {
            return false;
        }
        boolean match = false;
        EntityInformation information = repositories.getEntityInformationFor(targetClass);
        if (information != null) {
            match = sourceClass.equals(information.getIdType());
        }
        return match;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy