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

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

https://bitbucket.org/wbillingsley/ibmongo-2012-old-version-duct-tape-not-maintained
Scala | 77 lines | 57 code | 20 blank | 0 comment | 0 complexity | 273fcddab199e92cd2f3c2087d42beb0 MD5 | raw file
  1. package com.theintelligentbook.ibmodel.mongo
  2. import com.wbillingsley.handy._
  3. import com.mongodb.casbah.Imports._
  4. import com.novus.salat._
  5. import com.novus.salat.global._
  6. import com.novus.salat.dao.SalatDAO
  7. import java.util.Date
  8. import com.novus.salat.annotations._
  9. case class PollResponse(
  10. _id:ObjectId = new ObjectId(),
  11. @Key("book") _book:Option[ObjectId] = None,
  12. @Key("poll") _poll:Option[ObjectId] = None,
  13. @Key("reader") var _reader:Option[ObjectId] = None,
  14. var session:Option[String] = None,
  15. var answer:Set[Int] = Set.empty[Int],
  16. created:Date = new Date(),
  17. var lastUpdated:Date = new Date()
  18. ) extends HasObjectId {
  19. def id = _id
  20. def book = Ref.fromOptionId(classOf[Book], _book)
  21. def poll = Ref.fromOptionId(classOf[Poll], _poll)
  22. def reader = Ref.fromOptionId(classOf[Reader], _reader)
  23. def reader_=(r:Ref[Reader]) = _reader_=(r.getId)
  24. }
  25. object PollResponseDAO extends AbstractDAO[PollResponse]("pollResponse") {
  26. def newPollResponse(p:Ref[Poll], r:Ref[Reader], s:Option[String], ans:Set[Int]) = {
  27. val pr = new PollResponse(
  28. _poll = p.getId,
  29. _book = p.flatMap(_.book).getId,
  30. _reader = r.getId,
  31. session = s,
  32. answer = ans
  33. )
  34. DAO.cache(RefById(classOf[PollResponse], pr.id), RefItself(pr))
  35. pr
  36. }
  37. def save(p:PollResponse) {
  38. DAO.cache(RefById(classOf[PollResponse], p.id), RefItself(p))
  39. sdao.save(p)
  40. }
  41. def findVote(pollRef: Ref[Poll], readerRef: Ref[Reader], sessionOpt: Option[String]) = {
  42. readerRef.fetch match {
  43. case RefItself(r) => {
  44. val ccOpt = sdao.findOne(MongoDBObject("poll" -> pollRef.getId, "reader" -> r.id))
  45. ccOpt
  46. }
  47. case _ => {
  48. val ccOpt = sdao.findOne(MongoDBObject("poll" -> pollRef.getId, "session" -> sessionOpt))
  49. ccOpt
  50. }
  51. }
  52. }
  53. def getPollResponses(pollRef:Ref[Poll]) = {
  54. val res = sdao.find(MongoDBObject("poll" -> pollRef.getId))
  55. res.toSeq
  56. }
  57. }