package fr.natan.akkastreamfileprocessingapi.controller; import fr.natan.akkastreamfileprocessingapi.futurecompleteness.CompletableFutureBuilder; import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap; import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamFileProcessingFuture; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import play.api.libs.json.JsValue; import play.api.libs.json.Json; import scala.Option; import scala.collection.IndexedSeq; import scala.concurrent.Future; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @SuppressWarnings("SpellCheckingInspection") @RestController public class TvSeriesController { private final AkkaStreamFileProcessingFuture akkaStreamFilesProcessing; private final Logger logger = LoggerFactory.getLogger(this.getClass()); private static final int BORN_INF = 100, BORN_SUP = 2000; public TvSeriesController(AkkaStreamFileProcessingFuture akkaStreamFilesProcessing) { this.akkaStreamFilesProcessing = akkaStreamFilesProcessing; } @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET) private String getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException { Future> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID); CompletableFuture completableFuture = CompletableFutureBuilder .buildcompletableFuture1(futurePerson); while (!completableFuture.isDone()) { logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } ModelsAndJsonMap.Person person = completableFuture.get(); JsValue personJs = Json.toJson(person, person.personJsonFormatter()); return Json.prettyPrint(personJs); } @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET) private String getPersonByName(@PathVariable(name = "primaryName") String primaryName) throws ExecutionException, InterruptedException { Future> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName); CompletableFuture personCompletableFuture = CompletableFutureBuilder .buildcompletableFuture1(personFuture); while (!personCompletableFuture.isDone()) { logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } ModelsAndJsonMap.Person person = personCompletableFuture.get(); JsValue personJs = Json.toJson(person, person.personJsonFormatter()); return Json.prettyPrint(personJs); } @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET) private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID) throws ExecutionException, InterruptedException { Future> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID); CompletableFuture tvSerieCompletableFuture = CompletableFutureBuilder .buildcompletableFuture1(tvSerieFuture); while (!tvSerieCompletableFuture.isDone()) { logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } ModelsAndJsonMap.TvSerie tvSerie = tvSerieCompletableFuture.get(); JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieJsonFormatter()); return Json.prettyPrint(tvSerieJs); } @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET) private List getTvserieByPrimaryTitle( @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException { Future> listFuture = akkaStreamFilesProcessing .getTvSeriesByPrimaryTitleFuture(tvseriePrimaryTitle); CompletableFuture> completableFuture = CompletableFutureBuilder.buildcompletableFuture2(listFuture); while (!completableFuture.isDone()) { logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } IndexedSeq tvSerieList = completableFuture.get(); List tvSeries = new ArrayList<>(); tvSerieList.foreach(tvSerie -> { JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieJsonFormatter()); tvSeries.add(Json.prettyPrint(tvSerieJs)); return null; }); return tvSeries; } @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET) private List getPersonsForTvSerieByTvSerieTitle(@PathVariable(name = "tvSerieTitle") String tvSerieTitle) throws InterruptedException, ExecutionException { Future> futurePersonSeq = akkaStreamFilesProcessing .getPersonsForTvSerieByTvSerieTitleFuture(tvSerieTitle); CompletableFuture> completableFuture = CompletableFutureBuilder .buildcompletableFuture2(futurePersonSeq); while (!completableFuture.isDone()){ logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } IndexedSeq personIndexedSeq = completableFuture.get(); List personList = new ArrayList<>(); personIndexedSeq.foreach(person -> { JsValue personJs = Json.toJson(person, person.personJsonFormatter()); personList.add(Json.prettyPrint(personJs)); return null; }); return personList; } @RequestMapping(value = "/persons/tvseries/id/{tvSerieID}", method = RequestMethod.GET) private List getPersonsForTvSerieByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID) throws InterruptedException, ExecutionException { Future> futureIndexedSeqPersons = akkaStreamFilesProcessing .getPersonsForTvSerieByTvSerieIDFuture(tvSerieID); CompletableFuture> seqCompletableFuture = CompletableFutureBuilder .buildcompletableFuture2(futureIndexedSeqPersons); while (!seqCompletableFuture.isDone()){ logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } IndexedSeq personIndexedSeq = seqCompletableFuture.get(); List personsList = new ArrayList<>(); personIndexedSeq.foreach(person ->{ JsValue personJs = Json.toJson(person, person.personJsonFormatter()); personsList.add(Json.prettyPrint(personJs)); return null; }); return personsList; } @RequestMapping(value = "/persons", method = RequestMethod.GET) private ResponseEntity getAllPersons() { akkaStreamFilesProcessing.getAllPersonsFuture(); return new ResponseEntity<>("is running", HttpStatus.OK); } @RequestMapping(value = "/tvseries", method = RequestMethod.GET) private ResponseEntity getAllvSeries() { akkaStreamFilesProcessing.getAllTvSeriesFuture(); return new ResponseEntity<>("is running", HttpStatus.OK); } }