Нет описания

TvSeriesController.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. JsValue personJs = Json.toJson(person, person.personFormat());
  42. return Json.prettyPrint(personJs);
  43. }
  44. @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
  45. private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
  46. throws ExecutionException, InterruptedException {
  47. Future<Models.Person> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
  48. CompletableFuture<Models.Person> personCompletableFuture = CompletableFutureBuilder
  49. .buildcompletableFuture1(personFuture);
  50. while (!personCompletableFuture.isDone()) {
  51. logger.info("IS PROCESSING...");
  52. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  53. }
  54. Models.Person person = personCompletableFuture.get();
  55. JsValue personJs = Json.toJson(person, person.personFormat());
  56. return Json.prettyPrint(personJs);
  57. }
  58. @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
  59. private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
  60. throws ExecutionException, InterruptedException {
  61. Future<Models.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
  62. CompletableFuture<Models.TvSerie> tvSerieCompletableFuture = CompletableFutureBuilder
  63. .buildcompletableFuture1(tvSerieFuture);
  64. while (!tvSerieCompletableFuture.isDone()) {
  65. logger.info("IS PROCESSING...");
  66. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  67. }
  68. Models.TvSerie tvSerie = tvSerieCompletableFuture.get();
  69. JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
  70. return Json.prettyPrint(tvSerieJs);
  71. }
  72. @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
  73. private List<String> getTvserieByPrimaryTitle(
  74. @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
  75. Future<IndexedSeq<Models.TvSerie>> listFuture = akkaStreamFilesProcessing.getTvSeriesByPrimaryTitleFuture(tvseriePrimaryTitle);
  76. CompletableFuture<IndexedSeq<Models.TvSerie>> completableFuture =
  77. CompletableFutureBuilder.buildcompletableFuture2(listFuture);
  78. while (!completableFuture.isDone()) {
  79. logger.info("IS PROCESSING...");
  80. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  81. }
  82. IndexedSeq<Models.TvSerie> tvSerieList = completableFuture.get();
  83. List<String> tvSeries = new ArrayList<>();
  84. tvSerieList.foreach(tvSerie -> {
  85. JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
  86. tvSeries.add(Json.prettyPrint(tvSerieJs));
  87. return null;
  88. });
  89. return tvSeries;
  90. }
  91. @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET)
  92. private List<String> getPersonsForTvSerieByTvSerieTitle(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
  93. throws InterruptedException, ExecutionException {
  94. Future<IndexedSeq<Models.Person>> futurePersonSeq = akkaStreamFilesProcessing.getPersonsForTvSerieByTvSerieTitleFuture(tvSerieTitle);
  95. CompletableFuture<IndexedSeq<Models.Person>> completableFuture = CompletableFutureBuilder.buildcompletableFuture2(futurePersonSeq);
  96. while (!completableFuture.isDone()){
  97. logger.info("IS PROCESSING...");
  98. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  99. }
  100. IndexedSeq<Models.Person> personIndexedSeq = completableFuture.get();
  101. List<String> personList = new ArrayList<>();
  102. personIndexedSeq.foreach(person -> {
  103. JsValue personJs = Json.toJson(person, person.personFormat());
  104. personList.add(Json.prettyPrint(personJs));
  105. return null;
  106. });
  107. return personList;
  108. }
  109. @RequestMapping(value = "/persons/tvseries/id/{tvSerieID}", method = RequestMethod.GET)
  110. private List<String> getPersonsForTvSerieByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID)
  111. throws InterruptedException, ExecutionException {
  112. Future<IndexedSeq<Models.Person>> futureIndexedSeqPersons = akkaStreamFilesProcessing.getPersonsForTvSerieByTvSerieIdFuture(tvSerieID);
  113. CompletableFuture<IndexedSeq<Models.Person>> seqCompletableFuture = CompletableFutureBuilder.buildcompletableFuture2(futureIndexedSeqPersons);
  114. while (!seqCompletableFuture.isDone()){
  115. logger.info("IS PROCESSING");
  116. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  117. }
  118. IndexedSeq<Models.Person> personIndexedSeq = seqCompletableFuture.get();
  119. List<String> personsList = new ArrayList<>();
  120. personIndexedSeq.foreach(person ->{
  121. JsValue personJs = Json.toJson(person, person.personFormat());
  122. personsList.add(Json.prettyPrint(personJs));
  123. return null;
  124. });
  125. return personsList;
  126. }
  127. @RequestMapping(value = "/persons", method = RequestMethod.GET)
  128. private ResponseEntity<String> getAllPersons() {
  129. akkaStreamFilesProcessing.getAllPersonsFuture();
  130. return new ResponseEntity<>("is running", HttpStatus.OK);
  131. }
  132. @RequestMapping(value = "/tvseries", method = RequestMethod.GET)
  133. private ResponseEntity<String> getAllvSeries() {
  134. akkaStreamFilesProcessing.getAllTvSeriesFuture();
  135. return new ResponseEntity<>("is running", HttpStatus.OK);
  136. }
  137. }

Powered by TurnKey Linux.