#17 implementation JsonTaskDatabase.java, gradle import for jackson

This commit is contained in:
Gian-Andrea Hutter 2022-10-22 11:52:06 +02:00
parent a2008450e6
commit e228b9019d
4 changed files with 72 additions and 1 deletions

View File

@ -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 {

View File

@ -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<Task> getTaskList(Date start, Date end) throws IOException{
List<Task> 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<Task> 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;
}
}
}
}
}

View File

@ -7,7 +7,7 @@ import java.util.Date;
import java.util.List;
public interface TaskDatabase {
List<Task> getTaskList(Date start, Date end);
List<Task> getTaskList(Date start, Date end) throws IOException;
void saveTask(Task task) throws IOException;
void removeTask(Task task) throws IOException;
}

View File

@ -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;