Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerMain {
- public static void main(String[] args) {
- String url = "jdbc:postgresql://localhost:5432/MovieRentDb";
- String username = "postgres";
- String password = "pw12345";
- RepositoryInterface<Integer, Movie> movieDbRepo = new MovieDbRepository(new MovieValidator(), url, username, password);
- RepositoryInterface<Integer, Client> clientDbRepo = new ClientDbRepository(new ClientValidator(), url, username, password);
- RepositoryInterface<Integer, Rental> rentalDbRepo = new RentalDbRepository(new RentalValidator(), url, username, password);
- MovieController movieController = new MovieController(movieDbRepo);
- ClientController clientController = new ClientController(clientDbRepo);
- RentalController rentalController = new RentalController(rentalDbRepo,movieController,clientController);
- ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
- MyService myService = new MyServiceImpl(movieController, clientController, rentalController, executorService);
- TcpServer tcpServer = new TcpServer(executorService, MyService.SERVICE_HOST, MyService.SERVICE_PORT);
- tcpServer.addHandler(MyService.PRINT_ALL_CLIENTS, (request) -> {
- Future<String> result = myService.printAllClients();
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't print all clients.");
- });
- tcpServer.addHandler(MyService.PRINT_ALL_MOVIES, (request) -> {
- Future<String> result = myService.printAllMovies();
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't print all movies.");
- });
- tcpServer.addHandler(MyService.PRINT_ALL_RENTALS, (request) -> {
- Future<String> result = myService.printAllRentals();
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't print all rentals.");
- });
- tcpServer.addHandler(MyService.FILTER_BY_SCORE, (request) -> {
- int number = Integer.parseInt(request.body());
- Future<String> result = myService.filterByScore(number);
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't filter by score.");
- });
- tcpServer.addHandler(MyService.RENT_MOVIE, (request) -> {
- String[] twoValues = request.body().split(",");
- int firstValue = Integer.parseInt(twoValues[0]);
- int secondValue = Integer.parseInt(twoValues[1]);
- Future<String> result = myService.rentMovie(firstValue,secondValue);
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Rental operation failed.");
- });
- tcpServer.addHandler(MyService.MOST_RENTED, (request) -> {
- Future<String> result = myService.mostRented();
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't find the most rented movie.");
- });
- tcpServer.addHandler(MyService.ADD_MOVIE, (request) -> {
- String[] movieSplitted = request.body().split(",");
- String title = movieSplitted[0];
- String category = movieSplitted[1];
- String director = movieSplitted[2];
- try {
- Date launchDate = new SimpleDateFormat("dd/MM/yyyy").parse(movieSplitted[3]);
- int criticScore = Integer.parseInt(movieSplitted[4]);
- Future<String> result = myService.addMovie(new Movie(title,category,director,launchDate,criticScore));
- return new Message(Message.OK, result.get());
- } catch (ParseException | InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Adding movie failed.");
- });
- tcpServer.addHandler(MyService.REMOVE_MOVIE, (request) -> {
- int id = Integer.parseInt(request.body());
- Future<String> result = myService.removeMovie(id);
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't remove movie.");
- });
- tcpServer.addHandler(MyService.UPDATE_MOVIE, (request) -> {
- String[] movieSplitted = request.body().split(",");
- String title = movieSplitted[0];
- String category = movieSplitted[1];
- String director = movieSplitted[2];
- try {
- Date launchDate = new SimpleDateFormat("dd/MM/yyyy").parse(movieSplitted[3]);
- int criticScore = Integer.parseInt(movieSplitted[4]);
- Future<String> result = myService.updateMovie(new Movie(title,category,director,launchDate,criticScore));
- return new Message(Message.OK, result.get());
- } catch (ParseException | InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Updating movie failed.");
- });
- tcpServer.addHandler(MyService.ADD_CLIENT, (request) -> {
- String[] clientSplitted = request.body().split(",");
- String firstName = clientSplitted[0];
- String lastName = clientSplitted[1];
- String CNP = clientSplitted[2];
- try {
- Date dateOfBirth = new SimpleDateFormat("dd/MM/yyyy").parse(clientSplitted[3]);
- Future<String> result = myService.addClient(new Client(firstName,lastName,CNP,dateOfBirth));
- return new Message(Message.OK, result.get());
- } catch (ParseException | InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Adding client failed.");
- });
- tcpServer.addHandler(MyService.REMOVE_CLIENT, (request) -> {
- int id = Integer.parseInt(request.body());
- Future<String> result = myService.removeClient(id);
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't remove client.");
- });
- tcpServer.addHandler(MyService.UPDATE_CLIENT, (request) -> {
- String[] clientSplitted = request.body().split(",");
- String firstName = clientSplitted[0];
- String lastName = clientSplitted[1];
- String CNP = clientSplitted[2];
- try {
- Date dateOfBirth = new SimpleDateFormat("dd/MM/yyyy").parse(clientSplitted[3]);
- Future<String> result = myService.updateClient(new Client(firstName,lastName,CNP,dateOfBirth));
- return new Message(Message.OK, result.get());
- } catch (ParseException | InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Updating client failed.");
- });
- tcpServer.addHandler(MyService.SUM_OF_MINIMUM_AGE, (request) -> {
- Future<String> result = myService.sumOfMinimumAge();
- try {
- return new Message(Message.OK, result.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- return new Message(Message.ERROR, "Couldn't find the sum of minimum age.");
- });
- tcpServer.startServer();
- }
- }
Add Comment
Please, Sign In to add comment