| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package fr.natan.akkastreamfileprocessingapi.controller;
-
- import fr.natan.akkastreamfileprocessingapi.futurecompleteness.CompletableFutureBuilder;
- import fr.natan.akkastreamfileprocessingapi.models.Models;
- 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.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 = 500, BORN_SUP = 5000;
-
- 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<Models.Person> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
- CompletableFuture<Models.Person> completableFuture = CompletableFutureBuilder
- .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();
- JsValue personJs = Json.toJson(person, person.personFormat());
- return Json.prettyPrint(personJs);
- }
-
- @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
- private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
- throws ExecutionException, InterruptedException {
-
- Future<Models.Person> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
- CompletableFuture<Models.Person> personCompletableFuture = CompletableFutureBuilder
- .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();
- JsValue personJs = Json.toJson(person, person.personFormat());
- return Json.prettyPrint(personJs);
- }
-
- @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
- private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
- throws ExecutionException, InterruptedException {
- Future<Models.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
- CompletableFuture<Models.TvSerie> tvSerieCompletableFuture = CompletableFutureBuilder
- .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();
- JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
- return Json.prettyPrint(tvSerieJs);
- }
-
- @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
- private List<String> getTvserieByPrimaryTitle(
- @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
-
- Future<IndexedSeq<Models.TvSerie>> listFuture = akkaStreamFilesProcessing
- .getTvSeriesByPrimaryTitleFuture(tvseriePrimaryTitle);
- CompletableFuture<IndexedSeq<Models.TvSerie>> completableFuture =
- CompletableFutureBuilder.buildcompletableFuture2(listFuture);
- while (!completableFuture.isDone()) {
- logger.info("IS PROCESSING...");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
- IndexedSeq<Models.TvSerie> tvSerieList = completableFuture.get();
- List<String> tvSeries = new ArrayList<>();
- tvSerieList.foreach(tvSerie -> {
- JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
- tvSeries.add(Json.prettyPrint(tvSerieJs));
- return null;
- });
-
- return tvSeries;
- }
-
- @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET)
- private List<String> getPersonsForTvSerieByTvSerieTitle(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
- throws InterruptedException, ExecutionException {
-
- Future<IndexedSeq<Models.Person>> futurePersonSeq = akkaStreamFilesProcessing
- .getPersonsForTvSerieByTvSerieTitleFuture(tvSerieTitle);
- CompletableFuture<IndexedSeq<Models.Person>> completableFuture = CompletableFutureBuilder
- .buildcompletableFuture2(futurePersonSeq);
- while (!completableFuture.isDone()){
- logger.info("IS PROCESSING...");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
-
- IndexedSeq<Models.Person> personIndexedSeq = completableFuture.get();
- List<String> personList = new ArrayList<>();
- personIndexedSeq.foreach(person -> {
- JsValue personJs = Json.toJson(person, person.personFormat());
- personList.add(Json.prettyPrint(personJs));
- return null;
- });
-
- return personList;
- }
-
- @RequestMapping(value = "/persons/tvseries/id/{tvSerieID}", method = RequestMethod.GET)
- private List<String> getPersonsForTvSerieByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID)
- throws InterruptedException, ExecutionException {
-
- Future<IndexedSeq<Models.Person>> futureIndexedSeqPersons = akkaStreamFilesProcessing
- .getPersonsForTvSerieByTvSerieIDFuture(tvSerieID);
- CompletableFuture<IndexedSeq<Models.Person>> seqCompletableFuture = CompletableFutureBuilder
- .buildcompletableFuture2(futureIndexedSeqPersons);
-
- while (!seqCompletableFuture.isDone()){
- logger.info("IS PROCESSING");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
- IndexedSeq<Models.Person> personIndexedSeq = seqCompletableFuture.get();
- List<String> personsList = new ArrayList<>();
- personIndexedSeq.foreach(person ->{
- JsValue personJs = Json.toJson(person, person.personFormat());
- personsList.add(Json.prettyPrint(personJs));
- return null;
- });
-
- return personsList;
- }
-
- @RequestMapping(value = "/persons", method = RequestMethod.GET)
- private ResponseEntity<String> getAllPersons() {
- akkaStreamFilesProcessing.getAllPersonsFuture();
- return new ResponseEntity<>("is running", HttpStatus.OK);
- }
-
- @RequestMapping(value = "/tvseries", method = RequestMethod.GET)
- private ResponseEntity<String> getAllvSeries() {
- akkaStreamFilesProcessing.getAllTvSeriesFuture();
- return new ResponseEntity<>("is running", HttpStatus.OK);
- }
- }
|