From 29ad2fdae211e306c8413c7da4f76ec9f4aedf03 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 31 Oct 2022 13:19:14 +0100 Subject: [PATCH] #17 Bug Mismatchexception fixed --- .../gartenverwaltung/io/JsonTaskDatabase.java | 19 ++++-- .../ch/zhaw/gartenverwaltung/io/taskdb.json | 64 +++++++++---------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index 37c7016..a849bbc 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; import java.net.URL; import java.time.LocalDate; import java.time.format.DateTimeFormatter; @@ -25,6 +26,7 @@ import java.util.Map; public class JsonTaskDatabase implements TaskDatabase{ IdProvider idProvider; private final URL dataSource = getClass().getResource("taskdb.json"); + private final static String INVALID_DATASOURCE_MSG = "Invalid datasource specified!"; private Map taskMap = Collections.emptyMap(); @@ -99,12 +101,16 @@ public class JsonTaskDatabase implements TaskDatabase{ * @throws IOException If the database cannot be accessed */ private void writeTaskListToFile() throws IOException { - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(timeModule) - .registerModule(new Jdk8Module()); - if(dataSource != null) { - mapper.writeValue(new File(dataSource.getFile()), taskMap); + try { + new ObjectMapper() + .registerModule(timeModule) + .registerModule(new Jdk8Module()) + .writeValue(new File(dataSource.toURI()), taskMap.values()); + + } catch (URISyntaxException e) { + throw new IOException(INVALID_DATASOURCE_MSG, e); + } } } @@ -116,7 +122,8 @@ public class JsonTaskDatabase implements TaskDatabase{ private void loadTaskListFromFile() throws IOException { if (dataSource != null) { ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(timeModule); + mapper.registerModule(timeModule) + .registerModule(new Jdk8Module()); List result; result = mapper.readerForListOf(Task.class).readValue(dataSource); diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json index da201c9..8b955f9 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, - "name" : "sow plant", - "description": "Plant the seeds, crops in de bed.", - "startDate" : "2022-05-01", - "endDate" : "2022-05-01", - "interval" : 0 + "id": 1, + "name": "sow plant", + "description": "Plant the seeds, crops in de bed.", + "startDate": "2022-05-01", + "endDate": "2022-05-01", + "interval": 0 }, { - "id" : 2, - "name" : "water plant", + "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 + "startDate": "2022-05-01", + "endDate": "2022-09-01", + "interval": 2 }, { - "id" : 3, - "name" : "fertilize plant", + "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 + "startDate": "2022-06-01", + "endDate": "2022-08-01", + "interval": 28 }, { - "id" : 4, - "name" : "covering plant", + "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 + "startDate": "2022-07-01", + "endDate": "2022-07-01", + "interval": 0 }, { - "id" : 5, - "name" : "look after plant", + "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 + "startDate": "2022-05-01", + "endDate": "2022-09-01", + "interval": 5 }, { - "id" : 6, - "name" : "harvest plant", + "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 + "startDate": "2022-09-01", + "endDate": "2022-09-01", + "interval": 0 } -] \ No newline at end of file +]