⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernatedtogenerator.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.framework.bizLayer.hibernate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.proxy.HibernateProxyHelper;
import org.hibernate.type.ArrayType;
import org.hibernate.type.CollectionType;
import org.hibernate.type.EntityType;
import org.hibernate.type.ListType;
import org.hibernate.type.MapType;
import org.hibernate.type.SetType;
import org.hibernate.type.Type;

import com.cownew.PIS.framework.bizLayer.AbstractDTOGenerator;
import com.cownew.PIS.framework.common.db.Selectors;
import com.cownew.ctk.common.PropertyUtils;
import com.cownew.ctk.common.ExceptionUtils;

//以前是使用自己的元数据机制来实现的(见CommonDTOGenerator)
//现在我们改成了用hibernate的metadata API实现.
//这样的一个好处是这个DTOGenerator可以脱离我们的元数据框架使用
//以后如果改成其他ORM的话有可能需要还改用CommonDTOGenerator
public class HibernateDTOGenerator extends AbstractDTOGenerator
{
	private SessionFactory sessionFactory;

	public HibernateDTOGenerator(SessionFactory sessionFactory)
	{
		super();
		this.sessionFactory = sessionFactory;
	}

	public Object generateDTO(Object srcBean, Selectors selectors)
	{
		try
		{
			return copyValueObject(srcBean, selectors);
		} catch (InstantiationException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		} catch (IllegalAccessException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		}

	}

	private Object copyValueObject(Object srcVO, Selectors selectors)
			throws InstantiationException, IllegalAccessException
	{
		// 取得被代理之前的类型
		Class destClass = HibernateProxyHelper
				.getClassWithoutInitializingProxy(srcVO);

		Object newBean = destClass.newInstance();

		ClassMetadata entityMetaInfo = sessionFactory
				.getClassMetadata(destClass);

		String[] propertyNames = entityMetaInfo.getPropertyNames();
		for (int i = 0, n = propertyNames.length; i < n; i++)
		{
			String propertyName = propertyNames[i];
			Type propType = entityMetaInfo.getPropertyType(propertyName);

			// 如果不是实体类型也不是集合类型,即普通类型,则直接拷贝这些属性
			if (!(propType instanceof EntityType)
					&& !(propType instanceof CollectionType))
			{
				Object value = PropertyUtils.getProperty(srcVO, propertyName);
				PropertyUtils.setProperty(newBean, propertyName, value);
			} else if (selectors != null)
			{
				Selectors subSelector = selectors
						.generateSubSelectors(propertyName);
				// 如果是集合属性,并且用户在selectors中声明要求此属性
				// 则拷贝这些属性
				if (propType instanceof CollectionType
						&& selectors.contains(propertyName))
				{
					Object collValue = generateCollectionValue(srcVO,
							(CollectionType) propType, propertyName,
							subSelector);
					PropertyUtils.setProperty(newBean, propertyName, collValue);
				}
				// 如果是实体属性,并且用户在selectors中声明要求此属性
				// 则拷贝这些属性
				else if (selectors.contains(propertyName))
				{
					Object oldVO = PropertyUtils.getProperty(srcVO,
							propertyName);
					if (oldVO != null)
					{
						Object obj = copyValueObject(oldVO, subSelector);
						PropertyUtils.setProperty(newBean, propertyName, obj);
					}
				}
			}
		}

		// 由于主键字段没有在getPropertyNames中,所以要拷贝主键
		String idPropName = entityMetaInfo.getIdentifierPropertyName();
		Object value = PropertyUtils.getProperty(srcVO, idPropName);
		PropertyUtils.setProperty(newBean, idPropName, value);

		return newBean;
	}

