PageRenderTime 22ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/PersisterService/src/main/java/org/pasut/persister/MongoPersisterService.java

https://github.com/LaMaldicionDeMandos/PersisterService
Java | 108 lines | 96 code | 12 blank | 0 comment | 4 complexity | fae85fb890720833d197878abd9730c6 MD5 | raw file
  1. package org.pasut.persister;
  2. import java.util.List;
  3. import java.util.UUID;
  4. import org.pasut.persister.operators.Operator;
  5. import com.mongodb.BasicDBObjectBuilder;
  6. import com.mongodb.DB;
  7. import com.mongodb.DBCollection;
  8. import com.mongodb.DBObject;
  9. import com.mongodb.Mongo;
  10. public class MongoPersisterService implements PersisterService {
  11. private final Mongo mongo;
  12. private final DB db;
  13. private final GsonMongoMapper mapper = new GsonMongoMapper();
  14. private final ExampleFactory factory = new ExampleFactory();
  15. public MongoPersisterService(String dbName, String host, int port){
  16. try {
  17. mongo = new Mongo(host,port);
  18. db = mongo.getDB(dbName);
  19. } catch (Exception e) {
  20. throw new RuntimeException(e);
  21. }
  22. }
  23. public <T> T insert(T object) {
  24. DBCollection collection = db.getCollection(getCollectionName(object.getClass()));
  25. DBObject dbObject = mapper.toDbObject(object);
  26. if(dbObject.get("_id")==null){
  27. dbObject.put("_id", UUID.randomUUID().toString());
  28. collection.insert(dbObject);
  29. }
  30. else{
  31. collection.save(dbObject);
  32. }
  33. @SuppressWarnings("unchecked")
  34. T newObject = (T)mapper.fromDbObject(dbObject, object.getClass());
  35. return newObject;
  36. }
  37. public <T> List<T> find(Class<T> clazz) {
  38. DBCollection collection = db.getCollection(getCollectionName(clazz));
  39. List<DBObject> list = collection.find().toArray();
  40. return mapper.fromDbObject(list, clazz);
  41. }
  42. public <T> List<T> find(T example, String[] properties) {
  43. DBCollection collection = db.getCollection(getCollectionName(example.getClass()));
  44. List<DBObject> list = collection.find(factory.createExample(example,properties)).toArray();
  45. @SuppressWarnings("unchecked")
  46. List<T> result = (List<T>)mapper.fromDbObject(list, example.getClass());
  47. return result;
  48. }
  49. public <T> T update(T object) {
  50. DBCollection collection = db.getCollection(getCollectionName(object.getClass()));
  51. DBObject dbObject = mapper.toDbObject(object);
  52. collection.save(dbObject);
  53. @SuppressWarnings("unchecked")
  54. T newObject = (T)mapper.fromDbObject(dbObject, object.getClass());
  55. return newObject;
  56. }
  57. private <T> String getCollectionName(Class<T> clazz){
  58. Entity annotation = clazz.getAnnotation(Entity.class);
  59. if(annotation==null) throw new PersistenceException(clazz);
  60. return annotation.value();
  61. }
  62. public <T> long count(Class<T> clazz) {
  63. DBCollection collection = db.getCollection(getCollectionName(clazz));
  64. return collection.count();
  65. }
  66. public <T> long count(T example, String[] properties) {
  67. DBCollection collection = db.getCollection(getCollectionName(example.getClass()));
  68. return collection.count(factory.createExample(example,properties));
  69. }
  70. @SuppressWarnings("unchecked")
  71. public <T> T findOne(T example, String[] properties) {
  72. DBCollection collection = db.getCollection(getCollectionName(example.getClass()));
  73. DBObject dbObject = collection.findOne(factory.createExample(example,properties));
  74. if(dbObject==null) return null;
  75. return (T) mapper.fromDbObject(dbObject, example.getClass());
  76. }
  77. public <T> List<T> find(Class<T> clazz, Operator... operators) {
  78. DBCollection collection = db.getCollection(getCollectionName(clazz));
  79. BasicDBObjectBuilder builder = createObjectBuilder(operators);
  80. List<DBObject> list = collection.find(builder.get()).toArray();
  81. return mapper.fromDbObject(list, clazz);
  82. }
  83. public <T> T findOne(Class<T> clazz, Operator... operators) {
  84. DBCollection collection = db.getCollection(getCollectionName(clazz));
  85. BasicDBObjectBuilder builder = createObjectBuilder(operators);
  86. DBObject res = collection.findOne(builder.get());
  87. return mapper.fromDbObject(res, clazz);
  88. }
  89. private BasicDBObjectBuilder createObjectBuilder(Operator[] operators){
  90. BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
  91. for(Operator operator : operators){
  92. operator.perform(builder);
  93. }
  94. return builder;
  95. }
  96. }