diff options
Diffstat (limited to 'src/main/java/org/traccar/broadcast')
4 files changed, 149 insertions, 31 deletions
diff --git a/src/main/java/org/traccar/broadcast/BaseBroadcastService.java b/src/main/java/org/traccar/broadcast/BaseBroadcastService.java index a95d333f2..bb220d2bb 100644 --- a/src/main/java/org/traccar/broadcast/BaseBroadcastService.java +++ b/src/main/java/org/traccar/broadcast/BaseBroadcastService.java @@ -16,7 +16,6 @@ package org.traccar.broadcast; import java.util.HashSet; -import java.util.Map; import java.util.Set; import org.traccar.model.BaseModel; @@ -69,9 +68,16 @@ public abstract class BaseBroadcastService implements BroadcastService { } @Override - public void invalidateObject(boolean local, Class<? extends BaseModel> clazz, long id) { + public void invalidateObject( + boolean local, + Class<? extends BaseModel> clazz, long id, + ObjectOperation operation) { BroadcastMessage message = new BroadcastMessage(); - message.setChanges(Map.of(Permission.getKey(clazz), id)); + var invalidateObject = new BroadcastMessage.InvalidateObject(); + invalidateObject.setClazz(Permission.getKey(clazz)); + invalidateObject.setId(id); + invalidateObject.setOperation(operation); + message.setInvalidateObject(invalidateObject); sendMessage(message); } @@ -79,9 +85,16 @@ public abstract class BaseBroadcastService implements BroadcastService { public void invalidatePermission( boolean local, Class<? extends BaseModel> clazz1, long id1, - Class<? extends BaseModel> clazz2, long id2) { + Class<? extends BaseModel> clazz2, long id2, + boolean link) { BroadcastMessage message = new BroadcastMessage(); - message.setChanges(Map.of(Permission.getKey(clazz1), id1, Permission.getKey(clazz2), id2)); + var invalidatePermission = new BroadcastMessage.InvalidatePermission(); + invalidatePermission.setClazz1(Permission.getKey(clazz1)); + invalidatePermission.setId1(id1); + invalidatePermission.setClazz2(Permission.getKey(clazz2)); + invalidatePermission.setId2(id2); + invalidatePermission.setLink(link); + message.setInvalidatePermission(invalidatePermission); sendMessage(message); } @@ -96,22 +109,19 @@ public abstract class BaseBroadcastService implements BroadcastService { listeners.forEach(listener -> listener.updateEvent(false, message.getUserId(), message.getEvent())); } else if (message.getCommandDeviceId() != null) { listeners.forEach(listener -> listener.updateCommand(false, message.getCommandDeviceId())); - } else if (message.getChanges() != null) { - var iterator = message.getChanges().entrySet().iterator(); - if (iterator.hasNext()) { - var first = iterator.next(); - if (iterator.hasNext()) { - var second = iterator.next(); - listeners.forEach(listener -> listener.invalidatePermission( - false, - Permission.getKeyClass(first.getKey()), first.getValue(), - Permission.getKeyClass(second.getKey()), second.getValue())); - } else { - listeners.forEach(listener -> listener.invalidateObject( - false, - Permission.getKeyClass(first.getKey()), first.getValue())); - } - } + } else if (message.getInvalidateObject() != null) { + var invalidateObject = message.getInvalidateObject(); + listeners.forEach(listeners -> listeners.invalidateObject( + false, + Permission.getKeyClass(invalidateObject.getClazz()), invalidateObject.getId(), + invalidateObject.getOperation())); + } else if (message.getInvalidatePermission() != null) { + var invalidatePermission = message.getInvalidatePermission(); + listeners.forEach(listener -> listener.invalidatePermission( + false, + Permission.getKeyClass(invalidatePermission.getClazz1()), invalidatePermission.getId1(), + Permission.getKeyClass(invalidatePermission.getClazz2()), invalidatePermission.getId2(), + invalidatePermission.getLink())); } } diff --git a/src/main/java/org/traccar/broadcast/BroadcastInterface.java b/src/main/java/org/traccar/broadcast/BroadcastInterface.java index 673ebd8b8..ededbaa1a 100644 --- a/src/main/java/org/traccar/broadcast/BroadcastInterface.java +++ b/src/main/java/org/traccar/broadcast/BroadcastInterface.java @@ -34,12 +34,16 @@ public interface BroadcastInterface { default void updateCommand(boolean local, long deviceId) { } - default void invalidateObject(boolean local, Class<? extends BaseModel> clazz, long id) { + default void invalidateObject( + boolean local, + Class<? extends BaseModel> clazz, long id, + ObjectOperation operation) { } default void invalidatePermission( boolean local, Class<? extends BaseModel> clazz1, long id1, - Class<? extends BaseModel> clazz2, long id2) { + Class<? extends BaseModel> clazz2, long id2, + boolean link) { } } diff --git a/src/main/java/org/traccar/broadcast/BroadcastMessage.java b/src/main/java/org/traccar/broadcast/BroadcastMessage.java index 985848d04..6fe2d8b35 100644 --- a/src/main/java/org/traccar/broadcast/BroadcastMessage.java +++ b/src/main/java/org/traccar/broadcast/BroadcastMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,6 @@ import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; -import java.util.Map; - public class BroadcastMessage { private Device device; @@ -73,13 +71,112 @@ public class BroadcastMessage { this.commandDeviceId = commandDeviceId; } - private Map<String, Long> changes; + public static class InvalidateObject { + + private String clazz; + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + private long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + private ObjectOperation operation; + + public ObjectOperation getOperation() { + return operation; + } + + public void setOperation(ObjectOperation operation) { + this.operation = operation; + } - public Map<String, Long> getChanges() { - return changes; } - public void setChanges(Map<String, Long> changes) { - this.changes = changes; + private InvalidateObject invalidateObject; + + public InvalidateObject getInvalidateObject() { + return invalidateObject; + } + + public void setInvalidateObject(InvalidateObject invalidateObject) { + this.invalidateObject = invalidateObject; } + + public static class InvalidatePermission { + + private String clazz1; + + public String getClazz1() { + return clazz1; + } + + public void setClazz1(String clazz1) { + this.clazz1 = clazz1; + } + + private long id1; + + public long getId1() { + return id1; + } + + public void setId1(long id1) { + this.id1 = id1; + } + + private String clazz2; + + public String getClazz2() { + return clazz2; + } + + public void setClazz2(String clazz2) { + this.clazz2 = clazz2; + } + + private long id2; + + public long getId2() { + return id2; + } + + public void setId2(long id2) { + this.id2 = id2; + } + + private boolean link; + + public boolean getLink() { + return link; + } + + public void setLink(boolean link) { + this.link = link; + } + + } + + private InvalidatePermission invalidatePermission; + + public InvalidatePermission getInvalidatePermission() { + return invalidatePermission; + } + + public void setInvalidatePermission(InvalidatePermission invalidatePermission) { + this.invalidatePermission = invalidatePermission; + } + } diff --git a/src/main/java/org/traccar/broadcast/ObjectOperation.java b/src/main/java/org/traccar/broadcast/ObjectOperation.java new file mode 100644 index 000000000..27e5fb253 --- /dev/null +++ b/src/main/java/org/traccar/broadcast/ObjectOperation.java @@ -0,0 +1,7 @@ +package org.traccar.broadcast; + +public enum ObjectOperation { + ADD, + UPDATE, + DELETE, +} |