PageRenderTime 73ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/lydia/employeedetail/mongo/MongoService.java

https://bitbucket.org/Lydia_Andrews/employeedetails
Java | 105 lines | 82 code | 10 blank | 13 comment | 0 complexity | dc83b3c5fe891ac20ede4b53195f1146 MD5 | raw file
  1. package com.lydia.employeedetail.mongo;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.bson.Document;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.data.mongodb.MongoDbFactory;
  11. import org.springframework.data.mongodb.core.MongoOperations;
  12. import org.springframework.data.mongodb.core.MongoTemplate;
  13. import org.springframework.stereotype.Component;
  14. import com.fasterxml.jackson.databind.ObjectMapper;
  15. import com.google.gson.Gson;
  16. import com.lydia.employeedetail.domain.EmployeeDetails;
  17. import com.lydia.employeedetail.domain.EmployeeInfo;
  18. import com.mongodb.BasicDBObject;
  19. import com.mongodb.DB;
  20. import com.mongodb.DBCollection;
  21. import com.mongodb.DBObject;
  22. import com.mongodb.client.MongoCollection;
  23. import com.mongodb.client.MongoDatabase;
  24. @Component
  25. public class MongoService {
  26. @Autowired
  27. MongoConfiguration mongoConfiguration;
  28. /*
  29. * Function to bulk load data into mongo db
  30. * @param List of Employee details
  31. * @param collectionName
  32. * @return StatusCode
  33. * @throws exception
  34. *
  35. */
  36. private final MongoDbFactory mongo;
  37. @Autowired
  38. public MongoService(MongoDbFactory mongo) {
  39. this.mongo = mongo;
  40. }
  41. public Boolean bulkInsert(List<EmployeeDetails> emList, String collectionName) throws Exception {
  42. MongoDatabase db = this.mongo.getDb("test");
  43. try {
  44. MongoCollection<Document> col = db.getCollection("employee");
  45. ObjectMapper oMapper = new ObjectMapper();
  46. List<Document> liDocuments = new ArrayList<>();
  47. emList.forEach(em->{
  48. liDocuments.add(new Document(oMapper.convertValue(em, HashMap.class)));
  49. });
  50. /*for(EmployeeDetails emp : emList) {
  51. Map<String, Object> map = oMapper.convertValue(emp, Map.class);
  52. Document document = new Document(map);
  53. liDocuments.add(document);
  54. }*/
  55. col.insertMany(liDocuments);
  56. } catch (Exception e) {
  57. throw e;
  58. }
  59. return true;
  60. }
  61. public Boolean insertData(EmployeeDetails emList, String collectionName) throws Exception {
  62. List<DBObject> array = new ArrayList<DBObject>();
  63. MongoDatabase db = this.mongo.getDb("test");
  64. try {
  65. MongoCollection<Document> col = db.getCollection("employee");
  66. ObjectMapper oMapper = new ObjectMapper();
  67. Map<String, Object> map = oMapper.convertValue(emList, Map.class);
  68. Document document = new Document(map);
  69. col.insertOne(document);
  70. } catch (Exception e) {
  71. throw e;
  72. }
  73. return true;
  74. }
  75. public List<EmployeeDetails> getDetails(){
  76. MongoDatabase db = this.mongo.getDb("test");
  77. MongoCollection<Document> col = db.getCollection("employee");
  78. List<EmployeeDetails> liEmployeeDetails = new ArrayList<>();
  79. List<Document> liDocuments = col.find().into(new ArrayList<Document>());
  80. liDocuments.forEach(document->{
  81. liEmployeeDetails.add( new Gson().fromJson(document.toJson(), EmployeeDetails.class));
  82. });
  83. return liEmployeeDetails;
  84. }
  85. public List<EmployeeInfo> getInfo(){
  86. MongoDatabase db = this.mongo.getDb("test");
  87. MongoCollection<Document> col = db.getCollection("employee");
  88. List<EmployeeInfo> liEmployeeDetails = new ArrayList<>();
  89. List<Document> liDocuments = col.find().into(new ArrayList<Document>());
  90. liDocuments.forEach(document->{
  91. liEmployeeDetails.add( new Gson().fromJson(document.toJson(), EmployeeInfo.class));
  92. });
  93. return liEmployeeDetails;
  94. }
  95. }