PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/play/app/models/ApplicationSetting.scala

https://github.com/Bochenski/stackcanon
Scala | 77 lines | 67 code | 9 blank | 1 comment | 2 complexity | afdf61d0c11d6065735b8f8c5ddaa0e2 MD5 | raw file
  1. package models
  2. import scala.xml._
  3. import play._
  4. import play.libs.Codec;
  5. import com.mongodb.casbah.Imports._
  6. import net.liftweb.json._
  7. import net.liftweb.json.JsonAST
  8. import net.liftweb.json.JsonDSL._
  9. import scala.collection._
  10. class ApplicationSetting(o: DBObject) extends DBInstance("Setting", o) {
  11. lazy val oid = o.getAs[ObjectId]("_id")
  12. lazy val key = o.getAs[String]("key")
  13. lazy val value = o.getAs[String]("value")
  14. }
  15. object ApplicationSetting extends DBBase[ApplicationSetting]("Settings") {
  16. override def allXML = <ApplicationSetting>
  17. {super.allXML}
  18. </ApplicationSetting>
  19. def findByKey(key: String) = findOneBy("key", key)
  20. def create(key: String, value: String): Boolean = {
  21. Logger.info("in model create appplication setting")
  22. //check whether the user exists
  23. val setting = findByKey(key)
  24. setting match {
  25. case Some(_) => false
  26. case None => {
  27. val builder = MongoDBObject.newBuilder
  28. builder += "key" -> key
  29. builder += "value" -> value
  30. val newObj = builder.result().asDBObject
  31. coll += newObj
  32. Logger.info("Created setting %s", key)
  33. true
  34. }
  35. }
  36. }
  37. def update(key: String, value: String): Boolean = {
  38. val setting = findByKey(key)
  39. setting match {
  40. case None => {
  41. Logger.info("AppSetting not found to update " + key)
  42. false
  43. }
  44. case Some(_) => {
  45. ApplicationSetting.update(MongoDBObject("_id" -> setting.get.oid.get), $set("value" -> value))
  46. if (settings.contains(key)) {
  47. settings(key) = value;
  48. }
  49. true
  50. }
  51. }
  52. }
  53. private var settings = mutable.Map[String, String]()
  54. def getSetting(key: String) = {
  55. if (!settings.contains(key)) {
  56. models.ApplicationSetting.findByKey(key) match {
  57. case Some(x) => {
  58. settings += key -> x.value.get
  59. }
  60. case None => {
  61. create(key,"")
  62. settings += key -> ""
  63. }
  64. }
  65. }
  66. settings.get(key).get
  67. }
  68. }