/griffin-core/src/main/java/com/ebay/oss/griffin/repo/BaseRepo.java

https://github.com/eBay/griffin · Java · 95 lines · 61 code · 20 blank · 14 comment · 2 complexity · 168aa7d210916acd859c009861e2cbb3 MD5 · raw file

  1. /*
  2. Copyright (c) 2016 eBay Software Foundation.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.ebay.oss.griffin.repo;
  14. import java.io.IOException;
  15. import java.util.LinkedList;
  16. import java.util.List;
  17. import java.util.Properties;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import com.ebay.oss.griffin.common.Pair;
  21. import com.google.gson.Gson;
  22. import com.mongodb.BasicDBObject;
  23. import com.mongodb.DB;
  24. import com.mongodb.DBCollection;
  25. import com.mongodb.DBObject;
  26. import com.mongodb.MongoClient;
  27. import com.mongodb.util.JSON;
  28. public abstract class BaseRepo<T> implements BarkRepo<T> {
  29. protected static Logger logger = LoggerFactory.getLogger(BaseRepo.class);
  30. protected final DBCollection dbCollection;
  31. private final Class<T> clz;
  32. protected BaseRepo(String collectionName, Class<T> clz) throws RuntimeException {
  33. this.clz = clz;
  34. Properties env = new Properties();
  35. try {
  36. env.load(Thread.currentThread().getContextClassLoader()
  37. .getResourceAsStream("application.properties"));
  38. String mongoServer = env.getProperty("spring.data.mongodb.host");
  39. int mongoPort = Integer.parseInt(env
  40. .getProperty("spring.data.mongodb.port"));
  41. DB db = new MongoClient(mongoServer, mongoPort).getDB("unitdb0");
  42. dbCollection = db.getCollection(collectionName);
  43. } catch (IOException e) {
  44. throw new RuntimeException(e);
  45. }
  46. }
  47. @Override
  48. final public List<T> getAll() {
  49. List<T> result = new LinkedList<T>();
  50. for(DBObject dbo : dbCollection.find()) {
  51. result.add(toEntity(dbo));
  52. }
  53. return result;
  54. }
  55. protected T toEntity(DBObject dbo) {
  56. Gson gson = new Gson();
  57. return gson.fromJson(dbo.toString(), clz);
  58. }
  59. @Override
  60. final public void save(T t) {
  61. Gson gson = new Gson();
  62. DBObject t1 = (DBObject) JSON.parse(gson.toJson(t));
  63. dbCollection.save(t1);
  64. }
  65. @Override
  66. final public DBObject getByCondition(List<Pair> queryList) {
  67. BasicDBObject query = new BasicDBObject();
  68. for (Pair k : queryList) {
  69. query.put(k.key, k.value);
  70. }
  71. return dbCollection.findOne(query);
  72. }
  73. }