From 6523e1d79118af9e1720355240ef948f69a9609b Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 12 Nov 2022 16:13:02 +0100 Subject: [PATCH 001/105] added jacoco --- build.gradle | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 88a7a2b..35ebaa2 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,7 @@ plugins { id 'application' id 'org.openjfx.javafxplugin' version '0.0.13' id 'org.beryx.jlink' version '2.25.0' + id 'jacoco' } group 'ch.zhaw.gartenverwaltung' @@ -45,6 +46,15 @@ dependencies { test { useJUnitPlatform() + finalizedBy jacocoTestReport +} + +jacocoTestReport { + dependsOn test +} + +jacoco { + toolVersion = "0.8.8" } jlink { @@ -57,4 +67,4 @@ jlink { jlinkZip { group = 'distribution' -} \ No newline at end of file +} From a7fa58344c754b9004d417a92ce6ef95a789fb0e Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 12 Nov 2022 16:45:18 +0100 Subject: [PATCH 002/105] Continued testing for TaskDatabase Moved from branch and extended --- .../io/JsonTaskDatabaseTest.java | 103 +++++++++++++++--- .../zhaw/gartenverwaltung/io/test-taskdb.json | 56 ++++++++++ 2 files changed, 144 insertions(+), 15 deletions(-) create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java index d3710ab..3ffa0fc 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java @@ -4,44 +4,117 @@ import ch.zhaw.gartenverwaltung.types.Task; import org.junit.jupiter.api.*; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + public class JsonTaskDatabaseTest { TaskDatabase testDatabase; - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); - @BeforeEach - void connectToDb() { - // testDatabase = new JsonTaskDatabase(); - } + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + private final URL dbDataSource = this.getClass().getResource("taskdb.json"); + private final URL testFile = this.getClass().getResource("test-taskdb.json"); + + @BeforeEach + void connectToDb() throws URISyntaxException, IOException { + assertNotNull(testFile); + assertNotNull(dbDataSource); + Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); + testDatabase = new JsonTaskDatabase(); + } @Test @DisplayName("Check if results are retrieved completely") - void getTasks(){ - /* - List taskList=null; + void getTasks() { + + List taskList = null; try { - taskList = testDatabase.getTaskList(formatter.parse("01.05.2022"), formatter.parse("01.08.2022")); + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); } catch (IOException e) { throw new RuntimeException(e); - } catch (ParseException e) { - throw new RuntimeException(e); } - Assertions.assertTrue(taskList.size()>0); - */ + Assertions.assertEquals(3, taskList.size()); + + } + + //@Disabled("disabled until idProvider works") + @Test + @DisplayName("Add task.") + void addTask() { + Task task = new Task("Testtask", "This is a test Task.", LocalDate.parse("01.05.2022", formatter), 1); + try { + testDatabase.saveTask(task); + List taskList = null; + try { + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(4, taskList.size()); + + } catch (IOException e) { + throw new RuntimeException(e); + } } + @Test + @DisplayName("Remove task.") + void removeTask() { + Task task = new Task("Dummy", "Dummy", LocalDate.parse("31.05.2022", formatter), 1).withId(2); + try { + testDatabase.removeTask(task); + List taskList = null; + + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); + + Assertions.assertEquals(2, taskList.size()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + @Test void getTaskForCrop() { - // TODO implement Test + List taskList = null; + try { + taskList = testDatabase.getTaskForCrop(0); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(6, taskList.size()); + } @Test void removeTasksForCrop() { - // TODO implement Test + List taskList = null; + try { + testDatabase.removeTasksForCrop(0); + taskList = testDatabase.getTaskForCrop(0); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(0, taskList.size()); } } diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json new file mode 100644 index 0000000..ff69563 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json @@ -0,0 +1,56 @@ +[ + { + "id" : 1, + "name" : "sow plant", + "description": "Plant the seeds, crops in de bed.", + "startDate" : "2022-05-01", + "endDate" : "2022-05-01", + "interval" : 0, + "cropId" : 0 + }, + { + "id" : 2, + "name" : "water plant", + "description": "water the plant, so that the soil is wet around the plant.", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", + "interval" : 2, + "cropId" : 0 + }, + { + "id" : 3, + "name" : "fertilize plant", + "description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture", + "startDate" : "2022-06-01", + "endDate" : "2022-08-01", + "interval" : 28, + "cropId" : 0 + }, + { + "id" : 4, + "name" : "covering plant", + "description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage", + "startDate" : "2022-07-01", + "endDate" : "2022-07-01", + "interval" : 0, + "cropId" : 0 + }, + { + "id" : 5, + "name" : "look after plant", + "description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", + "interval" : 5, + "cropId" : 0 + }, + { + "id" : 6, + "name" : "harvest plant", + "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", + "startDate" : "2022-09-01", + "endDate" : "2022-09-01", + "interval" : 0, + "cropId" : 0 + } +] \ No newline at end of file From 6737e67cda643a6c655785684f50b0b441fd41f4 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sun, 13 Nov 2022 16:30:53 +0100 Subject: [PATCH 003/105] Task Test Copied over from dev --- .../io/JsonTaskDatabaseTest.java | 106 +++++++++++++++--- .../zhaw/gartenverwaltung/io/test-taskdb.json | 56 +++++++++ 2 files changed, 146 insertions(+), 16 deletions(-) create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java index d3710ab..255117e 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java @@ -4,44 +4,118 @@ import ch.zhaw.gartenverwaltung.types.Task; import org.junit.jupiter.api.*; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + public class JsonTaskDatabaseTest { TaskDatabase testDatabase; - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); - @BeforeEach - void connectToDb() { - // testDatabase = new JsonTaskDatabase(); - } + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + private final URL dbDataSource = this.getClass().getResource("taskdb.json"); + private final URL testFile = this.getClass().getResource("test-taskdb.json"); + + @BeforeEach + void connectToDb() throws URISyntaxException, IOException { + assertNotNull(testFile); + assertNotNull(dbDataSource); + Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); + testDatabase = new JsonTaskDatabase(); + } @Test @DisplayName("Check if results are retrieved completely") - void getTasks(){ - /* - List taskList=null; + void getTasks() { + + List taskList = null; try { - taskList = testDatabase.getTaskList(formatter.parse("01.05.2022"), formatter.parse("01.08.2022")); + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); } catch (IOException e) { throw new RuntimeException(e); - } catch (ParseException e) { - throw new RuntimeException(e); } - Assertions.assertTrue(taskList.size()>0); - */ + Assertions.assertEquals(3, taskList.size()); + } + @Disabled("disabled until adding works.") + @Test + @DisplayName("Add task.") + void addTask() { + Task task = new Task("Testtask", "This is a test Task.", LocalDate.parse("01.05.2022", formatter), 1); + try { + testDatabase.saveTask(task); + List taskList = null; + try { + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(4, taskList.size()); + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Test + @DisplayName("Remove task.") + void removeTask() { + Task task = new Task("Dummy", "Dummy", LocalDate.parse("31.05.2022", formatter), 1).withId(2); + try { + testDatabase.removeTask(task); + List taskList = null; + + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); + + Assertions.assertEquals(2, taskList.size()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + @Test void getTaskForCrop() { - // TODO implement Test + List taskList = null; + try { + taskList = testDatabase.getTaskForCrop(0); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(6, taskList.size()); + } + @Disabled("Disabled until removing works") @Test void removeTasksForCrop() { - // TODO implement Test + List taskList = null; + try { + testDatabase.removeTasksForCrop(0); + taskList = testDatabase.getTaskForCrop(0); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(0, taskList.size()); } -} +} \ No newline at end of file diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json new file mode 100644 index 0000000..7728bb0 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json @@ -0,0 +1,56 @@ +[ + { + "id" : 1, + "name" : "sow plant", + "description": "Plant the seeds, crops in de bed.", + "startDate" : "2022-05-01", + "endDate" : "2022-05-01", + "interval" : 0, + "cropId" : 0 + }, + { + "id" : 2, + "name" : "water plant", + "description": "water the plant, so that the soil is wet around the plant.", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", + "interval" : 2, + "cropId" : 0 + }, + { + "id" : 3, + "name" : "fertilize plant", + "description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture", + "startDate" : "2022-06-01", + "endDate" : "2022-08-01", + "interval" : 28, + "cropId" : 0 + }, + { + "id" : 4, + "name" : "covering plant", + "description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage", + "startDate" : "2022-07-01", + "endDate" : "2022-07-01", + "interval" : 0, + "cropId" : 0 + }, + { + "id" : 5, + "name" : "look after plant", + "description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", + "interval" : 5, + "cropId" : 0 + }, + { + "id" : 6, + "name" : "harvest plant", + "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", + "startDate" : "2022-09-01", + "endDate" : "2022-09-01", + "interval" : 0, + "cropId" : 0 + } +] \ No newline at end of file From 4f80a0a3e08f09f4f73bd541d43eb9b162e84958 Mon Sep 17 00:00:00 2001 From: David Guler Date: Mon, 14 Nov 2022 13:47:22 +0100 Subject: [PATCH 004/105] refactor: renamed everything --- .../java/ch/zhaw/gartenverwaltung/Config.java | 19 ------- .../CropDetailController.java | 14 ++--- .../gartenverwaltung/MyPlantsController.java | 16 +++--- .../MyScheduleController.java | 18 +++---- .../gartenverwaltung/PlantsController.java | 2 +- .../SelectSowDayController.java | 34 +++++------- .../ch/zhaw/gartenverwaltung/Settings.java | 26 +++++++++ .../io/{GardenPlan.java => CropList.java} | 2 +- ...{JsonGardenPlan.java => JsonCropList.java} | 6 +-- ...nPlantDatabase.java => JsonPlantList.java} | 8 +-- ...sonTaskDatabase.java => JsonTaskList.java} | 11 ++-- .../io/{PlantDatabase.java => PlantList.java} | 2 +- .../io/{TaskDatabase.java => TaskList.java} | 6 +-- .../json/PlantImageDeserializer.java | 4 +- .../Garden.java} | 53 +++++++++---------- .../GardenSchedule.java} | 37 +++++++------ .../{plantList => models}/PlantListModel.java | 20 +++---- .../PlantNotFoundException.java | 2 +- ...denPlanTest.java => JsonCropListTest.java} | 6 +-- ...tabaseTest.java => JsonPlantListTest.java} | 6 +-- ...atabaseTest.java => JsonTaskListTest.java} | 10 ++-- .../GardenPlanModelTest.java | 34 ++++++------ .../PlantListModelTest.java | 17 +++--- ...ModelTest.java => GardenScheduleTest.java} | 51 +++++++++--------- 24 files changed, 193 insertions(+), 211 deletions(-) delete mode 100644 src/main/java/ch/zhaw/gartenverwaltung/Config.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/Settings.java rename src/main/java/ch/zhaw/gartenverwaltung/io/{GardenPlan.java => CropList.java} (97%) rename src/main/java/ch/zhaw/gartenverwaltung/io/{JsonGardenPlan.java => JsonCropList.java} (97%) rename src/main/java/ch/zhaw/gartenverwaltung/io/{JsonPlantDatabase.java => JsonPlantList.java} (93%) rename src/main/java/ch/zhaw/gartenverwaltung/io/{JsonTaskDatabase.java => JsonTaskList.java} (94%) rename src/main/java/ch/zhaw/gartenverwaltung/io/{PlantDatabase.java => PlantList.java} (97%) rename src/main/java/ch/zhaw/gartenverwaltung/io/{TaskDatabase.java => TaskList.java} (89%) rename src/main/java/ch/zhaw/gartenverwaltung/{gardenplan/Gardenplanmodel.java => models/Garden.java} (61%) rename src/main/java/ch/zhaw/gartenverwaltung/{taskList/TaskListModel.java => models/GardenSchedule.java} (84%) rename src/main/java/ch/zhaw/gartenverwaltung/{plantList => models}/PlantListModel.java (93%) rename src/main/java/ch/zhaw/gartenverwaltung/{taskList => models}/PlantNotFoundException.java (79%) rename src/test/java/ch/zhaw/gartenverwaltung/io/{JsonGardenPlanTest.java => JsonCropListTest.java} (96%) rename src/test/java/ch/zhaw/gartenverwaltung/io/{JsonPlantDatabaseTest.java => JsonPlantListTest.java} (96%) rename src/test/java/ch/zhaw/gartenverwaltung/io/{JsonTaskDatabaseTest.java => JsonTaskListTest.java} (93%) rename src/test/java/ch/zhaw/gartenverwaltung/{gardenplan => models}/GardenPlanModelTest.java (85%) rename src/test/java/ch/zhaw/gartenverwaltung/{plantList => models}/PlantListModelTest.java (95%) rename src/test/java/ch/zhaw/gartenverwaltung/taskList/{TaskListModelTest.java => GardenScheduleTest.java} (68%) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Config.java b/src/main/java/ch/zhaw/gartenverwaltung/Config.java deleted file mode 100644 index 2fa0117..0000000 --- a/src/main/java/ch/zhaw/gartenverwaltung/Config.java +++ /dev/null @@ -1,19 +0,0 @@ -package ch.zhaw.gartenverwaltung; - -import ch.zhaw.gartenverwaltung.types.HardinessZone; - -public class Config { - private static HardinessZone currentHardinessZone; - - static { - currentHardinessZone = HardinessZone.ZONE_8A; - } - - public static HardinessZone getCurrentHardinessZone() { - return currentHardinessZone; - } - - public static void setCurrentHardinessZone(HardinessZone currentHardinessZone) { - Config.currentHardinessZone = currentHardinessZone; - } -} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index 6e5d792..622992c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -1,9 +1,9 @@ package ch.zhaw.gartenverwaltung; -import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.plantList.PlantListModel; -import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.models.PlantListModel; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Pest; import ch.zhaw.gartenverwaltung.types.Plant; @@ -24,8 +24,8 @@ import java.util.List; public class CropDetailController { private Crop crop = null; private final PlantListModel plantListModel = new PlantListModel(); - private final TaskListModel taskListModel = new TaskListModel(); - private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + private final GardenSchedule gardenSchedule = new GardenSchedule(); + private final Garden garden = new Garden(gardenSchedule); @FXML private ImageView imageView; @@ -89,7 +89,7 @@ public class CropDetailController { public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException { this.crop = crop; - Plant plant = plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0); + Plant plant = plantListModel.getFilteredPlantListById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(0); cropName_label.setText(plant.name()); description_label.setText(plant.description()); light_label.setText(String.valueOf(plant.light())); @@ -105,7 +105,7 @@ public class CropDetailController { } private void createTaskLists(Crop crop) throws IOException { - List taskList = taskListModel.getTaskListForCrop(crop.getCropId().get()); + List taskList = gardenSchedule.getTaskListForCrop(crop.getCropId().get()); for (Task task : taskList) { Label label = new Label(task.getDescription()); growthPhases_vbox.getChildren().add(label); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java index 3d9d82c..1639134 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java @@ -1,9 +1,9 @@ package ch.zhaw.gartenverwaltung; -import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.plantList.PlantListModel; -import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.models.PlantListModel; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Plant; import javafx.event.ActionEvent; @@ -30,8 +30,8 @@ import java.util.*; public class MyPlantsController implements Initializable { MainFXMLController mainController; - private final TaskListModel taskListModel = new TaskListModel(); - private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + private final GardenSchedule gardenSchedule = new GardenSchedule(); + private final Garden garden = new Garden(gardenSchedule); private final PlantListModel plantListModel = new PlantListModel(); @FXML @@ -79,13 +79,13 @@ public class MyPlantsController implements Initializable { private List getCropList() throws IOException { List cropList; - cropList = gardenplanmodel.getCrops(); + cropList = garden.getCrops(); return cropList; } private HBox createPlantView(Crop crop) throws HardinessZoneNotSetException, IOException { //ToDo add better design - Plant plant = plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0); + Plant plant = plantListModel.getFilteredPlantListById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(0); HBox hBox = new HBox(10); ImageView imageView = new ImageView(); imageView.setPreserveRatio(false); @@ -153,7 +153,7 @@ public class MyPlantsController implements Initializable { Optional option = alert.showAndWait(); if (option.get() == ButtonType.OK) { - gardenplanmodel.removeCrop(crop); + garden.removeCrop(crop); loadCropList(); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java index a73052c..a763754 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java @@ -1,9 +1,9 @@ package ch.zhaw.gartenverwaltung; -import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.plantList.PlantListModel; -import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.models.PlantListModel; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; import javafx.beans.property.ListProperty; @@ -28,8 +28,8 @@ import java.util.ResourceBundle; public class MyScheduleController implements Initializable { private Crop selectedCrop = null; - private final TaskListModel taskListModel = new TaskListModel(); - private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + private final GardenSchedule gardenSchedule = new GardenSchedule(); + private final Garden garden = new Garden(gardenSchedule); private final PlantListModel plantListModel = new PlantListModel(); private final ListProperty cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); @@ -89,7 +89,7 @@ public class MyScheduleController implements Initializable { public void initialize(URL location, ResourceBundle resources) { List cropList; try { - cropList = gardenplanmodel.getCrops(); + cropList = garden.getCrops(); cropListProperty.addAll(cropList); } catch (IOException e) { e.printStackTrace(); @@ -141,7 +141,7 @@ public class MyScheduleController implements Initializable { setText(null); } else { try { - setText(plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0).name()); + setText(plantListModel.getFilteredPlantListById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(0).name()); } catch (HardinessZoneNotSetException | IOException e) { e.printStackTrace(); } @@ -153,9 +153,9 @@ public class MyScheduleController implements Initializable { private void loadTaskList() throws IOException { List> taskLists = new LinkedList<>(); if (selectedCrop != null) { - taskLists = taskListModel.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); + taskLists = gardenSchedule.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); } else { - taskLists = taskListModel.getTasksUpcomingWeek(); + taskLists = gardenSchedule.getTasksUpcomingWeek(); } if (!taskLists.isEmpty()) { viewTaskListOfDay(day1_pane, taskLists.get(0)); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index c8ff333..1d832ef 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -1,7 +1,7 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.plantList.PlantListModel; +import ch.zhaw.gartenverwaltung.models.PlantListModel; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Seasons; diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index c5445e1..f00ba59 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -1,13 +1,11 @@ package ch.zhaw.gartenverwaltung; -import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException; -import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.Plant; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -23,8 +21,8 @@ import java.util.ResourceBundle; public class SelectSowDayController implements Initializable { private Plant selectedPlant = null; - private final TaskListModel taskListModel = new TaskListModel(); - private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + private final GardenSchedule gardenSchedule = new GardenSchedule(); + private final Garden garden = new Garden(gardenSchedule); @FXML private DatePicker datepicker; @@ -63,7 +61,7 @@ public class SelectSowDayController implements Initializable { //ToDo method to get current lifecycle group in plant sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); } - gardenplanmodel.plantAsCrop(selectedPlant, sowDate); + garden.plantAsCrop(selectedPlant, sowDate); closeWindow(); } @@ -97,12 +95,7 @@ public class SelectSowDayController implements Initializable { * clear date picker editor when radio button is changed */ private void clearDatePickerEntries() { - sow_radio.selectedProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Boolean oldValue, Boolean isNowSelected) { - datepicker.getEditor().clear(); - } - }); + sow_radio.selectedProperty().addListener((observable, oldValue, isNowSelected) -> datepicker.getEditor().clear()); } /** @@ -160,14 +153,11 @@ public class SelectSowDayController implements Initializable { */ private void enableDisableSaveButton() { save_button.setDisable(true); - datepicker.getEditor().textProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, String oldValue, String newValue) { - if (newValue == null || newValue.equals("")) { - save_button.setDisable(true); - } else { - save_button.setDisable(false); - } + datepicker.getEditor().textProperty().addListener((observable, oldValue, newValue) -> { + if (newValue == null || newValue.equals("")) { + save_button.setDisable(true); + } else { + save_button.setDisable(false); } }); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Settings.java b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java new file mode 100644 index 0000000..792f5c0 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java @@ -0,0 +1,26 @@ +package ch.zhaw.gartenverwaltung; + +import ch.zhaw.gartenverwaltung.types.HardinessZone; + +public class Settings { + private HardinessZone currentHardinessZone = HardinessZone.ZONE_8A; + private static Settings instance; + + static { + instance = new Settings(); + } + + public static Settings getInstance() { + return Settings.instance; + } + + private Settings() {} + + public HardinessZone getCurrentHardinessZone() { + return currentHardinessZone; + } + + public void setCurrentHardinessZone(HardinessZone currentHardinessZone) { + this.currentHardinessZone = currentHardinessZone; + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/GardenPlan.java b/src/main/java/ch/zhaw/gartenverwaltung/io/CropList.java similarity index 97% rename from src/main/java/ch/zhaw/gartenverwaltung/io/GardenPlan.java rename to src/main/java/ch/zhaw/gartenverwaltung/io/CropList.java index 38d0598..7b9fd47 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/GardenPlan.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/CropList.java @@ -6,7 +6,7 @@ import java.io.IOException; import java.util.List; import java.util.Optional; -public interface GardenPlan { +public interface CropList { /** * Yields a list of all {@link Crop}s in the database. * diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonCropList.java similarity index 97% rename from src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java rename to src/main/java/ch/zhaw/gartenverwaltung/io/JsonCropList.java index cfe2663..f9ddc3e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonCropList.java @@ -19,7 +19,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; -public class JsonGardenPlan implements GardenPlan { +public class JsonCropList implements CropList { private final URL dataSource; private IdProvider idProvider; @@ -42,7 +42,7 @@ public class JsonGardenPlan implements GardenPlan { /** * Default constructor */ - public JsonGardenPlan() { + public JsonCropList() { this.dataSource = getClass().getResource("user-crops.json"); } @@ -50,7 +50,7 @@ public class JsonGardenPlan implements GardenPlan { * Constructor to use a specified {@link URL} as a {@link #dataSource} * @param dataSource A {@link URL} to the file to be used as a data source */ - public JsonGardenPlan(URL dataSource) { + public JsonCropList(URL dataSource) { this.dataSource = dataSource; } /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantList.java similarity index 93% rename from src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java rename to src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantList.java index f09d90e..6132477 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantList.java @@ -20,11 +20,11 @@ import java.util.Map; import java.util.Optional; /** - * Implements the {@link PlantDatabase} interface for loading {@link Plant} objects + * Implements the {@link PlantList} interface for loading {@link Plant} objects * from a JSON file. * The reads are cached to minimize file-io operations. */ -public class JsonPlantDatabase implements PlantDatabase { +public class JsonPlantList implements PlantList { private final URL dataSource = getClass().getResource("plantdb.json"); private HardinessZone currentZone; @@ -49,7 +49,7 @@ public class JsonPlantDatabase implements PlantDatabase { * from the {@link #currentZone}, data is loaded from {@link #dataSource}. * In any case, the values of {@link #plantMap} are returned. * - * @see PlantDatabase#getPlantList(HardinessZone) + * @see PlantList#getPlantList(HardinessZone) */ @Override public List getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException { @@ -60,7 +60,7 @@ public class JsonPlantDatabase implements PlantDatabase { } /** - * @see PlantDatabase#getPlantById(long) + * @see PlantList#getPlantById(long) */ @Override public Optional getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java similarity index 94% rename from src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java rename to src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java index be0d2ab..bda9aa8 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java @@ -1,6 +1,5 @@ package ch.zhaw.gartenverwaltung.io; -import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; @@ -20,11 +19,11 @@ import java.util.List; import java.util.Map; /** - * Implements the {@link TaskDatabase} interface for loading and writing {@link Task} objects + * Implements the {@link TaskList} interface for loading and writing {@link Task} objects * from and to a JSON file. * The reads are cached to minimize file-io operations. */ -public class JsonTaskDatabase implements TaskDatabase{ +public class JsonTaskList implements TaskList { IdProvider idProvider; private final URL dataSource = getClass().getResource("taskdb.json"); private final static String INVALID_DATASOURCE_MSG = "Invalid datasource specified!"; @@ -48,7 +47,7 @@ public class JsonTaskDatabase implements TaskDatabase{ * If no data is currently loaded, data is loaded from {@link #dataSource}. * In any case, the values of {@link #taskMap} are returned. * - * @see TaskDatabase#getTaskList(LocalDate, LocalDate) + * @see TaskList#getTaskList(LocalDate, LocalDate) */ @Override public List getTaskList(LocalDate start, LocalDate end) throws IOException{ @@ -90,7 +89,7 @@ public class JsonTaskDatabase implements TaskDatabase{ * it to the {@link #taskMap}. In any case, the {@link #taskMap} is written * to the {@link #dataSource}. * - * @see TaskDatabase#saveTask(Task) + * @see TaskList#saveTask(Task) */ @Override public void saveTask(Task task) throws IOException { @@ -108,7 +107,7 @@ public class JsonTaskDatabase implements TaskDatabase{ * If the {@link Task}s id is found in the {@link #taskMap}, the Task is removed * from the {@link #taskMap}. Then the Task are written to the {@link #dataSource}. * - * @see TaskDatabase#removeTask(Task) + * @see TaskList#removeTask(Task) */ @Override public void removeTask(Task task) throws IOException { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantList.java similarity index 97% rename from src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java rename to src/main/java/ch/zhaw/gartenverwaltung/io/PlantList.java index 455791e..8650429 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantList.java @@ -11,7 +11,7 @@ import java.util.Optional; * A database of {@link Plant}s. * The interface specifies the minimal required operations. */ -public interface PlantDatabase { +public interface PlantList { /** * Yields a list of all {@link Plant}s in the database with only data relevant to the specfied {@link HardinessZone} * diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java similarity index 89% rename from src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java rename to src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java index 02bef7a..97ef3a7 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java @@ -1,20 +1,16 @@ package ch.zhaw.gartenverwaltung.io; -import ch.zhaw.gartenverwaltung.types.Crop; -import ch.zhaw.gartenverwaltung.types.HardinessZone; -import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; import java.io.IOException; import java.time.LocalDate; -import java.util.Date; import java.util.List; /** * A database of {@link Task}s. * The interface specifies the minimal required operations. */ -public interface TaskDatabase { +public interface TaskList { /** * Yields a list of all {@link Task}s in the database with the start and end date of a period of time. * diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java index 14da20c..d40b882 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java @@ -1,6 +1,6 @@ package ch.zhaw.gartenverwaltung.json; -import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.io.PlantList; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; @@ -18,7 +18,7 @@ public class PlantImageDeserializer extends JsonDeserializer { @Override public Image deserialize(JsonParser parser, DeserializationContext context) throws IOException { Image result = null; - URL imageUrl = PlantDatabase.class.getResource(String.format("images/%s", parser.getText())); + URL imageUrl = PlantList.class.getResource(String.format("images/%s", parser.getText())); if (imageUrl != null) { try (InputStream is = new FileInputStream(new File(imageUrl.toURI()))) { result = new Image(is); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java b/src/main/java/ch/zhaw/gartenverwaltung/models/Garden.java similarity index 61% rename from src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java rename to src/main/java/ch/zhaw/gartenverwaltung/models/Garden.java index 00ebd44..3d6032a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/Garden.java @@ -1,41 +1,37 @@ -package ch.zhaw.gartenverwaltung.gardenplan; +package ch.zhaw.gartenverwaltung.models; -import ch.zhaw.gartenverwaltung.io.GardenPlan; +import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.io.JsonGardenPlan; -import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException; -import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.io.JsonCropList; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import java.io.IOException; import java.time.LocalDate; -import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.function.Supplier; /** * The Gardenplan model manages the crops in the gardenplan. */ -public class Gardenplanmodel { - private GardenPlan gardenPlan; - private List cropList; - private TaskListModel taskListModel; - private Object IllegalArgumentException; +public class Garden { + private CropList cropList; + private final ObservableList plantedCrops = FXCollections.observableArrayList(); + private GardenSchedule gardenSchedule; /** * Constructor of Gardenplan model * - * @param taskListModel holds a reference to the task list object. + * @param gardenSchedule holds a reference to the task list object. */ - public Gardenplanmodel(TaskListModel taskListModel) throws IOException { - this.taskListModel = taskListModel; - gardenPlan = new JsonGardenPlan(); - cropList = new ArrayList<>(); - cropList = gardenPlan.getCrops(); + public Garden(GardenSchedule gardenSchedule) throws IOException { + this.gardenSchedule = gardenSchedule; + cropList = new JsonCropList(); + plantedCrops.addAll(cropList.getCrops()); } /** @@ -52,9 +48,10 @@ public class Gardenplanmodel { Crop crop = new Crop(plant.id(), plantingDate); //TODO Add Area to Plant //crop.withArea(0); - gardenPlan.saveCrop(crop); - taskListModel.planTasksForCrop(crop); - cropList = gardenPlan.getCrops(); + cropList.saveCrop(crop); + gardenSchedule.planTasksForCrop(crop); + plantedCrops.clear(); + plantedCrops.addAll(cropList.getCrops()); } /** @@ -64,9 +61,10 @@ public class Gardenplanmodel { * @throws IOException If the database cannot be accessed */ public void removeCrop(Crop crop) throws IOException { - gardenPlan.removeCrop(crop); - taskListModel.removeTasksForCrop(crop.getCropId().orElseThrow(IllegalArgumentException::new)); - cropList = gardenPlan.getCrops(); + cropList.removeCrop(crop); + gardenSchedule.removeTasksForCrop(crop.getCropId().orElseThrow(IllegalArgumentException::new)); + plantedCrops.clear(); + plantedCrops.addAll(cropList.getCrops()); } /** * Returns a list of {@link Crop}s which are currently in the gardenplan. @@ -74,10 +72,7 @@ public class Gardenplanmodel { * @throws IOException If the database cannot be accessed */ public List getCrops() throws IOException { - if(!cropList.isEmpty()){ - cropList = gardenPlan.getCrops(); - } - return cropList; + return cropList.getCrops(); } /** @@ -87,6 +82,6 @@ public class Gardenplanmodel { * @throws IOException If the database cannot be accessed */ public Optional getCrop(Long cropId) throws IOException { - return gardenPlan.getCropById(cropId); + return cropList.getCropById(cropId); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java similarity index 84% rename from src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java rename to src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index af0ca00..30a0a50 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -1,6 +1,6 @@ -package ch.zhaw.gartenverwaltung.taskList; +package ch.zhaw.gartenverwaltung.models; -import ch.zhaw.gartenverwaltung.Config; +import ch.zhaw.gartenverwaltung.Settings; import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.types.*; @@ -9,29 +9,28 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.function.Predicate; import java.util.stream.Collectors; -public class TaskListModel { - private TaskDatabase taskDatabase; - private PlantDatabase plantDatabase; +public class GardenSchedule { + private TaskList taskList; + private PlantList plantList; /** * Comparators to create sorted Task List */ static final Comparator sortByStartDate = Comparator.comparing(Task::getStartDate); - public TaskListModel(){ - taskDatabase = new JsonTaskDatabase(); - plantDatabase = new JsonPlantDatabase(); + public GardenSchedule(){ + taskList = new JsonTaskList(); + plantList = new JsonPlantList(); } /** * Constructor to create Database Objects. */ - public TaskListModel(TaskDatabase taskDatabase, PlantDatabase plantDatabase) { - this.taskDatabase = taskDatabase; - this.plantDatabase = plantDatabase; + public GardenSchedule(TaskList taskList, PlantList plantList) { + this.taskList = taskList; + this.plantList = plantList; } /** @@ -40,7 +39,7 @@ public class TaskListModel { * @throws IOException If the database cannot be accessed */ public void addTask(Task task) throws IOException { - taskDatabase.saveTask(task); + taskList.saveTask(task); } /** @@ -51,7 +50,7 @@ public class TaskListModel { * @throws IOException If the database cannot be accessed */ public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException { - Plant plant = plantDatabase.getPlantById(Config.getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new); + Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new); for (GrowthPhase growthPhase : plant.lifecycle()) { for (TaskTemplate taskTemplate : growthPhase.taskTemplates()) { addTask(taskTemplate.generateTask(crop.getStartDate(), crop.getCropId().orElse(0L))); @@ -65,7 +64,7 @@ public class TaskListModel { * @throws IOException If the database cannot be accessed */ public void removeTasksForCrop(long cropId) throws IOException { - taskDatabase.removeTasksForCrop(cropId); + taskList.removeTasksForCrop(cropId); } /** @@ -74,7 +73,7 @@ public class TaskListModel { * @throws IOException If the database cannot be accessed */ public void removeTask(Task task) throws IOException { - taskDatabase.removeTask(task); + taskList.removeTask(task); } private List filterListByCrop(List taskList, Long cropId) { @@ -144,7 +143,7 @@ public class TaskListModel { List> dayTaskList = new ArrayList<>(); for(int i = 0; i < 7; i++) { LocalDate date = LocalDate.now().plusDays(i); - dayTaskList.add(taskDatabase.getTaskList(date, date)); + dayTaskList.add(taskList.getTaskList(date, date)); } return dayTaskList; } @@ -158,7 +157,7 @@ public class TaskListModel { List> dayTaskList = new ArrayList<>(); for(int i = 0; i < 7; i++) { LocalDate date = LocalDate.now().plusDays(i); - dayTaskList.add(filterListByCrop(taskDatabase.getTaskList(date, date), cropId)); + dayTaskList.add(filterListByCrop(taskList.getTaskList(date, date), cropId)); } return dayTaskList; } @@ -171,7 +170,7 @@ public class TaskListModel { * @throws IOException If the database cannot be accessed */ public List getFilteredTaskList(LocalDate start, LocalDate end) throws IOException { - return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate); + return getSortedTaskList(taskList.getTaskList(start, end), sortByStartDate); } /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/models/PlantListModel.java similarity index 93% rename from src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java rename to src/main/java/ch/zhaw/gartenverwaltung/models/PlantListModel.java index ffccb30..6b96bee 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/PlantListModel.java @@ -1,8 +1,8 @@ -package ch.zhaw.gartenverwaltung.plantList; +package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; -import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.io.JsonPlantList; +import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; @@ -16,7 +16,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; public class PlantListModel { - private PlantDatabase plantDatabase; + private PlantList plantList; private HardinessZone currentZone; /** @@ -29,17 +29,17 @@ public class PlantListModel { * Constructor to create Database Object. */ public PlantListModel() { - plantDatabase = new JsonPlantDatabase(); + plantList = new JsonPlantList(); setDefaultZone(); } - public PlantListModel(PlantDatabase plantDatabase) { - this.plantDatabase = plantDatabase; + public PlantListModel(PlantList plantList) { + this.plantList = plantList; setDefaultZone(); } private void setDefaultZone() { - currentZone = HardinessZone.ZONE_8A; // TODO: get Default Zone from Config + currentZone = HardinessZone.ZONE_8A; // TODO: get Default Zone from Settings } public void setCurrentZone(HardinessZone currentZone) { @@ -72,7 +72,7 @@ public class PlantListModel { */ public List getSortedPlantList(HardinessZone zone, Comparator comparator) throws HardinessZoneNotSetException, IOException { setCurrentZone(zone); - return plantDatabase.getPlantList(zone).stream().sorted(comparator).collect(Collectors.toList()); + return plantList.getPlantList(zone).stream().sorted(comparator).collect(Collectors.toList()); } /** @@ -101,7 +101,7 @@ public class PlantListModel { public List getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException { setCurrentZone(zone); List plantList = new ArrayList<>(); - plantDatabase.getPlantById(zone, id).ifPresent(plantList::add); + this.plantList.getPlantById(zone, id).ifPresent(plantList::add); return plantList; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/PlantNotFoundException.java b/src/main/java/ch/zhaw/gartenverwaltung/models/PlantNotFoundException.java similarity index 79% rename from src/main/java/ch/zhaw/gartenverwaltung/taskList/PlantNotFoundException.java rename to src/main/java/ch/zhaw/gartenverwaltung/models/PlantNotFoundException.java index 149e1ef..62035a8 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/PlantNotFoundException.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/PlantNotFoundException.java @@ -1,4 +1,4 @@ -package ch.zhaw.gartenverwaltung.taskList; +package ch.zhaw.gartenverwaltung.models; public class PlantNotFoundException extends Exception { public PlantNotFoundException() { diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java similarity index 96% rename from src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java rename to src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java index ea22b3b..44291b6 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java @@ -22,8 +22,8 @@ import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -public class JsonGardenPlanTest { - private GardenPlan testDatabase; +public class JsonCropListTest { + private CropList testDatabase; private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); /** * Files to isolate the test-units @@ -36,7 +36,7 @@ public class JsonGardenPlanTest { assertNotNull(testFile); assertNotNull(dbDataSource); Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); - testDatabase = new JsonGardenPlan(dbDataSource); + testDatabase = new JsonCropList(dbDataSource); } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java similarity index 96% rename from src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java rename to src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java index b8bbeb3..7c62c0c 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java @@ -13,12 +13,12 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -public class JsonPlantDatabaseTest { - PlantDatabase testDatabase; +public class JsonPlantListTest { + PlantList testDatabase; @BeforeEach void connectToDb() { - testDatabase = new JsonPlantDatabase(); + testDatabase = new JsonPlantList(); } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java similarity index 93% rename from src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java rename to src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java index 255117e..9f585d4 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java @@ -9,19 +9,15 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.Date; import java.util.List; -import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertNotNull; -public class JsonTaskDatabaseTest { +public class JsonTaskListTest { - TaskDatabase testDatabase; + TaskList testDatabase; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); private final URL dbDataSource = this.getClass().getResource("taskdb.json"); @@ -32,7 +28,7 @@ public class JsonTaskDatabaseTest { assertNotNull(testFile); assertNotNull(dbDataSource); Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); - testDatabase = new JsonTaskDatabase(); + testDatabase = new JsonTaskList(); } @Test diff --git a/src/test/java/ch/zhaw/gartenverwaltung/gardenplan/GardenPlanModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java similarity index 85% rename from src/test/java/ch/zhaw/gartenverwaltung/gardenplan/GardenPlanModelTest.java rename to src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java index 16ece04..02c3f35 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/gardenplan/GardenPlanModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java @@ -1,16 +1,14 @@ -package ch.zhaw.gartenverwaltung.gardenplan; +package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.io.*; -import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException; -import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.models.Garden; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.File; import java.io.IOException; -import java.net.URL; -import java.nio.file.Files; import java.time.LocalDate; import java.time.MonthDay; import java.util.ArrayList; @@ -21,8 +19,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; public class GardenPlanModelTest { - GardenPlan gardenPlan; - List cropList; + CropList cropList; + List exampleCrops; Crop exampleCropOnion; Crop exampleCropCarrot; Crop exampleCrop1; @@ -30,7 +28,7 @@ public class GardenPlanModelTest { Crop exampleCrop3; Plant examplePlantOnion; Plant examplePlantCarrot; - Gardenplanmodel model; + Garden model; @BeforeEach void setUp() throws IOException { @@ -76,18 +74,18 @@ public class GardenPlanModelTest { exampleCrop3.withId(2); exampleCrop3.withArea(1.0); - cropList = new ArrayList<>(); - cropList.add(exampleCrop1); - cropList.add(exampleCrop2); - cropList.add(exampleCrop3); - gardenPlan = mockGardenPlan(cropList); + exampleCrops = new ArrayList<>(); + exampleCrops.add(exampleCrop1); + exampleCrops.add(exampleCrop2); + exampleCrops.add(exampleCrop3); + cropList = mockGardenPlan(exampleCrops); - TaskListModel taskListModel = new TaskListModel(new JsonTaskDatabase(), new JsonPlantDatabase()); - model = new Gardenplanmodel(taskListModel); + GardenSchedule gardenSchedule = new GardenSchedule(new JsonTaskList(), new JsonPlantList()); + model = new Garden(gardenSchedule); } - GardenPlan mockGardenPlan(List cropList) throws IOException { - GardenPlan gardenPlan = mock(GardenPlan.class); + CropList mockGardenPlan(List cropList) throws IOException { + CropList gardenPlan = mock(CropList.class); when(gardenPlan.getCrops()).thenReturn(cropList); when(gardenPlan.getCropById(5)).thenReturn(java.util.Optional.ofNullable(exampleCropCarrot)); when(gardenPlan.getCropById(3)).thenReturn(java.util.Optional.ofNullable(exampleCropOnion)); diff --git a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/PlantListModelTest.java similarity index 95% rename from src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java rename to src/test/java/ch/zhaw/gartenverwaltung/models/PlantListModelTest.java index 9ed857d..0033299 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/PlantListModelTest.java @@ -1,8 +1,9 @@ -package ch.zhaw.gartenverwaltung.plantList; +package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; -import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.io.JsonPlantList; +import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.models.PlantListModel; import ch.zhaw.gartenverwaltung.types.*; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -22,15 +23,15 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; class PlantListModelTest { - PlantDatabase plantDatabase; + PlantList plantList; List examplePlantList; PlantListModel model; @BeforeEach void setUp() throws HardinessZoneNotSetException, IOException { createExamplePlantList(); - plantDatabase = mockPlantDatabase(examplePlantList); - model = new PlantListModel(plantDatabase); + plantList = mockPlantDatabase(examplePlantList); + model = new PlantListModel(plantList); } @AfterEach @@ -79,8 +80,8 @@ class PlantListModelTest { ); } - PlantDatabase mockPlantDatabase(List plantList) throws HardinessZoneNotSetException, IOException { - PlantDatabase plantDatabase = mock(JsonPlantDatabase.class); + PlantList mockPlantDatabase(List plantList) throws HardinessZoneNotSetException, IOException { + PlantList plantDatabase = mock(JsonPlantList.class); when(plantDatabase.getPlantList(HardinessZone.ZONE_8A)).thenReturn(plantList); when(plantDatabase.getPlantList(HardinessZone.ZONE_1A)).thenReturn(new ArrayList<>()); when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 0)).thenReturn(Optional.of(plantList.get(1))); diff --git a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/taskList/GardenScheduleTest.java similarity index 68% rename from src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java rename to src/test/java/ch/zhaw/gartenverwaltung/taskList/GardenScheduleTest.java index 9caf65a..cf354de 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/taskList/GardenScheduleTest.java @@ -1,6 +1,7 @@ package ch.zhaw.gartenverwaltung.taskList; import ch.zhaw.gartenverwaltung.io.*; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; @@ -14,47 +15,47 @@ import java.util.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; -class TaskListModelTest { - TaskDatabase taskDatabase; - PlantDatabase plantDatabase; +class GardenScheduleTest { + TaskList taskList; + PlantList plantList; List exampleTaskList; Map examplePlantMap; - TaskListModel model; + GardenSchedule model; @BeforeEach void setUp() throws IOException { createExampleTaskList(); - taskDatabase = mockTaskDatabase(exampleTaskList); - plantDatabase = mockPlantDatabase(examplePlantMap); - model = new TaskListModel(taskDatabase, plantDatabase); + taskList = mockTaskDatabase(exampleTaskList); + plantList = mockPlantDatabase(examplePlantMap); + model = new GardenSchedule(taskList, plantList); } - private TaskDatabase mockTaskDatabase(List exampleTaskList) throws IOException { - TaskDatabase taskDatabase = mock(JsonTaskDatabase.class); - when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList); + private TaskList mockTaskDatabase(List exampleTaskList) throws IOException { + TaskList taskList = mock(JsonTaskList.class); + when(taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList); - when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.MAX)).thenReturn((exampleTaskList.subList(1, 4))); + when(taskList.getTaskList(LocalDate.now(), LocalDate.MAX)).thenReturn((exampleTaskList.subList(1, 4))); List pastTasks = new ArrayList<>(); pastTasks.add(exampleTaskList.get(0)); pastTasks.add(exampleTaskList.get(2)); pastTasks.add(exampleTaskList.get(4)); - when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.now())).thenReturn(pastTasks); + when(taskList.getTaskList(LocalDate.MIN, LocalDate.now())).thenReturn(pastTasks); - when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.now())).thenReturn(List.of(exampleTaskList.get(2))); - when(taskDatabase.getTaskList(LocalDate.now().plusDays(1L), LocalDate.now().plusDays(1L))).thenReturn(List.of(exampleTaskList.get(1))); - when(taskDatabase.getTaskList(LocalDate.now().plusDays(2L), LocalDate.now().plusDays(2L))).thenReturn(List.of()); - when(taskDatabase.getTaskList(LocalDate.now().plusDays(3L), LocalDate.now().plusDays(3L))).thenReturn(List.of()); - when(taskDatabase.getTaskList(LocalDate.now().plusDays(4L), LocalDate.now().plusDays(4L))).thenReturn(List.of()); - when(taskDatabase.getTaskList(LocalDate.now().plusDays(5L), LocalDate.now().plusDays(5L))).thenReturn(List.of()); - when(taskDatabase.getTaskList(LocalDate.now().plusDays(6L), LocalDate.now().plusDays(6L))).thenReturn(List.of()); + when(taskList.getTaskList(LocalDate.now(), LocalDate.now())).thenReturn(List.of(exampleTaskList.get(2))); + when(taskList.getTaskList(LocalDate.now().plusDays(1L), LocalDate.now().plusDays(1L))).thenReturn(List.of(exampleTaskList.get(1))); + when(taskList.getTaskList(LocalDate.now().plusDays(2L), LocalDate.now().plusDays(2L))).thenReturn(List.of()); + when(taskList.getTaskList(LocalDate.now().plusDays(3L), LocalDate.now().plusDays(3L))).thenReturn(List.of()); + when(taskList.getTaskList(LocalDate.now().plusDays(4L), LocalDate.now().plusDays(4L))).thenReturn(List.of()); + when(taskList.getTaskList(LocalDate.now().plusDays(5L), LocalDate.now().plusDays(5L))).thenReturn(List.of()); + when(taskList.getTaskList(LocalDate.now().plusDays(6L), LocalDate.now().plusDays(6L))).thenReturn(List.of()); - return taskDatabase; + return taskList; } - private PlantDatabase mockPlantDatabase(Map examplePlantMap) { - return new PlantDatabase() { + private PlantList mockPlantDatabase(Map examplePlantMap) { + return new PlantList() { @Override public List getPlantList(HardinessZone zone) { return null; @@ -86,14 +87,14 @@ class TaskListModelTest { void addTask() throws IOException { Task taskToAdd = new Task("name", "description", LocalDate.now(), 1L); model.addTask(taskToAdd); - verify(taskDatabase, times(1)).saveTask(taskToAdd); + verify(taskList, times(1)).saveTask(taskToAdd); } @Test void removeTask() throws IOException { Task taskToRemove = new Task("name", "description", LocalDate.now(), 1L); model.removeTask(taskToRemove); - verify(taskDatabase, times(1)).removeTask(taskToRemove); + verify(taskList, times(1)).removeTask(taskToRemove); } @Test @@ -153,6 +154,6 @@ class TaskListModelTest { @Test void removeTasksForCrop() throws IOException { model.removeTasksForCrop(1L); - verify(taskDatabase, times(1)).removeTasksForCrop(1L); + verify(taskList, times(1)).removeTasksForCrop(1L); } } \ No newline at end of file From 15279838b751b9d5f90e5721addc310919a6143f Mon Sep 17 00:00:00 2001 From: David Guler Date: Mon, 14 Nov 2022 20:00:01 +0100 Subject: [PATCH 005/105] refactor: first attempt at dependency injection also some more renaming and improving date-picker dialog --- .../CropDetailController.java | 50 ++++-- .../gartenverwaltung/MainFXMLController.java | 112 +++++++++--- .../gartenverwaltung/MyGardenController.java | 140 +++++++++++++++ .../gartenverwaltung/MyPlantsController.java | 160 ------------------ .../MyScheduleController.java | 61 +++---- .../gartenverwaltung/PlantsController.java | 32 ++-- .../SelectSowDayController.java | 96 ++++------- .../zhaw/gartenverwaltung/models/Garden.java | 18 +- .../models/GardenSchedule.java | 9 +- .../models/PlantListModel.java | 8 +- src/main/java/module-info.java | 2 + .../ch/zhaw/gartenverwaltung/MainFXML.fxml | 4 +- .../{MyPlants.fxml => MyGarden.fxml} | 4 +- .../models/GardenPlanModelTest.java | 15 +- 14 files changed, 378 insertions(+), 333 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java delete mode 100644 src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java rename src/main/resources/ch/zhaw/gartenverwaltung/{MyPlants.fxml => MyGarden.fxml} (93%) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index 622992c..1d887b5 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -1,9 +1,10 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.models.PlantListModel; import ch.zhaw.gartenverwaltung.models.GardenSchedule; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Pest; import ch.zhaw.gartenverwaltung.types.Plant; @@ -20,13 +21,23 @@ import javafx.stage.Stage; import java.io.IOException; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; public class CropDetailController { - private Crop crop = null; - private final PlantListModel plantListModel = new PlantListModel(); - private final GardenSchedule gardenSchedule = new GardenSchedule(); - private final Garden garden = new Garden(gardenSchedule); + private Crop crop; + private PlantList plantList; + private GardenSchedule gardenSchedule; + private Garden garden; + private static final Logger LOG = Logger.getLogger(CropDetailController.class.getName()); + + @SuppressWarnings("unused") + public void injectDependencies(Garden garden, PlantList plantList, GardenSchedule gardenSchedule) { + this.garden = garden; + this.plantList = plantList; + this.gardenSchedule = gardenSchedule; + } @FXML private ImageView imageView; @@ -63,9 +74,6 @@ public class CropDetailController { @FXML private Label spacing_label; - public CropDetailController() throws IOException { - } - @FXML void editTaskList(ActionEvent event) { @@ -87,9 +95,11 @@ public class CropDetailController { } - public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException { + public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException, PlantNotFoundException { this.crop = crop; - Plant plant = plantListModel.getFilteredPlantListById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(0); + Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()) + .orElseThrow(PlantNotFoundException::new); + cropName_label.setText(plant.name()); description_label.setText(plant.description()); light_label.setText(String.valueOf(plant.light())); @@ -104,12 +114,20 @@ public class CropDetailController { createPestList(plant); } - private void createTaskLists(Crop crop) throws IOException { - List taskList = gardenSchedule.getTaskListForCrop(crop.getCropId().get()); - for (Task task : taskList) { - Label label = new Label(task.getDescription()); - growthPhases_vbox.getChildren().add(label); - } + private void createTaskLists(Crop crop) { + crop.getCropId().ifPresent(id -> { + List taskList; + try { + taskList = gardenSchedule.getTaskListForCrop(id); + for (Task task : taskList) { + Label label = new Label(task.getDescription()); + growthPhases_vbox.getChildren().add(label); + } + } catch (IOException e) { + // TODO: Alert + LOG.log(Level.SEVERE, "Could not get task list for crop", e.getCause()); + } + }); } private void createPestList(Plant plant) { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java b/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java index 032bdb8..29eaa30 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java @@ -1,17 +1,35 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.io.CropList; +import ch.zhaw.gartenverwaltung.io.JsonCropList; +import ch.zhaw.gartenverwaltung.io.JsonPlantList; +import ch.zhaw.gartenverwaltung.io.JsonTaskList; +import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.io.TaskList; +import ch.zhaw.gartenverwaltung.models.Garden; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; +import ch.zhaw.gartenverwaltung.models.PlantListModel; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; +import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; @@ -20,9 +38,16 @@ public class MainFXMLController implements Initializable { /** * Caching the panes */ - private final Map panes = new HashMap<>(); + private final Map panes = new HashMap<>(); private static final Logger LOG = Logger.getLogger(MainFXMLController.class.getName()); + private final PlantList plantList = new JsonPlantList(); + private final CropList cropList = new JsonCropList(); + private final TaskList taskList = new JsonTaskList(); + + private final GardenSchedule gardenSchedule = new GardenSchedule(taskList, plantList); + private final Garden garden = new Garden(gardenSchedule, cropList); + @FXML private Button home_button; @@ -30,7 +55,7 @@ public class MainFXMLController implements Initializable { private AnchorPane mainPane; @FXML - private Button myPlants_button; + private Button myGarden_button; @FXML private Button mySchedule_button; @@ -38,27 +63,30 @@ public class MainFXMLController implements Initializable { @FXML private Button plants_button; + public MainFXMLController() throws IOException { + } + @FXML void goToHome(ActionEvent event) throws IOException { - loadPane("Home.fxml"); + showPaneAsMainView("Home.fxml"); styleChangeButton(home_button); } @FXML void goToMyPlants(ActionEvent event) throws IOException { - loadPane("MyPlants.fxml"); - styleChangeButton(myPlants_button); + showPaneAsMainView("MyGarden.fxml"); + styleChangeButton(myGarden_button); } @FXML void goToMySchedule(ActionEvent event) throws IOException { - loadPane("MySchedule.fxml"); + showPaneAsMainView("MySchedule.fxml"); styleChangeButton(mySchedule_button); } @FXML void goToPlants(ActionEvent event) throws IOException { - loadPane("Plants.fxml"); + showPaneAsMainView("Plants.fxml"); styleChangeButton(plants_button); } @@ -69,23 +97,68 @@ public class MainFXMLController implements Initializable { * @param fxmlFile string of fxml file * @throws IOException exception when file does not exist */ - public void loadPane(String fxmlFile) throws IOException { - - AnchorPane anchorPane = panes.get(fxmlFile); + public void showPaneAsMainView(String fxmlFile) throws IOException { + Pane anchorPane = panes.get(fxmlFile); if (anchorPane == null) { - FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); - anchorPane = loader.load(); - panes.put(fxmlFile, anchorPane); - - if(fxmlFile.equals("MyPlants.fxml")) { - MyPlantsController myPlantsController = loader.getController(); - myPlantsController.getMainController(this); - } + loadAndCacheFxml(fxmlFile); + anchorPane = panes.get(fxmlFile); } mainPane.getChildren().setAll(anchorPane); anchorPane.prefWidthProperty().bind(mainPane.widthProperty()); anchorPane.prefHeightProperty().bind(mainPane.heightProperty()); + } + public Object loadSceneToStage(String fxmlFile, Stage appendee) throws IOException { + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); + Pane root = loader.load(); + appendee.setScene(new Scene(root)); + Object controller = loader.getController(); + injectDependencies(controller); + return controller; + } + + public FXMLLoader loadAndCacheFxml(String fxmlFile) throws IOException { + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); + AnchorPane anchorPane = loader.load(); + panes.put(fxmlFile, anchorPane); + injectDependencies(loader.getController()); + return loader; + } + + public void injectDependencies(Object controller) { + Optional injectMethod = Arrays.stream(controller.getClass().getMethods()) + .filter(method -> method.getName().equals("injectDependencies") ) + .findFirst(); + if (injectMethod.isPresent()) { + Method injectee = injectMethod.get(); + + List params = new ArrayList<>(); + for (Class parameterType : injectee.getParameterTypes()) { + Object toInject = switch (parameterType.getSimpleName()) { + case "Garden" -> garden; + case "PlantList" -> plantList; + case "PlantListModel" -> new PlantListModel(plantList); + case "GardenSchedule" -> gardenSchedule; + case "MainFXMLController" -> this; + default -> null; + }; + if (toInject != null) { + params.add(toInject); + } + } + try { + injectee.invoke(controller, params.toArray()); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + + + private void preloadPanes() throws IOException { + loadAndCacheFxml("MyGarden.fxml"); + loadAndCacheFxml("MySchedule.fxml"); + loadAndCacheFxml("Plants.fxml").getController(); } private void styleChangeButton(Button button) { @@ -99,7 +172,8 @@ public class MainFXMLController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { try { - loadPane("Home.fxml"); + preloadPanes(); + showPaneAsMainView("Home.fxml"); styleChangeButton(home_button); } catch (IOException e) { LOG.log(Level.SEVERE, "Failed to load FXML-Pane!", e); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java new file mode 100644 index 0000000..afb94a3 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java @@ -0,0 +1,140 @@ +package ch.zhaw.gartenverwaltung; + +import ch.zhaw.gartenverwaltung.io.PlantList; +import ch.zhaw.gartenverwaltung.models.Garden; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.Plant; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.ButtonType; +import javafx.scene.control.Label; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.scene.layout.VBox; +import javafx.stage.Modality; +import javafx.stage.Stage; + +import java.io.IOException; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class MyGardenController { + private static final Logger LOG = Logger.getLogger(MyGardenController.class.getName()); + + MainFXMLController mainController; + private Garden garden; + private PlantList plantList; + + @FXML + private VBox myPlants_vbox; + + @SuppressWarnings("unused") + public void injectDependencies(Garden garden, PlantList plantList, MainFXMLController mainController) { + this.garden = garden; + this.plantList = plantList; + this.mainController = mainController; + + garden.getPlantedCrops().addListener((observable, oldValue, newValue) -> { + try { + createPlantView(newValue); + } catch (HardinessZoneNotSetException | IOException e) { + LOG.log(Level.SEVERE, "Could not update view of croplist!", e); + } + }); + try { + createPlantView(garden.getPlantedCrops()); + } catch (HardinessZoneNotSetException | IOException e) { + LOG.log(Level.SEVERE, "Could not update view of croplist!", e); + } + } + + @FXML + void addPlant(ActionEvent event) throws IOException { + mainController.showPaneAsMainView("Plants.fxml"); + } + + private void createPlantView(List crops) throws HardinessZoneNotSetException, IOException { + myPlants_vbox.getChildren().clear(); + for (Crop crop : crops) { + HBox hBox = createPlantView(crop); + myPlants_vbox.getChildren().add(hBox); + } + } + + private HBox createPlantView(Crop crop) throws HardinessZoneNotSetException, IOException { + //ToDo add better design + Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(); + HBox hBox = new HBox(10); + ImageView imageView = new ImageView(); + imageView.setPreserveRatio(false); + imageView.setFitHeight(100); + imageView.setFitWidth(100); + imageView.maxHeight(100); + if (plant.image() != null) { + imageView.setImage(plant.image()); + } + hBox.setMinHeight(100); + Label label = new Label(plant.name()); + label.setMaxWidth(2000); + HBox.setHgrow(label, Priority.ALWAYS); + Button details = new Button("Details"); + Button delete = new Button("delete"); + details.setOnAction(getGoToCropDetailEvent(crop)); + delete.setOnAction(getDeleteCropEvent(crop)); + hBox.getChildren().addAll(imageView, label, details, delete); + return hBox; + } + + private EventHandler getGoToCropDetailEvent(Crop crop) { + return (event) -> { + try { + Stage stage = new Stage(); + if (mainController.loadSceneToStage("CropDetail.fxml", stage) instanceof CropDetailController controller) { + controller.setPlantFromCrop(crop); + } + stage.initModality(Modality.APPLICATION_MODAL); + stage.setResizable(true); + stage.showAndWait(); + } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { + // TODO: show error alert + LOG.log(Level.SEVERE, "Could not load plant details.", e); + } + }; + } + + private EventHandler getDeleteCropEvent(Crop crop) { + return (event) -> { + try { + showConfirmation(crop); + } catch (IOException | HardinessZoneNotSetException e) { + e.printStackTrace(); + } + }; + } + + private void showConfirmation(Crop crop) throws IOException, HardinessZoneNotSetException { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Delete Crop"); + alert.setHeaderText("Are you sure want to delete this Crop?"); + alert.setContentText("placeholder"); + + alert.showAndWait() + .ifPresent(buttonType -> { + if (buttonType == ButtonType.OK) { + try { + garden.removeCrop(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/MyPlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java deleted file mode 100644 index 1639134..0000000 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java +++ /dev/null @@ -1,160 +0,0 @@ -package ch.zhaw.gartenverwaltung; - -import ch.zhaw.gartenverwaltung.models.Garden; -import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.models.PlantListModel; -import ch.zhaw.gartenverwaltung.models.GardenSchedule; -import ch.zhaw.gartenverwaltung.types.Crop; -import ch.zhaw.gartenverwaltung.types.Plant; -import javafx.event.ActionEvent; -import javafx.event.EventHandler; -import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; -import javafx.fxml.Initializable; -import javafx.scene.Parent; -import javafx.scene.Scene; -import javafx.scene.control.Alert; -import javafx.scene.control.Button; -import javafx.scene.control.ButtonType; -import javafx.scene.control.Label; -import javafx.scene.image.ImageView; -import javafx.scene.layout.HBox; -import javafx.scene.layout.Priority; -import javafx.scene.layout.VBox; -import javafx.stage.Modality; -import javafx.stage.Stage; - -import java.io.IOException; -import java.net.URL; -import java.util.*; - -public class MyPlantsController implements Initializable { - MainFXMLController mainController; - private final GardenSchedule gardenSchedule = new GardenSchedule(); - private final Garden garden = new Garden(gardenSchedule); - private final PlantListModel plantListModel = new PlantListModel(); - - @FXML - private VBox myPlants_vbox; - - public MyPlantsController() throws IOException { - } - - @FXML - void addPlant(ActionEvent event) throws IOException { - mainController.loadPane("Plants.fxml"); - } - - @Override - public void initialize(URL url, ResourceBundle resourceBundle) { - //ToDo update, when new crops are added - try { - loadCropList(); - } catch (HardinessZoneNotSetException | IOException e) { - e.printStackTrace(); - } - } - - private void loadCropList() throws HardinessZoneNotSetException, IOException { - List cropList = new LinkedList<>(); - try { - cropList = getCropList(); - } catch (IOException e) { - e.printStackTrace(); - } - createPlantView(cropList); - } - - private void createPlantView(List crops) throws HardinessZoneNotSetException, IOException { - myPlants_vbox.getChildren().clear(); - for(Crop crop : crops) { - HBox hBox = createPlantView(crop); - myPlants_vbox.getChildren().add(hBox); - } - } - - public void getMainController(MainFXMLController controller) { - mainController = controller; - } - - private List getCropList() throws IOException { - List cropList; - cropList = garden.getCrops(); - return cropList; - } - - private HBox createPlantView(Crop crop) throws HardinessZoneNotSetException, IOException { - //ToDo add better design - Plant plant = plantListModel.getFilteredPlantListById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(0); - HBox hBox = new HBox(10); - ImageView imageView = new ImageView(); - imageView.setPreserveRatio(false); - imageView.setFitHeight(100); - imageView.setFitWidth(100); - imageView.maxHeight(100); - if (plant.image() != null) { - imageView.setImage(plant.image()); - } - hBox.setMinHeight(100); - Label label = new Label(plant.name()); - label.setMaxWidth(2000); - HBox.setHgrow(label, Priority.ALWAYS); - Button details = new Button("Details"); - Button delete = new Button("delete"); - details.setOnAction(getGoToCropDetailEvent(crop)); - delete.setOnAction(getDeleteCropEvent(crop)); - hBox.getChildren().addAll(imageView, label, details, delete); - return hBox; - } - - private EventHandler getGoToCropDetailEvent(Crop crop) { - EventHandler event = new EventHandler() { - @Override - public void handle(ActionEvent event) { - Parent root; - FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("CropDetail.fxml"))); - try { - root = fxmlLoader.load(); - CropDetailController controller = fxmlLoader.getController(); - controller.setPlantFromCrop(crop); - Stage stage = new Stage(); - stage.setScene(new Scene(root)); - stage.initModality(Modality.APPLICATION_MODAL); - stage.setResizable(true); - stage.showAndWait(); - } catch (IOException | HardinessZoneNotSetException e) { - e.printStackTrace(); - } - } - }; - return event; - } - - private EventHandler getDeleteCropEvent(Crop crop) { - EventHandler event = new EventHandler() { - @Override - public void handle(ActionEvent event) { - try { - showConfirmation(crop); - } catch (IOException | HardinessZoneNotSetException e) { - e.printStackTrace(); - } - } - }; - return event; - } - - private void showConfirmation(Crop crop) throws IOException, HardinessZoneNotSetException { - Alert alert = new Alert(Alert.AlertType.CONFIRMATION); - alert.setTitle("Delete Crop"); - alert.setHeaderText("Are you sure want to delete this Crop?"); - alert.setContentText("placeholder"); - - Optional option = alert.showAndWait(); - - if (option.get() == ButtonType.OK) { - garden.removeCrop(crop); - loadCropList(); - } - } -} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java index a763754..6585675 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java @@ -1,18 +1,16 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.models.PlantListModel; import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; import javafx.beans.property.ListProperty; import javafx.beans.property.SimpleListProperty; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.fxml.FXML; -import javafx.fxml.Initializable; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; @@ -20,20 +18,29 @@ import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import java.io.IOException; -import java.net.URL; import java.time.LocalDate; -import java.util.LinkedList; import java.util.List; -import java.util.ResourceBundle; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class MyScheduleController { + private static final Logger LOG = Logger.getLogger(MyScheduleController.class.getName()); -public class MyScheduleController implements Initializable { private Crop selectedCrop = null; - private final GardenSchedule gardenSchedule = new GardenSchedule(); - private final Garden garden = new Garden(gardenSchedule); - private final PlantListModel plantListModel = new PlantListModel(); + private GardenSchedule gardenSchedule; + private Garden garden; + private PlantList plantList; private final ListProperty cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); + @SuppressWarnings("unused") + public void injectDependencies(Garden garden, GardenSchedule gardenSchedule, PlantList plantList) { + this.garden = garden; + this.gardenSchedule = gardenSchedule; + this.plantList = plantList; + + init(); + } @FXML private Label day1_label; @@ -82,11 +89,7 @@ public class MyScheduleController implements Initializable { @FXML private ListView scheduledPlants_listview; - public MyScheduleController() throws IOException { - } - - @Override - public void initialize(URL location, ResourceBundle resources) { + public void init() { List cropList; try { cropList = garden.getCrops(); @@ -107,15 +110,12 @@ public class MyScheduleController implements Initializable { } private void lookForSelectedListEntries() { - scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Crop oldValue, Crop newValue) { - selectedCrop = newValue; - try { - loadTaskList(); - } catch (IOException e) { - e.printStackTrace(); - } + scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { + selectedCrop = newValue; + try { + loadTaskList(); + } catch (IOException e) { + e.printStackTrace(); } }); } @@ -132,7 +132,7 @@ public class MyScheduleController implements Initializable { } private void setCellFactoryListView() { - scheduledPlants_listview.setCellFactory(param -> new ListCell() { + scheduledPlants_listview.setCellFactory(param -> new ListCell<>() { @Override protected void updateItem(Crop crop, boolean empty) { super.updateItem(crop, empty); @@ -141,9 +141,12 @@ public class MyScheduleController implements Initializable { setText(null); } else { try { - setText(plantListModel.getFilteredPlantListById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(0).name()); + String text = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()) + .map(Plant::name) + .orElse(""); + setText(text); } catch (HardinessZoneNotSetException | IOException e) { - e.printStackTrace(); + LOG.log(Level.WARNING, "Could not get plant for Cell", e); } } } @@ -151,7 +154,7 @@ public class MyScheduleController implements Initializable { } private void loadTaskList() throws IOException { - List> taskLists = new LinkedList<>(); + List> taskLists; if (selectedCrop != null) { taskLists = gardenSchedule.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); } else { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index 1d832ef..0004f30 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -10,11 +10,7 @@ import javafx.beans.property.SimpleListProperty; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; -import javafx.fxml.Initializable; import javafx.geometry.Insets; -import javafx.scene.Parent; -import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; @@ -23,22 +19,27 @@ import javafx.stage.Modality; import javafx.stage.Stage; import java.io.IOException; -import java.net.URL; import java.util.List; -import java.util.Objects; -import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; -public class PlantsController implements Initializable { +public class PlantsController { private static final Logger LOG = Logger.getLogger(PlantsController.class.getName()); - private final PlantListModel plantListModel = new PlantListModel(); + private PlantListModel plantListModel; + private MainFXMLController mainController; private Plant selectedPlant = null; private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A; // TODO: move to model private final ListProperty plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); + @SuppressWarnings("unused") + public void injectDependencies(PlantListModel plantListModel, MainFXMLController mainController) { + this.plantListModel = plantListModel; + this.mainController = mainController; + init(); + } + @FXML private VBox seasons; @@ -66,13 +67,11 @@ public class PlantsController implements Initializable { */ @FXML void selectSowDate(ActionEvent event) throws IOException { - Parent root; - FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("SelectSowDay.fxml"))); - root = fxmlLoader.load(); - SelectSowDayController controller = fxmlLoader.getController(); - controller.getSelectedPlant(selectedPlant); Stage stage = new Stage(); - stage.setScene(new Scene(root)); + if (mainController.loadSceneToStage("SelectSowDay.fxml", stage) instanceof SelectSowDayController controller) { + controller.setSelectedPlant(selectedPlant); + } + stage.initModality(Modality.APPLICATION_MODAL); stage.setResizable(false); stage.showAndWait(); @@ -85,8 +84,7 @@ public class PlantsController implements Initializable { * create event listener for selected list entry and search by query * {@inheritDoc} */ - @Override - public void initialize(URL url, ResourceBundle resourceBundle) { + public void init() { setListCellFactory(); fillPlantListWithHardinessZone(); list_plants.itemsProperty().bind(plantListProperty); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index f00ba59..7dc9389 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -3,7 +3,6 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; -import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.Plant; import javafx.event.ActionEvent; @@ -21,8 +20,12 @@ import java.util.ResourceBundle; public class SelectSowDayController implements Initializable { private Plant selectedPlant = null; - private final GardenSchedule gardenSchedule = new GardenSchedule(); - private final Garden garden = new Garden(gardenSchedule); + private Garden garden; + + @SuppressWarnings("unused") + public void injectDependencies(Garden garden) { + this.garden = garden; + } @FXML private DatePicker datepicker; @@ -34,9 +37,7 @@ public class SelectSowDayController implements Initializable { private Button save_button; @FXML - private RadioButton sow_radio; - - public SelectSowDayController() throws IOException {} + private RadioButton harvest_radio; /** * close the date selector window @@ -54,10 +55,8 @@ public class SelectSowDayController implements Initializable { */ @FXML void save(ActionEvent event) throws HardinessZoneNotSetException, IOException, PlantNotFoundException { - LocalDate sowDate; - if (sow_radio.isSelected()) { - sowDate = datepicker.getValue(); - } else { + LocalDate sowDate = datepicker.getValue(); + if (harvest_radio.isSelected()) { //ToDo method to get current lifecycle group in plant sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); } @@ -69,9 +68,9 @@ public class SelectSowDayController implements Initializable { * save the plant which will be planted and update label * @param plant Plant */ - public void getSelectedPlant(Plant plant) { + public void setSelectedPlant(Plant plant) { selectedPlant = plant; - popup_label.setText("Select Harvest/Sow Date for" + selectedPlant.name()); + popup_label.setText(String.format("Select Harvest/Sow Date for %s:", selectedPlant.name())); } /** @@ -86,16 +85,16 @@ public class SelectSowDayController implements Initializable { Callback dayCellFactory= getDayCellFactory(); datepicker.setDayCellFactory(dayCellFactory); - datepicker.getEditor().setEditable(false); + datepicker.setEditable(false); - enableDisableSaveButton(); + save_button.disableProperty().bind(datepicker.valueProperty().isNull()); } /** * clear date picker editor when radio button is changed */ private void clearDatePickerEntries() { - sow_radio.selectedProperty().addListener((observable, oldValue, isNowSelected) -> datepicker.getEditor().clear()); + harvest_radio.selectedProperty().addListener((observable, oldValue, isNowSelected) -> datepicker.setValue(null)); } /** @@ -104,40 +103,33 @@ public class SelectSowDayController implements Initializable { */ private Callback getDayCellFactory() { - final Callback dayCellFactory = new Callback() { - + return (datePicker) -> new DateCell() { @Override - public DateCell call(final DatePicker datePicker) { - return new DateCell() { - @Override - public void updateItem(LocalDate item, boolean empty) { - super.updateItem(item, empty); - setDisable(true); - setStyle("-fx-background-color: #ffc0cb;"); - List dates; - LocalDate today = LocalDate.now(); - if (sow_radio.isSelected()) { - dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.SOW); - } else { - dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.HARVEST); - } - for (LocalDate date : dates) { - if (item.getMonth() == date.getMonth() - && item.getDayOfMonth() == date.getDayOfMonth() - && item.compareTo(today) > 0) { - setDisable(false); - setStyle("-fx-background-color: #32CD32;"); - } - } - if ((!sow_radio.isSelected() && selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) < 0)) { - setDisable(true); - setStyle("-fx-background-color: #ffc0cb;"); - } + public void updateItem(LocalDate item, boolean empty) { + super.updateItem(item, empty); + setDisable(true); + setStyle("-fx-background-color: #ffc0cb;"); + List dates; + LocalDate today = LocalDate.now(); + if (harvest_radio.isSelected()) { + dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.HARVEST); + } else { + dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.SOW); + } + for (LocalDate date : dates) { + if (item.getMonth() == date.getMonth() + && item.getDayOfMonth() == date.getDayOfMonth() + && item.compareTo(today) > 0) { + setDisable(false); + setStyle("-fx-background-color: #32CD32;"); } - }; + } + if ((harvest_radio.isSelected() && selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) < 0)) { + setDisable(true); + setStyle("-fx-background-color: #ffc0cb;"); + } } }; - return dayCellFactory; } /** @@ -147,18 +139,4 @@ public class SelectSowDayController implements Initializable { Stage stage = (Stage) save_button.getScene().getWindow(); stage.close(); } - - /** - * disable save button, when there is no date selected in date picker - */ - private void enableDisableSaveButton() { - save_button.setDisable(true); - datepicker.getEditor().textProperty().addListener((observable, oldValue, newValue) -> { - if (newValue == null || newValue.equals("")) { - save_button.setDisable(true); - } else { - save_button.setDisable(false); - } - }); - } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/Garden.java b/src/main/java/ch/zhaw/gartenverwaltung/models/Garden.java index 3d6032a..fd25b2a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/Garden.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/Garden.java @@ -2,12 +2,12 @@ package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.io.JsonCropList; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; +import javafx.beans.property.ListProperty; +import javafx.beans.property.SimpleListProperty; import javafx.collections.FXCollections; -import javafx.collections.ObservableList; import java.io.IOException; import java.time.LocalDate; @@ -19,21 +19,25 @@ import java.util.Optional; * The Gardenplan model manages the crops in the gardenplan. */ public class Garden { - private CropList cropList; - private final ObservableList plantedCrops = FXCollections.observableArrayList(); - private GardenSchedule gardenSchedule; + private final CropList cropList; + private final ListProperty plantedCrops = new SimpleListProperty<>(FXCollections.observableArrayList()); + private final GardenSchedule gardenSchedule; /** * Constructor of Gardenplan model * * @param gardenSchedule holds a reference to the task list object. */ - public Garden(GardenSchedule gardenSchedule) throws IOException { + public Garden(GardenSchedule gardenSchedule, CropList cropList) throws IOException { this.gardenSchedule = gardenSchedule; - cropList = new JsonCropList(); + this.cropList = cropList; plantedCrops.addAll(cropList.getCrops()); } + public ListProperty getPlantedCrops() { + return plantedCrops; + } + /** * Creates a Crop with a {@link Plant} and the planting date of the plant. Then let the Tasklistmodel create the * gardening {@link Task} for the crop. Store the crop in the gardenplan file and the cache. diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index 30a0a50..9195d51 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -12,19 +12,14 @@ import java.util.List; import java.util.stream.Collectors; public class GardenSchedule { - private TaskList taskList; - private PlantList plantList; + private final TaskList taskList; + private final PlantList plantList; /** * Comparators to create sorted Task List */ static final Comparator sortByStartDate = Comparator.comparing(Task::getStartDate); - public GardenSchedule(){ - taskList = new JsonTaskList(); - plantList = new JsonPlantList(); - } - /** * Constructor to create Database Objects. */ diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/models/PlantListModel.java index 6b96bee..19cce96 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/PlantListModel.java @@ -1,7 +1,6 @@ package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.io.JsonPlantList; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.HardinessZone; @@ -16,7 +15,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; public class PlantListModel { - private PlantList plantList; + private final PlantList plantList; private HardinessZone currentZone; /** @@ -28,11 +27,6 @@ public class PlantListModel { /** * Constructor to create Database Object. */ - public PlantListModel() { - plantList = new JsonPlantList(); - setDefaultZone(); - } - public PlantListModel(PlantList plantList) { this.plantList = plantList; setDefaultZone(); diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 6900f7f..44103dd 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -9,6 +9,8 @@ module ch.zhaw.gartenverwaltung { opens ch.zhaw.gartenverwaltung to javafx.fxml; opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind; exports ch.zhaw.gartenverwaltung; + exports ch.zhaw.gartenverwaltung.io; exports ch.zhaw.gartenverwaltung.types; + exports ch.zhaw.gartenverwaltung.models; exports ch.zhaw.gartenverwaltung.json; } \ No newline at end of file diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/MainFXML.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/MainFXML.fxml index 569a89d..7fa27e1 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/MainFXML.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/MainFXML.fxml @@ -11,8 +11,8 @@