From a20edae4b8dfd83b6d04d508168cb9d418871183 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Sun, 11 Dec 2022 16:16:06 +0100 Subject: [PATCH] #23 refactoring of the WeatherGradenTaskPlanner --- .../weather/WeatherGradenTaskPlanner.java | 124 ++++++++---------- 1 file changed, 55 insertions(+), 69 deletions(-) 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 6f2a38c..555e88b 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -10,11 +10,8 @@ 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 */ @@ -45,12 +42,19 @@ public class WeatherGradenTaskPlanner { private void getSevereWeatherEvents() throws IOException { SevereWeather actualWeather = weatherService.causeSevereWeather(1); - if (SevereWeather.HAIL.equals(actualWeather)) { - createPreHailTask(); - } else if (SevereWeather.FROST.equals(actualWeather)) { - createPreFrostTask(); - } else if (SevereWeather.SNOW.equals(actualWeather)) { - createPreSnowTask(); + + List actualCrops = cropList.getCrops(); + for (Crop crop : actualCrops) { + + List actualCropTasks = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); + + if (SevereWeather.HAIL.equals(actualWeather)) { + createPreHailTask(crop, actualCropTasks); + } else if (SevereWeather.FROST.equals(actualWeather)) { + createPreFrostTask(crop, actualCropTasks); + } else if (SevereWeather.SNOW.equals(actualWeather)) { + createPreSnowTask(crop, actualCropTasks); + } } } @@ -60,87 +64,69 @@ public class WeatherGradenTaskPlanner { } /** - * Method to create a PreHailTask + * Method to create a PreHailTask and saves it in the tasklist * @throws IOException If the database cannot be accessed */ - private void createPreHailTask() throws IOException { + private void createPreHailTask(Crop crop, List actualTasksForCrop) 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,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L)); - List actualCrops = cropList.getCrops(); + List hailTasks = actualTasksForCrop.stream().filter(task -> task.getName().equals("Hail")).toList(); - 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); - } + if(isNoSevereWeatherTaskAtDate(preHailTask, hailTasks) && hailTasks.isEmpty()){ + taskList.saveTask(preHailTask); } } /** - * Method to create a PreFrosttask + * Method to create a PreFrosttask and saves it in the tasklist * @throws IOException If the database cannot be accessed */ - private void createPreFrostTask() throws IOException { - List actualCrops = cropList.getCrops(); - for (Crop crop : actualCrops) { - - Task preFrostTask = new Task("Frost", + private void createPreFrostTask(Crop crop, List actualTasksForCrop) throws IOException { + 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 frostTasks = actualTasksForCrop.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); - } + if(isNoSevereWeatherTaskAtDate(preFrostTask, frostTasks) && frostTasks.isEmpty()){ + taskList.saveTask(preFrostTask); + } + } + + /** + * Method to create a PreSnowTask and saves it in the tasklist + * @throws IOException If the database cannot be accessed + */ + private void createPreSnowTask(Crop crop, List actualTasksForCrop) throws IOException { + Task preSnowTask = new Task("Snow", + "The weather brings little snowfall. Cover your crops", + dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L)); + + List snowTasklist = actualTasksForCrop.stream().filter(task -> task.getName().equals("Snow")).toList(); + + if(isNoSevereWeatherTaskAtDate(preSnowTask, snowTasklist) && snowTasklist.isEmpty()){ + taskList.saveTask(preSnowTask); } } /** - * Method to create a PreSnowTask - * @throws IOException If the database cannot be accessed + * Method to create a PreSnowTask and saves it in the tasklist + * @param preSevereWeatherTask the Task which would be added if there is not already + * the same Type of severe weather task + * @param severeWeatherTasks List of severe weather tasks from e specific severe weather + * @return true If there is not already a severe weather task of the same type of preSevereWeatherTask + * task at the date of the preSevereWeatherTask */ - private void createPreSnowTask() throws IOException { - 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); - } + private boolean isNoSevereWeatherTaskAtDate(Task preSevereWeatherTask, List severeWeatherTasks) { + List severeWeatherTasksAtDate = new ArrayList<>(); + for (Task task : severeWeatherTasks) { + if (task.getStartDate() == preSevereWeatherTask.getStartDate()) { + severeWeatherTasksAtDate.add(task); } - if(snowTaskListAtDate .isEmpty() && snowTasklist.isEmpty()){ - taskList.saveTask(preSnowTask); - } - } + return severeWeatherTasksAtDate.isEmpty(); } /**