`
daniel.wuz
  • 浏览: 99621 次
  • 性别: Icon_minigender_1
  • 来自: 纽约
最近访客 更多访客>>
社区版块
存档分类

一个VO转换工具

HP 
阅读更多
贴在这里备查
package com.hp.mes.mat.pojo;

/**
 * ---------------------------------------------------------------------------------
 Confidential and Proprietary                                                                
 Copyright 2008 By                                                                                     
 SGAI & Hewlett-Packard Development Company, L.P. 	              
 All Rights Reserved                                                                                  

 Project Name : SGAI  MES                                                                                                                                       
 Class Name   : IValueObject.java    
 Package      : com.hp.mes.md.pojo                                                                   
 @version     $Id$                                                          
 @author Daniel
 @since  2008-8-6 
 */
public interface IValueObject {

	/**
	 * 返回value object属性域名称数组
	 * 
	 * @return
	 * @author Daniel
	 * @since  2008-8-6
	 */
	public String[] getAttributeName();
	
	/**
	 * 返回配置文件中定义的属性名称数组,与getAttributeName方法返回域一一对应
	 * 
	 * @return
	 * @author Daniel
	 * @since  2008-8-6
	 */
	public String[] getAttributeRef();
	
}


package com.hp.mes.mat.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import com.hp.common.util.StringUtil;
import com.hp.mes.mat.pojo.IValueObject;

/**
 * ---------------------------------------------------------------------------------
 Confidential and Proprietary                                                                
 Copyright 2008 By                                                                                     
 SGAI & Hewlett-Packard Development Company, L.P. 	              
 All Rights Reserved                                                                                  

 Project Name : SGAI  MES                                                                                                                                       
 Class Name   : VOConvertUtil.java    
 Package      : com.hp.mes.md.util                                                                   
 @version     $Id$                                                          
 @author Daniel
 @since  2008-8-7 
 */
public class VOConvertUtil {

	private Class valueObjectClass = null;

	private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

	public VOConvertUtil() {

	}

	public VOConvertUtil(Class valueObjectClass) {
		this.valueObjectClass = valueObjectClass;
	}

	/**
	 * 将符合properties格式的字串,转换为一个实现了IValueObject接口的对象
	 * 
	 * @param text 必须符合properties格式
	 * @return IValueObject 对象
	 * @throws Exception 可能抛出的异常
	 * @author Daniel
	 * @since  2008-8-13
	 */
	public IValueObject parse2ValueOject(String text) throws Exception {
		Properties prop = new Properties();
		ByteArrayInputStream byteStream = new ByteArrayInputStream(text
				.getBytes());
		try {
			prop.load(byteStream);
		} catch (IOException e) {
			e.printStackTrace();
		}

		IValueObject valueObject = (IValueObject) valueObjectClass
				.newInstance();

		// 获得IValueObject的所有属性名称
		String[] attrNames = valueObject.getAttributeName();

		String[] attrRefs = valueObject.getAttributeRef();

		for (int i = 0; i < attrNames.length; i++) {
			// 获得属性名
			String attrName = attrNames[i];

			// 获得对应xml文件中的键值
			String refKey = attrRefs[i];
			// 从properties中获得对应的值
			String refValue = prop.getProperty(refKey);
			// 为避免properties中汉字出现乱码,进行编码转换
			refValue = new String(refValue.getBytes("ISO8859-1"), "GBK");

			// 根据名称找属性类型
			Field field = valueObjectClass.getDeclaredField(attrName);
			Class fieldType = field.getType();
			// 将String类型的值转换成相应类型的值
			Object attrValue = castToAttributeValue(fieldType, refValue);

			// 取值,调用set方法赋值
			String methodName = getSetterMethod(attrName);
			Method setterMethod = valueObjectClass.getDeclaredMethod(
					methodName, fieldType);
			setterMethod.invoke(valueObject, attrValue);
		}

		return valueObject;
	}

	/**
	 * 获得setter方法名称
	 * 
	 * @param attrName
	 * @return
	 * @author Daniel
	 * @since  2008-8-7 
	 */
	private String getSetterMethod(String attrName) {
		String methodName = "set" + attrName.substring(0, 1).toUpperCase()
				+ attrName.substring(1);
		return methodName;
	}

	/**
	 * 获得getter方法名称
	 * 
	 * @param attrName
	 * @return
	 * @author Daniel
	 * @since  2008-8-7
	 */
	private String getGetterMethod(String attrName) {
		String methodName = "get" + attrName.substring(0, 1).toUpperCase()
				+ attrName.substring(1);
		return methodName;
	}

	private Object castToAttributeValue(Class fieldType, String refValue)
			throws Exception {
		Object value = null;
		if (StringUtil.isBlank(refValue)) {
			value = null;
		} else if (fieldType == String.class) {
			value = refValue;
		} else if (fieldType == Integer.class) {
			value = Integer.valueOf(refValue);
		} else if (fieldType == Long.class) {
			value = Long.valueOf(refValue);
		} else if (fieldType == Double.class) {
			value = Double.valueOf(refValue);
		} else if (fieldType == BigDecimal.class) {
			value = new BigDecimal(refValue);
		} else if (fieldType == char.class) {
			value = refValue.toCharArray()[0];
		} else if (fieldType == short.class) {
			value = Short.parseShort(refValue);
		} else if (fieldType == int.class) {
			value = Integer.parseInt(refValue);
		} else if (fieldType == double.class) {
			value = Double.valueOf(refValue);
		} else if (fieldType == Date.class) {
			value = dateFormatter.parse(refValue);
		}
		return value;
	}

	/**
	 * 将一个IValueObject的对象转换为对应的实体对象
	 * 
	 * @param valueObject
	 * @return
	 * @author Daniel
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @since  2008-8-7
	 */
	public Object parse2Entity(IValueObject valueObject) throws Exception {

		Object entity = valueObjectClass.newInstance();
		// 获得IValueObject中的所有属性名
		String[] attrNames = valueObject.getAttributeName();
		for (int i = 0; i < attrNames.length; i++) {
			String attrName = attrNames[i];
			// 调用ValueObject的getter方法从ValueObject从取值
			String getterName = getGetterMethod(attrName);
			Method getterMethod = valueObject.getClass().getDeclaredMethod(
					getterName);
			Object attrValue = getterMethod.invoke(valueObject);

			// 调用entity的set方法给Entity赋值
			String setterName = getSetterMethod(attrName);
			Method setterMethod = valueObjectClass.getDeclaredMethod(
					setterName, attrValue.getClass());
			setterMethod.invoke(entity, attrValue);
		}
		return entity;
	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics