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,17 +99,16 @@ public class AutomationController {
req.getScenes()
.stream()
.map(AutomationFastUpdateRequest.ScenePriorityDTO::toModel)
.map(
t -> {
t.setAutomationId(a.getId());
.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
t.setAutomation(a);
return t;
})
.collect(Collectors.toList()));
s.setAutomation(a);
}
a.getScenes().clear();
a.getTriggers().clear();

View file

@ -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;

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) {
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);
}

View file

@ -16,11 +16,9 @@ import org.springframework.stereotype.Component;
public class DeviceService {
private final DeviceRepository<Device> deviceRepository;
private final AutomationRepository automationRepository;
private final SceneRepository sceneRepository;
private final SceneService sceneService;
private final TriggerRepository<Trigger<? extends Device>> 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<Device> deviceRepository,
AutomationRepository automationRepository,
SceneRepository sceneRepository,
SceneService sceneService,
TriggerRepository<Trigger<? extends Device>> 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<Trigger<? extends Device>> triggers = triggerRepository.findAllByDeviceId(deviceId);
List<Trigger<Device>> 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));
}

View file

@ -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<State<?>> stateRepository;
private final SceneRepository sceneRepository;
public Scene findByValidatedId(Long id) {
return sceneRepository.findById(id).orElseThrow();
}
@Autowired
public SceneService(
DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService,
StateRepository<State<?>> stateRepository) {
StateRepository<State<?>> stateRepository,
SceneRepository sceneRepository) {
this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService;
this.stateRepository = stateRepository;
this.sceneRepository = sceneRepository;
}
private List<Device> copyStatesToDevices(Scene fromScene) {

View file

@ -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> T getEndpointInstance(Class<T> 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");