From be7c9ce99f3074396a5745c963bf5cb0a736013c Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 25 Apr 2020 22:51:30 +0200 Subject: [PATCH] fix duplicate device names --- .../controller/BooleanTriggerController.java | 4 ++-- .../smarthut/models/DeviceRepository.java | 4 ++++ .../smarthut/service/DeviceService.java | 14 +++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java index 927b66c..9331b56 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java @@ -29,7 +29,7 @@ public class BooleanTriggerController { return booleanTriggerRepository.findAllByAutomationId(automationId); } - private BooleanTrigger save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) { + private BooleanTrigger save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) { newRL.setDeviceId(s.getDeviceId()); newRL.setAutomationId(s.getAutomationId()); newRL.setOn(s.isOn()); @@ -40,7 +40,7 @@ public class BooleanTriggerController { @PostMapping public BooleanTrigger create( @Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) { - return save(new BooleanTrigger(), booleanTriggerSaveRequest); + return save(new BooleanTrigger<>(), booleanTriggerSaveRequest); } @PutMapping diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java index cf5c004..d1f828d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java @@ -14,6 +14,10 @@ import org.springframework.data.repository.query.Param; public interface DeviceRepository extends CrudRepository { List findByRoomId(@Param("roomId") long roomId); + @Query( + "SELECT COUNT(d) FROM Device d JOIN d.room r JOIN r.user u WHERE d.name = ?1 AND u.username = ?2") + Integer findDuplicates(String name, String username); + /** * Finds devices by their id and a username * 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 87df762..0bc2a6e 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 @@ -29,7 +29,15 @@ public class DeviceService { roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new); } - public void triggerTriggers(Device device) { + private void renameIfDuplicate(Device toCreate, String username) { + while (deviceRepository.findDuplicates(toCreate.getName(), username) + - (toCreate.getId() <= 0 ? 0 : 1) + > 0) { + toCreate.setName(toCreate.getName() + " (new)"); + } + } + + private void triggerTriggers(Device device) { final long deviceId = device.getId(); @@ -107,6 +115,8 @@ public class DeviceService { final User currentUser = userRepository.findByUsername(guestUsername); final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new); if (!host.getGuests().contains(currentUser)) throw new NotFoundException(); + renameIfDuplicate(device, host.getUsername()); + device = deviceRepository.save(device); final Set guests = Set.copyOf(host.getGuests()); // We're telling the host that a guest has modified a device. Therefore, fromGuest becomes @@ -145,6 +155,7 @@ public class DeviceService { public List saveAllAsOwner( Iterable devices, String username, boolean fromScene) { + devices.forEach(d -> renameIfDuplicate(d, username)); devices = deviceRepository.saveAll(devices); devices.forEach((d) -> propagateUpdateAsOwner(d, username)); @@ -156,6 +167,7 @@ public class DeviceService { } public T saveAsOwner(T device, String username, boolean fromScene) { + renameIfDuplicate(device, username); device = deviceRepository.save(device); propagateUpdateAsOwner(device, username);