package com.example.schedulerapp; import java.io.*; import java.net.*; /* Class Name: ReceiveThread Description: This class handles messages sent from the server, and the required parsing to be used for in the Model class. */ public class ReceiveThread extends Thread { private BufferedReader reader; private final Model client; /* Name: ReceiveThread Parameters: Socket socket: socket for communicating to the server. Model client: class that contains the employee and shift hashmaps. Description: Constructor class that gets the input stream to be read when server messages are sent. Return: ReceiveThread */ public ReceiveThread(Socket socket, Model client) { this.client = client; try { InputStream input = socket.getInputStream(); reader = new BufferedReader(new InputStreamReader(input)); } catch (IOException exception) { System.out.println("Error getting input stream: " + exception.getMessage()); exception.printStackTrace(); } } /* Name: Run Parameters: none Description: Responsible for handling and parsing messages from the server. Return: void */ public void run() { while(true) { try { String response = reader.readLine(); // Waits for messages from the server. String[] args = response.split("/"); switch (args[0]) { case "allEmployees" -> allEmployees(args); case "allShifts" -> allShifts(args); case "addEmployee" -> addEmployee(args[1]); case "removeEmployee" -> removeEmployee(args[1]); case "addShift" -> addShift(args[1]); case "removeShift" -> removeShiftByID(args[1]); case "editShift" -> editShift(args[1]); // fix case "allAvailabilities" -> allAvailability(args); case "editAvailability" -> editAvailability(args[1]); case "addTimeOff" -> addTimeOff(args[1]); case "removeTimeOff" -> removeTimeOff(args[1]); case "setTimeOffApproval" -> setTimeOffApproval(args[1]); case "allTimeOff" -> allTimeOff(args); case "addPosition" -> addPosition(args[1], args[2]); case "removePosition" -> removePosition(args[1], args[2]); } } catch (IOException exception) { System.out.println("Dropped connection from server: " + exception.getMessage()); exception.printStackTrace(); break; } } } /* Name: allEmployees Parameters: String[] allEmployees: All employee information sent from server. Employee fields are is separated by a '.' delimiter. Description: Separates all employees and adds them to the local employee HashMap by their ID. Return: void */ private void allEmployees(String[] allEmployees) { for (String employee : allEmployees) { if (!employee.equals("allEmployees")){ addEmployee(employee); } } } /* Name: addEmployee Parameters: String employeeData: All the information for a given employee separated by '.' Description: Adds an employee to the local employee HashMap by their ID. Return: void */ private void addEmployee(String employeeData) { try { String[] dataSplit = employeeData.split(","); this.client.employees.put(Integer.parseInt(dataSplit[0]), new Employee(employeeData)); } catch (Exception exception) { exception.printStackTrace(); } } /* Name: RemoveEmployee Parameters: String employeeID: The employeeID of the employee. Description: Removes an employee from the employee HashMap by there employee ID. Return: void */ private void removeEmployee(String employeeID) { try { int intID = Integer.parseInt(employeeID); this.client.employees.remove(intID); for (Shift shift : this.client.shifts.values()) { if (shift.getEmployeeID() == intID) { this.client.shifts.remove(shift.getShiftID()); } } } catch (Exception exception) { exception.printStackTrace(); } } /* Name: allShifts Parameters: String[] allShifts: All the shifts to be added locally from the server in the form String[]. Description: Adds all the shifts to the local shift hashmap by the shift id. Return: void */ private void allShifts(String[] allShifts) { for (String shift : allShifts) { if (!shift.equals("allShifts")){ addShift(shift); } } } /* Name: addShift Parameters: String shiftData: a string containing shift data from the server Description: Adds a shift to the local shift Hashmap by shift ID. Return: void */ private void addShift(String shiftData) { try { String[] dataSplit = shiftData.split(","); this.client.shifts.put(Integer.parseInt(dataSplit[4]), new Shift(shiftData)); } catch (Exception exception) { exception.printStackTrace(); } } /* Name: removeShiftByID Parameters: String id: The shift id of an existing shift. Description: Removes a shift from the local shift HashMap. Return: void */ private void removeShiftByID(String id) { try { this.client.shifts.remove(Integer.parseInt(id)); } catch (Exception exception) { exception.printStackTrace(); } } /* Name: editShift Parameters: String editedShift: The information of the editedShift from the server. Description: Edits an existing shift. Return: void */ private void editShift(String editedShift) { String[] shiftSplit = editedShift.split("\\."); try { Integer.parseInt(shiftSplit[4]); removeShiftByID(shiftSplit[4]); addShift(editedShift); } catch (Exception exception) { exception.printStackTrace(); } } /* Name: allAvailabilities Parameters: String[] allAvailabilities: Description: Return: void */ private void allAvailability(String[] allAvailabilities) { for (String availability : allAvailabilities) { if (!availability.equals("allAvailabilities")) { String[] availabilitySplit = availability.split(","); client.employees.get(Integer.parseInt(availabilitySplit[0])).populateAvailability(availabilitySplit); } } } /* Name: editAvailabilities Parameters: String editAvailabilities: Description: Return: void */ private void editAvailability(String editAvailability) { String[] editSplit = editAvailability.split(","); int employeeID = Integer.parseInt(editSplit[0]); int dayOfTheWeek = Integer.parseInt(editSplit[1]); int startTime = Integer.parseInt(editSplit[2]); int endTime = Integer.parseInt(editSplit[3]); client.getEmployee(Integer.parseInt(editSplit[0])).updateAvailability(dayOfTheWeek, startTime, endTime); } /* Name: addTimeOff Parameters: String timeOffData: Description: Return: void */ private void addTimeOff(String timeOffData) { String[] timeOffSplit = timeOffData.split(","); int timeOffID = Integer.parseInt(timeOffSplit[0]); client.timeOff.put(timeOffID, new TimeOff(timeOffSplit)); client.employees.get(Integer.parseInt(timeOffSplit[1])).addTimeOffID(timeOffID); } /* Name: removeTimeOff Parameters: String timeOffID: Description: Return: void */ private void removeTimeOff(String timeOffID) { try { int id = Integer.parseInt(timeOffID); int employeeID = this.client.timeOff.get(id).getEmployeeID(); client.getEmployee(employeeID).removeTimeOffID(id); client.timeOff.remove(id); } catch (Exception exception) { exception.printStackTrace(); } } /* Name: setTimeOffApproval Parameters: String approvalData: Description: Return: void */ private void setTimeOffApproval(String approvalData) { try { String[] dataSplit = approvalData.split(","); int id = Integer.parseInt(dataSplit[0]); this.client.timeOff.get(id).setApproval(Boolean.getBoolean(dataSplit[1])); } catch (Exception exception) { exception.printStackTrace(); } } /* Name: allTimeOff Parameters: String[] allTimeOff: Description: Return: void */ private void allTimeOff(String[] allTimeOff) { for (String timeOff : allTimeOff) { if (!timeOff.equals("allTimeOff")) { addTimeOff(timeOff); } } } /* Name: addPosition Parameters: String employeeID: String position: Description: Return: void */ private void addPosition(String employeeID, String position) { try { int id = Integer.parseInt(employeeID); this.client.getEmployee(id).addPosition(position); } catch (Exception exception) { exception.printStackTrace(); } } /* Name: removePosition Parameters: String employeeID: String position: Description: Return: void */ private void removePosition(String employeeID, String position) { try { int id = Integer.parseInt(employeeID); this.client.getEmployee(id).removePosition(position); } catch (Exception exception) { exception.printStackTrace(); } } }