Posted By

alexedy on 01/13/13


Tagged

object Singleton conversion mongodb


Versions (?)

MOngoDBUtil


 / Published in: Java
 

A Singleton class that serialize and deserialize an object to a DBObject.

  1. package ro.shiken.db;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.lang.reflect.Array;
  6. import java.lang.reflect.Field;
  7. import java.net.UnknownHostException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Properties;
  13. import java.util.Set;
  14.  
  15. import org.bson.types.ObjectId;
  16.  
  17. import com.mongodb.BasicDBList;
  18. import com.mongodb.BasicDBObject;
  19. import com.mongodb.DB;
  20. import com.mongodb.DBCollection;
  21. import com.mongodb.DBCursor;
  22. import com.mongodb.DBObject;
  23. import com.mongodb.Mongo;
  24. import com.mongodb.MongoException;
  25.  
  26. public class MongoUtils {
  27.  
  28. private Mongo mongo = null;
  29. private static MongoUtils instance = null;
  30. private DB db = null;
  31.  
  32. private final static String DB_NAME = "db_name";
  33. private final static String DB_HOST = "host";
  34. private final static String DB_PORT = "port";
  35.  
  36. private final static String ID = "_id";
  37.  
  38. private Properties properties;
  39.  
  40. private MongoUtils() {
  41. InputStream inputStream = this.getClass().getClassLoader()
  42. .getResourceAsStream("mongo.properties");
  43. properties = new Properties();
  44. try {
  45. properties.load(inputStream);
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. public static MongoUtils getMe() {
  52. if (instance == null) {
  53. instance = new MongoUtils();
  54. }
  55. return instance;
  56. }
  57.  
  58. private DB getMongo() {
  59. if (mongo == null) {
  60. try {
  61. mongo = new Mongo(properties.getProperty(DB_HOST),
  62. Integer.parseInt(properties.getProperty(DB_PORT)));
  63. } catch (UnknownHostException e) {
  64. e.printStackTrace();
  65. } catch (MongoException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. return mongo.getDB(properties.getProperty(DB_NAME));
  70. }
  71.  
  72. public DBCollection getCollection(Collection coll) {
  73. if (getMe().db == null) {
  74. getMe().db = getMongo();
  75. }
  76. return getMe().db.getCollection(coll.name());
  77. }
  78.  
  79. public static enum Collection {
  80. USER, ITEM, PUPIL, RESPONSE, SESSION, TCH, TEST, PUPIL_SESSION
  81. }
  82.  
  83. public static ObjectId save(Collection c, Object obj) {
  84. DBObject dbObj = getObject(obj);
  85. getMe().getCollection(c).save(dbObj);
  86. return (ObjectId) dbObj.get(ID);
  87. }
  88.  
  89. public static <T> T getInstace(Class<T> clazz, DBObject object) {
  90. T t = null;
  91. if (object != null) {
  92. try {
  93. t = clazz.newInstance();
  94. List<Field> fields = Arrays.asList(clazz.getDeclaredFields());
  95. Set<String> props = object.keySet();
  96. for (Field f : fields) {
  97. String fn = f.getName();
  98. if (props.contains(fn)) {
  99. f.setAccessible(true);
  100. for (String n : props) {
  101. if (n.equals(fn)) {
  102. Object oo = object.get(n);
  103. if (oo instanceof BasicDBList) {
  104. if (f.getType().isArray()) {
  105. f.set(t,
  106. getArrayField(f.getType()
  107. .getComponentType(),
  108. (BasicDBList) oo));
  109. } else {
  110. f.set(t, getListField((BasicDBList) oo));
  111. }
  112. } else {
  113. f.set(t, oo);
  114. }
  115. props.remove(n);
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. e.printStackTrace();
  122. } catch (SecurityException e) {
  123. e.printStackTrace();
  124. } catch (InstantiationException e) {
  125. e.printStackTrace();
  126. } catch (IllegalAccessException e) {
  127. e.printStackTrace();
  128. }
  129. return t;
  130. }
  131. return null;
  132. }
  133.  
  134. @SuppressWarnings({ "rawtypes", "unchecked" })
  135. private static <T> T[] getArrayField(Class<T> clazz, BasicDBList obj) {
  136. ArrayList list = new ArrayList();
  137. Iterator<Object> it = obj.iterator();
  138. while (it.hasNext()) {
  139. list.add(it.next());
  140. }
  141. T[] result = (T[]) Array.newInstance(clazz, list.size());
  142. result = (T[]) list.toArray(result);
  143. return result;
  144.  
  145. }
  146.  
  147. @SuppressWarnings({ "rawtypes", "unchecked" })
  148. private static ArrayList getListField(BasicDBList obj) {
  149. ArrayList list = new ArrayList();
  150.  
  151. Iterator<Object> it = obj.iterator();
  152. while (it.hasNext()) {
  153. list.add(it.next());
  154. }
  155. return list;
  156. }
  157.  
  158. public static DBObject getObject(Object obj) {
  159. DBObject bo = new BasicDBObject();
  160. try {
  161. Field[] fields = obj.getClass().getDeclaredFields();
  162. for (Field f : fields) {
  163. if (!f.getName().equals("serialVersionUID")) {
  164. f.setAccessible(true);
  165. Object fo = f.get(obj);
  166. if (fo != null) {
  167. bo.put(f.getName(), fo);
  168. }
  169. }
  170. }
  171. e.printStackTrace();
  172. } catch (IllegalAccessException e) {
  173. e.printStackTrace();
  174. }
  175. return bo;
  176. }
  177.  
  178. public static DBCursor find(Collection coll, DBObject ref) {
  179. return getMe().getCollection(coll).find(ref);
  180. }
  181.  
  182. public static DBCursor findByKeyValue(Collection coll, String key,
  183. Object value) {
  184. return find(coll, new BasicDBObject(key, value));
  185. }
  186.  
  187. public static DBObject findOne(Collection coll, DBObject ref) {
  188. return getMe().getCollection(coll).findOne(ref);
  189. }
  190.  
  191. public static DBObject findOneById(Collection coll, ObjectId id) {
  192. return findOne(coll, new BasicDBObject(ID, id));
  193. }
  194. }

Report this snippet  

You need to login to post a comment.