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