PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/michel-kraemer/spamihilator-setup-wizard
Scala | 113 lines | 60 code | 15 blank | 38 comment | 1 complexity | 2703d8f8471acdbd9fd8ec55df807505 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, Screenshot}
  16. import com.mongodb.casbah.Imports._
  17. import com.mongodb.casbah.gridfs.Imports._
  18. /** A data access object for screenshots
  19. * @param db the handle to the MongoDB database
  20. * @author Michel Kraemer */
  21. class ScreenshotDAO(db: MongoDB) extends DAO[Screenshot] {
  22. private lazy val coll = db("screenshots")
  23. coll.ensureIndex("client")
  24. coll.ensureIndex("title")
  25. /** Converts a database object to a screenshot. Sets the given client
  26. * as the screenshot's owner.
  27. * @param client the client
  28. * @param o the database object
  29. * @return the screenshot */
  30. private def toScreenshot(client: Client, o: DBObject): Screenshot = new Screenshot(
  31. o.getAs[ObjectId]("_id").get,
  32. o.getAs[String]("title") getOrElse "",
  33. o.getAs[Int]("width") getOrElse 0,
  34. o.getAs[Int]("height") getOrElse 0,
  35. client
  36. ) {
  37. override def getInputStream() = {
  38. val gridfs = GridFS(db)
  39. gridfs.find(o.getAs[ObjectId]("dataid").get).underlying.getInputStream()
  40. }
  41. }
  42. /** Converts a database object to a screenshot
  43. * @param o the database object
  44. * @return the screenshot */
  45. private def toScreenshot(o: DBObject): Screenshot = {
  46. val client = (for (id <- o.getAs[ObjectId]("client");
  47. c <- Database.clientDao.find(id)) yield c) getOrElse {
  48. throw new IllegalStateException("Screenshot has an unknown client")
  49. }
  50. toScreenshot(client, o)
  51. }
  52. /** Converts a screenshot to a database object
  53. * @param screenshot the screenshot
  54. * @return the database object */
  55. private implicit def screenshotToDBObject(screenshot: Screenshot)
  56. (implicit dataId: ObjectId): DBObject = MongoDBObject(
  57. "title" -> screenshot.title,
  58. "width" -> screenshot.width,
  59. "height" -> screenshot.height,
  60. "client" -> screenshot.client.id,
  61. "dataid" -> dataId
  62. )
  63. override def find(): Iterable[Screenshot] = coll map toScreenshot
  64. /** Finds all screenshots for a given client
  65. * @param client the client
  66. * @return all screenshots for this client */
  67. def find(client: Client): Iterable[Screenshot] =
  68. (coll.find(MongoDBObject("client" -> client.id)) map {
  69. s => toScreenshot(client, s) }).toList
  70. /** Finds a screenshot with a given title
  71. * @param title the title
  72. * @return the screenshot or <code>None</code> if there is no
  73. * screenshot with that title */
  74. def find(title: String): Option[Screenshot] =
  75. coll.findOne(MongoDBObject("title" -> title)) map toScreenshot
  76. override def insert(screenshot: Screenshot) {
  77. if (exists(screenshot.title)) {
  78. throw new IllegalStateException("A screenshot with the title \"" +
  79. screenshot.title + "\" already exists in the database")
  80. }
  81. val f = GridFS(db).createFile(screenshot.getInputStream, screenshot.title)
  82. f.save
  83. implicit val dataId = f._id.get
  84. coll += screenshot
  85. }
  86. /** Deletes a screenshot from the database
  87. * @param screenshot the screenshot to delete */
  88. def delete(screenshot: Screenshot) {
  89. coll.findOne(MongoDBObject("_id" -> screenshot.id)) map { obj =>
  90. coll.remove(obj)
  91. obj.getAs[ObjectId]("dataid") map GridFS(db).remove
  92. }
  93. }
  94. /** @return the number of screenshots having the given title */
  95. def count(title: String) = coll.count(MongoDBObject("title" -> title))
  96. /** @return true if there is a screenshot having the given title */
  97. def exists(title: String) = count(title) > 0
  98. }