From 5f53bb86c639e663aee6daba0f85a476da01d725 Mon Sep 17 00:00:00 2001 From: David Guler Date: Mon, 17 Oct 2022 17:01:50 +0200 Subject: [PATCH] #11 Very basic implementation with dummy data --- build.gradle | 3 +- .../gartenverwaltung/HelloApplication.java | 9 +++++ .../io/JsonPlantDatabase.java | 37 +++++++++++++++++++ .../gartenverwaltung/io/PlantDatabase.java | 5 ++- .../json/GrowthPhaseTypeDeserializer.java | 28 ++++++++++++++ .../json/HardinessZoneDeserializer.java | 29 +++++++++++++++ .../gartenverwaltung/types/GrowthPhase.java | 14 +++++-- .../ch/zhaw/gartenverwaltung/types/Plant.java | 5 +++ src/main/java/module-info.java | 5 +++ .../ch/zhaw/gartenverwaltung/io/plantdb.json | 31 ++++++++++++++++ 10 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java create mode 100644 src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json diff --git a/build.gradle b/build.gradle index 5268664..41f89ac 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'org.beryx.jlink' version '2.25.0' } -group 'ch.zhaw.pm3' +group 'ch.zhaw.gartenverwaltung' version '1.0-SNAPSHOT' repositories { @@ -37,6 +37,7 @@ 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' } test { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java index 0d70b44..7877221 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/HelloApplication.java @@ -1,5 +1,8 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; +import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.types.HardinessZone; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -18,6 +21,12 @@ public class HelloApplication extends Application { } public static void main(String[] args) { + PlantDatabase db = new JsonPlantDatabase(); + try { + System.out.println(db.getPlantList(HardinessZone.ZONE_8A)); + } catch (IOException e) { + e.printStackTrace(); + } launch(); } } \ No newline at end of file diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java new file mode 100644 index 0000000..c889195 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -0,0 +1,37 @@ +package ch.zhaw.gartenverwaltung.io; + +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.net.URL; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public class JsonPlantDatabase implements PlantDatabase { + private final URL dataSource = getClass().getResource("plantdb.json"); + + @Override + public List getPlantList(HardinessZone zone) throws IOException { + List result = Collections.emptyList(); + + if (dataSource != null) { + ObjectMapper mapper = new ObjectMapper(); + DateFormat dateFormat = new SimpleDateFormat("MM-dd"); + mapper.setDateFormat(dateFormat); + + result = mapper.readerForListOf(Plant.class).readValue(dataSource); + } + + return result; + } + + @Override + public Optional getPlantById(long id) { + return Optional.empty(); + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java index f0d9bf9..2a38e3b 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java @@ -3,10 +3,11 @@ package ch.zhaw.gartenverwaltung.io; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.HardinessZone; +import java.io.IOException; import java.util.List; import java.util.Optional; public interface PlantDatabase { - List getPlantList(HardinessZone zone); - Optional getPlantById(long id); + List getPlantList(HardinessZone zone) throws IOException; + Optional getPlantById(long id) throws IOException; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java new file mode 100644 index 0000000..f3bb60f --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java @@ -0,0 +1,28 @@ +package ch.zhaw.gartenverwaltung.json; + +import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +import java.io.IOException; + +public class GrowthPhaseTypeDeserializer extends StdDeserializer { + public GrowthPhaseTypeDeserializer(Class vc) { + super(vc); + } + + public GrowthPhaseTypeDeserializer() { this(null); } + + @Override + public GrowthPhaseType deserialize(JsonParser parser, DeserializationContext context) throws IOException { + GrowthPhaseType result = null; + try { + result = GrowthPhaseType.valueOf(parser.getText().toUpperCase()); + } catch (IllegalArgumentException e) { + // TODO: Log + System.err.println("bad growth phase type"); + } + return result; + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java new file mode 100644 index 0000000..cf89d6a --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java @@ -0,0 +1,29 @@ +package ch.zhaw.gartenverwaltung.json; + +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +import java.io.IOException; + +public class HardinessZoneDeserializer extends StdDeserializer { + public HardinessZoneDeserializer(Class vc) { + super(vc); + } + public HardinessZoneDeserializer() { + this(null); + } + + @Override + public HardinessZone deserialize(JsonParser parser, DeserializationContext context) throws IOException { + HardinessZone result = null; + try { + result = HardinessZone.valueOf(parser.getText().toUpperCase()); + } catch (IllegalArgumentException e) { + // TODO: Log + System.err.println("bad growth phase type"); + } + return result; + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java b/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java index 665b895..da33b2a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/GrowthPhase.java @@ -1,9 +1,15 @@ package ch.zhaw.gartenverwaltung.types; +import ch.zhaw.gartenverwaltung.json.GrowthPhaseTypeDeserializer; +import ch.zhaw.gartenverwaltung.json.HardinessZoneDeserializer; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + import java.util.Date; -public record GrowthPhase(Date startDate, - Date endDate, - GrowthPhaseType type, - HardinessZone zone) { + +public record GrowthPhase( + Date startDate, + Date endDate, + @JsonDeserialize(using = GrowthPhaseTypeDeserializer.class) GrowthPhaseType type, + @JsonDeserialize(using = HardinessZoneDeserializer.class) HardinessZone zone) { } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java index 99336e7..612679c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -7,5 +7,10 @@ public record Plant( String name, String description, int spacing, + Object water, + int light, + List maintenance, + List specialTasks, + List pests, List lifecycle) { } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 4bffe36..2bb0935 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,8 +1,13 @@ module ch.zhaw.gartenverwaltung { requires javafx.controls; requires javafx.fxml; + requires com.fasterxml.jackson.databind; opens ch.zhaw.gartenverwaltung to javafx.fxml; + opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind; +// opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind; exports ch.zhaw.gartenverwaltung; + exports ch.zhaw.gartenverwaltung.types; + exports ch.zhaw.gartenverwaltung.json; } \ No newline at end of file diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json new file mode 100644 index 0000000..7eb3366 --- /dev/null +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -0,0 +1,31 @@ +[ + { + "name": "Potato", + "description": "Tasty tubers.", + "water": { + "amount": "27-55", + "interval": 7 + }, + "light": 6, + "maintenance": [], + "specialTasks": [], + "lifecycle": [ + { + "startDate": "04-01", + "endDate": "05-31", + "type": "SOW", + "zone": "ZONE_8A" + }, + { + "startDate": "07-01", + "endDate": "08-31", + "type": "HARVEST", + "zone": "ZONE_8A" + } + ], + "spacing": 35, + "pests": [ + "Potato beetle" + ] + } +] \ No newline at end of file