123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package com.nuliji.tools;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.nuliji.tools.annotation.Dto;
- import com.nuliji.tools.exception.SystemException;
- import org.modelmapper.AbstractConverter;
- import org.modelmapper.Converter;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.modelmapper.ModelMapper;
- import org.modelmapper.config.Configuration;
- import java.lang.reflect.Field;
- import java.lang.reflect.Type;
- import java.util.*;
- import static org.modelmapper.convention.MatchingStrategies.STRICT;
- /**
- * 使用ModelMapper转换Dto和Entity
- */
- public class Transform {
- private static final ModelMapper modelMapper = new ModelMapper();
- private static final Logger logger = LoggerFactory.getLogger(Transform.class);
- static {
- // modelMapper.getConfiguration().setFieldMatchingEnabled(true);
- modelMapper.getConfiguration().setMatchingStrategy(STRICT);
- modelMapper.getConfiguration().setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);
- Converter<Collection<Long>, String> longToString = new AbstractConverter<Collection<Long>, String>() {
- protected String convert(Collection<Long> source) {
- return Tools.joinIds(source);
- }
- };
- modelMapper.addConverter(longToString);
- Converter<String, Collection<Long>> stringToLong = new AbstractConverter<String, Collection<Long>>() {
- protected Collection<Long> convert(String source) {
- return Tools.splitIds(source);
- }
- };
- modelMapper.addConverter(stringToLong);
- Converter<Date, Long> dateToLong = new AbstractConverter<Date, Long>() {
- protected Long convert(Date source) {
- return source.getTime() / 1000;
- }
- };
- modelMapper.addConverter(dateToLong);
- Converter<Long, Date> longToDate = new AbstractConverter<Long, Date>() {
- protected Date convert(Long source) {
- return new Date(source * 1000);
- }
- };
- modelMapper.addConverter(longToDate);
- Converter<JSONObject, JSONObject> jsonObjectConverter = new AbstractConverter<JSONObject, JSONObject>() {
- protected JSONObject convert(JSONObject source) {
- if (source == null) return null;
- logger.info(source.toJSONString());
- return JSONObject.parseObject(source.toJSONString());
- }
- };
- modelMapper.addConverter(jsonObjectConverter);
- Converter<String, JSONObject> stringJSONObjectConverter = new AbstractConverter<String, JSONObject>() {
- protected JSONObject convert(String source) {
- return JSONObject.parseObject(source);
- }
- };
- modelMapper.addConverter(stringJSONObjectConverter);
- Converter<Object, JSONObject> objectJSONObjectConverter = new AbstractConverter<Object, JSONObject>() {
- protected JSONObject convert(Object source) {
- return JSONObject.parseObject(JSONObject.toJSONString(source));
- }
- };
- modelMapper.addConverter(objectJSONObjectConverter);
- Converter<JSONArray, JSONArray> jsonArrayConverter = new AbstractConverter<JSONArray, JSONArray>() {
- protected JSONArray convert(JSONArray source) {
- if (source == null) return null;
- logger.info(source.toJSONString());
- return JSONObject.parseArray(source.toJSONString());
- }
- };
- modelMapper.addConverter(jsonArrayConverter);
- Converter<Object, JSONArray> objectJSONArrayConverter = new AbstractConverter<Object, JSONArray>() {
- protected JSONArray convert(Object source) {
- return JSONObject.parseArray(JSONObject.toJSONString(source));
- }
- };
- modelMapper.addConverter(objectJSONArrayConverter);
- Converter<String, JSONArray> stringJSONArrayConverter = new AbstractConverter<String, JSONArray>() {
- protected JSONArray convert(String source) {
- return JSONObject.parseArray(source);
- }
- };
- modelMapper.addConverter(stringJSONArrayConverter);
- }
- public static <S, T> T dtoToEntity(S source) {
- Class sourceClass = source.getClass();
- if(!sourceClass.isAnnotationPresent(Dto.class)){
- throw new SystemException("Class " + sourceClass.getName() + " need dto annotation");
- }
- Dto dto = (Dto) sourceClass.getAnnotation(Dto.class);
- return (T) transfer(source, dto.entity());
- }
- public static <S, T> T convert(S source, Class<?> clazz) {
- return (T) transfer(source, clazz);
- }
- public static <S, T> List<T> convert(Collection<S> source, Class<T> clazz) {
- List<T> al = new ArrayList<>();
- for(S s:source){
- al.add(Transform.convert(s, clazz));
- }
- return al;
- }
- public static <T> void replace(List<T> targetList, Collection<T> objectList, Class<?> objectClass, String indexProp) throws RuntimeException {
- if(objectList.size() == 0 || targetList.size() == 0) return ;
- Map<Object, T> objectMap = new HashMap<>(objectList.size());
- Field objectField = getField(objectClass, indexProp);
- try {
- for(T o: objectList){
- objectMap.put(objectField.get(o), o);
- }
- targetList.replaceAll(e-> {
- try {
- return objectMap.get(objectField.get(e));
- } catch (IllegalAccessException e1) {
- e1.printStackTrace();
- return e;
- }
- });
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("ObjectClass:"+objectClass.getName()+" access "+indexProp+" exception");
- }
- }
- public static <T, S> void combine(List<T> targetList, Collection<S> objectList, Class<?> targetClass, String keyProp, String targetProp, Class<?> objectClass, String indexProp) throws RuntimeException {
- if(objectList.size() == 0 || targetList.size() == 0) return ;
- Map<Object, S> objectMap = new HashMap<>(objectList.size());
- Field objectField = getField(objectClass, indexProp);
- try {
- for(S o: objectList){
- objectMap.put(objectField.get(o), o);
- }
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("ObjectClass:"+objectClass.getName()+" access "+indexProp+" exception");
- }
- combine(targetList, objectMap, targetClass, keyProp, targetProp);
- }
- public static <T, S, R> void combine(List<T> targetList, Collection<S> objectList, Class<?> targetClass, String keyProp, String targetProp, Class<?> objectClass, String indexProp, Class<?> transform) throws RuntimeException {
- if(objectList.size() == 0 || targetList.size() == 0) return ;
- Map<Object, R> objectMap = new HashMap<>(objectList.size());
- Field objectField = getField(objectClass, indexProp);
- try {
- for(S o: objectList){
- objectMap.put(objectField.get(o), convert(o, transform));
- }
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("ObjectClass:"+objectClass.getName()+" access "+indexProp+" exception");
- }
- combine(targetList, objectMap, targetClass, keyProp, targetProp);
- }
- public static <T, S> void combine(List<T> targetList, Map<Object, S> objectMap, Class<?> targetClass, String keyProp, String targetProp) throws RuntimeException {
- if(objectMap.size() == 0 || targetList.size() == 0) return ;
- Field targetField = getField(targetClass, targetProp);
- Field targetKeyField = getField(targetClass, keyProp);
- try {
- for(T t: targetList){
- S o = objectMap.get(targetKeyField.get(t));
- targetField.set(t, o);
- }
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("TargetClass:"+targetClass.getName()+" access "+keyProp+" exception");
- }
- }
- public static <T, S> void combine(List<T> targetList, Map<Object, Collection<S>> objectMap, Class<?> targetClass, String keyProp, String targetProp, Class<?> transform) throws RuntimeException {
- if(objectMap.size() == 0 || targetList.size() == 0) return ;
- Field targetField = getField(targetClass, targetProp);
- Field targetKeyField = getField(targetClass, keyProp);
- try {
- for(T t: targetList){
- Collection<S> o = objectMap.get(targetKeyField.get(t));
- if (transform == null){
- targetField.set(t, o);
- }else{
- targetField.set(t, convert(o, transform));
- }
- }
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("TargetClass:"+targetClass.getName()+" access "+keyProp+" exception");
- }
- }
- private static Object transfer(Object source, Class<?> clazz) {
- if (source == null) {
- return null;
- }
- return modelMapper.map(source, clazz);
- }
- private static Object transfer(Object source, Type type){
- if (source == null) {
- return null;
- }
- logger.info("{}", type);
- return modelMapper.map(source, type);
- }
- public static Field getField(Class<?> clazz, String prop){
- Field field;
- try {
- field = clazz.getDeclaredField(prop);
- field.setAccessible(true);
- } catch (NoSuchFieldException e) {
- // 关系继承类
- try {
- field = clazz.getSuperclass().getDeclaredField(prop);
- field.setAccessible(true);
- } catch (NoSuchFieldException e1) {
- throw new RuntimeException("TargetClass:"+clazz.getName()+" no "+prop);
- }
- }
- return field;
- }
- public static <T> Collection getIds(Collection<T> list, Class<?> clazz, String idProp) throws RuntimeException {
- Set ids = new HashSet<>();
- if(list == null || list.size() == 0) return ids;
- Field field = Transform.getField(clazz, idProp);
- try {
- for(T item: list){
- ids.add(field.get(item));
- }
- return ids;
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("Class:"+clazz.getName()+" access "+idProp+" exception");
- }
- }
- public static <T> Map getMap(Collection<T> list, Class<?> clazz, String keyProp) throws RuntimeException{
- Map map = new HashMap<>();
- if(list == null || list.size() == 0) return map;
- Field keyField = Transform.getField(clazz, keyProp);
- try{
- for(T item: list){
- map.put(keyField.get(item), item);
- }
- return map;
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("Class:"+clazz.getName()+" access "+keyProp+" exception");
- }
- }
- public static <T> Map getMap(Collection<T> list, Class<?> clazz, String keyProp, String valueProp) throws RuntimeException{
- Map map = new HashMap<>();
- if(list == null || list.size() == 0) return map;
- Field keyField = Transform.getField(clazz, keyProp);
- Field valueField = Transform.getField(clazz, valueProp);
- try{
- for(T item: list){
- map.put(keyField.get(item), valueField.get(item));
- }
- return map;
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- throw new RuntimeException("Class:"+clazz.getName()+" access "+keyProp+","+valueProp+" exception");
- }
- }
- public static <T,S> Map<Object, List<T>> getMapList(List<S> list, List<T> targetList, Class<?> clazz, String keyProp, String targetProp, Class<?> targetClazz, String indexProp){
- return new HashMap<Object, List<T>>();
- }
- }
|