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

/src/main/scala/com/spamihilator/setupwizard/db/DocumentDAO.scala

https://github.com/michel-kraemer/spamihilator-setup-wizard
Scala | 83 lines | 40 code | 10 blank | 33 comment | 2 complexity | 2edf22ecaec6d2010286a9a732a0eaf3 MD5 | raw file
  1. // Copyright 2011 Michel Kraemer
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package com.spamihilator.setupwizard.db
  15. import com.spamihilator.setupwizard.model.{Client, Document}
  16. import com.mongodb.casbah.Imports._
  17. import java.util.{Calendar, Date}
  18. /** A data access object for mail client tutorials. Documents
  19. * are saved as immutable objects in the database. There can
  20. * be several documents for one client, the latest one represents
  21. * the current version.
  22. * @param db the handle to the MongoDB database
  23. * @author Michel Kraemer */
  24. class DocumentDAO(db: MongoDB) extends DAO[Document] {
  25. private lazy val coll = db("documents")
  26. coll.ensureIndex("client")
  27. /** Converts a database object to a document. Sets the given client
  28. * as the document's owner.
  29. * @param client the client
  30. * @param o the database object
  31. * @return the document */
  32. private def toDocument(client: Client, o: DBObject): Document = Document(
  33. o.getAs[ObjectId]("_id").get,
  34. o.getAs[String]("text") getOrElse "",
  35. ((for (l <- o.getAs[BasicDBList]("authors"))
  36. yield l.toList) getOrElse List.empty).asInstanceOf[List[String]],
  37. o.getAs[Date]("date") getOrElse (Calendar.getInstance().getTime()),
  38. client
  39. )
  40. /** Converts a document to a database object
  41. * @param doc the document
  42. * @return the database object */
  43. private implicit def documentToDBObject(doc: Document): DBObject = MongoDBObject(
  44. "text" -> doc.text,
  45. "authors" -> doc.authors,
  46. "date" -> doc.date,
  47. "client" -> doc.client.id
  48. )
  49. override def find(): Iterable[Document] = coll map { o =>
  50. val client = (for (id <- o.getAs[ObjectId]("client");
  51. c <- Database.clientDao.find(id)) yield c) getOrElse {
  52. throw new IllegalStateException("Document has an unknown client")
  53. }
  54. toDocument(client, o)
  55. }
  56. /** Finds all documents for a given client
  57. * @param client the client
  58. * @return all documents about this client */
  59. def find(client: Client): Iterable[Document] =
  60. (coll.find(MongoDBObject("client" -> client.id)) map { o =>
  61. toDocument(client, o) }).toList
  62. /** Finds the latest document for a given client
  63. * @param client the client
  64. * @return the latest document */
  65. def findLatest(client: Client): Option[Document] = {
  66. val f = coll.find(MongoDBObject("client" -> client.id)).sort(
  67. MongoDBObject("date" -> -1))
  68. if (f.hasNext) Some(toDocument(client, f.next)) else None
  69. }
  70. override def insert(doc: Document) {
  71. coll += doc
  72. }
  73. }