#17 Bug Mismatchexception fixed

This commit is contained in:
Gian-Andrea Hutter 2022-10-31 13:19:14 +01:00
parent afac3ba855
commit 29ad2fdae2
2 changed files with 45 additions and 38 deletions

View File

@ -9,6 +9,7 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -25,6 +26,7 @@ import java.util.Map;
public class JsonTaskDatabase implements TaskDatabase{ public class JsonTaskDatabase implements TaskDatabase{
IdProvider idProvider; IdProvider idProvider;
private final URL dataSource = getClass().getResource("taskdb.json"); private final URL dataSource = getClass().getResource("taskdb.json");
private final static String INVALID_DATASOURCE_MSG = "Invalid datasource specified!";
private Map<Long, Task> taskMap = Collections.emptyMap(); private Map<Long, Task> taskMap = Collections.emptyMap();
@ -99,12 +101,16 @@ public class JsonTaskDatabase implements TaskDatabase{
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void writeTaskListToFile() throws IOException { private void writeTaskListToFile() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule)
.registerModule(new Jdk8Module());
if(dataSource != null) { 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 { private void loadTaskListFromFile() throws IOException {
if (dataSource != null) { if (dataSource != null) {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule); mapper.registerModule(timeModule)
.registerModule(new Jdk8Module());
List<Task> result; List<Task> result;
result = mapper.readerForListOf(Task.class).readValue(dataSource); result = mapper.readerForListOf(Task.class).readValue(dataSource);

View File

@ -1,50 +1,50 @@
[ [
{ {
"id" : 1, "id": 1,
"name" : "sow plant", "name": "sow plant",
"description": "Plant the seeds, crops in de bed.", "description": "Plant the seeds, crops in de bed.",
"startDate" : "2022-05-01", "startDate": "2022-05-01",
"endDate" : "2022-05-01", "endDate": "2022-05-01",
"interval" : 0 "interval": 0
}, },
{ {
"id" : 2, "id": 2,
"name" : "water plant", "name": "water plant",
"description": "water the plant, so that the soil is wet around the plant.", "description": "water the plant, so that the soil is wet around the plant.",
"startDate" : "2022-05-01", "startDate": "2022-05-01",
"endDate" : "2022-09-01", "endDate": "2022-09-01",
"interval" : 2 "interval": 2
}, },
{ {
"id" : 3, "id": 3,
"name" : "fertilize plant", "name": "fertilize plant",
"description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture", "description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture",
"startDate" : "2022-06-01", "startDate": "2022-06-01",
"endDate" : "2022-08-01", "endDate": "2022-08-01",
"interval" : 28 "interval": 28
}, },
{ {
"id" : 4, "id": 4,
"name" : "covering plant", "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", "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", "startDate": "2022-07-01",
"endDate" : "2022-07-01", "endDate": "2022-07-01",
"interval" : 0 "interval": 0
}, },
{ {
"id" : 5, "id": 5,
"name" : "look after plant", "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", "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", "startDate": "2022-05-01",
"endDate" : "2022-09-01", "endDate": "2022-09-01",
"interval" : 5 "interval": 5
}, },
{ {
"id" : 6, "id": 6,
"name" : "harvest plant", "name": "harvest plant",
"description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ",
"startDate" : "2022-09-01", "startDate": "2022-09-01",
"endDate" : "2022-09-01", "endDate": "2022-09-01",
"interval" : 0 "interval": 0
} }
] ]