issue-77 #105

Merged
sebastian merged 17 commits from issue-77 into master 2024-03-16 10:12:35 +01:00
2 changed files with 34 additions and 0 deletions
Showing only changes of commit 015d82587c - Show all commits

View File

@ -1,6 +1,7 @@
package core.entities.timemanager; package core.entities.timemanager;
import javax.persistence.*; import javax.persistence.*;
import java.util.Objects;
@Entity @Entity
@Table(name = "task_series_items") @Table(name = "task_series_items")
@ -61,4 +62,17 @@ public class TaskSerieItem {
public void setTask(Task task) { public void setTask(Task task) {
this.task = task; this.task = task;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TaskSerieItem that = (TaskSerieItem) o;
return itemID == that.itemID;
}
@Override
public int hashCode() {
return Objects.hash(itemID);
}
} }

View File

@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
@Service @Service
@ -65,6 +66,7 @@ public class TaskSeriesService {
public void deleteTaskSeriesItem(Task task) { public void deleteTaskSeriesItem(Task task) {
TaskSerieItem item = task.getTaskSerieItem(); TaskSerieItem item = task.getTaskSerieItem();
TaskSerie taskSerie = task.getTaskSerieItem().getTaskSerie(); TaskSerie taskSerie = task.getTaskSerieItem().getTaskSerie();
taskSerie.getTasks().remove(item);
task.setTaskSerieItem(null); task.setTaskSerieItem(null);
taskSerieItemRepository.delete(item); taskSerieItemRepository.delete(item);
if(taskSerie.getTasks().isEmpty()) { if(taskSerie.getTasks().isEmpty()) {
@ -73,9 +75,27 @@ public class TaskSeriesService {
} }
taskSerie.getTasks().clear(); taskSerie.getTasks().clear();
taskSeriesRepository.delete(taskSerie); taskSeriesRepository.delete(taskSerie);
} else {
repearIndexing(taskSerie);
} }
} }
private void repearIndexing(TaskSerie taskSerie) {
taskSerie.getTasks().sort(Comparator.comparingInt(TaskSerieItem::getSeriesIndex));
List<TaskSerieItem> updatedItems = new ArrayList<>();
int currentIndex = 1;
for(TaskSerieItem taskSerieItem : taskSerie.getTasks()) {
if(taskSerieItem.getSeriesIndex() != currentIndex) {
taskSerieItem.setSeriesIndex(currentIndex);
updatedItems.add(taskSerieItem);
}
currentIndex++;
}
taskSerieItemRepository.saveAll(updatedItems);
}
public void deleteTaskSerieByTaskgroup(Taskgroup taskgroup) { public void deleteTaskSerieByTaskgroup(Taskgroup taskgroup) {
taskSerieItemRepository.deleteByTaskgroup(taskgroup); taskSerieItemRepository.deleteByTaskgroup(taskgroup);
taskSeriesRepository.deleteUnreferenced(); taskSeriesRepository.deleteUnreferenced();