Merge pull request 'database' (#3) from database into main

Reviewed-on: #3
This commit is contained in:
2025-12-08 18:10:07 +01:00
14 changed files with 399 additions and 75 deletions

View File

@@ -27,7 +27,7 @@ public class AssignmentManager {
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, this);
Task task = new Task(++taskIdCounter, workerId, name, description);
taskMap.put(taskIdCounter, task);
return taskIdCounter;
}

View File

@@ -5,10 +5,8 @@ public class Task {
String description;
int taskId;
int workerId;
AssignmentManager manager;
TaskState state;
public Task(int taskId, int workerId, String name, String description, AssignmentManager manager) {
this.manager = manager;
public Task(int taskId, int workerId, String name, String description) {
this.name = name;
this.description = description;
this.taskId = taskId;

View File

@@ -4,6 +4,26 @@ import java.io.IOException;
import java.nio.file.Path;
import java.sql.SQLException;
/**
* A database interface with basic database connectivity
* @author Riley Schneider
*/
public interface Database {
public void connect() throws SQLException, IOException;
/**
* Creates a connection to the database
* @throws SQLException Thrown if database error occurs
*/
void connect() throws SQLException;
/**
* Closes a connection to the database
* @throws SQLException Thrown if database error occurs
*/
void close() throws SQLException;
/**
* Clears the entire tables in the database and reset primary key counter to each table
* @throws SQLException Thrown if database error occurs
*/
void clearDatabase() throws SQLException;
}

View File

@@ -1,10 +1,115 @@
package hhn.temp.project.provider;
import hhn.temp.project.Task;
import hhn.temp.project.Worker;
import java.sql.SQLException;
import java.util.Collection;
public interface DatabaseManager<E> extends Database {
public void saveObjects(Collection<E> objects);
public Collection<E> getObjects();
public void saveObject(E object);
public E getObject(int id);
/**
* Simple Database Manager to handle simple command such as insertion, deletion, updating of Task and Worker objects.
* @author Riley Schneider
*/
public interface DatabaseManager extends Database {
/**
* Saves a Task object into the database
* @param task Task to be saved into the database
* @throws SQLException Thrown if database error occurs
*/
void saveTask(Task task) throws SQLException;
/**
* Saves a collection of Task objects into the database
* @param tasks Tasks to be saved into the database
* @throws SQLException Thrown if database error occurs
*/
void saveTasks(Collection<Task> tasks) throws SQLException;
/**
* Saves a Worker object into the database
* @param worker Worker to be saved into the database
* @throws SQLException Thrown if database error occurs
*/
void saveWorker(Worker worker) throws SQLException;
/**
* Saves a collection Worker objects into the database
* @param workers Workers to be saved into the database
* @throws SQLException Thrown if database error occurs
*/
void saveWorkers(Collection<Worker> workers) throws SQLException;
/**
* Updates a Task object that already exists in the database
* @param taskId The ID of the given task
* @param newTaskObject The new Task object that will overwrite the existent task on database
* @throws SQLException Thrown if database error occurs
*/
void updateTask(int taskId, Task newTaskObject) throws SQLException;
/**
* Updates a Worker object that already exists in the database
* @param workerId The ID of the given worker
* @param newWorkerObject The new Worker object that will overwrite the existent task on database
* @throws SQLException Thrown if database error occurs
*/
void updateWorker(int workerId, Worker newWorkerObject) throws SQLException;
/**
* Deletes a task from the database
* @param taskId The ID of the given task that needs to be deleted
* @throws SQLException Thrown if database error occurs
*/
void deleteTask(int taskId) throws SQLException;
/**
* Deletes a worker from the database
* @param workerId The ID of the given worker that needs to be deleted
* @throws SQLException Thrown if database error occurs
*/
void deleteWorker(int workerId) throws SQLException;
/**
* Gets a collection of all tasks available in the database
* @return A collection of tasks (empty if no task available)
* @throws SQLException Thrown if database error occurs
*/
Collection<Task> getTasks() throws SQLException;
/**
* Gets a collection of all workers available in the database
* @return A collection of workers (empty if no task available)
* @throws SQLException Thrown if database error occurs
*/
Collection<Worker> getWorkers() throws SQLException;
/**
* Gets a task given by its ID from the database
* @param taskId The task ID
* @return A task from the database (NULL if no task found)
* @throws SQLException Thrown if database error occurs
*/
Task getTaskByTaskId(int taskId) throws SQLException;
/**
* Gets a worker given by its ID from the database
* @param workerId The worker ID
* @return A worker from the database (NULL if no worker found)
* @throws SQLException Thrown if database error occurs
*/
Worker getWorkerByWorkerId(int workerId) throws SQLException;
/**
* Gets the total number of available tasks in the database.
* @return The total number of tasks (0 = zero entries; -1 = error)
* @throws SQLException Thrown if database error occurs
*/
int getTotalNumberOfTasks() throws SQLException;
/**
* Gets the total number of available workers in the database.
* @return The total number of workers (0 = zero entries; -1 = error)
* @throws SQLException Thrown if database error occurs
*/
int getTotalNumberOfWorkers() throws SQLException;
}

View File

@@ -1,41 +1,193 @@
package hhn.temp.project.provider;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import hhn.temp.project.Task;
import hhn.temp.project.Worker;
public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
import java.sql.*;
import java.util.*;
private Connection connection;
public class SimpleDatabaseManager implements DatabaseManager {
private boolean connected = false;
private Map<Integer, Task> temporaryTaskList = new HashMap();
private Map<Integer, Worker> temporaryWorkerList = new HashMap<>();
@Override
public void saveObjects(Collection<E> objects) {
public void saveTask(Task task) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (!temporaryTaskList.containsKey(task.getTaskId())) {
temporaryTaskList.put(task.getTaskId(), task);
} else {
throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists.");
}
}
@Override
public Collection<E> getObjects() {
return List.of();
public void saveTasks(Collection<Task> tasks) throws SQLException {
for (Task task : tasks) {
saveTask(task);
}
}
@Override
public void saveObject(E object) {
public void saveWorker(Worker worker) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (!temporaryWorkerList.containsKey(worker.getId())) {
temporaryWorkerList.put(worker.getId(), worker);
} else {
throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists.");
}
}
@Override
public E getObject(int id) {
return null;
public void saveWorkers(Collection<Worker> workers) throws SQLException {
for (Worker worker : workers) {
saveWorker(worker);
}
}
@Override
public void connect() throws SQLException, IOException {
connection = DriverManager.getConnection("jdbc:mysql://sql7.freesqldatabase.com/sql7810540?user=sql7810540&password=mXdJCFtDZz");
public void updateTask(int taskId, Task newTaskObject) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryTaskList.containsKey(taskId) && newTaskObject.getTaskId() == taskId) {
temporaryTaskList.remove(taskId);
temporaryTaskList.put(taskId, newTaskObject);
return;
}
throw new SQLException("[UPDATE FAILED] Task ID not found OR Task ID does not equal New Task ID.");
}
@Override
public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryWorkerList.containsKey(workerId) && newWorkerObject.getId() == workerId) {
temporaryWorkerList.remove(workerId);
temporaryWorkerList.put(workerId, newWorkerObject);
return;
}
throw new SQLException("[UPDATE FAILED] Worker ID not found OR Worker ID does not equal New Worker ID.");
}
@Override
public void deleteTask(int taskId) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryTaskList.containsKey(taskId)) {
temporaryTaskList.remove(taskId);
return;
}
throw new SQLException("[DELETION FAILED] Could not find Task ID. Nothing to delete.");
}
@Override
public void deleteWorker(int workerId) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryWorkerList.containsKey(workerId)) {
temporaryWorkerList.remove(workerId);
return;
}
throw new SQLException("[DELETION FAILED] Could not find Worker ID. Nothing to delete.");
}
@Override
public Collection<Task> getTasks() throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryTaskList.values();
}
@Override
public Collection<Worker> getWorkers() throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryWorkerList.values();
}
@Override
public Task getTaskByTaskId(int taskId) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryTaskList.containsKey(taskId)) {
return temporaryTaskList.get(taskId);
}
throw new SQLException("[SELECTION FAILED] Could not find Task ID. Nothing to get.");
}
@Override
public Worker getWorkerByWorkerId(int workerId) throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryWorkerList.containsKey(workerId)) {
return temporaryWorkerList.get(workerId);
}
throw new SQLException("[SELECTION FAILED] Could not find Worker ID. Nothing to get.");
}
@Override
public int getTotalNumberOfTasks() throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryTaskList.size();
}
@Override
public int getTotalNumberOfWorkers() throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryWorkerList.size();
}
public void clearDatabase() throws SQLException {
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
temporaryTaskList.clear();
temporaryWorkerList.clear();
}
@Override
public void connect() throws SQLException {
connected = true;
}
@Override
public void close() throws SQLException {
connected = false;
}
}

