Minimum viable deserialization
Added TaskTemplate type Added dummy data with (probably) usable format Used the java.time classes instead of the legacy util.Date
This commit is contained in:
@@ -3,11 +3,13 @@ package ch.zhaw.gartenverwaltung.io;
|
||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.MonthDay;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -15,14 +17,20 @@ import java.util.Optional;
|
||||
public class JsonPlantDatabase implements PlantDatabase {
|
||||
private final URL dataSource = getClass().getResource("plantdb.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<Plant> getPlantList(HardinessZone zone) throws IOException {
|
||||
List<Plant> result = Collections.emptyList();
|
||||
|
||||
if (dataSource != null) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
DateFormat dateFormat = new SimpleDateFormat("MM-dd");
|
||||
mapper.setDateFormat(dateFormat);
|
||||
mapper.registerModule(timeModule);
|
||||
|
||||
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,16 @@ import ch.zhaw.gartenverwaltung.json.GrowthPhaseTypeDeserializer;
|
||||
import ch.zhaw.gartenverwaltung.json.HardinessZoneDeserializer;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
import java.util.Date;
|
||||
import java.time.MonthDay;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public record GrowthPhase(
|
||||
Date startDate,
|
||||
Date endDate,
|
||||
MonthDay startDate,
|
||||
MonthDay endDate,
|
||||
int group,
|
||||
Object wateringCycle,
|
||||
@JsonDeserialize(using = GrowthPhaseTypeDeserializer.class) GrowthPhaseType type,
|
||||
@JsonDeserialize(using = HardinessZoneDeserializer.class) HardinessZone zone) {
|
||||
@JsonDeserialize(using = HardinessZoneDeserializer.class) HardinessZone zone,
|
||||
List<TaskTemplate> taskTemplates) {
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package ch.zhaw.gartenverwaltung.types;
|
||||
|
||||
public enum GrowthPhaseType {
|
||||
SOW, PLANT, HARVEST
|
||||
SOW, PLANT, REPLANT, HARVEST
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@ public record Plant(
|
||||
String name,
|
||||
String description,
|
||||
int spacing,
|
||||
Object water,
|
||||
int light,
|
||||
List<String> maintenance,
|
||||
List<String> specialTasks,
|
||||
List<String> pests,
|
||||
String soil,
|
||||
List<Object> pests,
|
||||
List<GrowthPhase> lifecycle) {
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ch.zhaw.gartenverwaltung.types;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -11,14 +12,21 @@ public class Task {
|
||||
private long id;
|
||||
private final String name;
|
||||
private final String description;
|
||||
private final Date startDate;
|
||||
private final LocalDate startDate;
|
||||
private Integer interval;
|
||||
private Date endDate;
|
||||
private LocalDate endDate;
|
||||
|
||||
public Task(String name, String description, Date startDate) {
|
||||
public Task(String name, String description, LocalDate startDate) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.startDate = endDate;
|
||||
this.startDate = startDate;
|
||||
}
|
||||
public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.startDate = startDate;
|
||||
this.endDate = endDate;
|
||||
this.interval = interval;
|
||||
}
|
||||
|
||||
// Builder-pattern-style setters
|
||||
@@ -30,7 +38,7 @@ public class Task {
|
||||
this.interval = interval;
|
||||
return this;
|
||||
}
|
||||
public Task withEndDate(Date endDate) {
|
||||
public Task withEndDate(LocalDate endDate) {
|
||||
this.endDate = endDate;
|
||||
return this;
|
||||
}
|
||||
@@ -39,12 +47,12 @@ public class Task {
|
||||
public long getId() { return id; }
|
||||
public String getName() { return name; }
|
||||
public String getDescription() { return description; }
|
||||
public Date getStartDate() { return startDate; }
|
||||
public LocalDate getStartDate() { return startDate; }
|
||||
|
||||
public Optional<Integer> getInterval() {
|
||||
return Optional.ofNullable(interval);
|
||||
}
|
||||
public Optional<Date> getEndDate() {
|
||||
public Optional<LocalDate> getEndDate() {
|
||||
return Optional.ofNullable(endDate);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package ch.zhaw.gartenverwaltung.types;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class TaskTemplate {
|
||||
@JsonProperty
|
||||
private final String name;
|
||||
@JsonProperty
|
||||
private final String description;
|
||||
@JsonProperty
|
||||
private final int relativeStartDate;
|
||||
@JsonProperty
|
||||
private Integer relativeEndDate;
|
||||
@JsonProperty
|
||||
private Integer interval;
|
||||
|
||||
// TODO: reconsider if we need this
|
||||
@JsonProperty
|
||||
private boolean isOptional = false;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
* (Used by deserializer)
|
||||
*/
|
||||
public TaskTemplate() {
|
||||
this.name = "";
|
||||
this.description = "";
|
||||
this.relativeStartDate = 0;
|
||||
}
|
||||
|
||||
// Setters
|
||||
public void setRelativeEndDate(Integer relativeEndDate) {
|
||||
this.relativeEndDate = relativeEndDate;
|
||||
}
|
||||
public void setInterval(Integer interval) {
|
||||
this.interval = interval;
|
||||
}
|
||||
|
||||
public TaskTemplate(String name, String description, int relativeStartDate) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.relativeStartDate = relativeStartDate;
|
||||
}
|
||||
|
||||
public Task generateTask(LocalDate realStartDate) {
|
||||
Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate));
|
||||
if (relativeEndDate != null) {
|
||||
task.withEndDate(realStartDate.plusDays(relativeEndDate));
|
||||
}
|
||||
if (interval != null) {
|
||||
task.withInterval(interval);
|
||||
}
|
||||
return task;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ 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;
|
||||
opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind;
|
||||
|
||||
Reference in New Issue
Block a user