PageRenderTime 117ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/traktor/movies/MovieActor.scala

https://bitbucket.org/tdrobinson/traktor
Scala | 87 lines | 75 code | 12 blank | 0 comment | 1 complexity | eb35dbb720d3d96da466c15e522db0c0 MD5 | raw file
  1. package traktor.movies
  2. import akka.actor.{ActorRef, Actor}
  3. import traktor.util.{DBCollection, Logging}
  4. import akka.pattern.pipe
  5. import com.mongodb.DBObject
  6. import net.liftweb.json.JsonAST.{JObject, JArray, JValue}
  7. import com.mongodb.casbah.Implicits._
  8. import org.bson.types.ObjectId
  9. import com.mongodb.casbah.commons.MongoDBObject
  10. import scala.concurrent.Future
  11. case object AllMovies
  12. case object PendingMovies
  13. case class ApplySearchResult(obj: JValue, watched: Boolean)
  14. case class AddMovies(movies: List[String])
  15. case class AddMoviesToDB(movies: List[JObject])
  16. case class DeleteMovies(movies: List[String])
  17. case class DeleteMoviesFromDB(movies: List[String])
  18. class MovieActor(discoveryActor: ActorRef) extends Actor with DBCollection with Logging {
  19. val collection = db("movies")
  20. def receive = {
  21. case AllMovies => Future { listAll } pipeTo sender
  22. case PendingMovies => Future { listAllPending } pipeTo sender
  23. case ApplySearchResult(obj, watched) => Future { applySearchResult(obj, watched) } pipeTo sender
  24. case AddMovies(ms) => Future { addMovies(ms) } pipeTo sender
  25. case AddMoviesToDB(ms) => Future { addMoviesToDB(ms) } onFailure { case e => e.printStackTrace() }
  26. case DeleteMovies(ms) => Future { removeMovies(ms) } pipeTo sender
  27. case DeleteMoviesFromDB(ms) => Future { removeMoviesFromDB(ms) } onFailure { case e => e.printStackTrace() }
  28. }
  29. def listAll(implicit dbObject2JObject: DBObject => JValue) =
  30. JArray(collection.find().toList.map(db => dbObject2JObject(db)))
  31. def listAllPending(implicit dbObject2JObject: DBObject => JValue) =
  32. JArray(collection.find("results" $exists true).toList.map(db => dbObject2JObject(db)))
  33. def applySearchResult(obj: JValue, watched: Boolean)(implicit jValue2DBObject: JValue => DBObject) = {
  34. val result = jValue2DBObject(obj)
  35. collection.findOneByID(new ObjectId(result.removeField("movie_id").asInstanceOf[String])) match {
  36. case Some(movie) =>
  37. movie.removeField("results")
  38. result.foreach {
  39. case (key, value) => movie.put(key, value)
  40. }
  41. discoveryActor ! MovieCollectionAdd(movie.toString, watched)
  42. collection.save(movie)
  43. true
  44. case None => false
  45. }
  46. }
  47. def addMovies(ms: List[String]) = {
  48. try {
  49. val displayNames = ms.map(m => (m, m.substring(m.lastIndexOf('/') + 1, m.lastIndexOf('.'))))
  50. discoveryActor ! MovieSearch(displayNames)
  51. true
  52. } catch {
  53. case e: IndexOutOfBoundsException => false
  54. }
  55. }
  56. def removeMovies(ms: List[String]) = {
  57. try
  58. {
  59. val forRemoval = ms.map(m => collection.findOne(MongoDBObject("filename" -> m))).filter(m => {
  60. m.isDefined && !m.get.containsField("results")
  61. }).map(_.get.toString)
  62. discoveryActor ! MovieCollectionRemove(forRemoval)
  63. self ! DeleteMoviesFromDB(ms)
  64. true
  65. } catch {
  66. case _ : Throwable => false
  67. }
  68. }
  69. def addMoviesToDB(ms: List[JObject])(implicit jValue2DBObject: JValue => DBObject) {
  70. collection.insert(ms.map(jValue2DBObject):_*)
  71. }
  72. def removeMoviesFromDB(ms: List[String]) {
  73. ms.foreach(m => collection.remove(MongoDBObject("filename" -> m)))
  74. }
  75. }