json event conversion

This commit is contained in:
Jörg Prante 2024-02-06 16:00:36 +01:00
parent 757e62040b
commit 17f919393f

View file

@ -3,6 +3,7 @@ package org.xbib.event.common;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileTime; import java.nio.file.attribute.FileTime;
import java.time.Instant; import java.time.Instant;
@ -310,12 +311,36 @@ public final class EventManager extends AbstractEventManagerService implements E
return eventFromJson(Files.readString(file)); return eventFromJson(Files.readString(file));
} }
@SuppressWarnings("unchecked")
public static Event eventFromJson(String json) { public static Event eventFromJson(String json) {
Map<String, Object> map = Json.toMap(json); Map<String, Object> map = Json.toMap(json);
return eventBuilder() EventBuilder builder = eventBuilder();
.setType(map.getOrDefault("type", "generic").toString()) if (map.containsKey("type")) {
.setPayload(new Payload(map)) builder.setType(map.getOrDefault("type", "generic").toString());
.build(); }
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<String, Object>) 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 { 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("created", getCreated() != null ? getCreated().toString() : null);
builder.fieldIfNotNull("scheduled", getScheduledFor() != null ? getScheduledFor().toString() : null); builder.fieldIfNotNull("scheduled", getScheduledFor() != null ? getScheduledFor().toString() : null);
builder.fieldIfNotNull("path", getPath() != null ? getPath().toAbsolutePath().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(); builder.endMap();
return builder.build(); return builder.build();
} }
@ -451,10 +473,10 @@ public final class EventManager extends AbstractEventManagerService implements E
String message; String message;
Instant scheduled;
Instant created; Instant created;
Instant scheduled;
Payload payload; Payload payload;
Path path; Path path;
@ -495,6 +517,11 @@ public final class EventManager extends AbstractEventManagerService implements E
return this; return this;
} }
public EventBuilder setCreated(Instant created) {
this.created = created;
return this;
}
public EventBuilder setScheduledFor(Instant scheduled) { public EventBuilder setScheduledFor(Instant scheduled) {
this.scheduled = scheduled; this.scheduled = scheduled;
return this; return this;