PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/net/bbm485/db/DBManager.java

https://bitbucket.org/tahaaltuntas/restfulasg
Java | 153 lines | 127 code | 20 blank | 6 comment | 3 complexity | 203a98cd2d6a81bc78ff58f8114fe128 MD5 | raw file
  1. package net.bbm485.db;
  2. import java.net.UnknownHostException;
  3. import java.util.ArrayList;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import com.google.gson.GsonBuilder;
  7. import com.mongodb.BasicDBObject;
  8. import com.mongodb.DB;
  9. import com.mongodb.DBCollection;
  10. import com.mongodb.DBCursor;
  11. import com.mongodb.DBObject;
  12. import com.mongodb.Mongo;
  13. import com.mongodb.util.JSON;
  14. import org.bson.types.ObjectId;
  15. import org.codehaus.jettison.json.JSONException;
  16. import org.codehaus.jettison.json.JSONObject;
  17. import net.bbm485.exceptions.UserNotFoundException;
  18. public class DBManager {
  19. private Mongo mongo;
  20. private DB db;
  21. private DBCollection collection;
  22. private String dbName;
  23. private String collectionName;
  24. /**
  25. * * Getters and Setters **
  26. */
  27. public String getDbName() {
  28. return dbName;
  29. }
  30. public String getCollectionName() {
  31. return collectionName;
  32. }
  33. private void setDbName(String dbName) {
  34. this.dbName = dbName;
  35. }
  36. private void setCollectionName(String collectionName) {
  37. this.collectionName = collectionName;
  38. }
  39. /**
  40. * * End of Getters and Setters **
  41. */
  42. public DBManager(String dbName, String collectionName) {
  43. setDbName(dbName);
  44. setCollectionName(collectionName);
  45. initializeDB();
  46. }
  47. private void initializeDB() {
  48. try {
  49. mongo = new Mongo();
  50. db = mongo.getDB(dbName);
  51. collection = db.getCollection(collectionName);
  52. }
  53. catch (UnknownHostException ex) {
  54. Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
  55. }
  56. catch (Exception ex) {
  57. Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
  58. }
  59. }
  60. public User getUser(String userId) throws UserNotFoundException {
  61. try {
  62. DBObject obj = new BasicDBObject("_id", new ObjectId(userId));
  63. DBObject userObj = collection.findOne(obj);
  64. User user = convertDBObject2User(userObj);
  65. if (user == null)
  66. throw new Exception();
  67. return user;
  68. }
  69. catch (Exception e) {
  70. JSONObject errorMsg = new JSONObject();
  71. try {
  72. errorMsg.put("fieldName", "userId").put("rejectedValue", userId);
  73. }
  74. catch (JSONException ex) {
  75. }
  76. throw new UserNotFoundException(errorMsg);
  77. }
  78. }
  79. public void createUser(User user) {
  80. DBObject dbObj = (DBObject) JSON.parse(user.toJson());
  81. collection.insert(dbObj);
  82. ObjectId id = (ObjectId) dbObj.get("_id");
  83. user.setId(id.toString());
  84. collection.update(dbObj, (DBObject) JSON.parse(user.toJson()));
  85. }
  86. public ArrayList<User> getUserList() {
  87. ArrayList<User> userList = new ArrayList<User>();
  88. DBCursor cursor = collection.find();
  89. while (cursor.hasNext())
  90. userList.add(convertDBObject2User(cursor.next()));
  91. return userList;
  92. }
  93. public void updateUser(String userId, JSONObject info) throws UserNotFoundException {
  94. try {
  95. DBObject obj = new BasicDBObject("_id", new ObjectId(userId));
  96. DBObject userObj = collection.findOne(obj);
  97. User foundUser = convertDBObject2User(userObj);
  98. foundUser.updateInfo(info);
  99. collection.update(userObj, convertUser2DBObject(foundUser));
  100. }
  101. catch (Exception e) {
  102. JSONObject errorMsg = new JSONObject();
  103. try {
  104. errorMsg.put("fieldName", "userId").put("rejectedValue", userId);
  105. }
  106. catch (JSONException ex) {
  107. }
  108. throw new UserNotFoundException(errorMsg);
  109. }
  110. }
  111. public void deleteUser(String userId) throws UserNotFoundException {
  112. try {
  113. DBObject obj = new BasicDBObject("_id", new ObjectId(userId));
  114. DBObject userObj = collection.findOne(obj);
  115. collection.remove(userObj);
  116. }
  117. catch (Exception e) {
  118. JSONObject errorMsg = new JSONObject();
  119. try {
  120. errorMsg.put("fieldName", "userId").put("rejectedValue", userId);
  121. }
  122. catch (JSONException ex) {
  123. }
  124. throw new UserNotFoundException(errorMsg);
  125. }
  126. }
  127. private DBObject convertUser2DBObject(User user) {
  128. return (DBObject) JSON.parse(user.toJson());
  129. }
  130. private User convertDBObject2User(DBObject obj) {
  131. return new GsonBuilder().serializeNulls().create().fromJson(JSON.serialize(obj), User.class);
  132. }
  133. }