Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. package DataClasses.communication;
  2.  
  3. import com.mongodb.*;
  4. import org.json.simple.JSONArray;
  5. import org.json.simple.JSONObject;
  6. import org.json.simple.parser.JSONParser;
  7. import org.json.simple.parser.ParseException;
  8.  
  9. import java.time.LocalDateTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Locale;
  14.  
  15. public class user_message_box {
  16. String _UserID;
  17. message inbox[];
  18. message outbox[];
  19.  
  20. public user_message_box(String _UserID){
  21. this._UserID = _UserID;
  22. }
  23.  
  24. public void insert_message(String text, String other_user_id){
  25. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM-dd-yy kk:mm:ss", Locale.ENGLISH);
  26. LocalDateTime now = LocalDateTime.now();
  27. String date = dtf.format(now);
  28.  
  29. Mongo mongo = new Mongo("localhost", 27017);
  30. DB db = mongo.getDB("ergasia");
  31. DBCollection collection = db.getCollection("messages");
  32.  
  33. // sender box
  34. String s_count;
  35. BasicDBObject Query = new BasicDBObject();
  36. Query.put("_UserID", this._UserID);
  37. DBCursor cursor = collection.find(Query);
  38. if (cursor.hasNext()) {
  39. System.out.println(this._UserID);
  40. System.out.println(cursor.next());
  41. s_count = (String)cursor.curr().get("count");
  42. }
  43. else{
  44. this.new_box(this._UserID);
  45. // s_count = (String)cursor.curr().get("count");
  46. s_count = "0";
  47. }
  48. message sender_message = new message(text, other_user_id, date, s_count);
  49. sender_message.insert_sended_message(this._UserID);
  50.  
  51. // receiver box
  52. String r_count;
  53. BasicDBObject Query1 = new BasicDBObject();
  54. Query1.put("_UserID", other_user_id);
  55. DBCursor cursor1 = collection.find(Query1);
  56. if (cursor1.hasNext()) {
  57. System.out.println(other_user_id);
  58. System.out.println(cursor1.next());
  59. r_count = (String)cursor1.curr().get("count");
  60. }
  61. else{
  62. this.new_box(other_user_id);
  63. // r_count = (String)cursor1.curr().get("count");
  64. r_count = "0";
  65. }
  66. message receiver_message = new message(text, this._UserID, date, r_count);
  67. receiver_message.insert_received_message(other_user_id);
  68.  
  69. // count++ for sender box
  70. BasicDBObject newDocument1 = new BasicDBObject();
  71. newDocument1.append("$set", new BasicDBObject().append("count", String.valueOf(Integer.parseInt(s_count)+1)));
  72. BasicDBObject searchQuery1 = new BasicDBObject().append("_UserID", this._UserID);
  73. collection.update(searchQuery1, newDocument1);
  74.  
  75. // count++ for receiver box
  76. BasicDBObject newDocument2 = new BasicDBObject();
  77. newDocument2.append("$set", new BasicDBObject().append("count", String.valueOf(Integer.parseInt(r_count)+1)));
  78. BasicDBObject searchQuery2 = new BasicDBObject().append("_UserID", other_user_id);
  79. collection.update(searchQuery2, newDocument2);
  80. }
  81.  
  82. public void new_box(String user_id){
  83. Mongo mongo = new Mongo("localhost", 27017);
  84. DB db = mongo.getDB("ergasia");
  85. DBCollection collection = db.getCollection("messages");
  86.  
  87. BasicDBObject document = new BasicDBObject();
  88. document.put("_UserID", user_id);
  89. document.put("count", "0");
  90. collection.insert(document);
  91. }
  92.  
  93. public String get_inbox(){
  94. Mongo mongo = new Mongo("localhost", 27017);
  95. DB db = mongo.getDB("ergasia");
  96. DBCollection collection = db.getCollection("messages");
  97.  
  98. JSONObject obj = new JSONObject();
  99. obj.put("UserID", this._UserID);
  100.  
  101. BasicDBObject Query = new BasicDBObject();
  102. Query.put("_UserID", this._UserID);
  103. DBCursor cursor = collection.find(Query);
  104. if (cursor.hasNext()) {
  105. cursor.next();
  106. obj.put("inbox",cursor.curr().get("inbox"));
  107. }
  108. return obj.toJSONString();
  109. }
  110.  
  111. public String get_outbox(){
  112. Mongo mongo = new Mongo("localhost", 27017);
  113. DB db = mongo.getDB("ergasia");
  114. DBCollection collection = db.getCollection("messages");
  115.  
  116. JSONObject obj = new JSONObject();
  117. obj.put("UserID", this._UserID);
  118.  
  119. BasicDBObject Query = new BasicDBObject();
  120. Query.put("_UserID", this._UserID);
  121. DBCursor cursor = collection.find(Query);
  122. if (cursor.hasNext()) {
  123. cursor.next();
  124. obj.put("outbox",cursor.curr().get("outbox"));
  125. }
  126. return obj.toJSONString();
  127. }
  128.  
  129. public String delete_message(String message_id){
  130. Mongo mongo = new Mongo("localhost", 27017);
  131. DB db = mongo.getDB("ergasia");
  132. DBCollection collection = db.getCollection("messages");
  133.  
  134. JSONObject temp_object = new JSONObject();
  135. temp_object.put("message_id", message_id);
  136. // inbox
  137. BasicDBObject newDocument = new BasicDBObject();
  138. newDocument.append("$pull", new BasicDBObject().append("inbox", temp_object));
  139. BasicDBObject searchQuery = new BasicDBObject().append("_UserID", this._UserID);
  140. collection.update(searchQuery, newDocument);
  141. // outbox
  142. BasicDBObject newDocument1 = new BasicDBObject();
  143. newDocument1.append("$pull", new BasicDBObject().append("outbox", temp_object));
  144. BasicDBObject searchQuery1 = new BasicDBObject().append("_UserID", this._UserID);
  145. collection.update(searchQuery1, newDocument1);
  146.  
  147. return "ok!";
  148. }
  149.  
  150. public int get_number_of_unread_messages() throws ParseException {
  151. Mongo mongo = new Mongo("localhost", 27017);
  152. DB db = mongo.getDB("ergasia");
  153. DBCollection collection = db.getCollection("messages");
  154.  
  155. BasicDBObject Query = new BasicDBObject();
  156. Query.put("_UserID", this._UserID);
  157. DBCursor cursor = collection.find(Query);
  158. int count=0;
  159. while (cursor.hasNext()) {
  160. System.out.println(cursor.next());
  161.  
  162. BasicDBList array = (BasicDBList) cursor.curr().get("inbox");
  163. String inbox_str = array.toString();
  164. JSONParser parser = new org.json.simple.parser.JSONParser();
  165. System.out.println(inbox_str);
  166. JSONArray inbox_array = (JSONArray) parser.parse(inbox_str);
  167.  
  168. for (Object o: inbox_array){
  169. JSONObject obj = (JSONObject) o;
  170.  
  171. String type = (String) obj.get("type");
  172. if(type.equals("unread")){
  173. count++;
  174. }
  175. }
  176. }
  177. return count;
  178. }
  179.  
  180. public String read_message(String message_id) throws ParseException {
  181. Mongo mongo = new Mongo("localhost", 27017);
  182. DB db = mongo.getDB("ergasia");
  183. DBCollection collection = db.getCollection("messages");
  184.  
  185. BasicDBObject Query = new BasicDBObject();
  186. Query.put("_UserID", this._UserID);
  187. DBCursor cursor = collection.find(Query);
  188.  
  189. while (cursor.hasNext()) {
  190. System.out.println(cursor.next());
  191.  
  192. BasicDBList array = (BasicDBList) cursor.curr().get("inbox");
  193. String inbox_str = array.toString();
  194. JSONParser parser = new org.json.simple.parser.JSONParser();
  195. System.out.println(inbox_str);
  196. JSONArray inbox_array = (JSONArray) parser.parse(inbox_str);
  197.  
  198. for (Object o: inbox_array){
  199. JSONObject obj = (JSONObject) o;
  200.  
  201. String msg_id = (String) obj.get("message_id");
  202. if(message_id.equals(msg_id)){
  203. System.out.println(obj.toJSONString());
  204.  
  205. JSONObject message_object = new JSONObject();
  206. message_object.put("Other_UserID", obj.get("Other_UserID"));
  207. message_object.put("text", obj.get("text"));
  208. message_object.put("time", obj.get("time"));
  209. message_object.put("message_id", obj.get("message_id"));
  210. message_object.put("type", "read");
  211.  
  212. // inbox
  213. JSONObject temp_object = new JSONObject();
  214. temp_object.put("message_id", message_id);
  215. BasicDBObject newDocument = new BasicDBObject();
  216. newDocument.append("$pull", new BasicDBObject().append("inbox", temp_object));
  217. BasicDBObject searchQuery = new BasicDBObject().append("_UserID", this._UserID);
  218. collection.update(searchQuery, newDocument);
  219.  
  220. BasicDBObject newDocument1 = new BasicDBObject();
  221. newDocument1.append("$push", new BasicDBObject().append("inbox", message_object));
  222. BasicDBObject searchQuery1 = new BasicDBObject().append("_UserID", _UserID);
  223. collection.update(searchQuery1, newDocument1);
  224.  
  225. return message_object.toJSONString();
  226. }
  227. }
  228. }
  229.  
  230. return null;
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement