package hhn.temp.project; import hhn.temp.project.provider.SimpleDatabaseManager; import java.sql.SQLException; import java.util.*; public class AssignmentManager { private Map workerMap; private Map taskMap; private int workerIdCounter; private int taskIdCounter; private UserCommands userInterface; private SimpleDatabaseManager database; public AssignmentManager() { database = new SimpleDatabaseManager(); connect(); workerMap = new HashMap<>(); taskMap = new HashMap<>(); workerIdCounter = 1000; taskIdCounter = 0; sync(); userInterface = new UserCommands(this); } private void connect() { try { database.connect(); } catch (SQLException e) { System.err.println("Failed to connect to database"); } } private void sync() { workerMap.clear(); taskMap.clear(); try { Collection workers = database.getWorkers(); if (workers != null) { for (Worker worker : workers) { workerMap.put(worker.getId(),worker); if (worker.getId() > workerIdCounter) { workerIdCounter = worker.getId(); } } } Collection tasks = database.getTasks(); if (tasks != null) { for (Task task : tasks) { taskMap.put(task.getTaskId(),task); if (task.getTaskId() > taskIdCounter) { taskIdCounter = task.getTaskId(); } } } } catch (SQLException e) { System.err.println("Failed to sync"); } } public Map getTaskMap() { return taskMap; } public Map getWorkerMap() {return workerMap;} public int createWorker(String name) { sync(); Worker worker = new Worker(name, ++workerIdCounter); workerMap.put(workerIdCounter, worker); try { database.saveWorker(worker); } catch (SQLException e) { System.err.println("Failed to save worker"); } return workerIdCounter; } public void removeWorker(int workerId) { if (!workerMap.containsKey(workerId)) { throw new IllegalArgumentException("WorkerId must exist in order to remove it"); } workerMap.remove(workerId); try { database.deleteWorker(workerId); } catch (SQLException e) { System.err.println("Failed to delete worker"); } } public int addTask(int workerId, String name, String description) { if (!workerMap.containsKey(workerId) || name == null || description == null) { throw new IllegalArgumentException("WorkerId must exist and name or description can't be null"); } Task task = new Task(++taskIdCounter, workerId, name, description); taskMap.put(taskIdCounter, task); try { database.saveTask(task); } catch (SQLException e) { System.err.println("Failed to save task"); } return taskIdCounter; } public Task getTask(int taskId) { if (!taskMap.containsKey(taskId)) { throw new IllegalArgumentException("Task Id does not exist"); } return taskMap.get(taskId); } public Worker getWorker(int workerId) { if (!workerMap.containsKey(workerId)) { throw new IllegalArgumentException("Worker Id does not exist"); } return workerMap.get(workerId); } public void editTask(int workerId, int taskId, String name, String description) { if (!workerMap.containsKey(workerId) || !taskMap.containsKey(taskId)) { throw new IllegalArgumentException("Task Id or Worker Id does not exist"); } Task task = taskMap.get(taskId); task.setName(name); task.setDescription(description); try { database.updateTask(task.getTaskId(),task); } catch (SQLException e) { System.err.println("Failed to update Task"); } } public void removeTask(int taskId) { if (!taskMap.containsKey(taskId)) { throw new IllegalArgumentException("Task Id does not exist"); } taskMap.remove(taskId); try { database.deleteTask(taskId); } catch (SQLException e) { System.err.println("Failed to remove task"); } } public void finishTask(int workerId, int taskId) { if (!workerMap.containsKey(workerId) || !taskMap.containsKey(taskId)) { throw new IllegalArgumentException("Task Id or Worker Id does not exist"); } Task task = taskMap.get(taskId); task.setTaskState(TaskState.FINISHED); try { database.updateTask(taskId,task); } catch (SQLException e) { System.err.println("Failed to finish task"); } } public void unfinishTask(int workerId, int taskId) { if (!workerMap.containsKey(workerId) || !taskMap.containsKey(taskId)) { throw new IllegalArgumentException("Task Id or Worker Id does not exist"); } Task task = taskMap.get(taskId); task.setTaskState(TaskState.IN_PROGRESS); try { database.updateTask(taskId,task); } catch (SQLException e) { System.err.println("Failed to finish task"); } } public UserCommands getUserCommands() { return userInterface; } public void startCLI() { userInterface.start(); } public void stopCLI() { userInterface.stop(); } }