Loading src/main/java/cz/muni/fi/pv168/project/db/category/CategoryDao.java +4 −4 Original line number Diff line number Diff line Loading @@ -94,11 +94,11 @@ public class CategoryDao implements DataAccessObject<Category> { } // cleanup previous categoryTimeDao.deleteAllEntitiesFor(entity.getId()); categoryTimeDao.deleteAllCategoryTimesForCategory(entity.getId()); // add current for (var categoryTime : entity.getTimeSpents()) { categoryTime.setId(null); categoryTimeDao.addEntityFor(entity.getId(), categoryTime); categoryTimeDao.addCategoryTimeFor(entity.getId(), categoryTime); } } catch (SQLException ex) { throw new DataAccessException("Failed to update category " + entity, ex); Loading @@ -111,7 +111,7 @@ public class CategoryDao implements DataAccessObject<Category> { throw new IllegalArgumentException("Category has null ID: " + entity); } categoryTimeDao.deleteAllEntitiesFor(entity.getId()); categoryTimeDao.deleteAllCategoryTimesForCategory(entity.getId()); try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM CATEGORY WHERE ID = ?")) { Loading Loading @@ -158,7 +158,7 @@ public class CategoryDao implements DataAccessObject<Category> { ); category.setId(id); category.setTimeSpents(categoryTimeDao.getAllEntitiesFor(id)); category.setTimeSpents(categoryTimeDao.getAllCategoryTimesForCategory(id)); return category; } Loading src/main/java/cz/muni/fi/pv168/project/db/categorytime/CategoryTimeDao.java +38 −43 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package cz.muni.fi.pv168.project.db.categorytime; import cz.muni.fi.pv168.project.data.category.CategoryTime; import cz.muni.fi.pv168.project.db.DataAccessException; import cz.muni.fi.pv168.project.db.interfaces.OneToNDao; import javax.sql.DataSource; import java.sql.Date; Loading @@ -12,7 +11,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class CategoryTimeDao implements OneToNDao<CategoryTime> { public class CategoryTimeDao { private final DataSource dataSource; Loading @@ -20,16 +19,15 @@ public class CategoryTimeDao implements OneToNDao<CategoryTime> { this.dataSource = dataSource; } public List<CategoryTime> getAllEntitiesForTask(Long taskId) { return getAllEntitiesFor(taskId, "TASK_ID"); public List<CategoryTime> getAllCategoryTimesForTask(Long taskId) { return getAllCategoryTimesForCategory(taskId, "TASK_ID"); } @Override public List<CategoryTime> getAllEntitiesFor(Long categoryId) { return getAllEntitiesFor(categoryId, "CATEGORY_ID"); public List<CategoryTime> getAllCategoryTimesForCategory(Long categoryId) { return getAllCategoryTimesForCategory(categoryId, "CATEGORY_ID"); } private List<CategoryTime> getAllEntitiesFor(Long id, String attribute) { private List<CategoryTime> getAllCategoryTimesForCategory(Long id, String attribute) { if (id == null) { throw new IllegalArgumentException("Cannot look for categoryTime of null ID parent"); } Loading @@ -53,25 +51,23 @@ public class CategoryTimeDao implements OneToNDao<CategoryTime> { } } @Override public void deleteAllEntitiesFor(Long categoryId) { var categoryTimes = getAllEntitiesFor(categoryId); public void deleteAllCategoryTimesForCategory(Long categoryId) { var categoryTimes = getAllCategoryTimesForCategory(categoryId); for (var categoryTime : categoryTimes) { delete(categoryTime); } } public void deleteAllEntitiesForTask(Long taskId) { var categoryTimes = getAllEntitiesFor(taskId, "TASK_ID"); public void deleteAllCategoryTimesForTask(Long taskId) { var categoryTimes = getAllCategoryTimesForCategory(taskId, "TASK_ID"); for (var categoryTime : categoryTimes) { delete(categoryTime); } } @Override public void addEntityFor(Long categoryId, CategoryTime entity) { if (entity.getId() != null) { throw new IllegalArgumentException("CategoryTime already has ID: " + entity); public void addCategoryTimeFor(Long categoryId, CategoryTime categoryTime) { if (categoryTime.getId() != null) { throw new IllegalArgumentException("CategoryTime already has ID: " + categoryTime); } try (var connection = dataSource.getConnection(); Loading @@ -79,73 +75,72 @@ public class CategoryTimeDao implements OneToNDao<CategoryTime> { "INSERT INTO CATEGORYTIME (CATEGORY_ID, TASK_ID, HOURS_SPENT, COMPLETION_DATE) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) { st.setLong(1, categoryId); st.setLong(2, entity.getTaskId()); st.setLong(3, entity.getTimeSpent().toHours()); st.setDate(4, Date.valueOf(entity.getCompletionDate())); st.setLong(2, categoryTime.getTaskId()); st.setLong(3, categoryTime.getTimeSpent().toHours()); st.setDate(4, Date.valueOf(categoryTime.getCompletionDate())); st.executeUpdate(); try (var rs = st.getGeneratedKeys()) { if (rs.getMetaData().getColumnCount() != 1) { throw new DataAccessException("Failed to fetch generated key: compound key returned for categoryTime: " + entity); throw new DataAccessException("Failed to fetch generated key: compound key returned for categoryTime: " + categoryTime); } if (rs.next()) { entity.setId(rs.getLong(1)); categoryTime.setId(rs.getLong(1)); } else { throw new DataAccessException("Failed to fetch generated key: no key returned for categoryTime: " + entity); throw new DataAccessException("Failed to fetch generated key: no key returned for categoryTime: " + categoryTime); } if (rs.next()) { throw new DataAccessException("Failed to fetch generated key: multiple keys returned for categoryTime: " + entity); throw new DataAccessException( "Failed to fetch generated key: multiple keys returned for categoryTime: " + categoryTime ); } } } catch (SQLException ex) { throw new DataAccessException("Failed to store categoryTime " + entity, ex); throw new DataAccessException("Failed to store categoryTime " + categoryTime, ex); } } @Override public void update(CategoryTime entity) { if (entity.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + entity); public void update(CategoryTime categoryTime) { if (categoryTime.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + categoryTime); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement( "UPDATE CATEGORYTIME SET HOURS_SPENT = ?, COMPLETION_DATE = ?, TASK_ID = ? WHERE ID = ?")) { st.setLong(1, entity.getTimeSpent().toHours()); st.setDate(2, Date.valueOf(entity.getCompletionDate())); st.setLong(3, entity.getTaskId()); st.setLong(4, entity.getId()); st.setLong(1, categoryTime.getTimeSpent().toHours()); st.setDate(2, Date.valueOf(categoryTime.getCompletionDate())); st.setLong(3, categoryTime.getTaskId()); st.setLong(4, categoryTime.getId()); int rowsUpdated = st.executeUpdate(); if (rowsUpdated == 0) { throw new DataAccessException("Failed to update non-existing subtask: " + entity); throw new DataAccessException("Failed to update non-existing subtask: " + categoryTime); } } catch (SQLException ex) { throw new DataAccessException("Failed to update subtask " + entity, ex); throw new DataAccessException("Failed to update subtask " + categoryTime, ex); } } @Override public void delete(CategoryTime entity) { if (entity.getId() == null) { throw new IllegalArgumentException("CategoryTime has null ID: " + entity); public void delete(CategoryTime categoryTime) { if (categoryTime.getId() == null) { throw new IllegalArgumentException("CategoryTime has null ID: " + categoryTime); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM CATEGORYTIME WHERE ID = ?")) { st.setLong(1, entity.getId()); st.setLong(1, categoryTime.getId()); int rowsDeleted = st.executeUpdate(); if (rowsDeleted == 0) { throw new DataAccessException("Failed to delete non-existing categoryTime: " + entity); throw new DataAccessException("Failed to delete non-existing categoryTime: " + categoryTime); } } catch (SQLException ex) { throw new DataAccessException("Failed to delete categoryTime " + entity, ex); throw new DataAccessException("Failed to delete categoryTime " + categoryTime, ex); } } @Override public CategoryTime findById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look for categoryTime of null ID"); Loading src/main/java/cz/muni/fi/pv168/project/db/interfaces/OneToNDao.javadeleted 100644 → 0 +0 −18 Original line number Diff line number Diff line package cz.muni.fi.pv168.project.db.interfaces; import java.util.List; public interface OneToNDao<T> { void addEntityFor(Long taskId, T entity); List<T> getAllEntitiesFor(Long taskId); void deleteAllEntitiesFor(Long taskId); void update(T entity); void delete(T entity); T findById(Long id); } src/main/java/cz/muni/fi/pv168/project/db/subtask/SubTaskDao.java +28 −35 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package cz.muni.fi.pv168.project.db.subtask; import cz.muni.fi.pv168.project.data.task.SubTask; import cz.muni.fi.pv168.project.db.DataAccessException; import cz.muni.fi.pv168.project.db.interfaces.OneToNDao; import javax.sql.DataSource; import java.sql.ResultSet; Loading @@ -11,7 +10,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class SubTaskDao implements OneToNDao<SubTask> { public class SubTaskDao { private final DataSource dataSource; Loading @@ -19,8 +18,7 @@ public class SubTaskDao implements OneToNDao<SubTask> { this.dataSource = dataSource; } @Override public List<SubTask> getAllEntitiesFor(Long taskId) { public List<SubTask> getAllSubTasksForTask(Long taskId) { if (taskId == null) { throw new IllegalArgumentException("Cannot look for subtasks of null ID parent"); } Loading @@ -42,18 +40,16 @@ public class SubTaskDao implements OneToNDao<SubTask> { } } @Override public void deleteAllEntitiesFor(Long taskId) { var subTasks = getAllEntitiesFor(taskId); public void deleteAllSubTasksForTask(Long taskId) { var subTasks = getAllSubTasksForTask(taskId); for (var subTask : subTasks) { delete(subTask); } } @Override public void addEntityFor(Long taskId, SubTask entity) { if (entity.getId() != null) { throw new IllegalArgumentException("Subtask already has ID: " + entity); public void addSubTaskForTask(Long taskId, SubTask subTask) { if (subTask.getId() != null) { throw new IllegalArgumentException("Subtask already has ID: " + subTask); } try (var connection = dataSource.getConnection(); Loading @@ -61,71 +57,68 @@ public class SubTaskDao implements OneToNDao<SubTask> { "INSERT INTO SUBTASK (TASK_ID, TITLE, IS_COMPLETED) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) { st.setLong(1, taskId); st.setString(2, entity.getTitle()); st.setBoolean(3, entity.isCompleted()); st.setString(2, subTask.getTitle()); st.setBoolean(3, subTask.isCompleted()); st.executeUpdate(); try (var rs = st.getGeneratedKeys()) { if (rs.getMetaData().getColumnCount() != 1) { throw new DataAccessException("Failed to fetch generated key: compound key returned for subtask: " + entity); throw new DataAccessException("Failed to fetch generated key: compound key returned for subtask: " + subTask); } if (rs.next()) { entity.setId(rs.getLong(1)); subTask.setId(rs.getLong(1)); } else { throw new DataAccessException("Failed to fetch generated key: no key returned for subtask: " + entity); throw new DataAccessException("Failed to fetch generated key: no key returned for subtask: " + subTask); } if (rs.next()) { throw new DataAccessException("Failed to fetch generated key: multiple keys returned for subtask: " + entity); throw new DataAccessException("Failed to fetch generated key: multiple keys returned for subtask: " + subTask); } } } catch (SQLException ex) { throw new DataAccessException("Failed to store subtask " + entity, ex); throw new DataAccessException("Failed to store subtask " + subTask, ex); } } @Override public void update(SubTask entity) { if (entity.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + entity); public void update(SubTask subTask) { if (subTask.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + subTask); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement( "UPDATE SUBTASK SET TITLE = ?, IS_COMPLETED = ? WHERE ID = ?")) { st.setString(1, entity.getTitle()); st.setBoolean(2, entity.isCompleted()); st.setLong(3, entity.getId()); st.setString(1, subTask.getTitle()); st.setBoolean(2, subTask.isCompleted()); st.setLong(3, subTask.getId()); int rowsUpdated = st.executeUpdate(); if (rowsUpdated == 0) { throw new DataAccessException("Failed to update non-existing subtask: " + entity); throw new DataAccessException("Failed to update non-existing subtask: " + subTask); } } catch (SQLException ex) { throw new DataAccessException("Failed to update subtask " + entity, ex); throw new DataAccessException("Failed to update subtask " + subTask, ex); } } @Override public void delete(SubTask entity) { if (entity.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + entity); public void delete(SubTask subTask) { if (subTask.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + subTask); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM SUBTASK WHERE ID = ?")) { st.setLong(1, entity.getId()); st.setLong(1, subTask.getId()); int rowsDeleted = st.executeUpdate(); if (rowsDeleted == 0) { throw new DataAccessException("Failed to delete non-existing subtask: " + entity); throw new DataAccessException("Failed to delete non-existing subtask: " + subTask); } } catch (SQLException ex) { throw new DataAccessException("Failed to delete subtask " + entity, ex); throw new DataAccessException("Failed to delete subtask " + subTask, ex); } } @Override public SubTask findById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look for subtask of null ID"); Loading src/main/java/cz/muni/fi/pv168/project/db/task/TaskDao.java +6 −6 Original line number Diff line number Diff line Loading @@ -151,7 +151,7 @@ public class TaskDao implements DataAccessObject<Task> { rs.getString("TITLE"), rs.getLong("ESTIMATED_TIME"), rs.getDate("DUE_DATE").toLocalDate()) .setSubTasks(subTaskDao.getAllEntitiesFor(taskId)) .setSubTasks(subTaskDao.getAllSubTasksForTask(taskId)) .setDependencyTasks(List.of()) .setCategories(List.of()); var description = rs.getString("DESCRIPTION"); Loading Loading @@ -192,10 +192,10 @@ public class TaskDao implements DataAccessObject<Task> { } // cleanup previous subTaskDao.deleteAllEntitiesFor(entity.getId()); subTaskDao.deleteAllSubTasksForTask(entity.getId()); dependencyDao.deleteAllDependencyAssociationsForTask(entity.getId()); taskCategoryDao.deleteAllCategoryAssociationsForTask(entity.getId()); var timeSpents = categoryTimeDao.getAllEntitiesForTask(entity.getId()); var timeSpents = categoryTimeDao.getAllCategoryTimesForTask(entity.getId()); for (var timeSpent : timeSpents) { timeSpent.setHoursSpent(entity.getEstimatedTime()); categoryTimeDao.update(timeSpent); Loading @@ -211,7 +211,7 @@ public class TaskDao implements DataAccessObject<Task> { private void addTaskFieldsToDaos(Task entity) { for (var subtask : entity.getSubTasks()) { subtask.setId(null); subTaskDao.addEntityFor(entity.getId(), subtask); subTaskDao.addSubTaskForTask(entity.getId(), subtask); } for (var dependency : entity.getDependencyTasks()) { dependencyDao.addDependencyAssociationFor(entity.getId(), dependency.getId()); Loading @@ -227,10 +227,10 @@ public class TaskDao implements DataAccessObject<Task> { throw new IllegalArgumentException("Task has null ID: " + entity); } subTaskDao.deleteAllEntitiesFor(entity.getId()); subTaskDao.deleteAllSubTasksForTask(entity.getId()); dependencyDao.deleteAllDependencyAssociationsForTask(entity.getId()); taskCategoryDao.deleteAllCategoryAssociationsForTask(entity.getId()); categoryTimeDao.deleteAllEntitiesForTask(entity.getId()); categoryTimeDao.deleteAllCategoryTimesForTask(entity.getId()); try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM TASK WHERE ID = ?")) { Loading Loading
src/main/java/cz/muni/fi/pv168/project/db/category/CategoryDao.java +4 −4 Original line number Diff line number Diff line Loading @@ -94,11 +94,11 @@ public class CategoryDao implements DataAccessObject<Category> { } // cleanup previous categoryTimeDao.deleteAllEntitiesFor(entity.getId()); categoryTimeDao.deleteAllCategoryTimesForCategory(entity.getId()); // add current for (var categoryTime : entity.getTimeSpents()) { categoryTime.setId(null); categoryTimeDao.addEntityFor(entity.getId(), categoryTime); categoryTimeDao.addCategoryTimeFor(entity.getId(), categoryTime); } } catch (SQLException ex) { throw new DataAccessException("Failed to update category " + entity, ex); Loading @@ -111,7 +111,7 @@ public class CategoryDao implements DataAccessObject<Category> { throw new IllegalArgumentException("Category has null ID: " + entity); } categoryTimeDao.deleteAllEntitiesFor(entity.getId()); categoryTimeDao.deleteAllCategoryTimesForCategory(entity.getId()); try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM CATEGORY WHERE ID = ?")) { Loading Loading @@ -158,7 +158,7 @@ public class CategoryDao implements DataAccessObject<Category> { ); category.setId(id); category.setTimeSpents(categoryTimeDao.getAllEntitiesFor(id)); category.setTimeSpents(categoryTimeDao.getAllCategoryTimesForCategory(id)); return category; } Loading
src/main/java/cz/muni/fi/pv168/project/db/categorytime/CategoryTimeDao.java +38 −43 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package cz.muni.fi.pv168.project.db.categorytime; import cz.muni.fi.pv168.project.data.category.CategoryTime; import cz.muni.fi.pv168.project.db.DataAccessException; import cz.muni.fi.pv168.project.db.interfaces.OneToNDao; import javax.sql.DataSource; import java.sql.Date; Loading @@ -12,7 +11,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class CategoryTimeDao implements OneToNDao<CategoryTime> { public class CategoryTimeDao { private final DataSource dataSource; Loading @@ -20,16 +19,15 @@ public class CategoryTimeDao implements OneToNDao<CategoryTime> { this.dataSource = dataSource; } public List<CategoryTime> getAllEntitiesForTask(Long taskId) { return getAllEntitiesFor(taskId, "TASK_ID"); public List<CategoryTime> getAllCategoryTimesForTask(Long taskId) { return getAllCategoryTimesForCategory(taskId, "TASK_ID"); } @Override public List<CategoryTime> getAllEntitiesFor(Long categoryId) { return getAllEntitiesFor(categoryId, "CATEGORY_ID"); public List<CategoryTime> getAllCategoryTimesForCategory(Long categoryId) { return getAllCategoryTimesForCategory(categoryId, "CATEGORY_ID"); } private List<CategoryTime> getAllEntitiesFor(Long id, String attribute) { private List<CategoryTime> getAllCategoryTimesForCategory(Long id, String attribute) { if (id == null) { throw new IllegalArgumentException("Cannot look for categoryTime of null ID parent"); } Loading @@ -53,25 +51,23 @@ public class CategoryTimeDao implements OneToNDao<CategoryTime> { } } @Override public void deleteAllEntitiesFor(Long categoryId) { var categoryTimes = getAllEntitiesFor(categoryId); public void deleteAllCategoryTimesForCategory(Long categoryId) { var categoryTimes = getAllCategoryTimesForCategory(categoryId); for (var categoryTime : categoryTimes) { delete(categoryTime); } } public void deleteAllEntitiesForTask(Long taskId) { var categoryTimes = getAllEntitiesFor(taskId, "TASK_ID"); public void deleteAllCategoryTimesForTask(Long taskId) { var categoryTimes = getAllCategoryTimesForCategory(taskId, "TASK_ID"); for (var categoryTime : categoryTimes) { delete(categoryTime); } } @Override public void addEntityFor(Long categoryId, CategoryTime entity) { if (entity.getId() != null) { throw new IllegalArgumentException("CategoryTime already has ID: " + entity); public void addCategoryTimeFor(Long categoryId, CategoryTime categoryTime) { if (categoryTime.getId() != null) { throw new IllegalArgumentException("CategoryTime already has ID: " + categoryTime); } try (var connection = dataSource.getConnection(); Loading @@ -79,73 +75,72 @@ public class CategoryTimeDao implements OneToNDao<CategoryTime> { "INSERT INTO CATEGORYTIME (CATEGORY_ID, TASK_ID, HOURS_SPENT, COMPLETION_DATE) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) { st.setLong(1, categoryId); st.setLong(2, entity.getTaskId()); st.setLong(3, entity.getTimeSpent().toHours()); st.setDate(4, Date.valueOf(entity.getCompletionDate())); st.setLong(2, categoryTime.getTaskId()); st.setLong(3, categoryTime.getTimeSpent().toHours()); st.setDate(4, Date.valueOf(categoryTime.getCompletionDate())); st.executeUpdate(); try (var rs = st.getGeneratedKeys()) { if (rs.getMetaData().getColumnCount() != 1) { throw new DataAccessException("Failed to fetch generated key: compound key returned for categoryTime: " + entity); throw new DataAccessException("Failed to fetch generated key: compound key returned for categoryTime: " + categoryTime); } if (rs.next()) { entity.setId(rs.getLong(1)); categoryTime.setId(rs.getLong(1)); } else { throw new DataAccessException("Failed to fetch generated key: no key returned for categoryTime: " + entity); throw new DataAccessException("Failed to fetch generated key: no key returned for categoryTime: " + categoryTime); } if (rs.next()) { throw new DataAccessException("Failed to fetch generated key: multiple keys returned for categoryTime: " + entity); throw new DataAccessException( "Failed to fetch generated key: multiple keys returned for categoryTime: " + categoryTime ); } } } catch (SQLException ex) { throw new DataAccessException("Failed to store categoryTime " + entity, ex); throw new DataAccessException("Failed to store categoryTime " + categoryTime, ex); } } @Override public void update(CategoryTime entity) { if (entity.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + entity); public void update(CategoryTime categoryTime) { if (categoryTime.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + categoryTime); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement( "UPDATE CATEGORYTIME SET HOURS_SPENT = ?, COMPLETION_DATE = ?, TASK_ID = ? WHERE ID = ?")) { st.setLong(1, entity.getTimeSpent().toHours()); st.setDate(2, Date.valueOf(entity.getCompletionDate())); st.setLong(3, entity.getTaskId()); st.setLong(4, entity.getId()); st.setLong(1, categoryTime.getTimeSpent().toHours()); st.setDate(2, Date.valueOf(categoryTime.getCompletionDate())); st.setLong(3, categoryTime.getTaskId()); st.setLong(4, categoryTime.getId()); int rowsUpdated = st.executeUpdate(); if (rowsUpdated == 0) { throw new DataAccessException("Failed to update non-existing subtask: " + entity); throw new DataAccessException("Failed to update non-existing subtask: " + categoryTime); } } catch (SQLException ex) { throw new DataAccessException("Failed to update subtask " + entity, ex); throw new DataAccessException("Failed to update subtask " + categoryTime, ex); } } @Override public void delete(CategoryTime entity) { if (entity.getId() == null) { throw new IllegalArgumentException("CategoryTime has null ID: " + entity); public void delete(CategoryTime categoryTime) { if (categoryTime.getId() == null) { throw new IllegalArgumentException("CategoryTime has null ID: " + categoryTime); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM CATEGORYTIME WHERE ID = ?")) { st.setLong(1, entity.getId()); st.setLong(1, categoryTime.getId()); int rowsDeleted = st.executeUpdate(); if (rowsDeleted == 0) { throw new DataAccessException("Failed to delete non-existing categoryTime: " + entity); throw new DataAccessException("Failed to delete non-existing categoryTime: " + categoryTime); } } catch (SQLException ex) { throw new DataAccessException("Failed to delete categoryTime " + entity, ex); throw new DataAccessException("Failed to delete categoryTime " + categoryTime, ex); } } @Override public CategoryTime findById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look for categoryTime of null ID"); Loading
src/main/java/cz/muni/fi/pv168/project/db/interfaces/OneToNDao.javadeleted 100644 → 0 +0 −18 Original line number Diff line number Diff line package cz.muni.fi.pv168.project.db.interfaces; import java.util.List; public interface OneToNDao<T> { void addEntityFor(Long taskId, T entity); List<T> getAllEntitiesFor(Long taskId); void deleteAllEntitiesFor(Long taskId); void update(T entity); void delete(T entity); T findById(Long id); }
src/main/java/cz/muni/fi/pv168/project/db/subtask/SubTaskDao.java +28 −35 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package cz.muni.fi.pv168.project.db.subtask; import cz.muni.fi.pv168.project.data.task.SubTask; import cz.muni.fi.pv168.project.db.DataAccessException; import cz.muni.fi.pv168.project.db.interfaces.OneToNDao; import javax.sql.DataSource; import java.sql.ResultSet; Loading @@ -11,7 +10,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class SubTaskDao implements OneToNDao<SubTask> { public class SubTaskDao { private final DataSource dataSource; Loading @@ -19,8 +18,7 @@ public class SubTaskDao implements OneToNDao<SubTask> { this.dataSource = dataSource; } @Override public List<SubTask> getAllEntitiesFor(Long taskId) { public List<SubTask> getAllSubTasksForTask(Long taskId) { if (taskId == null) { throw new IllegalArgumentException("Cannot look for subtasks of null ID parent"); } Loading @@ -42,18 +40,16 @@ public class SubTaskDao implements OneToNDao<SubTask> { } } @Override public void deleteAllEntitiesFor(Long taskId) { var subTasks = getAllEntitiesFor(taskId); public void deleteAllSubTasksForTask(Long taskId) { var subTasks = getAllSubTasksForTask(taskId); for (var subTask : subTasks) { delete(subTask); } } @Override public void addEntityFor(Long taskId, SubTask entity) { if (entity.getId() != null) { throw new IllegalArgumentException("Subtask already has ID: " + entity); public void addSubTaskForTask(Long taskId, SubTask subTask) { if (subTask.getId() != null) { throw new IllegalArgumentException("Subtask already has ID: " + subTask); } try (var connection = dataSource.getConnection(); Loading @@ -61,71 +57,68 @@ public class SubTaskDao implements OneToNDao<SubTask> { "INSERT INTO SUBTASK (TASK_ID, TITLE, IS_COMPLETED) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) { st.setLong(1, taskId); st.setString(2, entity.getTitle()); st.setBoolean(3, entity.isCompleted()); st.setString(2, subTask.getTitle()); st.setBoolean(3, subTask.isCompleted()); st.executeUpdate(); try (var rs = st.getGeneratedKeys()) { if (rs.getMetaData().getColumnCount() != 1) { throw new DataAccessException("Failed to fetch generated key: compound key returned for subtask: " + entity); throw new DataAccessException("Failed to fetch generated key: compound key returned for subtask: " + subTask); } if (rs.next()) { entity.setId(rs.getLong(1)); subTask.setId(rs.getLong(1)); } else { throw new DataAccessException("Failed to fetch generated key: no key returned for subtask: " + entity); throw new DataAccessException("Failed to fetch generated key: no key returned for subtask: " + subTask); } if (rs.next()) { throw new DataAccessException("Failed to fetch generated key: multiple keys returned for subtask: " + entity); throw new DataAccessException("Failed to fetch generated key: multiple keys returned for subtask: " + subTask); } } } catch (SQLException ex) { throw new DataAccessException("Failed to store subtask " + entity, ex); throw new DataAccessException("Failed to store subtask " + subTask, ex); } } @Override public void update(SubTask entity) { if (entity.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + entity); public void update(SubTask subTask) { if (subTask.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + subTask); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement( "UPDATE SUBTASK SET TITLE = ?, IS_COMPLETED = ? WHERE ID = ?")) { st.setString(1, entity.getTitle()); st.setBoolean(2, entity.isCompleted()); st.setLong(3, entity.getId()); st.setString(1, subTask.getTitle()); st.setBoolean(2, subTask.isCompleted()); st.setLong(3, subTask.getId()); int rowsUpdated = st.executeUpdate(); if (rowsUpdated == 0) { throw new DataAccessException("Failed to update non-existing subtask: " + entity); throw new DataAccessException("Failed to update non-existing subtask: " + subTask); } } catch (SQLException ex) { throw new DataAccessException("Failed to update subtask " + entity, ex); throw new DataAccessException("Failed to update subtask " + subTask, ex); } } @Override public void delete(SubTask entity) { if (entity.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + entity); public void delete(SubTask subTask) { if (subTask.getId() == null) { throw new IllegalArgumentException("Subtask has null ID: " + subTask); } try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM SUBTASK WHERE ID = ?")) { st.setLong(1, entity.getId()); st.setLong(1, subTask.getId()); int rowsDeleted = st.executeUpdate(); if (rowsDeleted == 0) { throw new DataAccessException("Failed to delete non-existing subtask: " + entity); throw new DataAccessException("Failed to delete non-existing subtask: " + subTask); } } catch (SQLException ex) { throw new DataAccessException("Failed to delete subtask " + entity, ex); throw new DataAccessException("Failed to delete subtask " + subTask, ex); } } @Override public SubTask findById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look for subtask of null ID"); Loading
src/main/java/cz/muni/fi/pv168/project/db/task/TaskDao.java +6 −6 Original line number Diff line number Diff line Loading @@ -151,7 +151,7 @@ public class TaskDao implements DataAccessObject<Task> { rs.getString("TITLE"), rs.getLong("ESTIMATED_TIME"), rs.getDate("DUE_DATE").toLocalDate()) .setSubTasks(subTaskDao.getAllEntitiesFor(taskId)) .setSubTasks(subTaskDao.getAllSubTasksForTask(taskId)) .setDependencyTasks(List.of()) .setCategories(List.of()); var description = rs.getString("DESCRIPTION"); Loading Loading @@ -192,10 +192,10 @@ public class TaskDao implements DataAccessObject<Task> { } // cleanup previous subTaskDao.deleteAllEntitiesFor(entity.getId()); subTaskDao.deleteAllSubTasksForTask(entity.getId()); dependencyDao.deleteAllDependencyAssociationsForTask(entity.getId()); taskCategoryDao.deleteAllCategoryAssociationsForTask(entity.getId()); var timeSpents = categoryTimeDao.getAllEntitiesForTask(entity.getId()); var timeSpents = categoryTimeDao.getAllCategoryTimesForTask(entity.getId()); for (var timeSpent : timeSpents) { timeSpent.setHoursSpent(entity.getEstimatedTime()); categoryTimeDao.update(timeSpent); Loading @@ -211,7 +211,7 @@ public class TaskDao implements DataAccessObject<Task> { private void addTaskFieldsToDaos(Task entity) { for (var subtask : entity.getSubTasks()) { subtask.setId(null); subTaskDao.addEntityFor(entity.getId(), subtask); subTaskDao.addSubTaskForTask(entity.getId(), subtask); } for (var dependency : entity.getDependencyTasks()) { dependencyDao.addDependencyAssociationFor(entity.getId(), dependency.getId()); Loading @@ -227,10 +227,10 @@ public class TaskDao implements DataAccessObject<Task> { throw new IllegalArgumentException("Task has null ID: " + entity); } subTaskDao.deleteAllEntitiesFor(entity.getId()); subTaskDao.deleteAllSubTasksForTask(entity.getId()); dependencyDao.deleteAllDependencyAssociationsForTask(entity.getId()); taskCategoryDao.deleteAllCategoryAssociationsForTask(entity.getId()); categoryTimeDao.deleteAllEntitiesForTask(entity.getId()); categoryTimeDao.deleteAllCategoryTimesForTask(entity.getId()); try (var connection = dataSource.getConnection(); var st = connection.prepareStatement("DELETE FROM TASK WHERE ID = ?")) { Loading