1. 程式人生 > >MongoDB和JavaBean物件的轉化工具類

MongoDB和JavaBean物件的轉化工具類

package com.paile.utils.beans;


import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;


import org.apache.commons.beanutils.BeanUtils;


import com.mongodb.BasicDBList;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
//http://littie1987.iteye.com/blog/2193123
public class DBObjectToJavaBean {


public static <T> List<T> cursorToList(DBCursor cursor, Class<T> clazz)
throws Exception {
List<T> list = new ArrayList<T>();
while (cursor.hasNext()) {
DBObject dbObj = cursor.next();
T t = propertySetter(dbObj, clazz);
list.add(t);
}
return list;
}


public static <T> T propertySetter(DBObject dbObj, Class<T> clazz)
throws Exception {
if (dbObj == null)
return null;
T t = clazz.newInstance();
recyleSetter(dbObj, t);
return t;
}


/***
* 遞迴所有屬性

* @param dbObj
* @param bean
* @throws Exception
*/
private static void recyleSetter(DBObject dbObj, Object bean) throws Exception {
Iterator<String> it = dbObj.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
Object value = dbObj.get(key);
recyleValueJutisy(key, value, bean);
}
}


@SuppressWarnings("rawtypes")
private static void recyleValueJutisy(String key, Object value, Object bean)
throws Exception {
if (value instanceof BasicDBList) {
BasicDBList dblist = (BasicDBList) value;
Iterator<Object> it = dblist.iterator();
Class<?> type = null;
try {
type = getPropertyType(bean, key);
List list = new ArrayList();
while (it.hasNext()) {
Object object = it.next();
if (object instanceof DBObject) {
DBObject dbItem = (DBObject) object;
Object o = type.newInstance();
recyleSetter(dbItem, o);
list.add(o);
} else if (object instanceof String) {
list.add(object);
}
}
BeanUtils.setProperty(bean, key, list);
} catch (NoSuchFieldException e) {
}


} else if (value instanceof DBObject) {
DBObject dbItem = (DBObject) value;
Class<?> type = getPropertyType(bean, key);
Class tclazz = Timestamp.class;
if (type == tclazz) {
// 時間型別
Object otime = dbItem.get("time");
if (otime != null) {
long time = Long.parseLong(String.valueOf(otime));
Timestamp st = new Timestamp(time);
BeanUtils.setProperty(bean, key, st);
}
} else {
Object o = type.newInstance();
recyleSetter(dbItem, o);
BeanUtils.setProperty(bean, key, o);
}
} else {
Class<?> clazz = bean.getClass();
Field field;
try {
field = clazz.getDeclaredField(key);
if (field != null) {
if (value != null) {
if ("".equals(value)) {
if (field.getType() == String.class)
BeanUtils.setProperty(bean, key, value);
} else {
BeanUtils.setProperty(bean, key, value);
}
}


}
} catch (NoSuchFieldException e) {
}


}
}


/**
* @param bean
* @param key
* @return
* @throws Exception
*/
private static Class getPropertyType(Object bean, String key) throws Exception {
Class<?> clazz = bean.getClass();
Field f = clazz.getDeclaredField(key);
Class t = f.getType();
if (t.getName().startsWith("java.lang")) {
return t;
}
if (t.isAssignableFrom(List.class) || t.isAssignableFrom(Set.class)
|| t.isAssignableFrom(Vector.class)) {
Type gt = f.getGenericType();
if (gt == null) {
return t;
}
if (gt instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) gt;
Class genericClazz = (Class) pt.getActualTypeArguments()[0];
if (genericClazz != null) {
return genericClazz;
}
}
}
return t;
}
}


  使用說明:
   例如:想把一個DBOBject物件轉換為UserBean:


UserBean user = DBObjectToJavaBean .propertySetter(dbObj,UserBean.class);  
//該方法支援多層Mongo物件的轉化