Compare commits
No commits in common. "246fed7826815d911a94ecfe821ba9ff40128c22" and "5b860e0ff426ebfcb4c6803b2519d0883d1536da" have entirely different histories.
246fed7826
...
5b860e0ff4
|
@ -1,13 +1,10 @@
|
||||||
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;
|
||||||
|
@ -34,14 +31,10 @@ 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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,9 +75,8 @@ public class JsonPlantDatabase implements PlantDatabase {
|
||||||
}
|
}
|
||||||
if (dataSource != null) {
|
if (dataSource != null) {
|
||||||
currentZone = zone;
|
currentZone = zone;
|
||||||
ObjectMapper mapper = new ObjectMapper()
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
.registerModule(timeModule)
|
mapper.registerModule(timeModule);
|
||||||
.registerModule(imageModule);
|
|
||||||
|
|
||||||
List<Plant> result;
|
List<Plant> result;
|
||||||
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
|
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
|
||||||
|
|
|
@ -17,12 +17,11 @@ 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(token.toUpperCase());
|
result = GrowthPhaseType.valueOf(parser.getText().toUpperCase());
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// TODO: Log
|
// TODO: Log
|
||||||
System.err.printf("Bad growth phase type \"%s\"\n", token);
|
System.err.println("bad growth phase type");
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,11 @@ 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(token.toUpperCase());
|
result = HardinessZone.valueOf(parser.getText().toUpperCase());
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// TODO: Log
|
// TODO: Log
|
||||||
System.err.printf("Unknown Hardiness Zone \"%s\"\n", token);
|
System.err.println("bad growth phase type");
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +1,5 @@
|
||||||
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;
|
||||||
|
|
||||||
|
@ -11,7 +9,6 @@ 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,
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
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.
Before Width: | Height: | Size: 3.4 MiB |
Binary file not shown.
Before Width: | Height: | Size: 56 KiB |
Binary file not shown.
Before Width: | Height: | Size: 50 KiB |
|
@ -6,7 +6,6 @@
|
||||||
"light": 6,
|
"light": 6,
|
||||||
"spacing": "35",
|
"spacing": "35",
|
||||||
"soil": "sandy",
|
"soil": "sandy",
|
||||||
"image": "potato.jpg",
|
|
||||||
"pests": [
|
"pests": [
|
||||||
{
|
{
|
||||||
"name": "Rot",
|
"name": "Rot",
|
||||||
|
@ -87,7 +86,6 @@
|
||||||
"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",
|
||||||
|
@ -169,7 +167,6 @@
|
||||||
"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",
|
||||||
|
|
|
@ -30,7 +30,9 @@ 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 | HardinessZoneNotSetException e) {
|
} catch (IOException 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());
|
||||||
|
@ -45,8 +47,11 @@ public class JsonPlantDatabaseTest {
|
||||||
void getPlantById() {
|
void getPlantById() {
|
||||||
Optional<Plant> testPlant;
|
Optional<Plant> testPlant;
|
||||||
try {
|
try {
|
||||||
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,1);
|
testDatabase.getPlantList(HardinessZone.ZONE_8A);
|
||||||
} catch (IOException | HardinessZoneNotSetException e) {
|
testPlant = testDatabase.getPlantById(1);
|
||||||
|
} 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());
|
||||||
|
@ -58,8 +63,11 @@ public class JsonPlantDatabaseTest {
|
||||||
void getPlantByIdMustFail() {
|
void getPlantByIdMustFail() {
|
||||||
Optional<Plant> testPlant;
|
Optional<Plant> testPlant;
|
||||||
try {
|
try {
|
||||||
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,99);
|
testDatabase.getPlantList(HardinessZone.ZONE_8A);
|
||||||
} catch (IOException | HardinessZoneNotSetException e) {
|
testPlant = testDatabase.getPlantById(99);
|
||||||
|
} 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());
|
||||||
|
|
|
@ -43,7 +43,6 @@ 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",
|
||||||
|
@ -54,7 +53,6 @@ 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",
|
||||||
|
@ -65,7 +63,6 @@ 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",
|
||||||
|
|
Loading…
Reference in New Issue