From 45035e565c29ee7c5d900c24160202bc39ce1213 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Fri, 9 Dec 2022 01:27:37 +0100 Subject: [PATCH 1/2] update tasks with subscription from JsonTaskList --- .../CropDetailController.java | 24 ++++++++++----- .../MyScheduleController.java | 17 ++++------- .../TaskFormularController.java | 4 +-- .../weather/WeatherGradenTaskPlanner.java | 3 +- .../gartenverwaltung/io/JsonTaskList.java | 2 +- .../ch/zhaw/gartenverwaltung/io/TaskList.java | 2 +- .../models/GardenSchedule.java | 30 ++++++++++++++++--- 7 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index ab76e34..421e867 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -3,6 +3,7 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; import ch.zhaw.gartenverwaltung.bootstrap.Inject; import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.io.TaskList; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.models.GardenSchedule; @@ -128,7 +129,14 @@ public class CropDetailController { } area_label.setText(String.valueOf(crop.getArea())); - setTaskListProperty(crop); + initializeTaskListProperty(crop); + + TaskList.TaskListObserver taskListObserver = newTaskList -> { + taskListProperty.clear(); + taskListProperty.addAll(gardenSchedule.getTaskListForCrop(crop.getCropId().get())); + }; + gardenSchedule.setTaskListObserver(taskListObserver); + taskList_listView.itemsProperty().bind(taskListProperty); pestListProperty.addAll(plant.pests()); @@ -185,10 +193,10 @@ public class CropDetailController { } /** - * update task list + * initialize task list * @param crop {@link Crop} that is selected */ - private void setTaskListProperty(Crop crop) { + private void initializeTaskListProperty(Crop crop) { crop.getCropId().ifPresent(id -> { List taskList; try { @@ -326,13 +334,15 @@ public class CropDetailController { if (newTask) { try { gardenSchedule.addTask(task); - setTaskListProperty(this.crop); } catch (IOException e) { e.printStackTrace(); } } else { - //ToDo method to edit task - setTaskListProperty(this.crop); + try { + gardenSchedule.addTask(givenTask.updateTask(task)); + } catch (IOException e) { + throw new RuntimeException(e); + } } }); } @@ -387,7 +397,7 @@ public class CropDetailController { if (buttonType == ButtonType.OK) { try { gardenSchedule.removeTask(task); - setTaskListProperty(this.crop); + //setTaskListProperty(this.crop); } catch (IOException e) { // TODO: Show error alert LOG.log(Level.SEVERE, "Could not remove crop.", e); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java index 768b3aa..b72ad05 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java @@ -32,7 +32,6 @@ import java.util.logging.Logger; */ public class MyScheduleController { private static final Logger LOG = Logger.getLogger(MyScheduleController.class.getName()); - private final ListProperty> taskListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); private Crop selectedCrop = null; @@ -58,14 +57,10 @@ public class MyScheduleController { setCellFactoryCropListView(); setCellFactoryTaskListView(); scheduledPlants_listview.itemsProperty().bind(garden.getPlantedCrops()); + ListProperty> taskListProperty = gardenSchedule.getWeeklyTaskListProperty(); week_listView.itemsProperty().bind(taskListProperty); lookForSelectedListEntries(); information_label.setText(""); - try { - loadTaskList(); - } catch (IOException e) { - e.printStackTrace(); - } } /** @@ -117,8 +112,8 @@ public class MyScheduleController { super.updateItem(taskList, empty); if (empty || taskList == null) { - setGraphic(null); setText(null); + setGraphic(null); } else { setText(""); setGraphic(weekTaskVBox(taskList, this.getIndex())); @@ -134,12 +129,12 @@ public class MyScheduleController { private void loadTaskList() throws IOException { List> taskLists; if (selectedCrop != null) { - taskLists = gardenSchedule.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); + gardenSchedule.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); } else { - taskLists = gardenSchedule.getTasksUpcomingWeek(); + gardenSchedule.getTasksUpcomingWeek(); } - taskListProperty.clear(); - taskListProperty.addAll(taskLists); + //taskListProperty.clear(); + //taskListProperty.addAll(taskLists); } /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java b/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java index 2344995..791da18 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java @@ -59,7 +59,7 @@ public class TaskFormularController implements Initializable { */ public Task returnResult(Crop crop) { int interval = 0; - if (!(interval_field.getText().isEmpty() || interval_field.getText().equals(""))) { + if (interval_field.getText() != null && !(interval_field.getText().isEmpty() || interval_field.getText().equals(""))) { interval = Integer.parseInt(interval_field.getText()); } Task task = new Task(taskName_field.getText(), description_area.getText(), @@ -159,7 +159,7 @@ public class TaskFormularController implements Initializable { */ public void initSaveButton(Button button) { interval_field.textProperty().addListener((observable, oldValue, newValue) -> { - if (!newValue.matches("\\d*")) { + if (newValue != null && !newValue.matches("\\d*")) { interval_field.setText(newValue.replaceAll("[^\\d]", "")); } }); 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 ea9acfc..7852989 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/weather/WeatherGradenTaskPlanner.java @@ -43,7 +43,8 @@ public class WeatherGradenTaskPlanner { private void getSevereWeatherEvents() throws IOException { SevereWeather actualWeather = weatherService.causeSevereWeather(1); if (SevereWeather.HAIL.equals(actualWeather)) { - createPreHailTask(); + //ToDo creates hail task all 3 seconds, pls fix + //createPreHailTask(); } else if (SevereWeather.FROST.equals(actualWeather)) { createPreFrostTask(); } else if (SevereWeather.SNOW.equals(actualWeather)) { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java index 8183afb..4092330 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java @@ -154,7 +154,7 @@ public class JsonTaskList implements TaskList { /** * Calls the change handler method on all registered observers. */ - private void notifySubscribers() { + private void notifySubscribers() throws IOException { for (TaskListObserver subscriber : subscribers) { subscriber.onChange(taskMap.values().stream().toList()); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java index 60e745c..e03878c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java @@ -67,6 +67,6 @@ public interface TaskList { * Method which will be called when changes occur. * @param newTaskList The new values */ - void onChange(List newTaskList); + void onChange(List newTaskList) throws IOException; } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index f0868be..932d6b5 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -3,6 +3,9 @@ package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.Settings; import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.types.*; +import javafx.beans.property.ListProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.collections.FXCollections; import java.io.IOException; import java.time.LocalDate; @@ -20,13 +23,30 @@ public class GardenSchedule { */ static final Comparator sortByStartDate = Comparator.comparing(Task::getStartDate); static final Comparator sortByNextExecution = Comparator.comparing(Task::getNextExecution); + private final ListProperty> weeklyTaskListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); /** * Constructor to create Database Objects. */ - public GardenSchedule(TaskList taskList, PlantList plantList) { + public GardenSchedule(TaskList taskList, PlantList plantList) throws IOException { this.taskList = taskList; this.plantList = plantList; + TaskList.TaskListObserver taskListObserver = newTaskList -> { + getTasksUpcomingWeek(); + }; + setTaskListObserver(taskListObserver); + } + + public ListProperty> getWeeklyTaskListProperty() { + return weeklyTaskListProperty; + } + + /** + * subscribe task list observer to get notifications + * @param observer the task list which will be ovserved + */ + public void setTaskListObserver(TaskList.TaskListObserver observer) { + taskList.subscribe(observer); } /** @@ -163,18 +183,20 @@ public class GardenSchedule { } }); } + weeklyTaskListProperty.clear(); + weeklyTaskListProperty.addAll(dayTaskList); return dayTaskList; } /** * Method to get an List of 7 Tasklists for the next 7 days. (Filtered Index 0 is Tasklist for Today. - * @return List with length 7 (List>) * @throws IOException If the database cannot be accessed */ - public List> getTasksUpcomingWeekForCrop(Long cropId) throws IOException { + public void getTasksUpcomingWeekForCrop(Long cropId) throws IOException { List> dayTaskList = getTasksUpcomingWeek(); dayTaskList.forEach(taskList -> taskList.removeIf(task -> task.getCropId() != cropId)); - return dayTaskList; + weeklyTaskListProperty.clear(); + weeklyTaskListProperty.addAll(dayTaskList); } /** From 4d2057d0df0c6584268e1b44e65c2a8dc6458926 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Fri, 9 Dec 2022 14:16:12 +0100 Subject: [PATCH 2/2] fix update scheduler --- src/main/java/ch/zhaw/gartenverwaltung/Main.java | 5 +++-- .../java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java | 6 +++--- .../ch/zhaw/gartenverwaltung/models/GardenSchedule.java | 9 ++++++++- src/main/java/ch/zhaw/gartenverwaltung/types/Task.java | 2 +- .../ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Main.java b/src/main/java/ch/zhaw/gartenverwaltung/Main.java index d31860d..f757926 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/Main.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Main.java @@ -22,13 +22,14 @@ public class Main extends Application { AppLoader appLoader = new AppLoader(); backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(CropList) appLoader.getAppDependency(CropList.class), (PlantList) appLoader.getAppDependency(PlantList.class)); - // TODO reduce period - backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 1000); appLoader.loadSceneToStage("MainFXML.fxml", stage); stage.setTitle("Gartenverwaltung"); stage.show(); + + backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 60000); + } @Override diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java index 4092330..869783c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java @@ -67,7 +67,7 @@ public class JsonTaskList implements TaskList { * @see TaskList#getTaskList(LocalDate, LocalDate) */ @Override - public List getTaskList(LocalDate start, LocalDate end) throws IOException{ + public synchronized List getTaskList(LocalDate start, LocalDate end) throws IOException{ if(taskMap.isEmpty()) { loadTaskListFromFile(); } @@ -80,7 +80,7 @@ public class JsonTaskList implements TaskList { * @return List of Tasks for given Crop */ @Override - public List getTaskForCrop(long cropId) throws IOException { + public synchronized List getTaskForCrop(long cropId) throws IOException { if(taskMap.isEmpty()) { loadTaskListFromFile(); } @@ -111,7 +111,7 @@ public class JsonTaskList implements TaskList { * @see TaskList#saveTask(Task) */ @Override - public void saveTask(Task task) throws IOException { + public synchronized void saveTask(Task task) throws IOException { if(taskMap.isEmpty()) { loadTaskListFromFile(); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index 932d6b5..2e3eb3d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -3,6 +3,7 @@ package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.Settings; import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.types.*; +import javafx.application.Platform; import javafx.beans.property.ListProperty; import javafx.beans.property.SimpleListProperty; import javafx.collections.FXCollections; @@ -32,7 +33,13 @@ public class GardenSchedule { this.taskList = taskList; this.plantList = plantList; TaskList.TaskListObserver taskListObserver = newTaskList -> { - getTasksUpcomingWeek(); + Platform.runLater(() -> { + try { + getTasksUpcomingWeek(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); }; setTaskListObserver(taskListObserver); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 017b18f..4136d1d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -104,7 +104,7 @@ public class Task { * @return Whether the Task is within the given range */ public boolean isInTimePeriod(LocalDate searchStartDate, LocalDate searchEndDate) { - return endDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate); + return endDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate) || (nextExecution != null && nextExecution.isBefore(searchEndDate) && nextExecution.isAfter(searchStartDate)); } /** diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java index 00e21e3..2a515d7 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java @@ -130,7 +130,7 @@ public class JsonTaskListTest { } @Test - void testSubscription() { + void testSubscription() throws IOException { TaskList.TaskListObserver mockObs = Mockito.mock(TaskList.TaskListObserver.class); testDatabase.subscribe(mockObs); try {