json event conversion
This commit is contained in:
parent
757e62040b
commit
17f919393f
1 changed files with 36 additions and 9 deletions
|
@ -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<String, Object> 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<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 {
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue