PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/com/theintelligentbook/ibmodel/mongo/Identification.scala

https://bitbucket.org/wbillingsley/ibmongo-2012-old-version-duct-tape-not-maintained
Scala | 75 lines | 46 code | 20 blank | 9 comment | 0 complexity | 7a106245acb93859606ab77d755502fe MD5 | raw file
  1. package com.theintelligentbook.ibmodel.mongo
  2. import com.mongodb.casbah.Imports._
  3. import java.util.Date
  4. import com.novus.salat._
  5. import com.novus.salat.global._
  6. import com.novus.salat.dao.SalatDAO
  7. import com.wbillingsley.handy._
  8. import com.novus.salat.annotations._
  9. case class Identification(
  10. _id:ObjectId = new ObjectId(),
  11. @Key("owner") var _owner:Option[ObjectId] = None,
  12. /**
  13. * For example, OpenID, OAuth 1.0, OAuth 2.0, Email
  14. */
  15. var kind:String = "unknown",
  16. /**
  17. * For example, Google, Twitter, Facebook
  18. */
  19. var provider:String = "unknown",
  20. var value:Option[String] = None,
  21. var verified:Boolean = false,
  22. var since:Date = new Date()
  23. ) extends HasId[ObjectId] {
  24. def id = _id
  25. def owner = Ref.fromOptionId(classOf[Reader], _owner)
  26. def owner_=(r:Ref[Reader]) {
  27. _owner = r.getId.asInstanceOf[Option[ObjectId]]
  28. }
  29. var isNew = false
  30. def save() = {
  31. IdentificationDAO.save(this)
  32. }
  33. }
  34. object IdentificationDAO extends AbstractDAO[Identification]("identification") {
  35. /**
  36. * Finds an @link{Identification} based on the provider and the provider's id.
  37. */
  38. def find(kind: String, provider:String, value:String):ResolvedRef[Identification] = {
  39. val ccOpt = sdao.findOne(MongoDBObject("kind" -> kind, "provider" -> provider, "value" -> Some(value)))
  40. Ref.fromOptionItem(ccOpt)
  41. }
  42. def newIdentification(kind:String, provider:String, value:String) = {
  43. val i = new Identification()
  44. i.isNew = true
  45. i.kind = kind
  46. i.provider = provider
  47. i.value = Some(value)
  48. DAO.cache(RefById(classOf[Identification], i.id), RefItself(i))
  49. i
  50. }
  51. def save(i:Identification) {
  52. DAO.cache(RefById(classOf[Identification], i.id), RefItself(i))
  53. sdao.save(i)
  54. }
  55. }