From 21bc66d24b35cd1c5b4cbd38bf9a8de0a952d527 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Mon, 4 May 2020 11:58:03 +0200 Subject: [PATCH] Deletion propagation implemented --- .../controller/ButtonDimmerController.java | 2 +- .../smarthut/controller/CurtainsController.java | 2 +- .../controller/DimmableLightController.java | 6 ++++-- .../smarthut/controller/KnobDimmerController.java | 2 +- .../controller/MotionSensorController.java | 2 +- .../controller/RegularLightController.java | 6 ++++-- .../smarthut/controller/RoomController.java | 5 +++++ .../controller/SecurityCameraController.java | 2 +- .../smarthut/controller/SensorController.java | 2 +- .../smarthut/controller/SmartPlugController.java | 2 +- .../smarthut/controller/SwitchController.java | 2 +- .../smarthut/controller/ThermostatController.java | 2 +- .../sanmarinoes/smarthut/models/Automation.java | 2 +- .../sa4/sanmarinoes/smarthut/models/Switch.java | 8 +++++++- .../sanmarinoes/smarthut/models/Switchable.java | 9 ++++++++- .../smarthut/service/DeviceService.java | 15 +++++++++++---- 16 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 2663211..cf2cb56 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -75,6 +75,6 @@ public class ButtonDimmerController @DeleteMapping("/{id}") public void delete(@PathVariable("id") long id, final Principal principal) throws NotFoundException { - deviceService.delete(id, principal.getName()); + deviceService.deleteByIdAsOwner(id, principal.getName()); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java index 6644135..4065f12 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java @@ -49,7 +49,7 @@ public class CurtainsController { @DeleteMapping("/{id}") public void delete(@PathVariable("id") long id, final Principal principal) throws NotFoundException { - deviceService.delete(id, principal.getName()); + deviceService.deleteByIdAsOwner(id, principal.getName()); } @PostMapping("/{id}/state") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 60a4726..28648c1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -66,7 +66,9 @@ public class DimmableLightController extends GuestEnabledController @PutMapping public RegularLight update( - @Valid @RequestBody SwitchableSaveRequest rl, final Principal principal, Long hostId) + @Valid @RequestBody SwitchableSaveRequest rl, + final Principal principal, + @RequestParam(value = "hostId", required = false) Long hostId) throws NotFoundException { return save( fetchIfOwnerOrGuest(principal, rl.getId(), hostId), @@ -92,7 +94,7 @@ public class RegularLightController extends GuestEnabledController @DeleteMapping("/{id}") public void delete(@PathVariable("id") long id, final Principal principal) throws NotFoundException { - deviceService.delete(id, principal.getName()); + deviceService.deleteByIdAsOwner(id, principal.getName()); } // the full url should be: "/regularLight/{id}/state?sceneId={sceneId} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java index c4540c5..e6ab648 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java @@ -124,6 +124,11 @@ public class RoomController { roomRepository .findByIdAndUsername(id, principal.getName()) .orElseThrow(NotFoundException::new); + List devices = deviceService.findAll(r.getId(), null, principal.getName()); + for (Device d : devices) { + deviceService.deleteByIdAsOwner(d.getId(), principal.getName()); + } + roomRepository.delete(r); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java index c063ebf..1554f0a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java @@ -61,7 +61,7 @@ public class SecurityCameraController { @DeleteMapping("/{id}") public void delete(@PathVariable("id") long id, final Principal principal) throws NotFoundException { - deviceService.delete(id, principal.getName()); + deviceService.deleteByIdAsOwner(id, principal.getName()); } @PostMapping("/{id}/state") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index 41b9af0..3833452 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -57,6 +57,6 @@ public class SensorController { @DeleteMapping("/{id}") public void deleteById(@PathVariable("id") long id, final Principal principal) throws NotFoundException { - deviceService.delete(id, principal.getName()); + deviceService.deleteByIdAsOwner(id, principal.getName()); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index 468cd5d..790aee2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -63,7 +63,7 @@ public class SmartPlugController { @DeleteMapping("/{id}") public void deleteById(@PathVariable("id") long id, final Principal principal) throws NotFoundException { - deviceService.delete(id, principal.getName()); + deviceService.deleteByIdAsOwner(id, principal.getName()); } @PostMapping("/{id}/state") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index 6806b64..d92cd49 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -81,6 +81,6 @@ public class SwitchController extends InputDeviceConnectionController> triggers = new HashSet<>(); @OneToMany(mappedBy = "automation", cascade = CascadeType.REMOVE) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index 4b9ef3a..7825eae 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -10,7 +10,13 @@ import javax.persistence.*; @Entity public class Switch extends InputDevice implements BooleanTriggerable { - @ManyToMany(cascade = CascadeType.DETACH) + @ManyToMany( + cascade = { + CascadeType.DETACH, + CascadeType.MERGE, + CascadeType.REFRESH, + CascadeType.PERSIST + }) @GsonExclude @SocketGsonExclude @JoinTable( diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index c7abc7e..9db6361 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -14,7 +14,14 @@ public abstract class Switchable extends OutputDevice { public static final Connector SWITCH_SWITCHABLE_CONNECTOR = Connector.basic(Switch::getOutputs, Switchable::getSwitches); - @ManyToMany(mappedBy = "switchables", cascade = CascadeType.DETACH) + @ManyToMany( + mappedBy = "switchables", + cascade = { + CascadeType.DETACH, + CascadeType.MERGE, + CascadeType.REFRESH, + CascadeType.PERSIST + }) @GsonExclude @SocketGsonExclude private Set inputs = new HashSet<>(); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java index a0116c2..b0a8d7a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -206,13 +206,20 @@ public class DeviceService { return device; } - public void delete(Long id, String username) throws NotFoundException { - Device device = + public void deleteByIdAsOwner(Long id, String username) throws NotFoundException { + Device d = deviceRepository .findByIdAndUsername(id, username) .orElseThrow(NotFoundException::new); - deviceRepository.delete(device); - propagateUpdateAsOwner(device, username, false); + final User user = userRepository.findByUsername(username); + final Set guests = user.getGuests(); + // make sure we're broadcasting from host + for (final User guest : guests) { + // broadcast to endpoint the object device, with receiving user set to guest + endpoint.queueDeviceUpdate(d, guest, false, user.getId(), true); + } + + deviceRepository.delete(d); } }