	/**
	 * 生成srcVO的拷贝,关联属性由subSelector指定
	 */
	private Object generateCollectionValue(Object srcVO, CollectionType type,
			String propertyName, Selectors subSelector)
			throws InstantiationException, IllegalAccessException
	{
		if (type instanceof SetType)
		{

			Set valueSet = new HashSet();
			Set oldSet = (Set) PropertyUtils.getProperty(srcVO, propertyName);
			Iterator oldIt = oldSet.iterator();
			while (oldIt.hasNext())
			{
				Object oldValue = oldIt.next();
				if (oldValue != null)
				{
					Object obj = copyValueObject(oldValue, subSelector);
					valueSet.add(obj);
				}
			}
			return valueSet;
		} else if (type instanceof ArrayType)
		{
			Object[] oldArray = (Object[]) PropertyUtils.getProperty(srcVO,
					propertyName);

			Object[] valueArray = new Object[oldArray.length];

			for (int i = 0, n = oldArray.length; i < n; i++)
			{
				Object oldValue = oldArray[i];
				if (oldValue != null)
				{
					valueArray[i] = copyValueObject(oldValue, subSelector);
				}
			}
			return valueArray;
		} else if (type instanceof ListType)
		{
			List oldList = (List) PropertyUtils
					.getProperty(srcVO, propertyName);

			List valueList = new ArrayList(oldList.size());

			for (int i = 0, n = oldList.size(); i < n; i++)
			{
				Object oldValue = oldList.get(i);
				if (oldValue != null)
				{
					valueList.add(copyValueObject(oldValue, subSelector));
				}
			}
			return valueList;
		} else if (type instanceof MapType)
		{
			Map oldMap = (Map) PropertyUtils.getProperty(srcVO, propertyName);

			Map valueMap = new HashMap(oldMap.size());

			Set keySet = oldMap.keySet();
			Iterator keyIt = keySet.iterator();

			while (keyIt.hasNext())
			{
				Object key = keyIt.next();
				Object oldValue = oldMap.get(key);
				if (oldValue != null)
				{
					valueMap.put(key, copyValueObject(oldValue, subSelector));
				}
			}
			return valueMap;
		} else if (type instanceof SetType)
		{
			Set oldSet = (Set) PropertyUtils.getProperty(srcVO, propertyName);

			Set valueSet = new HashSet(oldSet.size());
			Iterator it = oldSet.iterator();

			while (it.hasNext())
			{
				Object oldValue = it.next();
				if (oldValue != null)
				{
					Object copyValue = copyValueObject(oldValue, subSelector);
					valueSet.add(copyValue);
				}
			}
			return valueSet;
		}
		throw new IllegalArgumentException("unsupport Type:"
				+ type.getClass().getName());
	}

	public Object generateDTO(Object srcBean)
	{
		try
		{
			return copyValueObject(srcBean);
		} catch (InstantiationException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		} catch (IllegalAccessException e)
		{
			throw ExceptionUtils.toRuntimeException(e);
		}

	}

	/**
	 * 得到srcVO的拷贝
	 */
	private Object copyValueObject(Object srcVO) throws InstantiationException,
			IllegalAccessException
	{
		Class destClass = HibernateProxyHelper
				.getClassWithoutInitializingProxy(srcVO);

		Object newBean = destClass.newInstance();

		ClassMetadata entityMetaInfo = sessionFactory
				.getClassMetadata(destClass);

		String[] propNames = entityMetaInfo.getPropertyNames();
		for (int i = 0, n = propNames.length; i < n; i++)
		{
			String propName = propNames[i];
			Type fType = entityMetaInfo.getPropertyType(propName);
			if (!(fType instanceof EntityType)
					&& !(fType instanceof CollectionType))
			{
				Object value = PropertyUtils.getProperty(srcVO, propName);
				PropertyUtils.setProperty(newBean, propName, value);
			}
		}

		String idPropName = entityMetaInfo.getIdentifierPropertyName();
		Object value = PropertyUtils.getProperty(srcVO, idPropName);
		PropertyUtils.setProperty(newBean, idPropName, value);

		return newBean;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -