From 694da97cd673f339784353672cce9c70cf3f88d0 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Mon, 24 Oct 2022 13:54:28 +0200 Subject: [PATCH] #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