#23 refactoring of the WeatherGradenTaskPlanner

This commit is contained in:
Gian-Andrea Hutter 2022-12-11 16:16:06 +01:00
parent dbba4e2662
commit a20edae4b8
1 changed files with 55 additions and 69 deletions

View File

@ -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<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
List<Task> 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<Task> 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<Crop> actualCrops = cropList.getCrops();
List<Task> 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<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> hailTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Hail")).toList();
List<Task> 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<Crop> actualCrops = cropList.getCrops();
for (Crop crop : actualCrops) {
Task preFrostTask = new Task("Frost",
private void createPreFrostTask(Crop crop, List<Task> 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<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> frostTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> frostTasks = actualTasksForCrop.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> 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<Task> 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<Task> 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<Crop> 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<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> snowTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Snow")).toList();
List<Task> snowTaskListAtDate = new ArrayList<>();
for (Task task : snowTasklist) {
if (task.getStartDate() == preSnowTask.getStartDate()) {
snowTaskListAtDate.add(task);
}
private boolean isNoSevereWeatherTaskAtDate(Task preSevereWeatherTask, List<Task> severeWeatherTasks) {
List<Task> 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();
}
/**