Guest User

Untitled

a guest
Mar 27th, 2017
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.69 KB | None | 0 0
  1. public class ServerMain {
  2.     public static void main(String[] args) {
  3.         String url = "jdbc:postgresql://localhost:5432/MovieRentDb";
  4.         String username = "postgres";
  5.         String password = "pw12345";
  6.  
  7.         RepositoryInterface<Integer, Movie> movieDbRepo = new MovieDbRepository(new MovieValidator(), url, username, password);
  8.         RepositoryInterface<Integer, Client> clientDbRepo = new ClientDbRepository(new ClientValidator(), url, username, password);
  9.         RepositoryInterface<Integer, Rental> rentalDbRepo = new RentalDbRepository(new RentalValidator(), url, username, password);
  10.  
  11.         MovieController movieController = new MovieController(movieDbRepo);
  12.         ClientController clientController = new ClientController(clientDbRepo);
  13.         RentalController rentalController = new RentalController(rentalDbRepo,movieController,clientController);
  14.  
  15.         ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  16.         MyService myService = new MyServiceImpl(movieController, clientController, rentalController, executorService);
  17.         TcpServer tcpServer = new TcpServer(executorService, MyService.SERVICE_HOST, MyService.SERVICE_PORT);
  18.  
  19.         tcpServer.addHandler(MyService.PRINT_ALL_CLIENTS, (request) -> {
  20.             Future<String> result = myService.printAllClients();
  21.             try {
  22.                 return new Message(Message.OK, result.get());
  23.             } catch (InterruptedException | ExecutionException e) {
  24.                 e.printStackTrace();
  25.             }
  26.             return new Message(Message.ERROR, "Couldn't print all clients.");
  27.         });
  28.         tcpServer.addHandler(MyService.PRINT_ALL_MOVIES, (request) -> {
  29.             Future<String> result = myService.printAllMovies();
  30.             try {
  31.                 return new Message(Message.OK, result.get());
  32.             } catch (InterruptedException | ExecutionException e) {
  33.                 e.printStackTrace();
  34.             }
  35.             return new Message(Message.ERROR, "Couldn't print all movies.");
  36.         });
  37.         tcpServer.addHandler(MyService.PRINT_ALL_RENTALS, (request) -> {
  38.             Future<String> result = myService.printAllRentals();
  39.             try {
  40.                 return new Message(Message.OK, result.get());
  41.             } catch (InterruptedException | ExecutionException e) {
  42.                 e.printStackTrace();
  43.             }
  44.             return new Message(Message.ERROR, "Couldn't print all rentals.");
  45.         });
  46.         tcpServer.addHandler(MyService.FILTER_BY_SCORE, (request) -> {
  47.             int number = Integer.parseInt(request.body());
  48.             Future<String> result = myService.filterByScore(number);
  49.             try {
  50.                 return new Message(Message.OK, result.get());
  51.             } catch (InterruptedException | ExecutionException e) {
  52.                 e.printStackTrace();
  53.             }
  54.             return new Message(Message.ERROR, "Couldn't filter by score.");
  55.         });
  56.         tcpServer.addHandler(MyService.RENT_MOVIE, (request) -> {
  57.             String[] twoValues = request.body().split(",");
  58.             int firstValue = Integer.parseInt(twoValues[0]);
  59.             int secondValue = Integer.parseInt(twoValues[1]);
  60.             Future<String> result = myService.rentMovie(firstValue,secondValue);
  61.             try {
  62.                 return new Message(Message.OK, result.get());
  63.             } catch (InterruptedException | ExecutionException e) {
  64.                 e.printStackTrace();
  65.             }
  66.             return new Message(Message.ERROR, "Rental operation failed.");
  67.         });
  68.         tcpServer.addHandler(MyService.MOST_RENTED, (request) -> {
  69.             Future<String> result = myService.mostRented();
  70.             try {
  71.                 return new Message(Message.OK, result.get());
  72.             } catch (InterruptedException | ExecutionException e) {
  73.                 e.printStackTrace();
  74.             }
  75.             return new Message(Message.ERROR, "Couldn't find the most rented movie.");
  76.         });
  77.         tcpServer.addHandler(MyService.ADD_MOVIE, (request) -> {
  78.             String[] movieSplitted = request.body().split(",");
  79.             String title = movieSplitted[0];
  80.             String category = movieSplitted[1];
  81.             String director = movieSplitted[2];
  82.             try {
  83.                 Date launchDate = new SimpleDateFormat("dd/MM/yyyy").parse(movieSplitted[3]);
  84.                 int criticScore = Integer.parseInt(movieSplitted[4]);
  85.                 Future<String> result = myService.addMovie(new Movie(title,category,director,launchDate,criticScore));
  86.                 return new Message(Message.OK, result.get());
  87.             } catch (ParseException | InterruptedException | ExecutionException e) {
  88.                 e.printStackTrace();
  89.             }
  90.             return new Message(Message.ERROR, "Adding movie failed.");
  91.         });
  92.         tcpServer.addHandler(MyService.REMOVE_MOVIE, (request) -> {
  93.             int id = Integer.parseInt(request.body());
  94.             Future<String> result = myService.removeMovie(id);
  95.             try {
  96.                 return new Message(Message.OK, result.get());
  97.             } catch (InterruptedException | ExecutionException e) {
  98.                 e.printStackTrace();
  99.             }
  100.             return new Message(Message.ERROR, "Couldn't remove movie.");
  101.         });
  102.         tcpServer.addHandler(MyService.UPDATE_MOVIE, (request) -> {
  103.             String[] movieSplitted = request.body().split(",");
  104.             String title = movieSplitted[0];
  105.             String category = movieSplitted[1];
  106.             String director = movieSplitted[2];
  107.             try {
  108.                 Date launchDate = new SimpleDateFormat("dd/MM/yyyy").parse(movieSplitted[3]);
  109.                 int criticScore = Integer.parseInt(movieSplitted[4]);
  110.                 Future<String> result = myService.updateMovie(new Movie(title,category,director,launchDate,criticScore));
  111.                 return new Message(Message.OK, result.get());
  112.             } catch (ParseException | InterruptedException | ExecutionException e) {
  113.                 e.printStackTrace();
  114.             }
  115.             return new Message(Message.ERROR, "Updating movie failed.");
  116.         });
  117.         tcpServer.addHandler(MyService.ADD_CLIENT, (request) -> {
  118.             String[] clientSplitted = request.body().split(",");
  119.             String firstName = clientSplitted[0];
  120.             String lastName = clientSplitted[1];
  121.             String CNP = clientSplitted[2];
  122.             try {
  123.                 Date dateOfBirth = new SimpleDateFormat("dd/MM/yyyy").parse(clientSplitted[3]);
  124.                 Future<String> result = myService.addClient(new Client(firstName,lastName,CNP,dateOfBirth));
  125.                 return new Message(Message.OK, result.get());
  126.             } catch (ParseException | InterruptedException | ExecutionException e) {
  127.                 e.printStackTrace();
  128.             }
  129.             return new Message(Message.ERROR, "Adding client failed.");
  130.         });
  131.         tcpServer.addHandler(MyService.REMOVE_CLIENT, (request) -> {
  132.             int id = Integer.parseInt(request.body());
  133.             Future<String> result = myService.removeClient(id);
  134.             try {
  135.                 return new Message(Message.OK, result.get());
  136.             } catch (InterruptedException | ExecutionException e) {
  137.                 e.printStackTrace();
  138.             }
  139.             return new Message(Message.ERROR, "Couldn't remove client.");
  140.         });
  141.         tcpServer.addHandler(MyService.UPDATE_CLIENT, (request) -> {
  142.             String[] clientSplitted = request.body().split(",");
  143.             String firstName = clientSplitted[0];
  144.             String lastName = clientSplitted[1];
  145.             String CNP = clientSplitted[2];
  146.             try {
  147.                 Date dateOfBirth = new SimpleDateFormat("dd/MM/yyyy").parse(clientSplitted[3]);
  148.                 Future<String> result = myService.updateClient(new Client(firstName,lastName,CNP,dateOfBirth));
  149.                 return new Message(Message.OK, result.get());
  150.             } catch (ParseException | InterruptedException | ExecutionException e) {
  151.                 e.printStackTrace();
  152.             }
  153.             return new Message(Message.ERROR, "Updating client failed.");
  154.         });
  155.         tcpServer.addHandler(MyService.SUM_OF_MINIMUM_AGE, (request) -> {
  156.             Future<String> result = myService.sumOfMinimumAge();
  157.             try {
  158.                 return new Message(Message.OK, result.get());
  159.             } catch (InterruptedException | ExecutionException e) {
  160.                 e.printStackTrace();
  161.             }
  162.             return new Message(Message.ERROR, "Couldn't find the sum of minimum age.");
  163.         });
  164.         tcpServer.startServer();
  165.     }
  166. }
Add Comment
Please, Sign In to add comment