Refactor Code for Repeating Weekly
This commit is contained in:
parent
f0d50a280e
commit
fea17c3fb9
@ -2,9 +2,13 @@ package core.api.models.timemanager.tasks.repeatinginfo;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import org.hibernate.validator.constraints.Length;
|
import org.hibernate.validator.constraints.Length;
|
||||||
|
import util.Tupel;
|
||||||
|
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
import java.time.DayOfWeek;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TaskRepeatWeekInfo {
|
public class TaskRepeatWeekInfo {
|
||||||
|
@ -29,44 +29,27 @@ public class TaskSeriesService {
|
|||||||
|
|
||||||
|
|
||||||
public ServiceExitCode createTaskSeries(TaskRepeatWeekInfo taskRepeatInfo) {
|
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();
|
TaskSerie taskSerie = new TaskSerie();
|
||||||
List<Task> clonedTasks = new ArrayList<>();
|
List<Task> clonedTasks = new ArrayList<>();
|
||||||
List<AbstractSchedule> clonedSchedules = new ArrayList<>();
|
List<AbstractSchedule> clonedSchedules = new ArrayList<>();
|
||||||
int weekDayIndex = 0;
|
int weekDayIndex = 0;
|
||||||
for(TaskRepeatWeekDayInfo weekDayInfo : taskRepeatInfo.getWeekDayInfos()) {
|
for(Map.Entry<Task, Tupel<Integer, Integer>> repeatingTaskInfo: offsetMap.entrySet()) {
|
||||||
Optional<Task> requestedTask = taskRepository.findById(weekDayInfo.getTaskID());
|
Task rootTask = repeatingTaskInfo.getKey();
|
||||||
if(requestedTask.isEmpty()) return ServiceExitCode.MISSING_ENTITY;
|
TaskSerieItem rootItem = new TaskSerieItem(rootTask, weekDayIndex);
|
||||||
Task rootTask = requestedTask.get();
|
taskSerie.addItem(rootItem);
|
||||||
|
|
||||||
LocalDate currentDate = rootTask.getStartDate().plusDays(weekDayInfo.getOffset());
|
int itemIndex = weekDayIndex +1;
|
||||||
|
Tupel<Collection<Task>, Collection<AbstractSchedule>> repeatingResult = repeatTask(rootTask, taskRepeatInfo.getEndDate(), offsetMap, taskSerie, itemIndex);
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
clonedTasks.addAll(repeatingResult.getValue00());
|
||||||
|
clonedSchedules.addAll(repeatingResult.getValue01());
|
||||||
|
|
||||||
weekDayIndex++;
|
weekDayIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
taskSeriesRepository.save(taskSerie);
|
taskSeriesRepository.save(taskSerie);
|
||||||
taskRepository.saveAll(clonedTasks);
|
taskRepository.saveAll(clonedTasks);
|
||||||
taskSerieItemRepository.saveAll(taskSerie.getTasks());
|
taskSerieItemRepository.saveAll(taskSerie.getTasks());
|
||||||
@ -75,33 +58,69 @@ public class TaskSeriesService {
|
|||||||
return ServiceExitCode.OK;
|
return ServiceExitCode.OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<TaskRepeatWeekDayInfo, Tupel<Integer, Integer>> calcOffsetMap(TaskRepeatWeekInfo weekInfo) {
|
private Tupel<Collection<Task>, Collection<AbstractSchedule>> repeatTask(Task rootTask, LocalDate endingDate, HashMap<Task, Tupel<Integer, Integer>> offsetMap,TaskSerie taskSerie, int itemIndex) {
|
||||||
HashMap<TaskRepeatWeekDayInfo, Tupel<Integer, Integer>> offsetMap = new HashMap<>();
|
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));
|
weekInfo.getWeekDayInfos().sort(Comparator.comparing(TaskRepeatWeekDayInfo::getDayOfWeek));
|
||||||
for(int i=0; i<weekInfo.getWeekDayInfos().size(); i++) {
|
for(int i=0; i<weekInfo.getWeekDayInfos().size(); i++) {
|
||||||
if(weekInfo.getDeadlineStrategy() == DeadlineStrategy.DEADLINE_EQUAL_START) {
|
Optional<Task> requestedTask = taskRepository.findById(weekInfo.getWeekDayInfos().get(i).getTaskID());
|
||||||
int offset = weekInfo.getWeekDayInfos().get(i).getOffset()-1;
|
if(requestedTask.isEmpty()) {
|
||||||
offsetMap.put(weekInfo.getWeekDayInfos().get(i), new Tupel<>(offset, offset));
|
throw new NoSuchElementException();
|
||||||
} else {
|
} else {
|
||||||
int startingOffset = weekInfo.getWeekDayInfos().get(i).getOffset() -1;
|
if(weekInfo.getDeadlineStrategy() == DeadlineStrategy.DEADLINE_EQUAL_START) {
|
||||||
|
int offset = weekInfo.getWeekDayInfos().get(i).getOffset()-1;
|
||||||
DayOfWeek nextWeekday = null;
|
offsetMap.put(requestedTask.get(), new Tupel<>(offset, offset));
|
||||||
if(i == weekInfo.getWeekDayInfos().size()-1) {
|
|
||||||
nextWeekday = weekInfo.getWeekDayInfos().get(0).getDayOfWeek();
|
|
||||||
} else {
|
} 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;
|
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) {
|
public ServiceExitCode createTaskSeries(Task rootTask, TaskRepeatDayInfo taskRepeatInfo) {
|
||||||
if(taskRepeatInfo.getDeadlineStrategy() == DeadlineStrategy.FIX_DEADLINE) {
|
if(taskRepeatInfo.getDeadlineStrategy() == DeadlineStrategy.FIX_DEADLINE) {
|
||||||
return ServiceExitCode.INVALID_PARAMETER;
|
return ServiceExitCode.INVALID_PARAMETER;
|
||||||
@ -113,12 +132,7 @@ public class TaskSeriesService {
|
|||||||
TaskSerieItem rootItem = taskSerie.addTask(rootTask);
|
TaskSerieItem rootItem = taskSerie.addTask(rootTask);
|
||||||
rootTask.setTaskSerieItem(rootItem);
|
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);
|
taskSeriesRepository.save(taskSerie);
|
||||||
taskRepository.saveAll(taskList);
|
taskRepository.saveAll(taskList);
|
||||||
|
Loading…
Reference in New Issue
Block a user