From e228b9019db52bfa40dfbae80e9fc8a7642b3a8c Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Sat, 22 Oct 2022 11:52:06 +0200 Subject: [PATCH] #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;