Merge pull request #39 from schrom01/feature_json-plant-db_M2

#36 Added images to Plant database
This commit is contained in:
Roman Schenk
2022-10-29 11:36:14 +02:00
committed by GitHub Enterprise
12 changed files with 66 additions and 19 deletions
@@ -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<Plant> result;
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
@@ -17,11 +17,12 @@ public class GrowthPhaseTypeDeserializer extends StdDeserializer<GrowthPhaseType
@Override
public GrowthPhaseType deserialize(JsonParser parser, DeserializationContext context) throws IOException {
GrowthPhaseType result = null;
String token = parser.getText();
try {
result = GrowthPhaseType.valueOf(parser.getText().toUpperCase());
result = GrowthPhaseType.valueOf(token.toUpperCase());
} catch (IllegalArgumentException e) {
// TODO: Log
System.err.println("bad growth phase type");
System.err.printf("Bad growth phase type \"%s\"\n", token);
}
return result;
}
@@ -18,11 +18,12 @@ public class HardinessZoneDeserializer extends StdDeserializer<HardinessZone> {
@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;
}
@@ -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;
}
}
@@ -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,