View File

@@ -0,0 +1,8 @@
CREATE TABLE Task (
id INT AUTO_INCREMENT PRIMARY KEY,
taskid INT,
name VARCHAR(255) NOT NULL,
description TEXT,
workerid INT,
taskstate INT
);

View File

@@ -0,0 +1 @@
DELETE FROM Task WHERE taskid = ?

View File

@@ -0,0 +1 @@
INSERT INTO Task (taskid, name, description, workerid, taskstate) VALUES (?, ?, ?, ?, ?)

View File

@@ -0,0 +1 @@
SELECT * FROM Task WHERE workerid = ?

View File

@@ -0,0 +1 @@
UPDATE Task SET name = ?, description = ?, taskstate = ? WHERE taskid = ?

View File

@@ -6,34 +6,41 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.*;
public class DatabaseBadCasesTest {
private DatabaseManager<DatabaseGoodCasesTest.TestClass> databaseManager;
public class TestClass {
private int id;
private String dataString;
private int dataInteger;
public TestClass(int id, String dataString, int dataInteger) {
this.id = id;
this.dataString = dataString;
this.dataInteger = dataInteger;
}
}
private DatabaseManager databaseManager;
@BeforeEach
public void setup() {
databaseManager = new SimpleDatabaseManager<>();
public void setup() throws SQLException {
databaseManager = new SimpleDatabaseManager();
databaseManager.connect();
databaseManager.clearDatabase();
}
@Test
@DisplayName("Assert connection failed")
public void assertConnectionFailed() {
assertThrows(SQLException.class, () -> databaseManager.connect());
@DisplayName("Inserting Tasks with the same ID")
public void insertTasksWithTheSameId() throws SQLException {
Task task = new Task(10, 10, "ABC", "XYZ");
Task taskFaker = new Task(10, 5, "ABC!", "XYZ!");
databaseManager.saveTask(task);
databaseManager.saveTask(taskFaker);
}
@Test
@DisplayName("Inserting Workers with the same ID")
public void insertWorkersWithTheSameId() throws SQLException {
Worker worker = new Worker("Worker-1", 1);
Worker workerFaker = new Worker("Worker-100", 1);
databaseManager.saveWorker(worker);
databaseManager.saveWorker(workerFaker);
}
}

View File

@@ -6,51 +6,81 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class DatabaseGoodCasesTest {
private DatabaseManager<DatabaseGoodCasesTest.TestClass> databaseManager;
public class TestClass {
private int id;
private String dataString;
private int dataInteger;
public TestClass(int id, String dataString, int dataInteger) {
this.id = id;
this.dataString = dataString;
this.dataInteger = dataInteger;
}
}
private DatabaseManager databaseManager;
@BeforeEach
public void setup() {
databaseManager = new SimpleDatabaseManager<>();
public void setup() throws SQLException {
databaseManager = new SimpleDatabaseManager();
databaseManager.connect();
databaseManager.clearDatabase();
}
@Test
@DisplayName("Assert that the TestClass could be inserted into the database")
public void assertGetTestClass() throws SQLException, IOException {
TestClass testClass = new TestClass(1, "Hello World", 123);
@DisplayName("Inserting test task into the database and receiving back the same test task")
public void insertTaskAndReceiveItBackTest() throws SQLException {
Task task = new Task(123, 10, "Hello World", "Doing Something, hell yeah!");
databaseManager.connect();
databaseManager.getObject(1);
databaseManager.saveTask(task);
Task reTask = databaseManager.getTaskByTaskId(task.getTaskId());
assertNotNull(reTask);
assertEquals(task.getTaskId(), reTask.getTaskId());
assertEquals(task.getDescription(), reTask.getDescription());
assertEquals(task.getName(), reTask.getName());
assertEquals(task.getWorkerId(), reTask.getWorkerId());
assertEquals(1, databaseManager.getTotalNumberOfTasks());
}
@Test
@DisplayName("Assert that the TestClass could be inserted into the database")
public void assertInsertTestClass() throws SQLException, IOException {
TestClass testClass = new TestClass(1, "Hello World", 123);
@DisplayName("Inserting test worker into the database and receiving back the same test worker")
public void insertWorkerAndReceivedItBackTest() throws SQLException {
Worker worker = new Worker("Worker-01", 12345678);
databaseManager.connect();
databaseManager.saveObject(testClass);
databaseManager.saveWorker(worker);
Worker reWorker = databaseManager.getWorkerByWorkerId(worker.getId());
assertNotNull(reWorker);
assertEquals(worker.getId(), reWorker.getId());
assertEquals(worker.getName(), reWorker.getName());
}
@Test
@DisplayName("Assert connecting to database")
public void assertConnectToDatabase() throws SQLException, IOException {
databaseManager.connect();
@DisplayName("Clearing the database (Task and Worker) test")
public void clearDatabaseTest() throws SQLException, InterruptedException {
Task task1 = new Task(10, 5, "Hello", "World");
Task task2 = new Task(9, 4, "Hochschule", "Heilbronn");
Task task3 = new Task(8, 9, "Gangnam", "Style");
Collection<Task> tasks = List.of(task1, task2, task3);
Worker worker1 = new Worker("ABC", 2);
Worker worker2 = new Worker("XYZ", 9);
Collection<Worker> workers = List.of(worker1, worker2);
databaseManager.saveTasks(tasks);
databaseManager.saveWorkers(workers);
Thread.sleep(500);
assertEquals(tasks.size(), databaseManager.getTotalNumberOfTasks());
assertEquals(workers.size(), databaseManager.getTotalNumberOfWorkers());
databaseManager.clearDatabase();
assertEquals(0, databaseManager.getTotalNumberOfTasks());
assertEquals(0, databaseManager.getTotalNumberOfWorkers());
}
}