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

com.soento.core.util.BeanUtilBean Maven / Gradle / Ivy

package com.soento.core.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.expression.Resolver;
import org.apache.commons.collections.CollectionUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Map;

/**
 * @author soento
 */
@Slf4j
public class BeanUtilBean extends BeanUtilsBean {
    private ConvertUtilsBean convertUtilsBean;
    private final PropertyUtilBean propertyUtilBean;

    public BeanUtilBean() {
        DateConverter dateConverter = new DateConverter();
        ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
        convertUtilsBean.deregister(java.util.Date.class);
        convertUtilsBean.register(dateConverter, java.util.Date.class);
        this.convertUtilsBean = convertUtilsBean;
        this.propertyUtilBean = new PropertyUtilBean();
    }


    @Override
    public ConvertUtilsBean getConvertUtils() {
        return this.convertUtilsBean;
    }


    private static Class dynaPropertyType(DynaProperty dynaProperty, Object value) {
        if (!dynaProperty.isMapped()) {
            return dynaProperty.getType();
        }
        return value == null ? String.class : value.getClass();
    }


    @Override
    protected Object convert(Object value, Class type) {
        return getConvertUtils().convert(value, type);
    }


    @Override
    public void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
        copyProperties(dest, orig, true);
    }


    public void copyProperties(Object dest, Object orig, boolean isRecursive)
            throws IllegalAccessException, InvocationTargetException {
        if (dest == null) {
            throw new IllegalArgumentException("No destination bean specified");
        }

        if (orig == null) {
            throw new IllegalArgumentException("No origin bean specified");
        }

        if ((dest instanceof Map)) {
            copyPropertiesToMap((Map) dest, orig);
            return;
        }

        if ((orig instanceof DynaBean)) {
            DynaProperty[] origDescriptors = ((DynaBean) orig).getDynaClass().getDynaProperties();

            for (DynaProperty origDescriptor : origDescriptors) {
                String name = origDescriptor.getName();
                if ((getPropertyUtils().isReadable(orig, name)) && (getPropertyUtils().isWriteable(dest, name))) {
                    Object value = ((DynaBean) orig).get(name);
                    if (isRecursive) {
                        copyPropertyRecursive(dest, name, value);
                    } else {
                        copyProperty(dest, name, value);
                    }
                }
            }
        } else if ((orig instanceof Map)) {
            Map propMap = (Map) orig;
            for (Map.Entry entry : propMap.entrySet()) {
                String destName = getDestBeanKey(dest, (String) entry.getKey());
                if (getPropertyUtils().isWriteable(dest, destName)) {
                    if (isRecursive) {
                        copyPropertyRecursive(dest, destName, entry.getValue());
                    } else {
                        copyProperty(dest, destName, entry.getValue());
                    }
                }
            }
        } else {
            PropertyDescriptor[] origDescriptors = getPropertyUtils().getPropertyDescriptors(orig);

            for (PropertyDescriptor origDescriptor : origDescriptors) {
                String name = origDescriptor.getName();
                if (!"class".equals(name)) {

                    String destName = getDestBeanKey(dest, name);
                    if ((getPropertyUtils().isReadable(orig, name)) && (getPropertyUtils().isWriteable(dest, destName))) {
                        try {
                            Object value = getPropertyUtils().getSimpleProperty(orig, name);
                            if (isRecursive) {
                                copyPropertyRecursive(dest, destName, value);
                            } else {
                                copyProperty(dest, destName, value);
                            }
                        } catch (Exception e) {
                            log.trace(e.getMessage(), e);
                        }
                    }
                }
            }
        }
    }

    protected String getDestBeanKey(Object destBean, String name) {
        PropertyUtilBean.ResolverExt resolverExt = getPropertyUtils().getResolver();
        String cacheName = name;
        try {
            PropertyDescriptor propertyDescriptor = getPropertyUtils().getPropertyDescriptor(destBean, name);

            if (propertyDescriptor == null) {
                cacheName = resolverExt.getUnderlineProperty(cacheName);
                propertyDescriptor = getPropertyUtils().getPropertyDescriptor(destBean, cacheName);
                if (propertyDescriptor == null) {
                    cacheName = resolverExt.getHumpProperty(name);
                    propertyDescriptor = getPropertyUtils().getPropertyDescriptor(destBean, cacheName);
                    if (propertyDescriptor != null) {
                        return cacheName;
                    }
                } else {
                    return cacheName;
                }
            }
        } catch (Exception e) {
            log.trace(e.getMessage(), e);
        }

        return cacheName;
    }

    @Override
    public PropertyUtilBean getPropertyUtils() {
        return this.propertyUtilBean;
    }

    public void copyProperty(Object bean, String name, Object value, boolean isRecursive) throws InvocationTargetException, IllegalAccessException {
        try {
            Object target = bean;
            Resolver resolver = getPropertyUtils().getResolver();
            while (resolver.hasNested(name)) {
                target = getPropertyUtils().getProperty(target, resolver.next(name));
                name = resolver.remove(name);
            }

            String propName = resolver.getProperty(name);
            Class type = null;
            int index = resolver.getIndex(name);
            String key = resolver.getKey(name);
            if ((target instanceof DynaBean)) {
                org.apache.commons.beanutils.DynaClass dynaClass = ((DynaBean) target).getDynaClass();
                DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
                if (dynaProperty == null) {
                    return;
                }
                type = dynaPropertyType(dynaProperty, value);
            } else {
                PropertyDescriptor descriptor = null;
                descriptor = getPropertyUtils().getPropertyDescriptor(target, name);
                if (descriptor == null) {
                    return;
                }
                type = descriptor.getPropertyType();
                if (type == null) {
                    return;
                }
            }

            if (index >= 0) {
                value = convertForCopy(value, type.getComponentType());
                getPropertyUtils().setIndexedProperty(target, propName, index, value);
            } else if (key != null) {
                getPropertyUtils().setMappedProperty(target, propName, key, value);
            } else {
                value = convertForCopy(value, type);
                if ((value instanceof Collection)) {
                    if (isRecursive) {
                        value = convertOrinBeanListToTargetBeanList(target, propName, (Collection) value);
                        getPropertyUtils().setSimpleProperty(target, propName, value);
                    }
                } else {
                    getPropertyUtils().setSimpleProperty(target, propName, value);
                }
            }
        } catch (Exception e) {
            log.trace(e.getMessage(), e);
        }
    }

    @Override
    public void copyProperty(Object bean, String name, Object value)
            throws IllegalAccessException, InvocationTargetException {
        copyProperty(bean, name, value, false);
    }

    public void copyPropertyRecursive(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException {
        copyProperty(bean, name, value, true);
    }

    private Collection convertOrinBeanListToTargetBeanList(Object target, String propName, Collection value) throws InvocationTargetException, IllegalAccessException {
        if (CollectionUtils.isNotEmpty(value)) {
            Field field;
            try {
                field = target.getClass().getDeclaredField(propName);
            } catch (NoSuchFieldException e) {
                try {
                    field = target.getClass().getSuperclass().getDeclaredField(propName);
                } catch (NoSuchFieldException ne) {
                    return null;
                }
            }
            Object targetB = null;
            Collection result = new java.util.ArrayList();
            for (Object vu : value) {
                if ((vu instanceof Map)) {
                    targetB = new java.util.HashMap();
                    copyProperties(targetB, vu);
                } else {
                    try {
                        java.lang.reflect.Type genericType = field.getGenericType();
                        if (!(genericType instanceof Class)) {
                            targetB = ((Class) ((java.lang.reflect.ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]).newInstance();
                            copyProperties(targetB, vu);
                        } else {
                            return value;
                        }
                    } catch (Exception e) {
                        log.trace(e.getMessage(), e);
                    }
                }
                result.add(targetB);
            }
            return result;
        }
        return value;
    }


    public void copyPropertiesToMap(Map dest, Object orig) {
        if (dest == null) {
            throw new IllegalArgumentException("No destination bean specified");
        }

        if (orig == null) {
            throw new IllegalArgumentException("No origin bean specified");
        }


        if ((orig instanceof Map)) {
            dest.putAll((Map) orig);
            return;
        }

        PropertyDescriptor[] propertyDescriptors = org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors(orig);
        if ((propertyDescriptors != null) && (propertyDescriptors.length > 0)) {
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                if (!"class".equals(propertyDescriptor.getName())) {
                    try {
                        Object value = propertyDescriptor.getReadMethod().invoke(orig, null);
                        if ((value instanceof Collection)) {
                            Collection collection = (Collection) value;
                            Collection result = new java.util.ArrayList();
                            for (Object ori : collection) {
                                Map destMap = new java.util.HashMap();
                                copyPropertiesToMap(destMap, ori);
                                result.add(destMap);
                            }
                            dest.put(propertyDescriptor.getName(), result);
                        } else if (!(value instanceof java.lang.reflect.Array)) {

                            dest.put(propertyDescriptor.getName(), value);
                        }
                    } catch (Exception e) {
                        log.trace(e.getMessage(), e);
                    }
                }
            }
        }
    }


    private Object convertForCopy(Object value, Class type) {
        return value != null ? convert(value, type) : value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy