From 9394a8439f9b4b298d406b8eb1c90ad1e1d777da Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 10 May 2020 12:09:53 +0200 Subject: [PATCH] Even another batch of sonar fixes --- .../controller/AutomationController.java | 19 ++++++------- .../smarthut/models/SecurityCamera.java | 4 +++ .../smarthut/service/AutomationService.java | 28 +++++++++++++++++++ .../service/DevicePropagationService.java | 2 +- .../smarthut/service/DeviceService.java | 22 ++++----------- .../smarthut/service/SceneService.java | 11 +++++++- .../smarthut/socket/SensorSocketConfig.java | 7 ++--- 7 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationService.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java index af5f727..1b3b42d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java @@ -99,18 +99,17 @@ public class AutomationController { req.getScenes() .stream() .map(AutomationFastUpdateRequest.ScenePriorityDTO::toModel) - .map( - t -> { - t.setAutomationId(a.getId()); - - // this is here just to pass the quality gate, - // please do not replicate unless the quality gate sees - // it as a bug - t.setAutomation(a); - return t; - }) .collect(Collectors.toList())); + for (final ScenePriority s : ss) { + s.setAutomationId(a.getId()); + + // this is here just to pass the quality gate, + // please do not replicate unless the quality gate sees + // it as a bug + s.setAutomation(a); + } + a.getScenes().clear(); a.getTriggers().clear(); ss.forEach(t -> a.getScenes().add(t)); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java index 3d4cef9..4a810ce 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java @@ -24,6 +24,10 @@ public class SecurityCamera extends Switchable implements BooleanTriggerable { return path; } + public void setPath(String path) { + this.path = path; + } + @Override public boolean isOn() { return on; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationService.java new file mode 100644 index 0000000..9eaf401 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/AutomationService.java @@ -0,0 +1,28 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AutomationService { + private final AutomationRepository automationRepository; + private final TriggerRepository> triggerRepository; + + @Autowired + public AutomationService( + AutomationRepository automationRepository, + TriggerRepository> triggerRepository) { + this.automationRepository = automationRepository; + this.triggerRepository = triggerRepository; + } + + public List> findTriggersByDeviceId(Long deviceId) { + return triggerRepository.findAllByDeviceId(deviceId); + } + + public Automation findByVerifiedId(Long automationId) { + return automationRepository.findById(automationId).orElseThrow(); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java index ae004a4..edba584 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePropagationService.java @@ -93,7 +93,7 @@ public class DevicePropagationService { Iterable devices, String username, boolean fromScene, boolean fromTrigger) { devices.forEach(d -> renameIfDuplicate(d, username)); devices = deviceRepository.saveAll(devices); - devices.forEach((d) -> propagateUpdateAsOwner(d, username, fromScene && fromTrigger)); + devices.forEach(d -> propagateUpdateAsOwner(d, username, fromScene && fromTrigger)); return toList(devices); } 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 3678bc0..54373eb 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 @@ -16,11 +16,9 @@ import org.springframework.stereotype.Component; public class DeviceService { private final DeviceRepository deviceRepository; - private final AutomationRepository automationRepository; - private final SceneRepository sceneRepository; private final SceneService sceneService; - private final TriggerRepository> triggerRepository; private final RoomRepository roomRepository; + private final AutomationService automationService; private final EagerUserRepository userRepository; private final DevicePopulationService devicePopulationService; private final DevicePropagationService devicePropagationService; @@ -28,20 +26,16 @@ public class DeviceService { @Autowired public DeviceService( DeviceRepository deviceRepository, - AutomationRepository automationRepository, - SceneRepository sceneRepository, SceneService sceneService, - TriggerRepository> triggerRepository, RoomRepository roomRepository, + AutomationService automationService, EagerUserRepository userRepository, DevicePopulationService devicePopulationService, DevicePropagationService devicePropagationService) { this.deviceRepository = deviceRepository; - this.automationRepository = automationRepository; - this.sceneRepository = sceneRepository; this.sceneService = sceneService; - this.triggerRepository = triggerRepository; this.roomRepository = roomRepository; + this.automationService = automationService; this.userRepository = userRepository; this.devicePopulationService = devicePopulationService; this.devicePropagationService = devicePropagationService; @@ -59,22 +53,18 @@ public class DeviceService { final long deviceId = device.getId(); - List> triggers = triggerRepository.findAllByDeviceId(deviceId); + List> triggers = automationService.findTriggersByDeviceId(deviceId); triggers.stream() .filter(Trigger::triggered) .map(Trigger::getAutomationId) - .map(t -> automationRepository.findById(t).orElseThrow(IllegalStateException::new)) + .map(automationService::findByVerifiedId) .distinct() .map(Automation::getScenes) .flatMap(Collection::stream) .distinct() .sorted(Comparator.comparing(ScenePriority::getPriority)) - .map( - t -> - sceneRepository - .findById(t.getSceneId()) - .orElseThrow(IllegalStateException::new)) + .map(t -> sceneService.findByValidatedId(t.getSceneId())) .forEach(s -> sceneService.apply(s, username, true)); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java index 0fcf1f8..824ea7e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java @@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.ArrayList; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component @@ -11,14 +12,22 @@ public class SceneService { private final DevicePopulationService devicePopulationService; private final DevicePropagationService devicePropagationService; private final StateRepository> stateRepository; + private final SceneRepository sceneRepository; + public Scene findByValidatedId(Long id) { + return sceneRepository.findById(id).orElseThrow(); + } + + @Autowired public SceneService( DevicePopulationService devicePopulationService, DevicePropagationService devicePropagationService, - StateRepository> stateRepository) { + StateRepository> stateRepository, + SceneRepository sceneRepository) { this.devicePopulationService = devicePopulationService; this.devicePropagationService = devicePropagationService; this.stateRepository = stateRepository; + this.sceneRepository = sceneRepository; } private List copyStatesToDevices(Scene fromScene) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java index e1b29ce..338a8f2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketConfig.java @@ -11,7 +11,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointRegistration @Configuration public class SensorSocketConfig extends ServerEndpointConfig.Configurator { - private SensorSocketEndpoint instance; + private final SensorSocketEndpoint instance; @Autowired public SensorSocketConfig(SensorSocketEndpoint instance) { @@ -41,9 +41,8 @@ public class SensorSocketConfig extends ServerEndpointConfig.Configurator { @Override public T getEndpointInstance(Class endpointClass) throws InstantiationException { try { - @SuppressWarnings("unchecked") - final T thaInstance = (T) this.instance; - return thaInstance; + //noinspection unchecked + return (T) this.instance; } catch (ClassCastException e) { final var e2 = new InstantiationException("Cannot cast SensorSocketEndpoint to desired type");