Compare commits

..

No commits in common. "8fd57d91f249cce56244fdd679b56a46be17e958" and "af0e73c007df88d03aec9b80443f1b6965e91e1e" have entirely different histories.

4 changed files with 61 additions and 75 deletions

View File

@ -1,11 +0,0 @@
package ch.zhaw.gartenverwaltung.io;
public class IdProvider {
private long currentId;
public IdProvider(long initialValue) {
currentId = initialValue;
}
public long incrementAndGet() {
return ++currentId;
}
}

View File

@ -1,17 +1,16 @@
package ch.zhaw.gartenverwaltung.io; package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Task; import ch.zhaw.gartenverwaltung.types.Task;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.YearMonth; import java.time.MonthDay;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -23,20 +22,26 @@ public class JsonTaskDatabase implements TaskDatabase{
private Map<Long, Task> taskMap = Collections.emptyMap(); private Map<Long, Task> taskMap = Collections.emptyMap();
private final static JavaTimeModule timeModule = new JavaTimeModule(); private final static JavaTimeModule timeModule = new JavaTimeModule();
static { static {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM"); DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM-dd");
YearMonthDeserializer dateDeserializer = new YearMonthDeserializer(dateFormat); LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat);
timeModule.addDeserializer(YearMonth.class, dateDeserializer); timeModule.addDeserializer(LocalDate.class, dateDeserializer);
} }
@Override @Override
public List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException{ public List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException{
List<Task> taskList = Collections.emptyList();
if(taskMap.isEmpty()) { if (dataSource != null) {
loadTaskListFromFile(); ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule);
taskList = mapper.readerForListOf(Task.class).readValue(dataSource);
} }
return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList(); return taskList;
} }
@Override @Override
@ -49,8 +54,18 @@ public class JsonTaskDatabase implements TaskDatabase{
@Override @Override
public void removeTask(Task task) throws IOException { public void removeTask(Task task) throws IOException {
if(!taskMap.isEmpty() && taskMap.containsKey(task.getId())){ List<Task> taskList;
taskMap.remove(task.getId());
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;
}
}
} }
} }
@ -61,39 +76,29 @@ public class JsonTaskDatabase implements TaskDatabase{
mapper.writeValue(new File(dataSource.getFile()), taskMap); mapper.writeValue(new File(dataSource.getFile()), taskMap);
} }
private void loadTaskListFromFile() throws IOException { private void loadTaskListFromFile() throws IOException {
if (dataSource != null) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule);
List<Task> result; ObjectMapper mapper = new ObjectMapper();
result = mapper.readerForListOf(Task.class).readValue(dataSource); mapper.registerModule(timeModule);
taskMap = result.stream() List<Task> result;
.collect(HashMap::new, result = mapper.readerForListOf(Task.class).readValue(dataSource);
(res, task) -> res.put(task.getId(), task),
(existing, replacement) -> {}); //taskMap = result.stream().collect(HashMap::new,
System.out.println(taskMap);
}
} }
//Test main method
public static void main(String[] args) { public static void main(String[] args) {
LocalDate date = LocalDate.now(); LocalDate date = LocalDate.now();
LocalDate yesterday = LocalDate.now(); LocalDate yesterday = LocalDate.now();
yesterday = yesterday.minusDays(1); yesterday.minusDays(1);
Task testTask = new Task("water", "apply water", date); Task testTask = new Task("water", "apply water", date);
JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase(); JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase();
JsonPlantDatabase jsonPlantDatabase = new JsonPlantDatabase();
try { try {
//test load file jsonTaskDatabase.saveTask(testTask);
jsonPlantDatabase.getPlantList(HardinessZone.ZONE_8A); System.out.println(jsonTaskDatabase.getTaskList(yesterday,date));
jsonTaskDatabase.loadTaskListFromFile();
} catch (Exception e){ } catch (Exception e){
System.out.println("Task load failed!: " + e.getMessage()); System.out.println("Task saving failed!");
} }
} }
} }

View File

@ -21,7 +21,6 @@ public class Task {
this.description = description; this.description = description;
this.startDate = startDate; this.startDate = startDate;
} }
public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) { public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) {
this.name = name; this.name = name;
this.description = description; this.description = description;
@ -44,13 +43,6 @@ public class Task {
return this; return this;
} }
public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){
if(startDate.isAfter(searchStartDate) &&startDate.isBefore(searchEndDate)){
return true;
}
return false;
}
// Getters // Getters
public long getId() { return id; } public long getId() { return id; }
public String getName() { return name; } public String getName() { return name; }

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" : "22-05", "startDate" : "01.05.2022",
"endDate" : "22-05", "endDate" : "01.05.2022",
"interval" : 0 "interval" : "null"
}, },
{ {
"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" : "22-05", "startDate" : "01.05.2022",
"endDate" : "22-09", "endDate" : "01.09.2022",
"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 plant's soil with the mixture", "description": "The fertilizer has to be mixed with water. Then fertilize the plant's soil with the mixture",
"startDate" : "22-06", "startDate" : "01.06.2022",
"endDate" : "22-08", "endDate" : "01.08.2022",
"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" : "22-07", "startDate" : "15.07.2022",
"endDate" : "22-07", "endDate" : "15.07.2022",
"interval" : 0 "interval" : "null"
}, },
{ {
"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" : "22-05", "startDate" : "01.05.2022",
"endDate" : "22-09", "endDate" : "01.09.2022",
"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" : "22-09", "startDate" : "01.09.2022",
"endDate" : "22-09", "endDate" : "01.09.2022",
"interval" : 0 "interval" : "null"
} }
] ]