package fr.natan.akkastreamfileprocessingapi.controller; import fr.natan.akkastreamfileprocessingapi.futurecompleteness.CompletableFutureResult; import fr.natan.akkastreamfileprocessingapi.models.Models; import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamFileProcessing; 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.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 AkkaStreamFileProcessing akkaStreamFilesProcessing; private final Logger logger = LoggerFactory.getLogger(this.getClass()); private static final int BORN_INF = 500, BORN_SUP = 5000; public TvSeriesController(AkkaStreamFileProcessing 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.getPersonById(personID); CompletableFuture completableFuture = CompletableFutureResult.buildcompletableFuture1(futurePerson); while (!completableFuture.isDone()) { logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } Models.Person person = completableFuture.get(); return Json.toJson(person, person.personFormat()).toString(); } @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET) private String getPersonByName(@PathVariable(name = "primaryName") String primaryName) throws ExecutionException, InterruptedException { Future personFuture = akkaStreamFilesProcessing.getPersonByName(primaryName); CompletableFuture personCompletableFuture = CompletableFutureResult.buildcompletableFuture1(personFuture); while (!personCompletableFuture.isDone()) { logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } Models.Person person = personCompletableFuture.get(); return Json.toJson(person, person.personFormat()).toString(); } @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET) private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID) throws ExecutionException, InterruptedException { Future tvSerieFuture = akkaStreamFilesProcessing.getTvSerieById(tvSerieID); CompletableFuture tvSerieCompletableFuture = CompletableFutureResult.buildcompletableFuture1(tvSerieFuture); while (!tvSerieCompletableFuture.isDone()) { logger.info("IS PROCESSING..."); Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF); } Models.TvSerie tvSerie = tvSerieCompletableFuture.get(); return Json.toJson(tvSerie, tvSerie.tvSerieFormat()).toString(); } @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET) private List getTvserieByPrimaryTitle( @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException { Future> listFuture = akkaStreamFilesProcessing.getTvSerieByPrimaryTitle(tvseriePrimaryTitle); CompletableFuture> completableFuture = CompletableFutureResult.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.tvSerieFormat()); tvSeries.add(tvSerieJs.toString()); return null; }); return tvSeries; } @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET) private List getPersonsForTvSerie(@PathVariable(name = "tvSerieTitle") String tvSerieTitle) throws InterruptedException, ExecutionException { Future> futurePersonSeq = akkaStreamFilesProcessing.getTeamOfPersonsForTvSerie(tvSerieTitle); CompletableFuture> completableFuture = CompletableFutureResult.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 persons = new ArrayList<>(); personIndexedSeq.foreach(person -> { JsValue personJs = Json.toJson(person, person.personFormat()); persons.add(personJs.toString()); return null; }); return persons; } @RequestMapping(value = "/persons", method = RequestMethod.GET) private ResponseEntity getAllPersons() { akkaStreamFilesProcessing.getAllPersons(); return new ResponseEntity<>("is running", HttpStatus.OK); } @RequestMapping(value = "/tvseries", method = RequestMethod.GET) private ResponseEntity getAllvSeries() { akkaStreamFilesProcessing.getAllTvSeries(); return new ResponseEntity<>("is running", HttpStatus.OK); } }