diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java index 96eed67..eea692a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java @@ -25,11 +25,12 @@ public class BackgroundTasks extends TimerTask { private void movePastTasks() throws IOException { List taskList = this.taskList.getTaskList(LocalDate.MIN, LocalDate.now().minusDays(1)); - taskList.forEach(task -> { + for (Task task : taskList) { if (!task.isDone()) { task.setNextExecution(LocalDate.now()); + this.taskList.saveTask(task); } - }); + } } public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java index d237a72..4141bea 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -9,8 +9,12 @@ import ch.zhaw.gartenverwaltung.types.*; import java.io.IOException; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import static java.util.stream.Collectors.toList; + /** * The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days * @@ -43,8 +47,7 @@ public class WeatherGradenTaskPlanner { private void getSevereWeatherEvents() throws IOException { SevereWeather actualWeather = weatherService.causeSevereWeather(1); if (SevereWeather.HAIL.equals(actualWeather)) { - //ToDo creates hail task all 3 seconds, pls fix - //createPreHailTask(); + createPreHailTask(); } else if (SevereWeather.FROST.equals(actualWeather)) { createPreFrostTask(); } else if (SevereWeather.SNOW.equals(actualWeather)) { @@ -63,11 +66,27 @@ public class WeatherGradenTaskPlanner { */ private void createPreHailTask() throws IOException { - Task preHailTask = new Task("Hail", - "During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin", - dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L); - taskList.saveTask(preHailTask); + List actualCrops = cropList.getCrops(); + for (Crop crop : actualCrops) { + Task preHailTask = new Task("Hail", + "During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin", + dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L)); + + List actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); + List hailTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Hail")).toList(); + + List hailTaskListAtDate = new ArrayList<>(); + for (Task task : hailTasklist) { + if (task.getStartDate() == (preHailTask.getStartDate())) { + hailTaskListAtDate.add(task); + } + } + + if(hailTaskListAtDate.isEmpty() && hailTasklist.isEmpty()){ + taskList.saveTask(preHailTask); + } + } } /** @@ -75,10 +94,26 @@ public class WeatherGradenTaskPlanner { * @throws IOException If the database cannot be accessed */ private void createPreFrostTask() throws IOException { - Task preFrostTask = new Task("Frost", - "The temperatur falls below zero degrees, cover especially the root with wool", - dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L); - taskList.saveTask(preFrostTask); + List actualCrops = cropList.getCrops(); + for (Crop crop : actualCrops) { + + Task preFrostTask = new Task("Frost", + "The temperatur falls below zero degrees, cover especially the root with wool", + dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L)); + + List actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); + List frostTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Frost")).toList(); + + List frostTaskListAtDate = new ArrayList<>(); + for (Task task : frostTasklist) { + if (task.getStartDate() == preFrostTask.getStartDate()) { + frostTaskListAtDate.add(task); + } + } + if(frostTaskListAtDate.isEmpty() && frostTasklist.isEmpty()){ + taskList.saveTask(preFrostTask); + } + } } /** @@ -86,10 +121,27 @@ public class WeatherGradenTaskPlanner { * @throws IOException If the database cannot be accessed */ private void createPreSnowTask() throws IOException { - Task preSnowTask = new Task("Snow", - "The weather brings little snowfall. Cover your crops", - dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L); - taskList.saveTask(preSnowTask); + List actualCrops = cropList.getCrops(); + for (Crop crop : actualCrops) { + + Task preSnowTask = new Task("Snow", + "The weather brings little snowfall. Cover your crops", + dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L)); + + List actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); + List snowTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Snow")).toList(); + + List snowTaskListAtDate = new ArrayList<>(); + for (Task task : snowTasklist) { + if (task.getStartDate() == preSnowTask.getStartDate()) { + snowTaskListAtDate.add(task); + } + } + if(snowTaskListAtDate .isEmpty() && snowTasklist.isEmpty()){ + taskList.saveTask(preSnowTask); + } + + } } /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index d235918..62e42b0 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -63,6 +63,7 @@ public class Task { this.startDate = startDate; nextExecution = startDate; this.endDate = endDate; + this.cropId = cropId; } /** diff --git a/src/test/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGardenTaskPlannerTest.java b/src/test/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGardenTaskPlannerTest.java index 8222484..34d62e7 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGardenTaskPlannerTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGardenTaskPlannerTest.java @@ -52,7 +52,7 @@ public class WeatherGardenTaskPlannerTest { exampleTask.setNextExecution(LocalDate.now().plusDays(1L)); exampleWeatherTask = new Task("Hail", "During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin", - LocalDate.now().minusDays(1L),LocalDate.now().plusDays(1L),3L); + LocalDate.now(),LocalDate.now().plusDays(1L),3L); taskList = new JsonTaskList(testFile);