Merging
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,41 +1,403 @@
|
||||
package hhn.temp.project.provider;
|
||||
|
||||
import java.io.File;
|
||||
import hhn.temp.project.Task;
|
||||
import hhn.temp.project.TaskState;
|
||||
import hhn.temp.project.Worker;
|
||||
|
||||
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 java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
||||
public class SimpleDatabaseManager implements DatabaseManager {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
@Override
|
||||
public void saveObjects(Collection<E> objects) {
|
||||
private final static Path INSERT_TASK = Path.of("resources/sql/InsertTaskTable.sql");
|
||||
private final static Path DELETE_TASK = Path.of("resources/sql/DeleteTaskTable.sql");
|
||||
private final static Path SELECT_TASK = Path.of("resources/sql/SelectTaskTable.sql");
|
||||
private final static Path UPDATE_TASK = Path.of("resources/sql/UpdateTaskTable.sql");
|
||||
private final static Path COUNT_ALL_TASK = Path.of("resources/sql/CountAllFieldsTask.sql");
|
||||
private final static Path SELECT_ALL_TASK = Path.of("resources/sql/SelectAllFieldsTask.sql");
|
||||
private final static Path SELECT_TASK_BY_ID = Path.of("resources/sql/SelectTaskById.sql");
|
||||
private final static Path INSERT_WORKER = Path.of("resources/sql/InsertWorkerTable.sql");
|
||||
private final static Path DELETE_WORKER = Path.of("resources/sql/DeleteWorkerTable.sql");
|
||||
private final static Path SELECT_WORKER = Path.of("resources/sql/SelectWorkerTable.sql");
|
||||
private final static Path UPDATE_WORKER = Path.of("resources/sql/UpdateWorkerTable.sql");
|
||||
private final static Path COUNT_ALL_WORKER = Path.of("resources/sql/CountAllFieldsWorker.sql");
|
||||
private final static Path SELECT_ALL_WORKER = Path.of("resources/sql/SelectAllFieldsWorker.sql");
|
||||
private final static Path SELECT_WORKER_BY_ID = Path.of("resources/sql/SelectWorkerById.sql");
|
||||
|
||||
public enum QueryMode {
|
||||
INSERT_TASK,
|
||||
SELECT_TASK,
|
||||
UPDATE_TASK,
|
||||
DELETE_TASK,
|
||||
SELECT_ALL_TASK,
|
||||
SELECT_TASK_BY_ID,
|
||||
COUNT_ALL_TASK,
|
||||
INSERT_WORKER,
|
||||
SELECT_WORKER,
|
||||
UPDATE_WORKER,
|
||||
DELETE_WORKER,
|
||||
COUNT_ALL_WORKER,
|
||||
SELECT_ALL_WORKER,
|
||||
SELECT_WORKER_BY_ID,
|
||||
}
|
||||
|
||||
public String loadFile(QueryMode queryMode) throws IOException {
|
||||
switch (queryMode) {
|
||||
case INSERT_TASK -> {
|
||||
return Files.readString(INSERT_TASK);
|
||||
}
|
||||
case SELECT_TASK -> {
|
||||
return Files.readString(SELECT_TASK);
|
||||
}
|
||||
case DELETE_TASK -> {
|
||||
return Files.readString(DELETE_TASK);
|
||||
}
|
||||
case UPDATE_TASK -> {
|
||||
return Files.readString(UPDATE_TASK);
|
||||
}
|
||||
case COUNT_ALL_TASK -> {
|
||||
return Files.readString(COUNT_ALL_TASK);
|
||||
}
|
||||
case SELECT_ALL_TASK -> {
|
||||
return Files.readString(SELECT_ALL_TASK);
|
||||
}
|
||||
case SELECT_TASK_BY_ID -> {
|
||||
return Files.readString(SELECT_TASK_BY_ID);
|
||||
}
|
||||
case INSERT_WORKER -> {
|
||||
return Files.readString(INSERT_WORKER);
|
||||
}
|
||||
case SELECT_WORKER -> {
|
||||
return Files.readString(SELECT_WORKER);
|
||||
}
|
||||
case DELETE_WORKER -> {
|
||||
return Files.readString(DELETE_WORKER);
|
||||
}
|
||||
case UPDATE_WORKER -> {
|
||||
return Files.readString(UPDATE_WORKER);
|
||||
}
|
||||
case COUNT_ALL_WORKER -> {
|
||||
return Files.readString(COUNT_ALL_WORKER);
|
||||
}
|
||||
case SELECT_ALL_WORKER -> {
|
||||
return Files.readString(SELECT_ALL_WORKER);
|
||||
}
|
||||
case SELECT_WORKER_BY_ID -> {
|
||||
return Files.readString(SELECT_WORKER_BY_ID);
|
||||
}
|
||||
default -> {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TaskState integerToTaskState(final int in) {
|
||||
return in == 1 ? TaskState.FINISHED : TaskState.IN_PROGRESS;
|
||||
}
|
||||
|
||||
private int taskStateToInteger(final TaskState taskState) {
|
||||
return taskState == TaskState.FINISHED ? 1 : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> getObjects() {
|
||||
return List.of();
|
||||
public void saveTask(final Task task) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.INSERT_TASK);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return;
|
||||
}
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||
preparedStatement.setInt(1, task.getTaskId());
|
||||
preparedStatement.setString(2, task.getName());
|
||||
preparedStatement.setString(3, task.getDescription());
|
||||
preparedStatement.setInt(4, task.getWorkerId());
|
||||
preparedStatement.setInt(5, taskStateToInteger(task.getTaskState()));
|
||||
preparedStatement.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveObject(E object) {
|
||||
|
||||
public void saveTasks(final Collection<Task> tasks) throws SQLException {
|
||||
for (Task task : tasks) {
|
||||
saveTask(task);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getObject(int id) {
|
||||
public void saveWorker(final Worker worker) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.INSERT_WORKER);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return;
|
||||
}
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||
preparedStatement.setInt(1, worker.getId());
|
||||
preparedStatement.setString(2, worker.getName());
|
||||
preparedStatement.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveWorkers(final Collection<Worker> workers) throws SQLException {
|
||||
for (Worker worker : workers) {
|
||||
saveWorker(worker);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTask(final int taskId, final Task newTaskObject) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.UPDATE_TASK);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return;
|
||||
}
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||
preparedStatement.setString(1, newTaskObject.getName());
|
||||
preparedStatement.setString(2, newTaskObject.getDescription());
|
||||
preparedStatement.setInt(3, taskStateToInteger(newTaskObject.getTaskState()));
|
||||
preparedStatement.setInt(4, taskId);
|
||||
preparedStatement.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateWorker(final int workerId, final Worker newWorkerObject) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.UPDATE_WORKER);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return;
|
||||
}
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||
preparedStatement.setInt(1, newWorkerObject.getId());
|
||||
preparedStatement.setString(2, newWorkerObject.getName());
|
||||
preparedStatement.setInt(3, workerId);
|
||||
preparedStatement.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTask(final int taskId) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.DELETE_TASK);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return;
|
||||
}
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||
preparedStatement.setInt(1, taskId);
|
||||
preparedStatement.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteWorker(final int workerId) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.DELETE_WORKER);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return;
|
||||
}
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||
preparedStatement.setInt(1, workerId);
|
||||
preparedStatement.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Task> getTasks() throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.SELECT_ALL_TASK);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return null;
|
||||
}
|
||||
|
||||
Collection<Task> tasks = new ArrayList<>();
|
||||
|
||||
try (Statement statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(query);) {
|
||||
|
||||
while (rs.next()) {
|
||||
int taskId = rs.getInt("taskid");
|
||||
String name = rs.getString("name");
|
||||
String description = rs.getString("description");
|
||||
int workerId = rs.getInt("workerid");
|
||||
TaskState taskState = integerToTaskState(rs.getInt("taskstate"));
|
||||
|
||||
Task task = new Task(taskId, workerId, name, description);
|
||||
task.setTaskState(taskState);
|
||||
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return tasks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Worker> getWorkers() throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.SELECT_ALL_WORKER);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return null;
|
||||
}
|
||||
|
||||
Collection<Worker> workers = new ArrayList<>();
|
||||
|
||||
try (Statement statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(query);) {
|
||||
|
||||
while (rs.next()) {
|
||||
int workerId = rs.getInt("workerid");
|
||||
String name = rs.getString("name");
|
||||
|
||||
Worker worker = new Worker(name, workerId);
|
||||
|
||||
workers.add(worker);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return workers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Task getTaskByTaskId(final int taskId) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.SELECT_TASK_BY_ID);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return null;
|
||||
}
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement(query);) {
|
||||
|
||||
statement.setInt(1, taskId);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
int taskId1 = rs.getInt("taskid");
|
||||
String name = rs.getString("name");
|
||||
String description = rs.getString("description");
|
||||
int workerId = rs.getInt("workerid");
|
||||
TaskState taskState = integerToTaskState(rs.getInt("taskstate"));
|
||||
|
||||
Task task = new Task(taskId1, workerId, name, description);
|
||||
task.setTaskState(taskState);
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws SQLException, IOException {
|
||||
connection = DriverManager.getConnection("jdbc:mysql://sql7.freesqldatabase.com/sql7810540?user=sql7810540&password=mXdJCFtDZz");
|
||||
public Worker getWorkerByWorkerId(final int workerId) throws SQLException {
|
||||
String query;
|
||||
try {
|
||||
query = loadFile(QueryMode.SELECT_WORKER_BY_ID);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return null;
|
||||
}
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement(query);) {
|
||||
|
||||
statement.setInt(1, workerId);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
int workerId1 = rs.getInt("workerid");
|
||||
String name = rs.getString("name");
|
||||
|
||||
Worker worker = new Worker(name, workerId1);
|
||||
|
||||
return worker;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalNumberOfTasks() throws SQLException {
|
||||
String query = "";
|
||||
try {
|
||||
query = loadFile(QueryMode.COUNT_ALL_TASK);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return -1;
|
||||
}
|
||||
try (Statement statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(query);) {
|
||||
|
||||
if (rs.next()) {
|
||||
return rs.getInt("total");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalNumberOfWorkers() throws SQLException {
|
||||
String query = "";
|
||||
try {
|
||||
query = loadFile(QueryMode.COUNT_ALL_WORKER);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
return -1;
|
||||
}
|
||||
try (Statement statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(query);) {
|
||||
|
||||
if (rs.next()) {
|
||||
return rs.getInt("total");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void clearDatabase() throws SQLException {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("TRUNCATE TABLE Task");
|
||||
statement.execute("TRUNCATE TABLE Worker");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws SQLException {
|
||||
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sql7810540?user=sql7810540&password=mXdJCFtDZz");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user