No Description

UtilitiesClass.scala 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package fr.natan.akkastreamfileprocessingapi.service
  2. import akka.NotUsed
  3. import akka.stream.scaladsl.{Flow, Sink, Source}
  4. import com.typesafe.scalalogging.slf4j.Logger
  5. import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap.Person
  6. import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildPersonModel
  7. import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{actorSystem, buildAndValidateSource}
  8. import java.io.File
  9. import scala.concurrent.Future
  10. object UtilitiesClass {
  11. def getTvSerieIdByPrimaryTitle(primaryTitle: String, inputFile: File): Future[Option[String]] = {
  12. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
  13. val tvSerieIdFuture: Future[Option[String]] = source
  14. .filter((rowMap: Map[String, String]) => {
  15. rowMap.getOrElse(key = "primaryTitle", default = "") == primaryTitle
  16. })
  17. .map((rowMap: Map[String, String]) => rowMap.get("tconst"))
  18. .runWith(Sink.head)
  19. tvSerieIdFuture
  20. }
  21. def getListOfPersonsIDByTvSerieID(tvSerieIdFuture: Future[Option[String]], inputFile: File): Future[IndexedSeq[Option[String]]] = {
  22. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
  23. val personsIDsFuture: Future[IndexedSeq[Option[String]]] = source
  24. .filter((rowMaps: Map[String, String]) => {
  25. rowMaps.getOrElse(key = "tconst", default = "") == tvSerieIdFuture.value.get.get.get
  26. })
  27. .map((rowMap: Map[String, String]) => {
  28. rowMap.get(key = "nconst")
  29. })
  30. .runWith(Sink.collection)
  31. personsIDsFuture
  32. }
  33. def getListOfPersonsForTvSerie(listPersonsIDsFuture: Future[IndexedSeq[Option[String]]], inputFile: File): Future[IndexedSeq[Person]] = {
  34. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
  35. val personsFuture: Future[IndexedSeq[Person]] = source
  36. .filter((rowMaps: Map[String, String]) => {
  37. listPersonsIDsFuture.value.get.get.contains(rowMaps.get(key = "nconst"))
  38. })
  39. .map((rowMap: Map[String, String]) => {
  40. buildPersonModel(rowMap)
  41. })
  42. .runWith(Sink.collection)
  43. personsFuture
  44. }
  45. private def getTvSerieIdFlow(tvSerieID: String): Flow[Map[String, String], Option[String], NotUsed] = {
  46. val tvSerieIDFlow = Flow[Map[String, String]]
  47. .filter(rowMap => rowMap.getOrElse(key = "tconst", default = "") == tvSerieID)
  48. .map(row => row.get("tconst"))
  49. tvSerieIDFlow
  50. }
  51. def getTvSerieIDFuture(tvSerieID: String, logger: Logger, inputFile: File): Future[Option[String]] = {
  52. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
  53. val tvSerieIdFuture: Future[Option[String]] = source
  54. .filter(rowMap => rowMap.getOrElse(key = "tconst", default = "") == tvSerieID)
  55. .map(row => row.get("tconst"))
  56. .runWith(Sink.head)
  57. tvSerieIdFuture
  58. }
  59. }

Powered by TurnKey Linux.