PageRenderTime 45ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/wbillingsley/ibmongo-2012-old-version-duct-tape-not-maintained
Scala | 66 lines | 48 code | 18 blank | 0 comment | 0 complexity | a979840ca7cb438101d360099cb7c837 MD5 | raw file
  1. package com.theintelligentbook.ibmodel.mongo
  2. import com.wbillingsley.handy._
  3. import com.mongodb.casbah.Imports._
  4. import java.util.Date
  5. import com.novus.salat._
  6. import com.novus.salat.global._
  7. import com.novus.salat.dao.SalatDAO
  8. import com.novus.salat.annotations._
  9. case class Registration(
  10. var _id:ObjectId = new ObjectId(),
  11. @Key("roles") var _roles:Set[String] = Set(BookRole.Reader.toString),
  12. @Key("reader") var _reader:Option[ObjectId] = None,
  13. @Key("book") var _book:Option[ObjectId] = None
  14. ) extends HasObjectId {
  15. def id = _id
  16. def roles = _roles.map {BookRole.valueOf(_)}
  17. def roles_=(s:Set[BookRole]) = _roles_=(s.map(_.toString))
  18. def addRole(r:BookRole) {
  19. _roles += r.toString
  20. }
  21. def reader = Ref.fromOptionId(classOf[Reader], _reader)
  22. def reader_=(r:Ref[Reader]) = _reader_=(r.getId)
  23. def book = Ref.fromOptionId(classOf[Book], _book)
  24. def book_=(r:Ref[Book]) = _book_=(r.getId)
  25. }
  26. object RegistrationDAO extends AbstractDAO[Registration]("registration") {
  27. def newRegistration(b:Ref[Book], r:Ref[Reader], roles:Set[BookRole]) = {
  28. val reg = new Registration
  29. reg.reader = r
  30. reg.book = b
  31. reg.roles = roles
  32. DAO.cache(RefById(classOf[Registration], reg.id), RefItself(reg))
  33. reg
  34. }
  35. def save(r:Registration) {
  36. DAO.cache(RefById(classOf[Registration], r.id), RefItself(r))
  37. sdao.save(r)
  38. }
  39. def findByReader(rid:ObjectId) = {
  40. val cursor = sdao.find(MongoDBObject("reader" -> rid))
  41. cursor.toSeq
  42. }
  43. def findByReaderAndBook(r:Ref[Reader], b:Ref[Book]):TraversableOnce[Registration] = {
  44. (for (rid <- r.getId; bid <- b.getId) yield {
  45. sdao.find(MongoDBObject("reader" -> rid, "book" -> bid))
  46. }) getOrElse Seq.empty[Registration]
  47. }
  48. }