Delete Unreferenced TaskSeries
This commit is contained in:
parent
717e8d4e45
commit
ec4a1cfbc7
@ -12,7 +12,7 @@ public class TaskSerie {
|
|||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private long taskSerieID;
|
private long taskSerieID;
|
||||||
|
|
||||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "taskSerie")
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "taskSerie", orphanRemoval = true)
|
||||||
List<TaskSerieItem> tasks = new ArrayList<>();
|
List<TaskSerieItem> tasks = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ public interface TaskRepository extends CrudRepository<Task, Long> {
|
|||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@Query(value = "DELETE FROM Task t WHERE t.taskgroup = ?1")
|
||||||
void deleteAllByTaskgroup(Taskgroup taskgroup);
|
void deleteAllByTaskgroup(Taskgroup taskgroup);
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -1,9 +1,19 @@
|
|||||||
package core.repositories.timemanager;
|
package core.repositories.timemanager;
|
||||||
|
|
||||||
import core.entities.timemanager.TaskSerieItem;
|
import core.entities.timemanager.TaskSerieItem;
|
||||||
|
import core.entities.timemanager.Taskgroup;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface TaskSerieItemRepository extends CrudRepository<TaskSerieItem, Long> {
|
public interface TaskSerieItemRepository extends CrudRepository<TaskSerieItem, Long> {
|
||||||
|
|
||||||
|
@Query(value = "DELETE FROM TaskSerieItem tsi WHERE tsi.task IN (SELECT t FROM Task t WHERE t.taskgroup = ?1)")
|
||||||
|
@Modifying
|
||||||
|
@Transactional
|
||||||
|
void deleteByTaskgroup(Taskgroup taskgroup);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,23 @@
|
|||||||
package core.repositories.timemanager;
|
package core.repositories.timemanager;
|
||||||
|
|
||||||
import core.entities.timemanager.TaskSerie;
|
import core.entities.timemanager.TaskSerie;
|
||||||
|
import core.entities.timemanager.Taskgroup;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface TaskSeriesRepository extends CrudRepository<TaskSerie, Long> {
|
public interface TaskSeriesRepository extends CrudRepository<TaskSerie, Long> {
|
||||||
|
@Query("SELECT DISTINCT ts FROM TaskSerie ts JOIN ts.tasks tsi JOIN tsi.task t WHERE t.taskgroup = :taskgroup")
|
||||||
|
List<TaskSerie> findByTaskgroup(@Param("taskgroup")Taskgroup taskgroup);
|
||||||
|
|
||||||
|
@Modifying
|
||||||
|
@Transactional
|
||||||
|
@Query(value = "DELETE FROM TaskSerie ts WHERE ts.tasks.size = 0")
|
||||||
|
void deleteUnreferenced();
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import core.api.models.timemanager.tasks.repeatinginfo.TaskRepeatWeekInfo;
|
|||||||
import core.entities.timemanager.Task;
|
import core.entities.timemanager.Task;
|
||||||
import core.entities.timemanager.TaskSerie;
|
import core.entities.timemanager.TaskSerie;
|
||||||
import core.entities.timemanager.TaskSerieItem;
|
import core.entities.timemanager.TaskSerieItem;
|
||||||
|
import core.entities.timemanager.Taskgroup;
|
||||||
import core.repositories.timemanager.TaskRepository;
|
import core.repositories.timemanager.TaskRepository;
|
||||||
import core.repositories.timemanager.TaskSerieItemRepository;
|
import core.repositories.timemanager.TaskSerieItemRepository;
|
||||||
import core.repositories.timemanager.TaskSeriesRepository;
|
import core.repositories.timemanager.TaskSeriesRepository;
|
||||||
@ -40,7 +41,6 @@ public class TaskSeriesService {
|
|||||||
rootTask.setTaskSerieItem(rootItem);
|
rootTask.setTaskSerieItem(rootItem);
|
||||||
|
|
||||||
LocalDate currentTaskDate = rootTask.getStartDate().plusDays(taskRepeatInfo.getOffset());
|
LocalDate currentTaskDate = rootTask.getStartDate().plusDays(taskRepeatInfo.getOffset());
|
||||||
int index = 2;
|
|
||||||
while(currentTaskDate.isBefore(taskRepeatInfo.getEndingDate())) {
|
while(currentTaskDate.isBefore(taskRepeatInfo.getEndingDate())) {
|
||||||
Task task = Task.cloneTask(rootTask);
|
Task task = Task.cloneTask(rootTask);
|
||||||
task.setStartDate(currentTaskDate);
|
task.setStartDate(currentTaskDate);
|
||||||
@ -53,7 +53,6 @@ public class TaskSeriesService {
|
|||||||
taskList.add(task);
|
taskList.add(task);
|
||||||
task.setTaskSerieItem(taskSerieItem);
|
task.setTaskSerieItem(taskSerieItem);
|
||||||
currentTaskDate = currentTaskDate.plusDays(taskRepeatInfo.getOffset());
|
currentTaskDate = currentTaskDate.plusDays(taskRepeatInfo.getOffset());
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
taskSeriesRepository.save(taskSerie);
|
taskSeriesRepository.save(taskSerie);
|
||||||
@ -77,6 +76,11 @@ public class TaskSeriesService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteTaskSerieByTaskgroup(Taskgroup taskgroup) {
|
||||||
|
taskSerieItemRepository.deleteByTaskgroup(taskgroup);
|
||||||
|
taskSeriesRepository.deleteUnreferenced();
|
||||||
|
}
|
||||||
|
|
||||||
public static String convertIndexToString(int index) {
|
public static String convertIndexToString(int index) {
|
||||||
if(index < 10) {
|
if(index < 10) {
|
||||||
return "0" + index;
|
return "0" + index;
|
||||||
|
@ -100,6 +100,7 @@ public class TaskService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void clearTasks(Taskgroup taskgroup) {
|
public void clearTasks(Taskgroup taskgroup) {
|
||||||
|
taskSeriesService.deleteTaskSerieByTaskgroup(taskgroup);
|
||||||
taskRepository.deleteAllByTaskgroup(taskgroup);
|
taskRepository.deleteAllByTaskgroup(taskgroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user