Newer
Older
package com.example.schedulerapp;
import java.io.*;
import java.net.*;
/*
Rafi Zereselasie (raz070)
committed
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 {
/*
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
*/
this.client = client;
try {
InputStream input = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
Rafi Zereselasie (raz070)
committed
} 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);
Rafi Zereselasie (raz070)
committed
case "addPosition" -> addPosition(args[1], args[2]);
case "removePosition" -> removePosition(args[1], args[2]);
Rafi Zereselasie (raz070)
committed
} catch (IOException exception) {
System.out.println("Dropped connection from server: " + exception.getMessage());
Rafi Zereselasie (raz070)
committed
exception.printStackTrace();
/*
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();
}
}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
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);
}
}
}
Rafi Zereselasie (raz070)
committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
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();
}
}