
jadeutils.mongo.impl.MongoUtil Maven / Gradle / Ivy
The newest version!
package jadeutils.mongo.impl;
import jadeutils.mongo.Condition;
import jadeutils.mongo.MongoDocument;
import jadeutils.mongo.MongoField;
import jadeutils.mongo.MongoModel;
import java.lang.reflect.Field;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class MongoUtil {
/*
* get table Name from Object
*
* @param obj
* @return table name
*/
public static String getCollectionNameFromOjbect(
T obj) {
MongoDocument md = obj.getClass().getAnnotation(MongoDocument.class);
return null == md ? null : md.collectionName();
}
/*
* generate rec from object
*
* @param obj
* @return DBObject
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static DBObject genRecFromModel(T obj)
throws IllegalArgumentException, IllegalAccessException {
return genRecFromObject(obj);
}
public static DBObject genRecFromObject(Object obj)
throws IllegalArgumentException, IllegalAccessException {
DBObject rec = new BasicDBObject();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field f : fields) {
if (f.getAnnotation(MongoField.class) != null) {
f.setAccessible(true);
rec.put(f.getName(), parseObject(f.get(obj)));
}
}
return rec;
}
/*
*
* @param clazz
* @param rec
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static T genModelFromRec(
Class extends MongoModel> clazz, DBObject rec)
throws InstantiationException, IllegalAccessException {
Object obj = genObjectFromRec(clazz, rec);
if (null != obj) {
T result = (T) obj;
ObjectId mid = (ObjectId) rec.get("_id");
result.setMongoId(null == mid ? null : mid.toString());
return result;
} else {
return null;
}
}
public static Object genObjectFromRec(Class> clazz, DBObject rec)
throws InstantiationException, IllegalAccessException {
Object obj = null;
if (null != rec) {
obj = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
MongoField mf = f.getAnnotation(MongoField.class);
if (mf != null && rec.containsField(f.getName())) {
f.setAccessible(true);
Object value = parseField(mf.ElemType(),
rec.get(f.getName()));
f.set(obj, value);
}
}
}
return obj;
}
/*
*
* @param clazz
* @param value
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static Object parseField(Class> clazz, Object value)
throws IllegalArgumentException, IllegalAccessException,
InstantiationException {
Object result = null;
if (null == value) {
// do nothing
} else if (value instanceof Boolean || value instanceof Byte
|| value instanceof Short || value instanceof Integer
|| value instanceof Long || value instanceof Float
|| value instanceof Double || value instanceof Character
|| value instanceof String || value instanceof Date) //
{
result = value;
} else if (value instanceof List>) {
@SuppressWarnings("unchecked")
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy