PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/codepulse/src/main/scala/com/secdec/codepulse/data/model/slick/SlickRecordingMetadataAccess.scala

https://github.com/codedx/codepulse
Scala | 99 lines | 56 code | 16 blank | 27 comment | 0 complexity | cdef4eacc9a8907ee6daa654954005e0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, EPL-1.0, Apache-2.0, MIT
  1. /*
  2. * Code Pulse: A real-time code coverage testing tool. For more information
  3. * see http://code-pulse.com
  4. *
  5. * Copyright (C) 2014 Applied Visions - http://securedecisions.avi.com
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. package com.secdec.codepulse.data.model.slick
  20. import scala.slick.jdbc.JdbcBackend.{ Database, Session }
  21. import com.secdec.codepulse.data.model.{ RecordingMetadata, RecordingMetadataAccess }
  22. import net.liftweb.util.Helpers.AsBoolean
  23. /** Slick-backed RecordingMetadata implementation.
  24. *
  25. * @author robertf
  26. */
  27. private[slick] class SlickRecordingMetadata(val id: Int, dao: RecordingMetadataDao, db: Database) extends RecordingMetadata {
  28. private val cache = collection.mutable.Map.empty[String, Option[String]]
  29. private def get(key: String)(implicit session: Session) = cache.getOrElseUpdate(key, dao.get(id, key))
  30. private def set(key: String, value: String)(implicit session: Session): Unit = set(key, Some(value))
  31. private def set(key: String, value: Option[String])(implicit session: Session) {
  32. dao.set(id, key, value)
  33. cache += key -> value
  34. }
  35. def running = db withSession { implicit session =>
  36. get("running").flatMap(AsBoolean.unapply) getOrElse true
  37. }
  38. def running_=(newState: Boolean) = db withTransaction { implicit transaction =>
  39. set("running", newState.toString)
  40. newState
  41. }
  42. def clientLabel = db withSession { implicit session =>
  43. get("clientLabel")
  44. }
  45. def clientLabel_=(newLabel: Option[String]) = db withTransaction { implicit transaction =>
  46. set("clientLabel", newLabel)
  47. newLabel
  48. }
  49. def clientColor = db withSession { implicit session =>
  50. get("clientColor")
  51. }
  52. def clientColor_=(newColor: Option[String]) = db withTransaction { implicit transaction =>
  53. set("clientColor", newColor)
  54. newColor
  55. }
  56. }
  57. /** Slick-backed RecordingMetadataAccess implementation.
  58. *
  59. * @author robertf
  60. */
  61. private[slick] class SlickRecordingMetadataAccess(dao: RecordingMetadataDao, db: Database) extends RecordingMetadataAccess {
  62. private def metadataFor(id: Int) = new SlickRecordingMetadata(id, dao, db)
  63. private lazy val cache = {
  64. import collection.JavaConverters._
  65. // because scala apparently has no mutable sorted map, we'll use a java TreeMap with a Scala wrapper
  66. val recordings = new java.util.TreeMap[Int, RecordingMetadata].asScala
  67. recordings ++= db withSession { dao.getRecordings()(_) } map { id => id -> metadataFor(id) }
  68. recordings
  69. }
  70. def all: List[RecordingMetadata] = cache.values.toList
  71. def contains(id: Int): Boolean = cache.contains(id)
  72. def create(): RecordingMetadata = {
  73. val newId = db withTransaction { implicit transaction => dao.createRecording }
  74. get(newId)
  75. }
  76. def get(id: Int): RecordingMetadata = cache.getOrElseUpdate(id, metadataFor(id))
  77. def remove(id: Int) {
  78. db withTransaction { implicit transaction =>
  79. dao deleteRecording id
  80. cache -= id
  81. }
  82. }
  83. }