Merge pull request #62 from schrom01/feature_taskList_m2

completed Tests for GardenScheduleTest and new Structure of Task
This commit is contained in:
gulerdav
2022-11-25 13:28:33 +01:00
committed by GitHub Enterprise
8 changed files with 166 additions and 83 deletions
@@ -9,6 +9,7 @@ import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class GardenSchedule {
@@ -19,6 +20,7 @@ public class GardenSchedule {
* Comparators to create sorted Task List
*/
static final Comparator<Task> sortByStartDate = Comparator.comparing(Task::getStartDate);
static final Comparator<Task> sortByNextExecution = Comparator.comparing(Task::getNextExecution);
/**
* Constructor to create Database Objects.
@@ -46,7 +48,8 @@ public class GardenSchedule {
*/
public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException {
Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new);
for (GrowthPhase growthPhase : plant.lifecycle()) {
int growPhaseGroup = plant.getGrowphaseGroupForDate(crop.getStartDate());
for (GrowthPhase growthPhase : plant.lifecycleForGroup(growPhaseGroup)) {
for (TaskTemplate taskTemplate : growthPhase.taskTemplates()) {
addTask(taskTemplate.generateTask(crop.getStartDate(), crop.getCropId().orElse(0L)));
}
@@ -135,10 +138,22 @@ public class GardenSchedule {
* @throws IOException If the database cannot be accessed
*/
public List<List<Task>> getTasksUpcomingWeek() throws IOException {
final int listLength = 7;
List<Task> weekTasks = taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(listLength - 1));
List<List<Task>> dayTaskList = new ArrayList<>();
for(int i = 0; i < 7; i++) {
for(int i = 0; i < listLength; i++) {
LocalDate date = LocalDate.now().plusDays(i);
dayTaskList.add(taskList.getTaskList(date, date));
dayTaskList.add(new ArrayList<>());
final int finalI = i;
weekTasks.forEach(task -> {
LocalDate checkDate = task.getNextExecution();
do {
if (date.equals(checkDate) && !date.isAfter(task.getEndDate().orElse(LocalDate.MIN))) {
dayTaskList.get(finalI).add(task);
}
checkDate = checkDate.plusDays(task.getInterval().orElse(0));
} while (task.getInterval().isPresent() && checkDate.isBefore(LocalDate.now().plusDays(listLength)));
});
}
return dayTaskList;
}
@@ -149,11 +164,8 @@ public class GardenSchedule {
* @throws IOException If the database cannot be accessed
*/
public List<List<Task>> getTasksUpcomingWeekForCrop(Long cropId) throws IOException {
List<List<Task>> dayTaskList = new ArrayList<>();
for(int i = 0; i < 7; i++) {
LocalDate date = LocalDate.now().plusDays(i);
dayTaskList.add(filterListByCrop(taskList.getTaskList(date, date), cropId));
}
List<List<Task>> dayTaskList = getTasksUpcomingWeek();
dayTaskList.forEach(taskList -> taskList.removeIf(task -> task.getCropId() != cropId));
return dayTaskList;
}
@@ -165,7 +177,7 @@ public class GardenSchedule {
* @throws IOException If the database cannot be accessed
*/
public List<Task> getFilteredTaskList(LocalDate start, LocalDate end) throws IOException {
return getSortedTaskList(taskList.getTaskList(start, end), sortByStartDate);
return getSortedTaskList(taskList.getTaskList(start, end), sortByNextExecution);
}
/**
@@ -39,6 +39,16 @@ public record Plant(
.collect(Collectors.toList());
}
public int getGrowphaseGroupForDate(LocalDate date) {
for(GrowthPhase growthPhase : lifecycle){
MonthDay plantingDate = MonthDay.of(date.getMonth().getValue(), date.getDayOfMonth());
if(plantingDate.isAfter(growthPhase.startDate()) && plantingDate.isBefore(growthPhase.endDate())){
return growthPhase.group();
}
}
return 0; // TODO implement
}
/**
* get sow date from given harvest day from lifecycle group
* @param harvestDate date of the harvest
@@ -15,6 +15,8 @@ public class Task {
private final LocalDate startDate;
private Integer interval;
private LocalDate endDate;
private LocalDate nextExecution;
private LocalDate nextNotification;
private long cropId;
/**
@@ -25,12 +27,16 @@ public class Task {
name= "";
description= "";
startDate = LocalDate.now();
endDate = startDate;
nextExecution = startDate;
}
public Task(String name, String description, LocalDate startDate, long cropId) {
this.name = name;
this.description = description;
this.startDate = startDate;
this.endDate = startDate;
nextExecution = startDate;
this.cropId = cropId;
}
@@ -38,6 +44,7 @@ public class Task {
this.name = name;
this.description = description;
this.startDate = startDate;
nextExecution = startDate;
this.endDate = endDate;
this.interval = interval;
this.cropId = cropId;
@@ -58,10 +65,32 @@ public class Task {
}
public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){
return startDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate);
return endDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate);
}
public void done(){
if(interval != null && !nextExecution.plusDays(interval).isAfter(endDate)){
nextExecution = nextExecution.plusDays(interval);
} else {
nextExecution = null;
}
}
public boolean isDone(){
return nextExecution == null;
}
public void setNextExecution(LocalDate nextExecution) {
this.nextExecution = nextExecution;
}
public void setNextNotification(LocalDate nextNotification) {
this.nextNotification = nextNotification;
}
// Getters
public LocalDate getNextNotification() { return nextNotification; }
public LocalDate getNextExecution() { return nextExecution; }
public Optional<Long> getId() { return Optional.ofNullable(id); }
public String getName() { return name; }
public String getDescription() { return description; }
@@ -16,10 +16,6 @@ public class TaskTemplate {
@JsonProperty
private Integer interval;
// TODO: reconsider if we need this
@JsonProperty
private boolean isOptional = false;
/**
* Default constructor
* (Used by deserializer)