/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
- package io.prediction.commons.settings.mongodb
- import io.prediction.commons.settings.{App, Apps}
- import com.mongodb.casbah.Imports._
- /** MongoDB implementation of Apps. */
- class MongoApps(db: MongoDB) extends Apps {
- private val emptyObj = MongoDBObject()
- private val appColl = db("apps")
- private val seq = new MongoSequences(db)
- private val getFields = MongoDBObject("userid" -> 1, "appkey" -> 1, "display" -> 1, "url" -> 1, "cat" -> 1, "desc" -> 1, "timezone" -> 1)
- private def dbObjToApp(dbObj: DBObject) = {
- App(
- id = dbObj.as[Int]("_id"),
- userid = dbObj.as[Int]("userid"),
- appkey = dbObj.as[String]("appkey"),
- display = dbObj.as[String]("display"),
- url = dbObj.getAs[String]("url"),
- cat = dbObj.getAs[String]("cat"),
- desc = dbObj.getAs[String]("desc"),
- timezone = dbObj.as[String]("timezone")
- )
- }
- class MongoAppIterator(it: MongoCursor) extends Iterator[App] {
- def next = dbObjToApp(it.next)
- def hasNext = it.hasNext
- }
- def insert(app: App) = {
- val id = seq.genNext("appid")
- val must = MongoDBObject(
- "_id" -> id,
- "userid" -> app.userid,
- "appkey" -> app.appkey,
- "display" -> app.display,
- "timezone" -> app.timezone
- )
- val url = app.url map { url => MongoDBObject("url" -> url) } getOrElse emptyObj
- val cat = app.cat map { cat => MongoDBObject("cat" -> cat) } getOrElse emptyObj
- val desc = app.desc map { desc => MongoDBObject("desc" -> desc) } getOrElse emptyObj
- appColl.insert(must ++ url ++ cat ++ desc)
- id
- }
- def get(id: Int) = appColl.findOne(MongoDBObject("_id" -> id), getFields) map { dbObjToApp(_) }
- def getAll() = new MongoAppIterator(appColl.find())
- def getByUserid(userid: Int) = new MongoAppIterator(appColl.find(MongoDBObject("userid" -> userid), getFields))
- def getByAppkey(appkey: String) = appColl.findOne(MongoDBObject("appkey" -> appkey), getFields) map { dbObjToApp(_) }
- def getByAppkeyAndUserid(appkey: String, userid: Int) = appColl.findOne(MongoDBObject("appkey" -> appkey, "userid" -> userid), getFields) map { dbObjToApp(_) }
- def getByIdAndUserid(id: Int, userid: Int) = appColl.findOne(MongoDBObject("_id" -> id, "userid" -> userid), getFields) map { dbObjToApp(_) }
- def update(app: App, upsert: Boolean = false) = {
- val must = MongoDBObject(
- "_id" -> app.id,
- "userid" -> app.userid,
- "appkey" -> app.appkey,
- "display" -> app.display,
- "timezone" -> app.timezone)
- val url = app.url map { url => MongoDBObject("url" -> url) } getOrElse emptyObj
- val cat = app.cat map { cat => MongoDBObject("cat" -> cat) } getOrElse emptyObj
- val desc = app.desc map { desc => MongoDBObject("desc" -> desc) } getOrElse emptyObj
- appColl.update(MongoDBObject("_id" -> app.id), must ++ url ++ cat ++ desc, upsert)
- }
- def updateAppkeyByAppkeyAndUserid(appkey: String, userid: Int, newAppkey: String) = {
- appColl.findAndModify(MongoDBObject("appkey" -> appkey, "userid" -> userid), MongoDBObject("$set" -> MongoDBObject("appkey" -> newAppkey))) map { dbObjToApp(_) }
- }
- def updateTimezoneByAppkeyAndUserid(appkey: String, userid: Int, timezone: String) = {
- appColl.findAndModify(MongoDBObject("appkey" -> appkey, "userid" -> userid), MongoDBObject("$set" -> MongoDBObject("timezone" -> timezone))) map { dbObjToApp(_) }
- }
- def deleteByIdAndUserid(id: Int, userid: Int) = appColl.remove(MongoDBObject("_id" -> id, "userid" -> userid))
- def existsByIdAndAppkeyAndUserid(id: Int, appkey: String, userid: Int) = appColl.findOne(MongoDBObject("_id" -> id, "appkey" -> appkey, "userid" -> userid)) map { _ => true } getOrElse false
- }