From 96b5dba36ce8638a2c739c168decc9ef88934562 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Thu, 20 Oct 2022 21:59:18 +0200 Subject: [PATCH 1/9] #17 first version of tasks --- .../ch/zhaw/gartenverwaltung/io/taskdb.json | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json new file mode 100644 index 0000000..049b341 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json @@ -0,0 +1,32 @@ +[ + { + "name" : "water plant", + "description": "water the plant, that the soil is wet around the plant.", + "tools": "watering can" + }, + { + "name" : "sow plant", + "description": "", + "tools": "crops, small shovel, gardening gloves, soil" + }, + { + "name" : "harvest plant", + "description": "", + "tools": "box or bag for the harvested vegetable" + }, + { + "name" : "fertilize plant", + "description": "", + "tools": "Fertilizer, small shovel, gardening gloves" + }, + { + "name" : "covering plant", + "description": "", + "tools": "" + }, + { + "name" : "look for plant", + "description": "look for pest or illness at the leaves", + "tools": "" + } +] \ No newline at end of file From a2008450e649ddcf68e48817c9977fa96a76776b Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Fri, 21 Oct 2022 21:18:29 +0200 Subject: [PATCH 2/9] #17 implementation of an example task database --- .../ch/zhaw/gartenverwaltung/io/taskdb.json | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json index 049b341..1155b9a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json @@ -1,32 +1,50 @@ [ { + "id" : "1", + "name" : "sow plant", + "description": "Plant the seeds/ crops in de bed.", + "startDate" : "01.05.2022", + "endDate" : "01.05.2022", + "interval" : "null" + }, + { + "id" : "2", "name" : "water plant", - "description": "water the plant, that the soil is wet around the plant.", - "tools": "watering can" - }, - { - "name" : "sow plant", - "description": "", - "tools": "crops, small shovel, gardening gloves, soil" - }, - { - "name" : "harvest plant", - "description": "", - "tools": "box or bag for the harvested vegetable" + "description": "water the plant, so that the soil is wet around the plant.", + "startDate" : "01.05.2022", + "endDate" : "01.09.2022", + "interval" : "2" }, { + "id" : "3", "name" : "fertilize plant", - "description": "", - "tools": "Fertilizer, small shovel, gardening gloves" + "description": "The fertilizer has to be mixed with water. Then fertilize the plant's soil with the mixture", + "startDate" : "01.06.2022", + "endDate" : "01.08.2022", + "interval" : "28" }, { + "id" : "4", "name" : "covering plant", - "description": "", - "tools": "" + "description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage", + "startDate" : "15.07.2022", + "endDate" : "15.07.2022", + "interval" : "null" }, { - "name" : "look for plant", - "description": "look for pest or illness at the leaves", - "tools": "" + "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" : "01.05.2022", + "endDate" : "01.09.2022", + "interval" : "5" + }, + { + "id" : "6", + "name" : "harvest plant", + "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", + "startDate" : "01.09.2022", + "endDate" : "01.09.2022", + "interval" : "null" } ] \ No newline at end of file From e228b9019db52bfa40dfbae80e9fc8a7642b3a8c Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Sat, 22 Oct 2022 11:52:06 +0200 Subject: [PATCH 3/9] #17 implementation JsonTaskDatabase.java, gradle import for jackson --- build.gradle | 2 + .../gartenverwaltung/io/JsonTaskDatabase.java | 67 +++++++++++++++++++ .../gartenverwaltung/io/TaskDatabase.java | 2 +- src/main/java/module-info.java | 2 + 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java diff --git a/build.gradle b/build.gradle index 5268664..ad6e0a5 100644 --- a/build.gradle +++ b/build.gradle @@ -37,6 +37,8 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4' + implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4' } test { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java new file mode 100644 index 0000000..c6a52a2 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -0,0 +1,67 @@ +package ch.zhaw.gartenverwaltung.io; + +import ch.zhaw.gartenverwaltung.types.Task; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.time.MonthDay; +import java.time.format.DateTimeFormatter; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Optional; + +public class JsonTaskDatabase implements TaskDatabase{ + private final URL dataSource = getClass().getResource("taskdb.json"); + + private final static JavaTimeModule timeModule = new JavaTimeModule(); + static { + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd"); + MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat); + timeModule.addDeserializer(MonthDay.class, dateDeserializer); + } + + @Override + public List getTaskList(Date start, Date end) throws IOException{ + List taskList = Collections.emptyList(); + + if (dataSource != null) { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(timeModule); + + taskList = mapper.readerForListOf(Task.class).readValue(dataSource); + } + + return taskList; + } + + @Override + public void saveTask(Task task) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + if(dataSource != null) { + mapper.writeValue(new File(dataSource.getFile()), task); + } else { + mapper.writeValue(new File("taskdb.json"),task); + } + } + + @Override + public void removeTask(Task task) throws IOException { + List taskList; + + if(dataSource != null) { + ObjectMapper mapper = new ObjectMapper(); + taskList = mapper.readerForListOf(Task.class).readValue(dataSource); + for (int index = 0; index < taskList.size(); index++){ + if(task.equals(taskList.get(index))){ + taskList.remove(task); + break; + } + } + } + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java index 73cf8e0..4c71397 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java @@ -7,7 +7,7 @@ import java.util.Date; import java.util.List; public interface TaskDatabase { - List getTaskList(Date start, Date end); + List getTaskList(Date start, Date end) throws IOException; void saveTask(Task task) throws IOException; void removeTask(Task task) throws IOException; } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 4bffe36..fce2f6b 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,6 +1,8 @@ module ch.zhaw.gartenverwaltung { requires javafx.controls; requires javafx.fxml; + requires com.fasterxml.jackson.databind; + requires com.fasterxml.jackson.datatype.jsr310; opens ch.zhaw.gartenverwaltung to javafx.fxml; From 904041afc0fb5ae0f21f59eb6602c78bb64143ae Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 24 Oct 2022 12:43:50 +0200 Subject: [PATCH 4/9] #17 implementation JsonTaskDatabase.java, gradle import for jackson --- .../zhaw/gartenverwaltung/io/JsonTaskDatabase.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index c6a52a2..1889706 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -4,10 +4,12 @@ import ch.zhaw.gartenverwaltung.types.Task; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer; +import javafx.util.converter.LocalDateStringConverter; import java.io.File; import java.io.IOException; import java.net.URL; +import java.time.LocalDate; import java.time.MonthDay; import java.time.format.DateTimeFormatter; import java.util.Collections; @@ -18,6 +20,18 @@ import java.util.Optional; public class JsonTaskDatabase implements TaskDatabase{ private final URL dataSource = getClass().getResource("taskdb.json"); + public static void main(String[] args) { + Date date = new Date(); + Date yesterday = new Date(-1); + Task testTask = new Task("water", "apply water", date); + JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase(); + try { + jsonTaskDatabase.saveTask(testTask); + System.out.println(jsonTaskDatabase.getTaskList(yesterday,date)); + } catch (Exception e){ + System.out.println("Task saving failed!"); + } + } private final static JavaTimeModule timeModule = new JavaTimeModule(); static { DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd"); From 694da97cd673f339784353672cce9c70cf3f88d0 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 24 Oct 2022 13:54:28 +0200 Subject: [PATCH 5/9] #17 implementation JsonTaskDatabase.java, writeTasklistToFile --- .../gartenverwaltung/io/JsonTaskDatabase.java | 69 ++++++++++++------- .../gartenverwaltung/io/TaskDatabase.java | 3 +- .../ch/zhaw/gartenverwaltung/io/taskdb.json | 0 3 files changed, 48 insertions(+), 24 deletions(-) rename src/main/{java => resources}/ch/zhaw/gartenverwaltung/io/taskdb.json (100%) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index 1889706..5c7211b 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -1,10 +1,10 @@ package ch.zhaw.gartenverwaltung.io; +import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer; -import javafx.util.converter.LocalDateStringConverter; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import java.io.File; import java.io.IOException; @@ -13,34 +13,26 @@ import java.time.LocalDate; import java.time.MonthDay; import java.time.format.DateTimeFormatter; import java.util.Collections; -import java.util.Date; +import java.util.HashMap; import java.util.List; -import java.util.Optional; +import java.util.Map; public class JsonTaskDatabase implements TaskDatabase{ private final URL dataSource = getClass().getResource("taskdb.json"); - public static void main(String[] args) { - Date date = new Date(); - Date yesterday = new Date(-1); - Task testTask = new Task("water", "apply water", date); - JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase(); - try { - jsonTaskDatabase.saveTask(testTask); - System.out.println(jsonTaskDatabase.getTaskList(yesterday,date)); - } catch (Exception e){ - System.out.println("Task saving failed!"); - } - } + private Map taskMap = Collections.emptyMap(); + + + private final static JavaTimeModule timeModule = new JavaTimeModule(); static { - DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd"); - MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat); - timeModule.addDeserializer(MonthDay.class, dateDeserializer); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM-dd"); + LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat); + timeModule.addDeserializer(LocalDate.class, dateDeserializer); } @Override - public List getTaskList(Date start, Date end) throws IOException{ + public List getTaskList(LocalDate start, LocalDate end) throws IOException{ List taskList = Collections.emptyList(); if (dataSource != null) { @@ -49,7 +41,6 @@ public class JsonTaskDatabase implements TaskDatabase{ taskList = mapper.readerForListOf(Task.class).readValue(dataSource); } - return taskList; } @@ -58,8 +49,6 @@ public class JsonTaskDatabase implements TaskDatabase{ ObjectMapper mapper = new ObjectMapper(); if(dataSource != null) { mapper.writeValue(new File(dataSource.getFile()), task); - } else { - mapper.writeValue(new File("taskdb.json"),task); } } @@ -70,6 +59,7 @@ public class JsonTaskDatabase implements TaskDatabase{ if(dataSource != null) { ObjectMapper mapper = new ObjectMapper(); taskList = mapper.readerForListOf(Task.class).readValue(dataSource); + for (int index = 0; index < taskList.size(); index++){ if(task.equals(taskList.get(index))){ taskList.remove(task); @@ -78,4 +68,37 @@ public class JsonTaskDatabase implements TaskDatabase{ } } } + + private void writeTaskListToFile() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(timeModule); + + mapper.writeValue(new File(dataSource.getFile()), taskMap); + } + + private void loadTaskListFromFile() throws IOException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(timeModule); + + List result; + result = mapper.readerForListOf(Task.class).readValue(dataSource); + + //taskMap = result.stream().collect(HashMap::new, + } + + public static void main(String[] args) { + LocalDate date = LocalDate.now(); + LocalDate yesterday = LocalDate.now(); + yesterday.minusDays(1); + + Task testTask = new Task("water", "apply water", date); + JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase(); + try { + jsonTaskDatabase.saveTask(testTask); + System.out.println(jsonTaskDatabase.getTaskList(yesterday,date)); + } catch (Exception e){ + System.out.println("Task saving failed!"); + } + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java index 4c71397..e9c52a8 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java @@ -3,11 +3,12 @@ package ch.zhaw.gartenverwaltung.io; import ch.zhaw.gartenverwaltung.types.Task; import java.io.IOException; +import java.time.LocalDate; import java.util.Date; import java.util.List; public interface TaskDatabase { - List getTaskList(Date start, Date end) throws IOException; + List getTaskList(LocalDate start, LocalDate end) throws IOException; void saveTask(Task task) throws IOException; void removeTask(Task task) throws IOException; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json similarity index 100% rename from src/main/java/ch/zhaw/gartenverwaltung/io/taskdb.json rename to src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json From af0e73c007df88d03aec9b80443f1b6965e91e1e Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 24 Oct 2022 14:20:59 +0200 Subject: [PATCH 6/9] Prepare for tests --- .../io/JsonTaskDatabaseTest.java | 37 ++++++++++++++ .../ch/zhaw/gartenverwaltung/io/taskdb.json | 50 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/taskdb.json diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java new file mode 100644 index 0000000..e75ea40 --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java @@ -0,0 +1,37 @@ +package ch.zhaw.gartenverwaltung.io; + +import ch.zhaw.gartenverwaltung.types.Task; +import org.junit.jupiter.api.*; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +public class JsonTaskDatabaseTest { + + TaskDatabase testDatabase; + SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); + @BeforeEach + void connectToDb() { + // testDatabase = new JsonTaskDatabase(); + } + + + @Test + @DisplayName("Check if results are retrieved completely") + void getTasks(){ + /* + List taskList=null; + try { + taskList = testDatabase.getTaskList(formatter.parse("01.05.2022"), formatter.parse("01.08.2022")); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (ParseException e) { + throw new RuntimeException(e); + } + + Assertions.assertTrue(taskList.size()>0); + */ + } +} diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/taskdb.json new file mode 100644 index 0000000..1155b9a --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/taskdb.json @@ -0,0 +1,50 @@ +[ + { + "id" : "1", + "name" : "sow plant", + "description": "Plant the seeds/ crops in de bed.", + "startDate" : "01.05.2022", + "endDate" : "01.05.2022", + "interval" : "null" + }, + { + "id" : "2", + "name" : "water plant", + "description": "water the plant, so that the soil is wet around the plant.", + "startDate" : "01.05.2022", + "endDate" : "01.09.2022", + "interval" : "2" + }, + { + "id" : "3", + "name" : "fertilize plant", + "description": "The fertilizer has to be mixed with water. Then fertilize the plant's soil with the mixture", + "startDate" : "01.06.2022", + "endDate" : "01.08.2022", + "interval" : "28" + }, + { + "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" : "15.07.2022", + "endDate" : "15.07.2022", + "interval" : "null" + }, + { + "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" : "01.05.2022", + "endDate" : "01.09.2022", + "interval" : "5" + }, + { + "id" : "6", + "name" : "harvest plant", + "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", + "startDate" : "01.09.2022", + "endDate" : "01.09.2022", + "interval" : "null" + } +] \ No newline at end of file From 23d87f7a85e6b609bc32d0c22e412d9d18c72db3 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 25 Oct 2022 10:10:40 +0200 Subject: [PATCH 7/9] #17 taskdb.json changed to the right variable types, ad isIntimePeriode to , JasonTaskDatabase --- .../gartenverwaltung/io/JsonTaskDatabase.java | 69 +++++++++---------- .../ch/zhaw/gartenverwaltung/types/Task.java | 8 +++ .../ch/zhaw/gartenverwaltung/io/taskdb.json | 48 ++++++------- 3 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index 5c7211b..caf02d1 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -1,16 +1,17 @@ package ch.zhaw.gartenverwaltung.io; -import ch.zhaw.gartenverwaltung.types.Plant; +import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Task; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer; import java.io.File; import java.io.IOException; import java.net.URL; import java.time.LocalDate; -import java.time.MonthDay; +import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.HashMap; @@ -22,26 +23,20 @@ public class JsonTaskDatabase implements TaskDatabase{ private Map taskMap = Collections.emptyMap(); - - private final static JavaTimeModule timeModule = new JavaTimeModule(); static { - DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM-dd"); - LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat); - timeModule.addDeserializer(LocalDate.class, dateDeserializer); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM"); + YearMonthDeserializer dateDeserializer = new YearMonthDeserializer(dateFormat); + timeModule.addDeserializer(YearMonth.class, dateDeserializer); } @Override public List getTaskList(LocalDate start, LocalDate end) throws IOException{ - List taskList = Collections.emptyList(); - if (dataSource != null) { - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(timeModule); - - taskList = mapper.readerForListOf(Task.class).readValue(dataSource); + if(taskMap.isEmpty()) { + loadTaskListFromFile(); } - return taskList; + return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList(); } @Override @@ -54,18 +49,8 @@ public class JsonTaskDatabase implements TaskDatabase{ @Override public void removeTask(Task task) throws IOException { - List taskList; - - if(dataSource != null) { - ObjectMapper mapper = new ObjectMapper(); - taskList = mapper.readerForListOf(Task.class).readValue(dataSource); - - for (int index = 0; index < taskList.size(); index++){ - if(task.equals(taskList.get(index))){ - taskList.remove(task); - break; - } - } + if(!taskMap.isEmpty() && taskMap.containsKey(task.getId())){ + taskMap.remove(task.getId()); } } @@ -76,29 +61,39 @@ public class JsonTaskDatabase implements TaskDatabase{ mapper.writeValue(new File(dataSource.getFile()), taskMap); } + private void loadTaskListFromFile() throws IOException { + if (dataSource != null) { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(timeModule); - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(timeModule); + List result; + result = mapper.readerForListOf(Task.class).readValue(dataSource); - List result; - result = mapper.readerForListOf(Task.class).readValue(dataSource); - - //taskMap = result.stream().collect(HashMap::new, + taskMap = result.stream() + .collect(HashMap::new, + (res, task) -> res.put(task.getId(), task), + (existing, replacement) -> {}); + System.out.println(taskMap); + } } + //Test main method public static void main(String[] args) { LocalDate date = LocalDate.now(); LocalDate yesterday = LocalDate.now(); - yesterday.minusDays(1); - + yesterday = yesterday.minusDays(1); Task testTask = new Task("water", "apply water", date); JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase(); + JsonPlantDatabase jsonPlantDatabase = new JsonPlantDatabase(); + try { - jsonTaskDatabase.saveTask(testTask); - System.out.println(jsonTaskDatabase.getTaskList(yesterday,date)); + //test load file + jsonPlantDatabase.getPlantList(HardinessZone.ZONE_8A); + jsonTaskDatabase.loadTaskListFromFile(); + } catch (Exception e){ - System.out.println("Task saving failed!"); + System.out.println("Task load failed!: " + e.getMessage()); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 90e00f4..64d1331 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -21,6 +21,7 @@ public class Task { this.description = description; this.startDate = startDate; } + public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) { this.name = name; this.description = description; @@ -43,6 +44,13 @@ public class Task { return this; } + public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){ + if(startDate.isAfter(searchStartDate) &&startDate.isBefore(searchEndDate)){ + return true; + } + return false; + } + // Getters public long getId() { return id; } public String getName() { return name; } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json index 1155b9a..1597ab0 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json @@ -1,50 +1,50 @@ [ { - "id" : "1", + "id" : 1, "name" : "sow plant", "description": "Plant the seeds/ crops in de bed.", - "startDate" : "01.05.2022", - "endDate" : "01.05.2022", - "interval" : "null" + "startDate" : "22-05", + "endDate" : "22-05", + "interval" : 0 }, { - "id" : "2", + "id" : 2, "name" : "water plant", "description": "water the plant, so that the soil is wet around the plant.", - "startDate" : "01.05.2022", - "endDate" : "01.09.2022", - "interval" : "2" + "startDate" : "22-05", + "endDate" : "22-09", + "interval" : 2 }, { - "id" : "3", + "id" : 3, "name" : "fertilize plant", "description": "The fertilizer has to be mixed with water. Then fertilize the plant's soil with the mixture", - "startDate" : "01.06.2022", - "endDate" : "01.08.2022", - "interval" : "28" + "startDate" : "22-06", + "endDate" : "22-08", + "interval" : 28 }, { - "id" : "4", + "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" : "15.07.2022", - "endDate" : "15.07.2022", - "interval" : "null" + "startDate" : "22-07", + "endDate" : "22-07", + "interval" : 0 }, { - "id" : "5", + "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" : "01.05.2022", - "endDate" : "01.09.2022", - "interval" : "5" + "startDate" : "22-05", + "endDate" : "22-09", + "interval" : 5 }, { - "id" : "6", + "id" : 6, "name" : "harvest plant", "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", - "startDate" : "01.09.2022", - "endDate" : "01.09.2022", - "interval" : "null" + "startDate" : "22-09", + "endDate" : "22-09", + "interval" : 0 } ] \ No newline at end of file From 6c75fcd0ecf509a5b50c99e265c69cb5e8cf4cee Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 25 Oct 2022 18:11:29 +0200 Subject: [PATCH 8/9] #17 taskdb.json changed to the right variable types, JsonTaskDatabase.java fully implemented and documented --- .../gartenverwaltung/io/JsonTaskDatabase.java | 91 ++++++++++++------- .../gartenverwaltung/io/TaskDatabase.java | 28 ++++++ .../ch/zhaw/gartenverwaltung/types/Task.java | 10 ++ .../ch/zhaw/gartenverwaltung/io/taskdb.json | 28 +++--- 4 files changed, 110 insertions(+), 47 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index caf02d1..a460f2f 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -1,67 +1,112 @@ package ch.zhaw.gartenverwaltung.io; -import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Task; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; -import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer; import java.io.File; import java.io.IOException; import java.net.URL; import java.time.LocalDate; -import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +/** + * Implements the {@link TaskDatabase} 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{ + IdProvider idProvider; private final URL dataSource = getClass().getResource("taskdb.json"); private Map taskMap = Collections.emptyMap(); + /** + * Creating constant objects required to deserialize the {@link LocalDate} classes + */ private final static JavaTimeModule timeModule = new JavaTimeModule(); static { - DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM"); - YearMonthDeserializer dateDeserializer = new YearMonthDeserializer(dateFormat); - timeModule.addDeserializer(YearMonth.class, dateDeserializer); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat); + timeModule.addDeserializer(LocalDate.class, dateDeserializer); } + /** + * 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) + */ @Override public List getTaskList(LocalDate start, LocalDate end) throws IOException{ - if(taskMap.isEmpty()) { loadTaskListFromFile(); } return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList(); } + /** + * If no data is currently loaded, data is loaded from {@link #dataSource}. + * If the {@link Task} has an id than the task is added to the {@link #taskMap} + * otherwise the id is generated with the {@link IdProvider} before adding + * it to the {@link #taskMap}. In any case, the {@link #taskMap} is written + * to the {@link #dataSource}. + * + * @see TaskDatabase#saveTask(Task) + */ @Override public void saveTask(Task task) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - if(dataSource != null) { - mapper.writeValue(new File(dataSource.getFile()), task); + if(taskMap.isEmpty()) { + loadTaskListFromFile(); } + if(task.getId() == 0) { + task.withId(idProvider.incrementAndGet()); + } + writeTaskListToFile(); } + /** + * If no data is currently loaded, data is loaded from {@link #dataSource}. + * 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) + */ @Override public void removeTask(Task task) throws IOException { - if(!taskMap.isEmpty() && taskMap.containsKey(task.getId())){ + if(taskMap.isEmpty()) { + loadTaskListFromFile(); + } + if(taskMap.containsKey(task.getId())){ taskMap.remove(task.getId()); + writeTaskListToFile(); } } + /** + * Writes cached data to the {@link #dataSource}. + * + * @throws IOException If the database cannot be accessed + */ private void writeTaskListToFile() throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(timeModule); - mapper.writeValue(new File(dataSource.getFile()), taskMap); + if(dataSource != null) { + mapper.writeValue(new File(dataSource.getFile()), taskMap); + } } - + /** + * Loads the database from {@link #dataSource} and updates the cached data. + * + * @throws IOException If the database cannot be accessed + */ private void loadTaskListFromFile() throws IOException { if (dataSource != null) { ObjectMapper mapper = new ObjectMapper(); @@ -74,26 +119,6 @@ public class JsonTaskDatabase implements TaskDatabase{ .collect(HashMap::new, (res, task) -> res.put(task.getId(), task), (existing, replacement) -> {}); - System.out.println(taskMap); - } - } - - //Test main method - public static void main(String[] args) { - LocalDate date = LocalDate.now(); - LocalDate yesterday = LocalDate.now(); - yesterday = yesterday.minusDays(1); - Task testTask = new Task("water", "apply water", date); - JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase(); - JsonPlantDatabase jsonPlantDatabase = new JsonPlantDatabase(); - - try { - //test load file - jsonPlantDatabase.getPlantList(HardinessZone.ZONE_8A); - jsonTaskDatabase.loadTaskListFromFile(); - - } catch (Exception e){ - System.out.println("Task load failed!: " + e.getMessage()); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java index e9c52a8..19ad778 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java @@ -1,5 +1,7 @@ package ch.zhaw.gartenverwaltung.io; +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; import java.io.IOException; @@ -7,8 +9,34 @@ 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 { + /** + * Yields a list of all {@link Task}s in the database with the start and end date of a period of time. + * + * @param start The start date of the wanted time period + * @param end The end date of the wanted time period + * @return A list of {@link Task}s planned in the specified time period + * @throws IOException If the database cannot be accessed + */ List getTaskList(LocalDate start, LocalDate end) throws IOException; + + /** + * Saves the {@link Task} in the Cache. + * + * @param task The {@link Task} which is wanted to be saved in the TaskList + * @throws IOException If the database cannot be accessed + */ void saveTask(Task task) throws IOException; + + /** + * Removes the {@link Task}from the Cache. + * + * @param task The {@link Task} which has to be removed from the {@link Task} list. + * @throws IOException If the database cannot be accessed + */ void removeTask(Task task) throws IOException; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 64d1331..3b26745 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -16,6 +16,16 @@ public class Task { private Integer interval; private LocalDate endDate; + /** + * default constructor + * (used by Json deserializer) + */ + public Task(){ + name= ""; + description= ""; + startDate = LocalDate.now(); + } + public Task(String name, String description, LocalDate startDate) { this.name = name; this.description = description; diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json index 1597ab0..da201c9 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json @@ -2,49 +2,49 @@ { "id" : 1, "name" : "sow plant", - "description": "Plant the seeds/ crops in de bed.", - "startDate" : "22-05", - "endDate" : "22-05", + "description": "Plant the seeds, crops in de bed.", + "startDate" : "2022-05-01", + "endDate" : "2022-05-01", "interval" : 0 }, { "id" : 2, "name" : "water plant", "description": "water the plant, so that the soil is wet around the plant.", - "startDate" : "22-05", - "endDate" : "22-09", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", "interval" : 2 }, { "id" : 3, "name" : "fertilize plant", - "description": "The fertilizer has to be mixed with water. Then fertilize the plant's soil with the mixture", - "startDate" : "22-06", - "endDate" : "22-08", + "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 }, { "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" : "22-07", - "endDate" : "22-07", + "startDate" : "2022-07-01", + "endDate" : "2022-07-01", "interval" : 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" : "22-05", - "endDate" : "22-09", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", "interval" : 5 }, { "id" : 6, "name" : "harvest plant", "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", - "startDate" : "22-09", - "endDate" : "22-09", + "startDate" : "2022-09-01", + "endDate" : "2022-09-01", "interval" : 0 } ] \ No newline at end of file From 51e8f27a20bdc7ca28b6bd28ff8f67f6ae6ebc48 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Fri, 28 Oct 2022 12:24:55 +0200 Subject: [PATCH 9/9] #17 implementation objectmapper writeFile --- .../zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java | 8 ++++++++ .../ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java new file mode 100644 index 0000000..24757bd --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java @@ -0,0 +1,8 @@ +package ch.zhaw.gartenverwaltung.gardenplan; + +public class Gardenplanmodel { +// private JasonGardenplan gardenplan; + // liste von crops + //task generieren + //plant holen, task template mit tasktemplateklasse tasks erstellen +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index a460f2f..e16a74d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -2,6 +2,7 @@ package ch.zhaw.gartenverwaltung.io; import ch.zhaw.gartenverwaltung.types.Task; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; @@ -95,7 +96,8 @@ public class JsonTaskDatabase implements TaskDatabase{ */ private void writeTaskListToFile() throws IOException { ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(timeModule); + mapper.registerModule(timeModule) + .registerModule(new Jdk8Module()); if(dataSource != null) { mapper.writeValue(new File(dataSource.getFile()), taskMap);