issue-106 #107

Merged
sebastian merged 18 commits from issue-106 into master 2024-03-17 09:17:11 +01:00
2 changed files with 66 additions and 48 deletions
Showing only changes of commit fea17c3fb9 - Show all commits

View File

@ -2,9 +2,13 @@ package core.api.models.timemanager.tasks.repeatinginfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.hibernate.validator.constraints.Length;
import util.Tupel;
import javax.validation.constraints.Size;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
public class TaskRepeatWeekInfo {

View File

@ -29,44 +29,27 @@ public class TaskSeriesService {
public ServiceExitCode createTaskSeries(TaskRepeatWeekInfo taskRepeatInfo) {
Map<TaskRepeatWeekDayInfo, Tupel<Integer, Integer>> offsetMap = calcOffsetMap(taskRepeatInfo);
HashMap<Task, Tupel<Integer, Integer>> offsetMap = calcWeeklyOffsetMap(taskRepeatInfo);
TaskSerie taskSerie = new TaskSerie();
List<Task> clonedTasks = new ArrayList<>();
List<AbstractSchedule> clonedSchedules = new ArrayList<>();
int weekDayIndex = 0;
for(TaskRepeatWeekDayInfo weekDayInfo : taskRepeatInfo.getWeekDayInfos()) {
Optional<Task> requestedTask = taskRepository.findById(weekDayInfo.getTaskID());
if(requestedTask.isEmpty()) return ServiceExitCode.MISSING_ENTITY;
Task rootTask = requestedTask.get();
for(Map.Entry<Task, Tupel<Integer, Integer>> repeatingTaskInfo: offsetMap.entrySet()) {
Task rootTask = repeatingTaskInfo.getKey();
TaskSerieItem rootItem = new TaskSerieItem(rootTask, weekDayIndex);
taskSerie.addItem(rootItem);
LocalDate currentDate = rootTask.getStartDate().plusDays(weekDayInfo.getOffset());
int itemIndex = weekDayIndex;
while(currentDate.isBefore(taskRepeatInfo.getEndDate())) {
var cloneResult = rootTask.cloneTask();
Task clonedRootTask = cloneResult.getValue00();
clonedTasks.addAll(cloneResult.getValue01());
clonedSchedules.addAll(cloneResult.getValue02());
for(Task clonedTask : cloneResult.getValue01()) {
TaskSerieItem item = new TaskSerieItem(clonedTask, itemIndex);
taskSerie.addItem(item);
}
Tupel<Integer, Integer> offsetEntry = offsetMap.get(weekDayInfo);
clonedRootTask.shiftTask(offsetEntry.getValue00(), offsetEntry.getValue01());
currentDate = currentDate.plusDays(weekDayInfo.getOffset());
itemIndex += taskRepeatInfo.getWeekDayInfos().size();
}
int itemIndex = weekDayIndex +1;
Tupel<Collection<Task>, Collection<AbstractSchedule>> repeatingResult = repeatTask(rootTask, taskRepeatInfo.getEndDate(), offsetMap, taskSerie, itemIndex);
clonedTasks.addAll(repeatingResult.getValue00());
clonedSchedules.addAll(repeatingResult.getValue01());
weekDayIndex++;
}
taskSeriesRepository.save(taskSerie);
taskRepository.saveAll(clonedTasks);
taskSerieItemRepository.saveAll(taskSerie.getTasks());
@ -75,33 +58,69 @@ public class TaskSeriesService {
return ServiceExitCode.OK;
}
private HashMap<TaskRepeatWeekDayInfo, Tupel<Integer, Integer>> calcOffsetMap(TaskRepeatWeekInfo weekInfo) {
HashMap<TaskRepeatWeekDayInfo, Tupel<Integer, Integer>> offsetMap = new HashMap<>();
private Tupel<Collection<Task>, Collection<AbstractSchedule>> repeatTask(Task rootTask, LocalDate endingDate, HashMap<Task, Tupel<Integer, Integer>> offsetMap,TaskSerie taskSerie, int itemIndex) {
List<Task> clonedTasks = new ArrayList<>();
List<AbstractSchedule> clonedSchedules = new ArrayList<>();
LocalDate currentDate = rootTask.getStartDate().plusDays(offsetMap.get(rootTask).getValue00());
while(currentDate.isBefore(endingDate)) {
var cloneResult = rootTask.cloneTask();
Task clonedRootTask = cloneResult.getValue00();
clonedTasks.addAll(cloneResult.getValue01());
clonedSchedules.addAll(cloneResult.getValue02());
for(Task clonedTask : cloneResult.getValue01()) {
TaskSerieItem item = new TaskSerieItem(clonedTask, itemIndex);
taskSerie.addItem(item);
}
Tupel<Integer, Integer> offsetEntry = offsetMap.get(rootTask);
clonedRootTask.shiftTask(offsetEntry.getValue00(), offsetEntry.getValue01());
currentDate = currentDate.plusDays(offsetEntry.getValue00());
itemIndex += offsetMap.size();
}
return new Tupel<>(clonedTasks, clonedSchedules);
}
private HashMap<Task, Tupel<Integer, Integer>> calcWeeklyOffsetMap(TaskRepeatWeekInfo weekInfo) throws NoSuchElementException {
HashMap<Task, Tupel<Integer, Integer>> offsetMap = new HashMap<>();
weekInfo.getWeekDayInfos().sort(Comparator.comparing(TaskRepeatWeekDayInfo::getDayOfWeek));
for(int i=0; i<weekInfo.getWeekDayInfos().size(); i++) {
if(weekInfo.getDeadlineStrategy() == DeadlineStrategy.DEADLINE_EQUAL_START) {
int offset = weekInfo.getWeekDayInfos().get(i).getOffset()-1;
offsetMap.put(weekInfo.getWeekDayInfos().get(i), new Tupel<>(offset, offset));
Optional<Task> requestedTask = taskRepository.findById(weekInfo.getWeekDayInfos().get(i).getTaskID());
if(requestedTask.isEmpty()) {
throw new NoSuchElementException();
} else {
int startingOffset = weekInfo.getWeekDayInfos().get(i).getOffset() -1;
DayOfWeek nextWeekday = null;
if(i == weekInfo.getWeekDayInfos().size()-1) {
nextWeekday = weekInfo.getWeekDayInfos().get(0).getDayOfWeek();
if(weekInfo.getDeadlineStrategy() == DeadlineStrategy.DEADLINE_EQUAL_START) {
int offset = weekInfo.getWeekDayInfos().get(i).getOffset()-1;
offsetMap.put(requestedTask.get(), new Tupel<>(offset, offset));
} else {
nextWeekday = weekInfo.getWeekDayInfos().get(i+1).getDayOfWeek();
int startingOffset = weekInfo.getWeekDayInfos().get(i).getOffset() -1;
int endingOffset = getEndingOffset(weekInfo, i, startingOffset);
offsetMap.put(requestedTask.get(), new Tupel<>(startingOffset, endingOffset));
}
DayOfWeek currentWeekDay = weekInfo.getWeekDayInfos().get(i).getDayOfWeek();
int endingOffset = startingOffset + Math.abs(nextWeekday.getValue() - currentWeekDay.getValue());
offsetMap.put(weekInfo.getWeekDayInfos().get(i), new Tupel<>(startingOffset, endingOffset));
}
}
return offsetMap;
}
private static int getEndingOffset(TaskRepeatWeekInfo weekInfo, int i, int startingOffset) {
DayOfWeek nextWeekday;
if(i == weekInfo.getWeekDayInfos().size()-1) {
nextWeekday = weekInfo.getWeekDayInfos().get(0).getDayOfWeek();
} else {
nextWeekday = weekInfo.getWeekDayInfos().get(i +1).getDayOfWeek();
}
DayOfWeek currentWeekDay = weekInfo.getWeekDayInfos().get(i).getDayOfWeek();
int endingOffset = startingOffset + Math.abs(nextWeekday.getValue() - currentWeekDay.getValue());
return endingOffset;
}
public ServiceExitCode createTaskSeries(Task rootTask, TaskRepeatDayInfo taskRepeatInfo) {
if(taskRepeatInfo.getDeadlineStrategy() == DeadlineStrategy.FIX_DEADLINE) {
return ServiceExitCode.INVALID_PARAMETER;
@ -113,12 +132,7 @@ public class TaskSeriesService {
TaskSerieItem rootItem = taskSerie.addTask(rootTask);
rootTask.setTaskSerieItem(rootItem);
LocalDate currentTaskDate = rootTask.getStartDate().plusDays(taskRepeatInfo.getOffset());
while(currentTaskDate.isBefore(taskRepeatInfo.getEndingDate())) {
Tupel<Task, Collection<Task>> clonedTasks = rootTask.cloneTask();
currentTaskDate = currentTaskDate.plusDays(taskRepeatInfo.getOffset());
}
taskSeriesRepository.save(taskSerie);
taskRepository.saveAll(taskList);