No Description

TvSeriesController.java 7.5KB

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

Powered by TurnKey Linux.