From 17f919393fc02e90fbb341d97a862fe825e82b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Tue, 6 Feb 2024 16:00:36 +0100 Subject: [PATCH] json event conversion --- .../org/xbib/event/common/EventManager.java | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/event-common/src/main/java/org/xbib/event/common/EventManager.java b/event-common/src/main/java/org/xbib/event/common/EventManager.java index 81e6db7..ae8b556 100644 --- a/event-common/src/main/java/org/xbib/event/common/EventManager.java +++ b/event-common/src/main/java/org/xbib/event/common/EventManager.java @@ -3,6 +3,7 @@ package org.xbib.event.common; import java.lang.reflect.InvocationTargetException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.FileTime; import java.time.Instant; @@ -310,12 +311,36 @@ public final class EventManager extends AbstractEventManagerService implements E return eventFromJson(Files.readString(file)); } + @SuppressWarnings("unchecked") public static Event eventFromJson(String json) { Map map = Json.toMap(json); - return eventBuilder() - .setType(map.getOrDefault("type", "generic").toString()) - .setPayload(new Payload(map)) - .build(); + EventBuilder builder = eventBuilder(); + if (map.containsKey("type")) { + builder.setType(map.getOrDefault("type", "generic").toString()); + } + if (map.containsKey("code")) { + builder.setCode(map.getOrDefault("code", "").toString()); + } + if (map.containsKey("message")) { + builder.setMessage(map.getOrDefault("message", "").toString()); + } + if (map.containsKey("created")) { + String created = map.getOrDefault("created", "").toString(); + builder.setCreated(Instant.parse(created)); + } + if (map.containsKey("scheduled")) { + String scheduled = map.getOrDefault("scheduled", "").toString(); + builder.setCreated(Instant.parse(scheduled)); + } + if (map.containsKey("payload")) { + Payload payload = new Payload((Map) map.get("payload")); + builder.setPayload(payload); + } + if (map.containsKey("path")) { + Path path = Paths.get((String) map.get("path")); + builder.setPath(path); + } + return builder.build(); } public static class EventImpl implements Event { @@ -391,9 +416,6 @@ public final class EventManager extends AbstractEventManagerService implements E builder.fieldIfNotNull("created", getCreated() != null ? getCreated().toString() : null); builder.fieldIfNotNull("scheduled", getScheduledFor() != null ? getScheduledFor().toString() : null); builder.fieldIfNotNull("path", getPath() != null ? getPath().toAbsolutePath().toString() : null); - builder.fieldIfNotNull("base", getBase()); - builder.fieldIfNotNull("suffix", getSuffix()); - builder.fieldIfNotNull("filesize", getFileSize()); builder.endMap(); return builder.build(); } @@ -451,10 +473,10 @@ public final class EventManager extends AbstractEventManagerService implements E String message; - Instant scheduled; - Instant created; + Instant scheduled; + Payload payload; Path path; @@ -495,6 +517,11 @@ public final class EventManager extends AbstractEventManagerService implements E return this; } + public EventBuilder setCreated(Instant created) { + this.created = created; + return this; + } + public EventBuilder setScheduledFor(Instant scheduled) { this.scheduled = scheduled; return this;