diff --git a/src/main/java/world/bentobox/bentobox/api/logs/LogEntry.java b/src/main/java/world/bentobox/bentobox/api/logs/LogEntry.java new file mode 100644 index 000000000..0cb4fd9f9 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/logs/LogEntry.java @@ -0,0 +1,41 @@ +package world.bentobox.bentobox.api.logs; + +import java.util.Map; + +/** + * Represents an event that occurred and that is logged. + *
+ * An {@link world.bentobox.bentobox.database.objects.adapters.AdapterInterface} is provided to be able to save/retrieve + * a list of instances of this object to/from the database: {@link world.bentobox.bentobox.database.objects.adapters.LogEntryListAdapter}. + * + * @author Poslovitch + */ +public class LogEntry { + private long timestamp; + private String type; + private Map data; + + public LogEntry(String type, Map data) { + this.timestamp = System.currentTimeMillis(); + this.type = type; + this.data = data; + } + + public LogEntry(long timestamp, String type, Map data) { + this.timestamp = timestamp; + this.type = type; + this.data = data; + } + + public long getTimestamp() { + return timestamp; + } + + public String getType() { + return type; + } + + public Map getData() { + return data; + } +} diff --git a/src/main/java/world/bentobox/bentobox/database/objects/adapters/LogEntryListAdapter.java b/src/main/java/world/bentobox/bentobox/database/objects/adapters/LogEntryListAdapter.java new file mode 100644 index 000000000..a08087dc3 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/database/objects/adapters/LogEntryListAdapter.java @@ -0,0 +1,73 @@ +package world.bentobox.bentobox.database.objects.adapters; + +import world.bentobox.bentobox.api.logs.LogEntry; + +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * @author Poslovitch + */ +/* The serialization might look a bit weird here, as I'm using JSON's array of object. + This means that, once serialized, the data will look like this (on YAML): + history: + - timestamp: 0 + type: "test" + data: + player: "uuid" + action: "ISLAND_LEVEL_UPDATED" + value: 45 + - timestamp: 4181 + type: "lol" + data: + help: "yep" + */ +public class LogEntryListAdapter implements AdapterInterface, List>> { + + private static final String TIMESTAMP = "timestamp"; + private static final String TYPE = "type"; + private static final String DATA = "data"; + + @SuppressWarnings("unchecked") + @Override + public List deserialize(Object object) { + List result = new LinkedList<>(); + if (object == null) { + return result; + } + + List> serialized = (List>) object; + for (Map entry : serialized) { + long timestamp = (long) entry.get(TIMESTAMP); + String type = (String) entry.get(TYPE); + Map data = (Map) entry.get(DATA); + + result.add(new LogEntry(timestamp, type, data)); + } + + return result; + } + + @SuppressWarnings("unchecked") + @Override + public List> serialize(Object object) { + List> result = new LinkedList<>(); + if (object == null) { + return result; + } + + List history = (List) object; + history.forEach(logEntry -> { + Map value = new LinkedHashMap<>(); + value.put(TIMESTAMP, logEntry.getTimestamp()); + value.put(TYPE, logEntry.getType()); + value.put(DATA, logEntry.getData()); + + result.add(value); + }); + + return result; + } +}