package com.dcit.common.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

/**
* <p>Title: BeanCopyUtil</p>
* <p>Description: 属性拷贝工具类</p>
* <p>Company: SJZ DC-IT co. LTD</p> 
* @author ZZZ
* @date 2018年9月1日 下午5:32:36
 */
public class BeanCopyUtil {
	
	/**
	 * 属性拷贝时忽略null和指定的字段
	 * @param source 源bean
	 * @param ignoreProperties 忽略的字段
	 * @return
	 */
	public static String[] getNullOrIgnorePropertyNames (Object source,String... ignoreProperties) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
        
        List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); 

        Set<String> emptyNames =  new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            //值为null的字段
            if (srcValue == null || srcValue.equals("")){
            	emptyNames.add(pd.getName());
            }
            //指定忽略的字段
            if(ignoreList != null && ignoreList.contains(pd.getName())){
            	if(!emptyNames.contains(pd.getName())){
            		emptyNames.add(pd.getName());
            	}
            }
        }
        String[] result =  new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

}
