Deletion propagation implemented

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-04 11:58:03 +02:00
parent 69d1b38ff2
commit 21bc66d24b
16 changed files with 49 additions and 20 deletions

View file

@ -75,6 +75,6 @@ public class ButtonDimmerController
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id, final Principal principal) public void delete(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
} }

View file

@ -49,7 +49,7 @@ public class CurtainsController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id, final Principal principal) public void delete(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")

View file

@ -66,7 +66,9 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
*/ */
@PutMapping @PutMapping
public DimmableLight update( public DimmableLight update(
@Valid @RequestBody DimmableSaveRequest sp, final Principal principal, Long hostId) @Valid @RequestBody DimmableSaveRequest sp,
final Principal principal,
@RequestParam(value = "hostId", required = false) Long hostId)
throws NotFoundException { throws NotFoundException {
return save( return save(
@ -79,7 +81,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id, final Principal principal) public void delete(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
// the full url should be: "/dimmableLight/{id}/state?sceneId={sceneId} // the full url should be: "/dimmableLight/{id}/state?sceneId={sceneId}

View file

@ -62,6 +62,6 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id, final Principal principal) public void delete(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
} }

View file

@ -53,6 +53,6 @@ public class MotionSensorController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id, final Principal principal) public void delete(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
} }

View file

@ -80,7 +80,9 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
@PutMapping @PutMapping
public RegularLight update( 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 { throws NotFoundException {
return save( return save(
fetchIfOwnerOrGuest(principal, rl.getId(), hostId), fetchIfOwnerOrGuest(principal, rl.getId(), hostId),
@ -92,7 +94,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id, final Principal principal) public void delete(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
// the full url should be: "/regularLight/{id}/state?sceneId={sceneId} // the full url should be: "/regularLight/{id}/state?sceneId={sceneId}

View file

@ -124,6 +124,11 @@ public class RoomController {
roomRepository roomRepository
.findByIdAndUsername(id, principal.getName()) .findByIdAndUsername(id, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
List<Device> devices = deviceService.findAll(r.getId(), null, principal.getName());
for (Device d : devices) {
deviceService.deleteByIdAsOwner(d.getId(), principal.getName());
}
roomRepository.delete(r); roomRepository.delete(r);
} }

View file

@ -61,7 +61,7 @@ public class SecurityCameraController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id, final Principal principal) public void delete(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")

View file

@ -57,6 +57,6 @@ public class SensorController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id, final Principal principal) public void deleteById(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
} }

View file

@ -63,7 +63,7 @@ public class SmartPlugController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id, final Principal principal) public void deleteById(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")

View file

@ -81,6 +81,6 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id, final Principal principal) public void deleteById(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
} }

View file

@ -61,7 +61,7 @@ public class ThermostatController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id, final Principal principal) public void deleteById(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
deviceService.delete(id, principal.getName()); deviceService.deleteByIdAsOwner(id, principal.getName());
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")

View file

@ -27,7 +27,7 @@ public class Automation {
@GsonExclude @GsonExclude
private Long userId; private Long userId;
@OneToMany(mappedBy = "automation", orphanRemoval = true) @OneToMany(mappedBy = "automation", orphanRemoval = true, cascade = CascadeType.REMOVE)
private Set<Trigger<?>> triggers = new HashSet<>(); private Set<Trigger<?>> triggers = new HashSet<>();
@OneToMany(mappedBy = "automation", cascade = CascadeType.REMOVE) @OneToMany(mappedBy = "automation", cascade = CascadeType.REMOVE)

View file

@ -10,7 +10,13 @@ import javax.persistence.*;
@Entity @Entity
public class Switch extends InputDevice implements BooleanTriggerable { public class Switch extends InputDevice implements BooleanTriggerable {
@ManyToMany(cascade = CascadeType.DETACH) @ManyToMany(
cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.REFRESH,
CascadeType.PERSIST
})
@GsonExclude @GsonExclude
@SocketGsonExclude @SocketGsonExclude
@JoinTable( @JoinTable(

View file

@ -14,7 +14,14 @@ public abstract class Switchable extends OutputDevice {
public static final Connector<Switch, Switchable> SWITCH_SWITCHABLE_CONNECTOR = public static final Connector<Switch, Switchable> SWITCH_SWITCHABLE_CONNECTOR =
Connector.basic(Switch::getOutputs, Switchable::getSwitches); 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 @GsonExclude
@SocketGsonExclude @SocketGsonExclude
private Set<Switch> inputs = new HashSet<>(); private Set<Switch> inputs = new HashSet<>();

View file

@ -206,13 +206,20 @@ public class DeviceService {
return device; return device;
} }
public void delete(Long id, String username) throws NotFoundException { public void deleteByIdAsOwner(Long id, String username) throws NotFoundException {
Device device = Device d =
deviceRepository deviceRepository
.findByIdAndUsername(id, username) .findByIdAndUsername(id, username)
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
deviceRepository.delete(device);
propagateUpdateAsOwner(device, username, false); final User user = userRepository.findByUsername(username);
final Set<User> 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);
} }
} }