mirror of https://github.com/bvn13/VoidForum.git
56 lines
1.5 KiB
Java
56 lines
1.5 KiB
Java
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 <mail4bvn@gmail.com>
|
|
*/
|
|
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 <S, T> T map(S source, Class<T> targetClass) {
|
|
return getMapper().map(source, targetClass);
|
|
}
|
|
|
|
public static <S, T> void mapTo(S source, T dist) {
|
|
getMapper().map(source, dist);
|
|
}
|
|
|
|
public static <S, T> List<T> mapList(List<S> source, Class<T> targetClass) {
|
|
List<T> list = new ArrayList<>();
|
|
for (S s : source) {
|
|
list.add(getMapper().map(s, targetClass));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static <T> 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;
|
|
}
|
|
}
|