Merge branch 'sonar-fix' into 'dev'

Even another batch of sonar fixes

See merge request sa4-2020/the-sanmarinoes/backend!131
This commit is contained in:
Claudio Maggioni 2020-05-10 12:13:28 +02:00
commit 2976212f18
7 changed files with 61 additions and 32 deletions

View file

@ -99,18 +99,17 @@ public class AutomationController {
req.getScenes() req.getScenes()
.stream() .stream()
.map(AutomationFastUpdateRequest.ScenePriorityDTO::toModel) .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())); .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.getScenes().clear();
a.getTriggers().clear(); a.getTriggers().clear();
ss.forEach(t -> a.getScenes().add(t)); ss.forEach(t -> a.getScenes().add(t));

View file

@ -24,6 +24,10 @@ public class SecurityCamera extends Switchable implements BooleanTriggerable {
return path; return path;
} }
public void setPath(String path) {
this.path = path;
}
@Override @Override
public boolean isOn() { public boolean isOn() {
return on; return on;

View file

@ -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<Trigger<Device>> triggerRepository;
@Autowired
public AutomationService(
AutomationRepository automationRepository,
TriggerRepository<Trigger<Device>> triggerRepository) {
this.automationRepository = automationRepository;
this.triggerRepository = triggerRepository;
}
public List<Trigger<Device>> findTriggersByDeviceId(Long deviceId) {
return triggerRepository.findAllByDeviceId(deviceId);
}
public Automation findByVerifiedId(Long automationId) {
return automationRepository.findById(automationId).orElseThrow();
}
}

View file

@ -93,7 +93,7 @@ public class DevicePropagationService {
Iterable<T> devices, String username, boolean fromScene, boolean fromTrigger) { Iterable<T> devices, String username, boolean fromScene, boolean fromTrigger) {
devices.forEach(d -> renameIfDuplicate(d, username)); devices.forEach(d -> renameIfDuplicate(d, username));
devices = deviceRepository.saveAll(devices); devices = deviceRepository.saveAll(devices);
devices.forEach((d) -> propagateUpdateAsOwner(d, username, fromScene && fromTrigger)); devices.forEach(d -> propagateUpdateAsOwner(d, username, fromScene && fromTrigger));
return toList(devices); return toList(devices);
} }

View file

@ -16,11 +16,9 @@ import org.springframework.stereotype.Component;
public class DeviceService { public class DeviceService {
private final DeviceRepository<Device> deviceRepository; private final DeviceRepository<Device> deviceRepository;
private final AutomationRepository automationRepository;
private final SceneRepository sceneRepository;
private final SceneService sceneService; private final SceneService sceneService;
private final TriggerRepository<Trigger<? extends Device>> triggerRepository;
private final RoomRepository roomRepository; private final RoomRepository roomRepository;
private final AutomationService automationService;
private final EagerUserRepository userRepository; private final EagerUserRepository userRepository;
private final DevicePopulationService devicePopulationService; private final DevicePopulationService devicePopulationService;
private final DevicePropagationService devicePropagationService; private final DevicePropagationService devicePropagationService;
@ -28,20 +26,16 @@ public class DeviceService {
@Autowired @Autowired
public DeviceService( public DeviceService(
DeviceRepository<Device> deviceRepository, DeviceRepository<Device> deviceRepository,
AutomationRepository automationRepository,
SceneRepository sceneRepository,
SceneService sceneService, SceneService sceneService,
TriggerRepository<Trigger<? extends Device>> triggerRepository,
RoomRepository roomRepository, RoomRepository roomRepository,
AutomationService automationService,
EagerUserRepository userRepository, EagerUserRepository userRepository,
DevicePopulationService devicePopulationService, DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService) { DevicePropagationService devicePropagationService) {
this.deviceRepository = deviceRepository; this.deviceRepository = deviceRepository;
this.automationRepository = automationRepository;
this.sceneRepository = sceneRepository;
this.sceneService = sceneService; this.sceneService = sceneService;
this.triggerRepository = triggerRepository;
this.roomRepository = roomRepository; this.roomRepository = roomRepository;
this.automationService = automationService;
this.userRepository = userRepository; this.userRepository = userRepository;
this.devicePopulationService = devicePopulationService; this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService; this.devicePropagationService = devicePropagationService;
@ -59,22 +53,18 @@ public class DeviceService {
final long deviceId = device.getId(); final long deviceId = device.getId();
List<Trigger<? extends Device>> triggers = triggerRepository.findAllByDeviceId(deviceId); List<Trigger<Device>> triggers = automationService.findTriggersByDeviceId(deviceId);
triggers.stream() triggers.stream()
.filter(Trigger::triggered) .filter(Trigger::triggered)
.map(Trigger::getAutomationId) .map(Trigger::getAutomationId)
.map(t -> automationRepository.findById(t).orElseThrow(IllegalStateException::new)) .map(automationService::findByVerifiedId)
.distinct() .distinct()
.map(Automation::getScenes) .map(Automation::getScenes)
.flatMap(Collection::stream) .flatMap(Collection::stream)
.distinct() .distinct()
.sorted(Comparator.comparing(ScenePriority::getPriority)) .sorted(Comparator.comparing(ScenePriority::getPriority))
.map( .map(t -> sceneService.findByValidatedId(t.getSceneId()))
t ->
sceneRepository
.findById(t.getSceneId())
.orElseThrow(IllegalStateException::new))
.forEach(s -> sceneService.apply(s, username, true)); .forEach(s -> sceneService.apply(s, username, true));
} }

View file

@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@ -11,14 +12,22 @@ public class SceneService {
private final DevicePopulationService devicePopulationService; private final DevicePopulationService devicePopulationService;
private final DevicePropagationService devicePropagationService; private final DevicePropagationService devicePropagationService;
private final StateRepository<State<?>> stateRepository; private final StateRepository<State<?>> stateRepository;
private final SceneRepository sceneRepository;
public Scene findByValidatedId(Long id) {
return sceneRepository.findById(id).orElseThrow();
}
@Autowired
public SceneService( public SceneService(
DevicePopulationService devicePopulationService, DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService, DevicePropagationService devicePropagationService,
StateRepository<State<?>> stateRepository) { StateRepository<State<?>> stateRepository,
SceneRepository sceneRepository) {
this.devicePopulationService = devicePopulationService; this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService; this.devicePropagationService = devicePropagationService;
this.stateRepository = stateRepository; this.stateRepository = stateRepository;
this.sceneRepository = sceneRepository;
} }
private List<Device> copyStatesToDevices(Scene fromScene) { private List<Device> copyStatesToDevices(Scene fromScene) {

View file

@ -11,7 +11,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointRegistration
@Configuration @Configuration
public class SensorSocketConfig extends ServerEndpointConfig.Configurator { public class SensorSocketConfig extends ServerEndpointConfig.Configurator {
private SensorSocketEndpoint instance; private final SensorSocketEndpoint instance;
@Autowired @Autowired
public SensorSocketConfig(SensorSocketEndpoint instance) { public SensorSocketConfig(SensorSocketEndpoint instance) {
@ -41,9 +41,8 @@ public class SensorSocketConfig extends ServerEndpointConfig.Configurator {
@Override @Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
try { try {
@SuppressWarnings("unchecked") //noinspection unchecked
final T thaInstance = (T) this.instance; return (T) this.instance;
return thaInstance;
} catch (ClassCastException e) { } catch (ClassCastException e) {
final var e2 = final var e2 =
new InstantiationException("Cannot cast SensorSocketEndpoint to desired type"); new InstantiationException("Cannot cast SensorSocketEndpoint to desired type");