com.frameworkset.platform.sanylog.common.BeanConvertUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.frameworkset.platform.sanylog.common.BeanConvertUtil.java

Source

/*
 * @(#)BeanConvertUtil.java
 * 
 * Copyright(c)2001-2012 SANY Heavy Industry Co.,Ltd
 * All right reserved.
 * 
 * ??????
 * ???????
 * This software is the confidential and proprietary information 
 * of SANY Heavy Industry Co, Ltd. You shall not disclose such 
 * Confidential Information and shall use it only in accordance 
 * with the terms of the license agreement you entered into with 
 * SANY Heavy Industry Co, Ltd.
 */
package com.frameworkset.platform.sanylog.common;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.frameworkset.util.ClassUtil;
import org.frameworkset.util.ClassUtil.ClassInfo;
import org.frameworkset.util.ClassUtil.PropertieDescription;

/**
 * BeanConvertUtil.java
 * 
 * Bean??.
 * 
 * @author          gw_liaozh
 * @company         SANY Heavy Industry Co, Ltd
 * @creation date   2012-7-30
 * @version         $Revision: 3 $
 */
public class BeanConvertUtil {

    /**
     * Map??Bean?.
     * 
     * @param clazz
     * @param dataMap ?Map
     * @param fieldMap Map??fieldName
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException 
     */
    public static <T> T convert(Class<T> clazz, Map<String, Object> dataMap, Map<String, String> fieldMap)
            throws InstantiationException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException {
        T obj = clazz.newInstance();
        ClassInfo classInfo = ClassUtil.getClassInfo(clazz);
        for (String mapKey : fieldMap.keySet()) {
            Object value = dataMap.get(mapKey);
            String fieldName = fieldMap.get(mapKey);
            if (value != null) {
                //classInfo.getDeclaredField(fieldName).set(obj, value);
                PropertieDescription pd = classInfo.getPropertyDescriptor(fieldName);
                //??
                Class<?> pclazz = pd.getPropertyType();

                if (Timestamp.class.isAssignableFrom(pclazz)) {
                    if (value instanceof Date) {
                        pd.setValue(obj, new Timestamp(((Date) value).getTime()));
                    }
                } else if (Date.class.isAssignableFrom(pclazz)) {
                    //String
                    if (value instanceof Date) {
                        pd.setValue(obj, value);
                    }
                } else {
                    Object targetValue = pclazz.getConstructor(String.class).newInstance(value.toString());
                    if (String.class.isAssignableFrom(pclazz) && value instanceof Number) {
                        targetValue = new DecimalFormat("0").format(value);
                    }
                    pd.setValue(obj, targetValue);
                }
                //PropertyUtils.setProperty(obj, fieldName, value);
            }
        }
        return obj;
    }

    /**
     * ??BeanMap?.
     * 
     * @param obj
     * @param fieldName
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws IllegalArgumentException 
     */
    public static Object getProperty(Object obj, String fieldName)
            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        if (obj == null) {
            throw new IllegalArgumentException("obj is null");
        }
        if (obj instanceof Map) {
            return ((Map<?, ?>) obj).get(fieldName);
        } else {
            ClassInfo classInfo = ClassUtil.getClassInfo(obj.getClass());
            return classInfo.getPropertyDescriptor(fieldName).getValue(obj);
        }
    }

    /**
     * List?Map?.
     * 
     * @param beanList
     * @param fieldName
     * @param clazz
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static <T> Map<String, T> map(List<T> beanList, String fieldName, Class<T> clazz)
            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Map<String, T> map = new HashMap<String, T>();
        ClassInfo classInfo = ClassUtil.getClassInfo(clazz);
        for (T obj : beanList) {
            Object value = classInfo.getPropertyDescriptor(fieldName).getValue(obj);
            if (value != null) {
                map.put(value.toString(), obj);
            }
        }
        return map;
    }

    /**
     * ???Map.
     * 
     * @param fieldDesc "?:user_id,??:user_name,:type_name"
     * @return
     */
    public static Map<String, String> map(String fieldDesc) {
        Map<String, String> map = new HashMap<String, String>();
        String[] fieldArr = StringUtils.split(fieldDesc, ',');
        for (String field : fieldArr) {
            String[] titleFieldArr = StringUtils.split(field, ':');
            map.put(titleFieldArr[0].trim(), titleFieldArr[1].trim());
        }
        return map;
    }

}