/commons/src/main/scala/io/prediction/commons/settings/mongodb/MongoApps.scala

https://github.com/activetrader/PredictionIO · Scala · 85 lines · 66 code · 18 blank · 1 comment · 0 complexity · f1504a5dbacf55d5794d4f2ebbd3da01 MD5 · raw file

  1. package io.prediction.commons.settings.mongodb
  2. import io.prediction.commons.settings.{App, Apps}
  3. import com.mongodb.casbah.Imports._
  4. /** MongoDB implementation of Apps. */
  5. class MongoApps(db: MongoDB) extends Apps {
  6. private val emptyObj = MongoDBObject()
  7. private val appColl = db("apps")
  8. private val seq = new MongoSequences(db)
  9. private val getFields = MongoDBObject("userid" -> 1, "appkey" -> 1, "display" -> 1, "url" -> 1, "cat" -> 1, "desc" -> 1, "timezone" -> 1)
  10. private def dbObjToApp(dbObj: DBObject) = {
  11. App(
  12. id = dbObj.as[Int]("_id"),
  13. userid = dbObj.as[Int]("userid"),
  14. appkey = dbObj.as[String]("appkey"),
  15. display = dbObj.as[String]("display"),
  16. url = dbObj.getAs[String]("url"),
  17. cat = dbObj.getAs[String]("cat"),
  18. desc = dbObj.getAs[String]("desc"),
  19. timezone = dbObj.as[String]("timezone")
  20. )
  21. }
  22. class MongoAppIterator(it: MongoCursor) extends Iterator[App] {
  23. def next = dbObjToApp(it.next)
  24. def hasNext = it.hasNext
  25. }
  26. def insert(app: App) = {
  27. val id = seq.genNext("appid")
  28. val must = MongoDBObject(
  29. "_id" -> id,
  30. "userid" -> app.userid,
  31. "appkey" -> app.appkey,
  32. "display" -> app.display,
  33. "timezone" -> app.timezone
  34. )
  35. val url = app.url map { url => MongoDBObject("url" -> url) } getOrElse emptyObj
  36. val cat = app.cat map { cat => MongoDBObject("cat" -> cat) } getOrElse emptyObj
  37. val desc = app.desc map { desc => MongoDBObject("desc" -> desc) } getOrElse emptyObj
  38. appColl.insert(must ++ url ++ cat ++ desc)
  39. id
  40. }
  41. def get(id: Int) = appColl.findOne(MongoDBObject("_id" -> id), getFields) map { dbObjToApp(_) }
  42. def getAll() = new MongoAppIterator(appColl.find())
  43. def getByUserid(userid: Int) = new MongoAppIterator(appColl.find(MongoDBObject("userid" -> userid), getFields))
  44. def getByAppkey(appkey: String) = appColl.findOne(MongoDBObject("appkey" -> appkey), getFields) map { dbObjToApp(_) }
  45. def getByAppkeyAndUserid(appkey: String, userid: Int) = appColl.findOne(MongoDBObject("appkey" -> appkey, "userid" -> userid), getFields) map { dbObjToApp(_) }
  46. def getByIdAndUserid(id: Int, userid: Int) = appColl.findOne(MongoDBObject("_id" -> id, "userid" -> userid), getFields) map { dbObjToApp(_) }
  47. def update(app: App, upsert: Boolean = false) = {
  48. val must = MongoDBObject(
  49. "_id" -> app.id,
  50. "userid" -> app.userid,
  51. "appkey" -> app.appkey,
  52. "display" -> app.display,
  53. "timezone" -> app.timezone)
  54. val url = app.url map { url => MongoDBObject("url" -> url) } getOrElse emptyObj
  55. val cat = app.cat map { cat => MongoDBObject("cat" -> cat) } getOrElse emptyObj
  56. val desc = app.desc map { desc => MongoDBObject("desc" -> desc) } getOrElse emptyObj
  57. appColl.update(MongoDBObject("_id" -> app.id), must ++ url ++ cat ++ desc, upsert)
  58. }
  59. def updateAppkeyByAppkeyAndUserid(appkey: String, userid: Int, newAppkey: String) = {
  60. appColl.findAndModify(MongoDBObject("appkey" -> appkey, "userid" -> userid), MongoDBObject("$set" -> MongoDBObject("appkey" -> newAppkey))) map { dbObjToApp(_) }
  61. }
  62. def updateTimezoneByAppkeyAndUserid(appkey: String, userid: Int, timezone: String) = {
  63. appColl.findAndModify(MongoDBObject("appkey" -> appkey, "userid" -> userid), MongoDBObject("$set" -> MongoDBObject("timezone" -> timezone))) map { dbObjToApp(_) }
  64. }
  65. def deleteByIdAndUserid(id: Int, userid: Int) = appColl.remove(MongoDBObject("_id" -> id, "userid" -> userid))
  66. def existsByIdAndAppkeyAndUserid(id: Int, appkey: String, userid: Int) = appColl.findOne(MongoDBObject("_id" -> id, "appkey" -> appkey, "userid" -> userid)) map { _ => true } getOrElse false
  67. }