PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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