package ru.bvn13.voidforum.utils; import org.hibernate.Hibernate; import org.hibernate.proxy.HibernateProxy; import org.modelmapper.ModelMapper; import org.modelmapper.convention.MatchingStrategies; import java.util.ArrayList; import java.util.List; /** * @author bvn13 */ public class DTOUtil { private static ModelMapper MAPPER = null; private static ModelMapper getMapper(){ if(MAPPER == null){ MAPPER = new ModelMapper(); MAPPER.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); } return MAPPER; } public static T map(S source, Class targetClass) { return getMapper().map(source, targetClass); } public static void mapTo(S source, T dist) { getMapper().map(source, dist); } public static List mapList(List source, Class targetClass) { List list = new ArrayList<>(); for (S s : source) { list.add(getMapper().map(s, targetClass)); } return list; } public static T initializeAndUnproxy(T entity) { if (entity == null) { throw new NullPointerException("Entity passed for initialization is null"); } Hibernate.initialize(entity); if (entity instanceof HibernateProxy) { entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation(); } return entity; } }