diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java index aa06e65..b7c982a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -1,10 +1,13 @@ package ch.zhaw.gartenverwaltung.io; +import ch.zhaw.gartenverwaltung.json.PlantImageDeserializer; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer; +import javafx.scene.image.Image; import java.io.IOException; import java.net.URL; @@ -31,10 +34,14 @@ public class JsonPlantDatabase implements PlantDatabase { * Creating constant objects required to deserialize the {@link MonthDay} classes */ private final static JavaTimeModule timeModule = new JavaTimeModule(); + private final static SimpleModule imageModule = new SimpleModule(); + static { DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd"); MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat); timeModule.addDeserializer(MonthDay.class, dateDeserializer); + + imageModule.addDeserializer(Image.class, new PlantImageDeserializer()); } /** @@ -75,8 +82,9 @@ public class JsonPlantDatabase implements PlantDatabase { } if (dataSource != null) { currentZone = zone; - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(timeModule); + ObjectMapper mapper = new ObjectMapper() + .registerModule(timeModule) + .registerModule(imageModule); List result; result = mapper.readerForListOf(Plant.class).readValue(dataSource); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java index f3bb60f..285536a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java @@ -17,11 +17,12 @@ public class GrowthPhaseTypeDeserializer extends StdDeserializer { @Override public HardinessZone deserialize(JsonParser parser, DeserializationContext context) throws IOException { HardinessZone result = null; + String token = parser.getText(); try { - result = HardinessZone.valueOf(parser.getText().toUpperCase()); + result = HardinessZone.valueOf(token.toUpperCase()); } catch (IllegalArgumentException e) { // TODO: Log - System.err.println("bad growth phase type"); + System.err.printf("Unknown Hardiness Zone \"%s\"\n", token); } return result; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java new file mode 100644 index 0000000..e5611fc --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java @@ -0,0 +1,31 @@ +package ch.zhaw.gartenverwaltung.json; + +import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import javafx.scene.image.Image; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +public class PlantImageDeserializer extends JsonDeserializer { + + @Override + public Image deserialize(JsonParser parser, DeserializationContext context) throws IOException { + Image result = null; + URL imageUrl = PlantDatabase.class.getResource(String.format("images/%s", parser.getText())); + if (imageUrl != null) { + try (InputStream is = new FileInputStream(imageUrl.getFile())) { + result = new Image(is); + } catch (IllegalArgumentException e) { + // TODO: Log + e.printStackTrace(); + System.err.printf("Cannot find Image \"%s\"\n", imageUrl.getFile()); + } + } + return result; + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java index 13652a6..683983d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -1,5 +1,7 @@ package ch.zhaw.gartenverwaltung.types; +import javafx.scene.image.Image; + import java.time.LocalDate; import java.util.List; @@ -9,6 +11,7 @@ public record Plant( long id, String name, String description, + Image image, String spacing, int light, String soil, diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/images/ATTRIBUTION b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/ATTRIBUTION new file mode 100644 index 0000000..4fdeb79 --- /dev/null +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/ATTRIBUTION @@ -0,0 +1,5 @@ +Potato, Onion + Photos by Lars Blankers: https://unsplash.com/@lmablankers?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText + +Carrot + Photo by Maja Vujic: https://unsplash.com/@majavujic87?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText \ No newline at end of file diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/images/carrot.jpg b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/carrot.jpg new file mode 100644 index 0000000..41b3343 Binary files /dev/null and b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/carrot.jpg differ diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/images/onion.jpg b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/onion.jpg new file mode 100644 index 0000000..20c95e8 Binary files /dev/null and b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/onion.jpg differ diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/images/potato.jpg b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/potato.jpg new file mode 100644 index 0000000..b62660e Binary files /dev/null and b/src/main/resources/ch/zhaw/gartenverwaltung/io/images/potato.jpg differ diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index 927835d..5a23d09 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -6,6 +6,7 @@ "light": 6, "spacing": "35", "soil": "sandy", + "image": "potato.jpg", "pests": [ { "name": "Rot", @@ -86,6 +87,7 @@ "id": 1, "name": "Early Carrot", "description": "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", + "image": "carrot.jpg", "lifecycle": [ { "startDate": "02-20", @@ -167,6 +169,7 @@ "id": 2, "name": "Summertime Onion", "description": "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", + "image": "onion.jpg", "lifecycle": [ { "startDate": "03-15", diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java index b554502..a21b440 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java @@ -30,9 +30,7 @@ public class JsonPlantDatabaseTest { List testList; try { testList = testDatabase.getPlantList(HardinessZone.ZONE_8A); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { + } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); } Assertions.assertEquals(3, testList.size()); @@ -47,11 +45,8 @@ public class JsonPlantDatabaseTest { void getPlantById() { Optional testPlant; try { - testDatabase.getPlantList(HardinessZone.ZONE_8A); - testPlant = testDatabase.getPlantById(1); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,1); + } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); } Assertions.assertTrue(testPlant.isPresent()); @@ -63,11 +58,8 @@ public class JsonPlantDatabaseTest { void getPlantByIdMustFail() { Optional testPlant; try { - testDatabase.getPlantList(HardinessZone.ZONE_8A); - testPlant = testDatabase.getPlantById(99); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,99); + } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); } Assertions.assertFalse(testPlant.isPresent()); diff --git a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java index 0aecc35..82225b8 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java @@ -43,6 +43,7 @@ class PlantListModelTest { 20, "summertime onion", "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", + null, "15,30,2", 0, "sandy to loamy, loose soil, free of stones", @@ -53,6 +54,7 @@ class PlantListModelTest { 0, "Potato", "The potato is a tuber, round or oval, with small white roots called 'eyes', that are growth buds. The size varies depending on the variety; the colour of the skin can be white, yellow or even purple.", + null, "35", 6, "sandy", @@ -63,6 +65,7 @@ class PlantListModelTest { 1, "Early Carrot", "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", + null, "5,35,2.5", 0, "sandy to loamy, loose soil, free of stones",