#17 implementation JsonTaskDatabase.java, writeTasklistToFile
This commit is contained in:
parent
0ca381f8cc
commit
694da97cd6
|
@ -1,10 +1,10 @@
|
||||||
package ch.zhaw.gartenverwaltung.io;
|
package ch.zhaw.gartenverwaltung.io;
|
||||||
|
|
||||||
|
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.MonthDayDeserializer;
|
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
||||||
import javafx.util.converter.LocalDateStringConverter;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -13,34 +13,26 @@ import java.time.LocalDate;
|
||||||
import java.time.MonthDay;
|
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.Date;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Map;
|
||||||
|
|
||||||
public class JsonTaskDatabase implements TaskDatabase{
|
public class JsonTaskDatabase implements TaskDatabase{
|
||||||
private final URL dataSource = getClass().getResource("taskdb.json");
|
private final URL dataSource = getClass().getResource("taskdb.json");
|
||||||
|
|
||||||
public static void main(String[] args) {
|
private Map<Long, Task> taskMap = Collections.emptyMap();
|
||||||
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 final static JavaTimeModule timeModule = new JavaTimeModule();
|
private final static JavaTimeModule timeModule = new JavaTimeModule();
|
||||||
static {
|
static {
|
||||||
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd");
|
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM-dd");
|
||||||
MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat);
|
LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat);
|
||||||
timeModule.addDeserializer(MonthDay.class, dateDeserializer);
|
timeModule.addDeserializer(LocalDate.class, dateDeserializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Task> getTaskList(Date start, Date end) throws IOException{
|
public List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException{
|
||||||
List<Task> taskList = Collections.emptyList();
|
List<Task> taskList = Collections.emptyList();
|
||||||
|
|
||||||
if (dataSource != null) {
|
if (dataSource != null) {
|
||||||
|
@ -49,7 +41,6 @@ public class JsonTaskDatabase implements TaskDatabase{
|
||||||
|
|
||||||
taskList = mapper.readerForListOf(Task.class).readValue(dataSource);
|
taskList = mapper.readerForListOf(Task.class).readValue(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
return taskList;
|
return taskList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,8 +49,6 @@ public class JsonTaskDatabase implements TaskDatabase{
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
if(dataSource != null) {
|
if(dataSource != null) {
|
||||||
mapper.writeValue(new File(dataSource.getFile()), task);
|
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) {
|
if(dataSource != null) {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
taskList = mapper.readerForListOf(Task.class).readValue(dataSource);
|
taskList = mapper.readerForListOf(Task.class).readValue(dataSource);
|
||||||
|
|
||||||
for (int index = 0; index < taskList.size(); index++){
|
for (int index = 0; index < taskList.size(); index++){
|
||||||
if(task.equals(taskList.get(index))){
|
if(task.equals(taskList.get(index))){
|
||||||
taskList.remove(task);
|
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<Task> 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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,12 @@ package ch.zhaw.gartenverwaltung.io;
|
||||||
import ch.zhaw.gartenverwaltung.types.Task;
|
import ch.zhaw.gartenverwaltung.types.Task;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface TaskDatabase {
|
public interface TaskDatabase {
|
||||||
List<Task> getTaskList(Date start, Date end) throws IOException;
|
List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException;
|
||||||
void saveTask(Task task) throws IOException;
|
void saveTask(Task task) throws IOException;
|
||||||
void removeTask(Task task) throws IOException;
|
void removeTask(Task task) throws IOException;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue