This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package hhn.temp.project;
|
||||
|
||||
import hhn.temp.project.expections.TaskHasNoWorkerException;
|
||||
import hhn.temp.project.provider.MySql;
|
||||
|
||||
public class Task {
|
||||
|
||||
@@ -8,17 +9,17 @@ public class Task {
|
||||
private String description;
|
||||
private TaskStatus taskStatus;
|
||||
private int taskID;
|
||||
private static int idCounter = 0;
|
||||
private String workername;
|
||||
private MySql mySql;
|
||||
|
||||
public Task(String name, String description) {
|
||||
|
||||
public Task(int id, String name, String description, String workername, TaskStatus status, MySql mySql) {
|
||||
|
||||
this.taskID = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.taskStatus = TaskStatus.OPEN;
|
||||
//TODO when DB then auto IDs
|
||||
this.taskID = idCounter++;
|
||||
this.taskStatus = status;
|
||||
this.workername = workername;
|
||||
this.mySql = mySql;
|
||||
}
|
||||
|
||||
public int getTaskID() {
|
||||
@@ -43,6 +44,7 @@ public class Task {
|
||||
throw new IllegalArgumentException("Description is null!");
|
||||
}
|
||||
|
||||
this.mySql.updateDescription(taskID, description);
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@@ -52,6 +54,7 @@ public class Task {
|
||||
throw new IllegalArgumentException("TaskStatus is null!");
|
||||
}
|
||||
|
||||
this.mySql.updateStatus(taskID, taskStatus);
|
||||
this.taskStatus = taskStatus;
|
||||
}
|
||||
|
||||
@@ -62,6 +65,9 @@ public class Task {
|
||||
if(!checkOnlyLetter(workerName)){
|
||||
throw new IllegalArgumentException("Only Lettery as Worker Name!");
|
||||
}
|
||||
|
||||
this.mySql.updateStatus(taskID, TaskStatus.INPROCESS);
|
||||
this.mySql.updateWorker(taskID, workerName);
|
||||
this.workername = workerName;
|
||||
this.setStatus(TaskStatus.INPROCESS);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package hhn.temp.project;
|
||||
|
||||
import hhn.temp.project.expections.TaskAlreadyExistsException;
|
||||
import hhn.temp.project.provider.MySql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -10,9 +11,18 @@ import java.util.Map;
|
||||
public class TaskManager {
|
||||
|
||||
private Map<Integer, Task> taskMap;
|
||||
private MySql mysql;
|
||||
|
||||
public TaskManager() {
|
||||
public TaskManager(String user, String password, String port, String host) {
|
||||
taskMap = new HashMap<>();
|
||||
this.mysql = new MySql(user, password, port, host);
|
||||
this.mysql.connect();
|
||||
}
|
||||
/**
|
||||
* only for Testing
|
||||
*/
|
||||
public void resetTest() {
|
||||
this.mysql.reset();
|
||||
}
|
||||
|
||||
public Task createTask(String name, String description) {
|
||||
@@ -28,31 +38,35 @@ public class TaskManager {
|
||||
throw new IllegalArgumentException("Only Letters or Digit are allowed in the name: " + name);
|
||||
}
|
||||
|
||||
|
||||
boolean taskExited = this.taskMap.values().stream().anyMatch(task -> task.getName().equals(name));
|
||||
boolean taskExited = this.mysql.existTask(name);
|
||||
if(taskExited) {
|
||||
throw new TaskAlreadyExistsException("Task already exits, with the name: " + name);
|
||||
}
|
||||
|
||||
int taskId = this.mysql.createTask(name, description);
|
||||
Task task = this.mysql.getTask(taskId);
|
||||
|
||||
Task task = new Task(name, description);
|
||||
taskMap.put(task.getTaskID(), task);
|
||||
//taskMap.put(task.getTaskID(), task);
|
||||
return task;
|
||||
}
|
||||
|
||||
public List<Task> getTaskList() {
|
||||
return new ArrayList<>(this.taskMap.values());
|
||||
|
||||
return this.mysql.getTaskList();
|
||||
}
|
||||
|
||||
public Task getTask(String name) {
|
||||
return taskMap.values().stream().filter(t -> t.getName().equals(name)).findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Wrong name"));
|
||||
if(!this.mysql.existTask(name)) {
|
||||
throw new IllegalArgumentException("Wrong name");
|
||||
}
|
||||
|
||||
return this.mysql.getTask(name);
|
||||
}
|
||||
public Task getTask(int taskID) {
|
||||
if(!this.taskMap.containsKey(taskID)) {
|
||||
if(!this.mysql.existTask(taskID)) {
|
||||
throw new IllegalArgumentException("Wrong id");
|
||||
}
|
||||
return this.taskMap.get(taskID);
|
||||
return this.mysql.getTask(taskID);
|
||||
}
|
||||
|
||||
public void deleteTask(String name) {
|
||||
@@ -60,12 +74,11 @@ public class TaskManager {
|
||||
|
||||
throw new IllegalArgumentException("Name is null!");
|
||||
}
|
||||
if(name.isEmpty() || this.taskMap.values().stream().noneMatch(t -> t.getName().equals(name))) {
|
||||
if(name.isEmpty() || !this.mysql.existTask(name)) {
|
||||
throw new IllegalArgumentException("Wrong name!");
|
||||
}
|
||||
|
||||
this.taskMap.remove(this.taskMap.values().stream().filter(t -> t.getName().equals(name)).findFirst()
|
||||
.orElseThrow().getTaskID());
|
||||
this.mysql.deleteTask(name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package hhn.temp.project.expections;
|
||||
|
||||
public class SQLNoConectionException extends RuntimeException {
|
||||
|
||||
public SQLNoConectionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package hhn.temp.project.expections;
|
||||
|
||||
public class SQLStatmentException extends RuntimeException {
|
||||
|
||||
public SQLStatmentException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package hhn.temp.project.expections;
|
||||
|
||||
public class TaskNotExistsException extends RuntimeException {
|
||||
|
||||
public TaskNotExistsException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
226
src/main/java/hhn/temp/project/provider/MySql.java
Normal file
226
src/main/java/hhn/temp/project/provider/MySql.java
Normal file
@@ -0,0 +1,226 @@
|
||||
package hhn.temp.project.provider;
|
||||
|
||||
import hhn.temp.project.Task;
|
||||
import hhn.temp.project.TaskStatus;
|
||||
import hhn.temp.project.expections.SQLNoConectionException;
|
||||
import hhn.temp.project.expections.SQLStatmentException;
|
||||
import hhn.temp.project.expections.TaskNotExistsException;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MySql {
|
||||
|
||||
private String user;
|
||||
private String password;
|
||||
private String db;
|
||||
private String host;
|
||||
private Connection connection;
|
||||
|
||||
public MySql(String user, String password, String db, String host) {
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
this.db = db;
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
String url = "jdbc:mysql://" + host + ":3306/" + db + "?autoReconnect=true&useSSL=false";
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
} catch (SQLException e) {
|
||||
throw new SQLNoConectionException("Cant connect to the database");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new SQLNoConectionException("Cant connect to the database: Driver class not found!");
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("TRUNCATE task");
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant insert data in database");
|
||||
}
|
||||
}
|
||||
|
||||
public int createTask(String name, String description) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("INSERT INTO task ( taskName, taskDescription) VALUES ( ? , ? )", Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setString(1, name);
|
||||
stmt.setString(2, description);
|
||||
stmt.executeUpdate();
|
||||
ResultSet rs = stmt.getGeneratedKeys();
|
||||
if(rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}else{
|
||||
throw new SQLStatmentException("Can insert data but not select");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant insert data in database");
|
||||
}
|
||||
}
|
||||
|
||||
// public void deleteTask(int id) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("DELETE task WHERE taskID=?");
|
||||
// stmt.setString(1, ""+id);
|
||||
// stmt.executeUpdate();
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant delete data in database");
|
||||
// }
|
||||
// }
|
||||
|
||||
public boolean existTask(String name) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("Select * FROM task WHERE taskName=?");
|
||||
stmt.setString(1, name);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant select data from database");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean existTask(int id) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("Select * FROM task WHERE taskID=?");
|
||||
stmt.setString(1, ""+id);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant select data from database");
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteTask(String name) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("DELETE FROM task WHERE taskName=?");
|
||||
stmt.setString(1, name);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant delete data in database");
|
||||
}
|
||||
}
|
||||
|
||||
public void updateDescription(int id, String description) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskDescription=? WHERE taskID=?");
|
||||
stmt.setString(1, description);
|
||||
stmt.setString(2, ""+id);
|
||||
stmt.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant update data in database");
|
||||
}
|
||||
}
|
||||
// public void updateDescription(String name, String description) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskDescription=? WHERE taskName=?");
|
||||
// stmt.setString(1, description);
|
||||
// stmt.setString(2, name);
|
||||
// stmt.executeUpdate();
|
||||
//
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant update data in database");
|
||||
// }
|
||||
// }
|
||||
public void updateStatus(int id, TaskStatus status) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskStatus=? WHERE taskID=?");
|
||||
stmt.setString(1, status.name());
|
||||
stmt.setString(2, ""+id);
|
||||
stmt.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant update data in database");
|
||||
}
|
||||
}
|
||||
// public void updateStatus(String name, TaskStatus status) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskStatus=? WHERE taskName=?");
|
||||
// stmt.setString(1, status.name());
|
||||
// stmt.setString(2, name);
|
||||
// stmt.executeUpdate();
|
||||
//
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant update data in database");
|
||||
// }
|
||||
// }
|
||||
|
||||
public void updateWorker(int id, String worker) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskWorker=? WHERE taskID=?");
|
||||
stmt.setString(1, worker);
|
||||
stmt.setString(2, ""+id);
|
||||
stmt.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant update data in database");
|
||||
}
|
||||
}
|
||||
|
||||
// public void updateWorker(String name, String worker) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskWorker=? WHERE taskName=?");
|
||||
// stmt.setString(1, worker);
|
||||
// stmt.setString(2, name);
|
||||
// stmt.executeUpdate();
|
||||
//
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant update data in database");
|
||||
// }
|
||||
// }
|
||||
|
||||
public Task getTask(int id) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM task WHERE taskID=?");
|
||||
stmt.setString(1, ""+id);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if(rs.next()) {
|
||||
return getTaskFromDatabase(rs);
|
||||
} else {
|
||||
throw new TaskNotExistsException("No Task found in databse with id: " + id);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant select data from database");
|
||||
}
|
||||
}
|
||||
public Task getTask(String name) {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM task WHERE taskName=?");
|
||||
stmt.setString(1, name);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if(rs.next()) {
|
||||
return getTaskFromDatabase(rs);
|
||||
}else{
|
||||
throw new TaskNotExistsException("No Task found in databse with name: " + name);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant select data from database");
|
||||
}
|
||||
}
|
||||
|
||||
public List<Task> getTaskList() {
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM task");
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
List<Task> taskList = new ArrayList<>();
|
||||
while(rs.next()) {
|
||||
taskList.add(getTaskFromDatabase(rs));
|
||||
}
|
||||
return taskList;
|
||||
} catch (SQLException e) {
|
||||
throw new SQLStatmentException("Cant select data from database");
|
||||
}
|
||||
}
|
||||
|
||||
private Task getTaskFromDatabase(ResultSet rs) throws SQLException {
|
||||
Task task = new Task(rs.getInt("taskID"), rs.getString("taskName"), rs.getString("taskDescription"), rs.getString("taskWorker"),TaskStatus.valueOf(rs.getString("taskStatus")), this);
|
||||
return task;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user