Skip to content
Snippets Groups Projects
UserThread.java 5.87 KiB
Newer Older
  • Learn to ignore specific revisions
  • package com.example.schedulerapp;
    
    import java.io.*;
    import java.net.*;
    
    
    /*
        Name: UserThread
        Description: This class handles messages sent from the clients, and the required parsing to be used for in the
        Shift and Schedule classes.
    
     */
    public class UserThread extends Thread {
    
        private final Socket socket;
        private final ScheduleServer server;
    
        private PrintWriter writer;
    
        /*
        Name: UserThread
        Parameters:
            Socket socket: socket for communicating to the client.
            ScheduleServer server: The class responsible for establishing the first connection to a new client and creating
                a UserThread for it.
            DataBaseQuery dbQuery: Class responsible for connecting to the database, and containing all the methods on it.
        Description: Constructor class that gets the input stream to be read when client messages are sent.
        Return: UserThread
         */
        public UserThread(Socket socket, ScheduleServer server, DataBaseQuery dbQuery) {
    
            this.socket = socket;
            this.server = server;
    
        }
    
        public void run() {
            try {
    
                InputStream input = socket.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    
                OutputStream output = socket.getOutputStream();
                writer = new PrintWriter(output, true);
    
                String serverMessage;
                String clientMessage;
    
    
                sendMessage(serverMessage);
    
                do {
                    clientMessage = reader.readLine();
                    String[] args = clientMessage.split("/");
                    switch (args[0]) {
                        case "addEmployee":
    
                            if(args.length == 7){
                                try {
                                    String newEmployee = dbQuery.staff.addEmployee(args[1], args[2], args[3], args[4], args[5],
                                            Float.parseFloat(args[6]));
                                    if (!newEmployee.isEmpty()){
                                        server.broadcast("addEmployee/" + newEmployee);
                                    }
                                } catch (Exception exception) {
                                    exception.printStackTrace();
    
                            }
                            break;
                        case "removeEmployee":
                            try {
    
                                dbQuery.schedule.removeAllShiftsByID(employeeID);
                                if (employeeID == dbQuery.staff.removeEmployee(employeeID)) {
    
                                    server.broadcast("removeEmployee/" + employeeID);
                                }
    
                            } catch (NumberFormatException e) {
                                System.out.println("Client Message Error: " + clientMessage);
                            }
                            break;
    
                                String editedShift = dbQuery.schedule.editShift(Integer.parseInt(args[1]), args[2], args[3],
                                        args[4], args[5], Integer.parseInt(args[6]));
    
                                if (!editedShift.isEmpty()) {
                                    server.broadcast("editShift/" + editedShift);
                                }
    
                            } catch (NumberFormatException e) {
                                System.out.println("Client Message Error: " + clientMessage);
    
                        case "addShift":
                            try {
    
                                String newShift = dbQuery.schedule.addShift(Integer.parseInt(args[1]), args[2], Integer.parseInt(args[3]), Integer.parseInt(args[4]));
    
                                if (!newShift.isEmpty()){
                                    server.broadcast("addShift/" + newShift);
                                }
    
                            } catch (NumberFormatException exception) {
                                System.out.println("Error: Formatting exception while adding shift");
                            }
                            break;
                        case "removeShift":
                            try {
    
                                int shiftID = Integer.parseInt(args[1]);
    
                                if (shiftID == dbQuery.schedule.removeShiftByID(shiftID)) {
    
                                    server.broadcast("removeShift/" + shiftID);
                                }
    
                            } catch (NumberFormatException exception) {
                                System.out.println("Error: Formatting exception while removing shift");
                            }
                            break;
    
                        case "allEmployees":
                            //Returns all Employees
    
                            sendMessage(serverMessage);
                            break;
                        case "allShifts":
    
                    }
    
                } while (!clientMessage.equals("logout"));
    
                server.removeUser(this);
                socket.close();
    
    
            } catch (IOException exception) {
                System.out.println("Error in UserThread: " + exception.getMessage());
                exception.printStackTrace();
    
        /*
        Name: sendMessage
        Parameters:
            String message: message to be sent.
        Description: Sends a message to the client of this thread.
        Return: void
    
         */
        void sendMessage(String message) {
            writer.println(message);
        }
    }