package com.goafanti.common.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class BeanUtilsExt extends BeanUtils {

    private BeanUtilsExt() {
    }

    static {
        ConvertUtils.register(new org.apache.commons.beanutils.converters.DateConverter(null), java.sql.Date.class);
        ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.util.Date.class);
        ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlTimestampConverter(null), java.sql.Timestamp.class);
        ConvertUtils.register(new org.apache.commons.beanutils.converters.BigDecimalConverter(null) , java.math.BigDecimal.class);
    }

    public static void copyProperties(Object target, Object source) throws InvocationTargetException,
            IllegalAccessException {
        BeanUtils.copyProperties(target, source);
        
    }

    public static Map<String, Object> pushMap(Object in) {
        HashMap<String, Object> map = new HashMap<>();
        Class<?> clazz = in.getClass();
        for (Field field :clazz.getDeclaredFields()){
            field.setAccessible(true);
            try {
                if(field.get(in)!=null)map.put(field.getName(),field.get(in));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return map;
    }
}
