/rest-api/src/main/scala/com/apetheriotis/streaming/logtuc/restapi/dao/CommonDao.scala

https://github.com/apetheriotis/log-tuc · Scala · 100 lines · 45 code · 13 blank · 42 comment · 9 complexity · d99508fffad29adae347aebbb5c0a76b MD5 · raw file

  1. package com.apetheriotis.streaming.logtuc.restapi.dao
  2. import com.apetheriotis.streaming.logtuc.restapi.domain.DaoObject
  3. import com.mongodb.DBObject
  4. import com.mongodb.casbah.MongoCollection
  5. import com.mongodb.casbah.commons.MongoDBObject
  6. abstract class CommonDao[T](val db: com.mongodb.casbah.MongoDB = MongoFactory.mongoClient) {
  7. /**
  8. * @return the collection name to store data
  9. */
  10. def getCollectionName: String
  11. /**
  12. * Save document to collection
  13. * @param doc the document to persist
  14. */
  15. def save(doc: DaoObject): AnyRef = {
  16. db(getCollectionName).insert(doc.asDbObject())
  17. }
  18. /**
  19. * Drops collection
  20. */
  21. def drop(): Unit = {
  22. db(getCollectionName).drop()
  23. }
  24. /**
  25. * Query for the only entry
  26. * @return the only entry if any
  27. */
  28. protected def getOne: Option[MongoCollection#T] = {
  29. db(getCollectionName).findOne()
  30. }
  31. /**
  32. * List all documents in collection
  33. * @return all documents
  34. * @param orderBy on which field to orderBy if any
  35. * @param asc true for ascending else descending
  36. * @param offset how many entries to skip
  37. */
  38. protected def listAll(orderBy: Option[String] = None, asc: Option[Boolean] = None,
  39. limit: Int = 100, offset: Int = 0): MongoCollection#CursorType = {
  40. if (orderBy != None) {
  41. val order = if (asc.get) 1 else -1
  42. val orderQ = MongoDBObject(orderBy.get -> order)
  43. db(getCollectionName).find().sort(orderQ).skip(offset).limit(limit)
  44. } else {
  45. db(getCollectionName).find().skip(offset).limit(limit)
  46. }
  47. }
  48. /**
  49. * List all documents in collection using a filter
  50. * @param field the field to filter
  51. * @param value the value of the filter
  52. * @param limit how many to return
  53. * @param orderBy on which field to orderBy
  54. * @param asc true for ascending else descending
  55. * @param offset how many entries to skip
  56. * @return all documents
  57. */
  58. protected def listAllByFilter(orderBy: Option[String], asc: Option[Boolean], field: String,
  59. value: AnyRef, limit: Int = 100, offset: Int = 0): MongoCollection#CursorType = {
  60. val q: DBObject = MongoDBObject(field -> value)
  61. if (orderBy != None) {
  62. val order = if (asc.get) 1 else -1
  63. val orderQ = MongoDBObject(orderBy.get -> order)
  64. db(getCollectionName).find(q).sort(orderQ).skip(offset).limit(limit)
  65. }
  66. db(getCollectionName).find(q).skip(offset).limit(limit)
  67. }
  68. /**
  69. * Get a document by filtering one field
  70. * @param field the field to filter
  71. * @param value the value of the filter
  72. * @return the document if found else None
  73. */
  74. protected def getByField(field: String, value: AnyRef): Option[MongoCollection#T] = {
  75. val q: DBObject = MongoDBObject(field -> value)
  76. db(getCollectionName).findOne(q)
  77. }
  78. /**
  79. * Remove a document by filtering one field
  80. * @param field the field to filter
  81. * @param value the value of the filter
  82. */
  83. protected def removeByField(field: String, value: AnyRef): Unit = {
  84. val q: DBObject = MongoDBObject(field -> value)
  85. db(getCollectionName).findAndRemove(q)
  86. }
  87. }