No Description

TvSeriesController.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package fr.natan.akkastreamfileprocessingapi.controller;
  2. import fr.natan.akkastreamfileprocessingapi.futurecompleteness.CompletableFutureBuilder;
  3. import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap;
  4. import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamFileProcessingFuture;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import play.api.libs.json.JsValue;
  14. import play.api.libs.json.Json;
  15. import scala.Option;
  16. import scala.collection.IndexedSeq;
  17. import scala.concurrent.Future;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Random;
  21. import java.util.concurrent.CompletableFuture;
  22. import java.util.concurrent.ExecutionException;
  23. @SuppressWarnings("SpellCheckingInspection")
  24. @RestController
  25. public class TvSeriesController {
  26. private final AkkaStreamFileProcessingFuture akkaStreamFilesProcessing;
  27. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  28. private static final int BORN_INF = 100, BORN_SUP = 2000;
  29. public TvSeriesController(AkkaStreamFileProcessingFuture akkaStreamFilesProcessing) {
  30. this.akkaStreamFilesProcessing = akkaStreamFilesProcessing;
  31. }
  32. @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET)
  33. private String getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException {
  34. Future<Option<ModelsAndJsonMap.Person>> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
  35. CompletableFuture<ModelsAndJsonMap.Person> completableFuture = CompletableFutureBuilder
  36. .buildcompletableFuture1(futurePerson);
  37. while (!completableFuture.isDone()) {
  38. logger.info("IS PROCESSING...");
  39. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  40. }
  41. ModelsAndJsonMap.Person person = completableFuture.get();
  42. JsValue personJs = Json.toJson(person, person.personJsonFormatter());
  43. return Json.prettyPrint(personJs);
  44. }
  45. @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
  46. private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
  47. throws ExecutionException, InterruptedException {
  48. Future<Option<ModelsAndJsonMap.Person>> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
  49. CompletableFuture<ModelsAndJsonMap.Person> personCompletableFuture = CompletableFutureBuilder
  50. .buildcompletableFuture1(personFuture);
  51. while (!personCompletableFuture.isDone()) {
  52. logger.info("IS PROCESSING...");
  53. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  54. }
  55. ModelsAndJsonMap.Person person = personCompletableFuture.get();
  56. JsValue personJs = Json.toJson(person, person.personJsonFormatter());
  57. return Json.prettyPrint(personJs);
  58. }
  59. @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
  60. private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
  61. throws ExecutionException, InterruptedException {
  62. Future<Option<ModelsAndJsonMap.TvSerie>> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
  63. CompletableFuture<ModelsAndJsonMap.TvSerie> tvSerieCompletableFuture = CompletableFutureBuilder
  64. .buildcompletableFuture1(tvSerieFuture);
  65. while (!tvSerieCompletableFuture.isDone()) {
  66. logger.info("IS PROCESSING...");
  67. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  68. }
  69. ModelsAndJsonMap.TvSerie tvSerie = tvSerieCompletableFuture.get();
  70. JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieJsonFormatter());
  71. return Json.prettyPrint(tvSerieJs);
  72. }
  73. @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
  74. private List<String> getTvserieByPrimaryTitle(
  75. @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
  76. Future<IndexedSeq<ModelsAndJsonMap.TvSerie>> listFuture = akkaStreamFilesProcessing
  77. .getTvSeriesByPrimaryTitleFuture(tvseriePrimaryTitle);
  78. CompletableFuture<IndexedSeq<ModelsAndJsonMap.TvSerie>> completableFuture =
  79. CompletableFutureBuilder.buildcompletableFuture2(listFuture);
  80. while (!completableFuture.isDone()) {
  81. logger.info("IS PROCESSING...");
  82. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  83. }
  84. IndexedSeq<ModelsAndJsonMap.TvSerie> tvSerieList = completableFuture.get();
  85. List<String> tvSeries = new ArrayList<>();
  86. tvSerieList.foreach(tvSerie -> {
  87. JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieJsonFormatter());
  88. tvSeries.add(Json.prettyPrint(tvSerieJs));
  89. return null;
  90. });
  91. return tvSeries;
  92. }
  93. @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET)
  94. private List<String> getPersonsForTvSerieByTvSerieTitle(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
  95. throws InterruptedException, ExecutionException {
  96. Future<IndexedSeq<ModelsAndJsonMap.Person>> futurePersonSeq = akkaStreamFilesProcessing
  97. .getPersonsForTvSerieByTvSerieTitleFuture(tvSerieTitle);
  98. CompletableFuture<IndexedSeq<ModelsAndJsonMap.Person>> completableFuture = CompletableFutureBuilder
  99. .buildcompletableFuture2(futurePersonSeq);
  100. while (!completableFuture.isDone()){
  101. logger.info("IS PROCESSING...");
  102. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  103. }
  104. IndexedSeq<ModelsAndJsonMap.Person> personIndexedSeq = completableFuture.get();
  105. List<String> personList = new ArrayList<>();
  106. personIndexedSeq.foreach(person -> {
  107. JsValue personJs = Json.toJson(person, person.personJsonFormatter());
  108. personList.add(Json.prettyPrint(personJs));
  109. return null;
  110. });
  111. return personList;
  112. }
  113. @RequestMapping(value = "/persons/tvseries/id/{tvSerieID}", method = RequestMethod.GET)
  114. private List<String> getPersonsForTvSerieByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID)
  115. throws InterruptedException, ExecutionException {
  116. Future<IndexedSeq<ModelsAndJsonMap.Person>> futureIndexedSeqPersons = akkaStreamFilesProcessing
  117. .getPersonsForTvSerieByTvSerieIDFuture(tvSerieID);
  118. CompletableFuture<IndexedSeq<ModelsAndJsonMap.Person>> seqCompletableFuture = CompletableFutureBuilder
  119. .buildcompletableFuture2(futureIndexedSeqPersons);
  120. while (!seqCompletableFuture.isDone()){
  121. logger.info("IS PROCESSING...");
  122. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  123. }
  124. IndexedSeq<ModelsAndJsonMap.Person> personIndexedSeq = seqCompletableFuture.get();
  125. List<String> personsList = new ArrayList<>();
  126. personIndexedSeq.foreach(person ->{
  127. JsValue personJs = Json.toJson(person, person.personJsonFormatter());
  128. personsList.add(Json.prettyPrint(personJs));
  129. return null;
  130. });
  131. return personsList;
  132. }
  133. @RequestMapping(value = "/persons", method = RequestMethod.GET)
  134. private ResponseEntity<String> getAllPersons() {
  135. akkaStreamFilesProcessing.getAllPersonsFuture();
  136. return new ResponseEntity<>("is running", HttpStatus.OK);
  137. }
  138. @RequestMapping(value = "/tvseries", method = RequestMethod.GET)
  139. private ResponseEntity<String> getAllvSeries() {
  140. akkaStreamFilesProcessing.getAllTvSeriesFuture();
  141. return new ResponseEntity<>("is running", HttpStatus.OK);
  142. }
  143. }

Powered by TurnKey Linux.