#36 Added images to Plant database

This commit is contained in:
David Guler 2022-10-29 09:44:02 +02:00
parent f5fcfa78f6
commit bfbda8d753
12 changed files with 66 additions and 19 deletions

View File

@ -1,10 +1,13 @@
package ch.zhaw.gartenverwaltung.io; package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.json.PlantImageDeserializer;
import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Plant;
import com.fasterxml.jackson.databind.ObjectMapper; 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.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer;
import javafx.scene.image.Image;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
@ -31,10 +34,14 @@ public class JsonPlantDatabase implements PlantDatabase {
* Creating constant objects required to deserialize the {@link MonthDay} classes * Creating constant objects required to deserialize the {@link MonthDay} classes
*/ */
private final static JavaTimeModule timeModule = new JavaTimeModule(); private final static JavaTimeModule timeModule = new JavaTimeModule();
private final static SimpleModule imageModule = new SimpleModule();
static { static {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd"); DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd");
MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat); MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat);
timeModule.addDeserializer(MonthDay.class, dateDeserializer); timeModule.addDeserializer(MonthDay.class, dateDeserializer);
imageModule.addDeserializer(Image.class, new PlantImageDeserializer());
} }
/** /**
@ -75,8 +82,9 @@ public class JsonPlantDatabase implements PlantDatabase {
} }
if (dataSource != null) { if (dataSource != null) {
currentZone = zone; currentZone = zone;
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper()
mapper.registerModule(timeModule); .registerModule(timeModule)
.registerModule(imageModule);
List<Plant> result; List<Plant> result;
result = mapper.readerForListOf(Plant.class).readValue(dataSource); result = mapper.readerForListOf(Plant.class).readValue(dataSource);

View File

@ -17,11 +17,12 @@ public class GrowthPhaseTypeDeserializer extends StdDeserializer<GrowthPhaseType
@Override @Override
public GrowthPhaseType deserialize(JsonParser parser, DeserializationContext context) throws IOException { public GrowthPhaseType deserialize(JsonParser parser, DeserializationContext context) throws IOException {
GrowthPhaseType result = null; GrowthPhaseType result = null;
String token = parser.getText();
try { try {
result = GrowthPhaseType.valueOf(parser.getText().toUpperCase()); result = GrowthPhaseType.valueOf(token.toUpperCase());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// TODO: Log // TODO: Log
System.err.println("bad growth phase type"); System.err.printf("Bad growth phase type \"%s\"\n", token);
} }
return result; return result;
} }

View File

@ -18,11 +18,12 @@ public class HardinessZoneDeserializer extends StdDeserializer<HardinessZone> {
@Override @Override
public HardinessZone deserialize(JsonParser parser, DeserializationContext context) throws IOException { public HardinessZone deserialize(JsonParser parser, DeserializationContext context) throws IOException {
HardinessZone result = null; HardinessZone result = null;
String token = parser.getText();
try { try {
result = HardinessZone.valueOf(parser.getText().toUpperCase()); result = HardinessZone.valueOf(token.toUpperCase());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// TODO: Log // TODO: Log
System.err.println("bad growth phase type"); System.err.printf("Unknown Hardiness Zone \"%s\"\n", token);
} }
return result; return result;
} }

View File

@ -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<Image> {
@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;
}
}

View File

@ -1,5 +1,7 @@
package ch.zhaw.gartenverwaltung.types; package ch.zhaw.gartenverwaltung.types;
import javafx.scene.image.Image;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
@ -9,6 +11,7 @@ public record Plant(
long id, long id,
String name, String name,
String description, String description,
Image image,
String spacing, String spacing,
int light, int light,
String soil, String soil,

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

View File

@ -6,6 +6,7 @@
"light": 6, "light": 6,
"spacing": "35", "spacing": "35",
"soil": "sandy", "soil": "sandy",
"image": "potato.jpg",
"pests": [ "pests": [
{ {
"name": "Rot", "name": "Rot",
@ -86,6 +87,7 @@
"id": 1, "id": 1,
"name": "Early Carrot", "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.", "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": [ "lifecycle": [
{ {
"startDate": "02-20", "startDate": "02-20",
@ -167,6 +169,7 @@
"id": 2, "id": 2,
"name": "Summertime Onion", "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.", "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": [ "lifecycle": [
{ {
"startDate": "03-15", "startDate": "03-15",

View File

@ -30,9 +30,7 @@ public class JsonPlantDatabaseTest {
List<Plant> testList; List<Plant> testList;
try { try {
testList = testDatabase.getPlantList(HardinessZone.ZONE_8A); testList = testDatabase.getPlantList(HardinessZone.ZONE_8A);
} catch (IOException e) { } catch (IOException | HardinessZoneNotSetException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Assertions.assertEquals(3, testList.size()); Assertions.assertEquals(3, testList.size());
@ -47,11 +45,8 @@ public class JsonPlantDatabaseTest {
void getPlantById() { void getPlantById() {
Optional<Plant> testPlant; Optional<Plant> testPlant;
try { try {
testDatabase.getPlantList(HardinessZone.ZONE_8A); testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,1);
testPlant = testDatabase.getPlantById(1); } catch (IOException | HardinessZoneNotSetException e) {
} catch (IOException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Assertions.assertTrue(testPlant.isPresent()); Assertions.assertTrue(testPlant.isPresent());
@ -63,11 +58,8 @@ public class JsonPlantDatabaseTest {
void getPlantByIdMustFail() { void getPlantByIdMustFail() {
Optional<Plant> testPlant; Optional<Plant> testPlant;
try { try {
testDatabase.getPlantList(HardinessZone.ZONE_8A); testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,99);
testPlant = testDatabase.getPlantById(99); } catch (IOException | HardinessZoneNotSetException e) {
} catch (IOException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Assertions.assertFalse(testPlant.isPresent()); Assertions.assertFalse(testPlant.isPresent());

View File

@ -43,6 +43,7 @@ class PlantListModelTest {
20, 20,
"summertime onion", "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.", "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", "15,30,2",
0, 0,
"sandy to loamy, loose soil, free of stones", "sandy to loamy, loose soil, free of stones",
@ -53,6 +54,7 @@ class PlantListModelTest {
0, 0,
"Potato", "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.", "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", "35",
6, 6,
"sandy", "sandy",
@ -63,6 +65,7 @@ class PlantListModelTest {
1, 1,
"Early Carrot", "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.", "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", "5,35,2.5",
0, 0,
"sandy to loamy, loose soil, free of stones", "sandy to loamy, loose soil, free of stones",