Merge branch 'sonar-fix' into 'dev'
Sonarqube fixes See merge request sa4-2020/the-sanmarinoes/backend!120
This commit is contained in:
commit
774bbc5ab9
13 changed files with 23 additions and 28 deletions
|
@ -64,8 +64,8 @@ public class CurtainsController {
|
|||
.findByIdAndUsername(deviceId, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
State<? extends Dimmable> s = c.cloneState();
|
||||
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sceneId);
|
||||
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sc.getId());
|
||||
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
|
||||
throw new DuplicateStateException();
|
||||
return stateRepository.save(s);
|
||||
|
|
|
@ -5,6 +5,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.BadDataException;
|
|||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
||||
import java.security.Principal;
|
||||
|
@ -40,11 +41,12 @@ public class DeviceController {
|
|||
.orElseThrow(NotFoundException::new);
|
||||
|
||||
// check if roomId is valid
|
||||
final Room r =
|
||||
roomRepository
|
||||
.findByIdAndUsername(deviceSaveRequest.getRoomId(), principal.getName())
|
||||
.orElseThrow(() -> new BadDataException("roomId is not a valid room id"));
|
||||
|
||||
d.setRoomId(deviceSaveRequest.getRoomId());
|
||||
d.setRoomId(r.getId());
|
||||
d.setName(deviceSaveRequest.getName());
|
||||
|
||||
deviceService.saveAsOwner(d, principal.getName());
|
||||
|
|
|
@ -98,8 +98,8 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
|
|||
.findByIdAndUsername(deviceId, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
State<? extends Dimmable> s = d.cloneState();
|
||||
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sceneId);
|
||||
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sc.getId());
|
||||
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
|
||||
throw new DuplicateStateException();
|
||||
return stateRepository.save(s);
|
||||
|
|
|
@ -110,8 +110,8 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
|
|||
.findByIdAndUsername(deviceId, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
State<? extends Switchable> s = d.cloneState();
|
||||
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sceneId);
|
||||
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sc.getId());
|
||||
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
|
||||
throw new DuplicateStateException();
|
||||
return stateRepository.save(s);
|
||||
|
|
|
@ -78,8 +78,8 @@ public class SmartPlugController {
|
|||
.findByIdAndUsername(deviceId, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
State<? extends Switchable> s = d.cloneState();
|
||||
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sceneId);
|
||||
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sc.getId());
|
||||
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
|
||||
throw new DuplicateStateException();
|
||||
return stateRepository.save(s);
|
||||
|
|
|
@ -75,8 +75,8 @@ public class ThermostatController {
|
|||
.findByIdAndUsername(deviceId, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
State<? extends Switchable> s = d.cloneState();
|
||||
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sceneId);
|
||||
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
|
||||
s.setSceneId(sc.getId());
|
||||
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)
|
||||
throw new DuplicateStateException();
|
||||
return stateRepository.save(s);
|
||||
|
|
|
@ -7,7 +7,6 @@ import io.swagger.annotations.ApiModelProperty;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/** Generic abstraction for a smart home device */
|
||||
@Entity
|
||||
|
@ -46,11 +45,9 @@ public abstract class Device {
|
|||
* a REST call.
|
||||
*/
|
||||
@Column(name = "room_id", nullable = false)
|
||||
@NotNull
|
||||
private Long roomId;
|
||||
|
||||
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
|
||||
@NotNull
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
|
|
|
@ -43,7 +43,6 @@ public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D
|
|||
GREATER_EQUAL
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Column(nullable = false)
|
||||
private Operator operator;
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import javax.persistence.Column;
|
|||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */
|
||||
@Entity
|
||||
|
@ -45,7 +44,6 @@ public class Sensor extends InputDevice implements RangeTriggerable {
|
|||
|
||||
/** The type of this sensor */
|
||||
@Column(nullable = false)
|
||||
@NotNull
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
private SensorType sensor;
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
|
@ -20,7 +19,6 @@ public class SmartPlug extends Switchable implements BooleanTriggerable {
|
|||
|
||||
/** Whether the smart plug is on */
|
||||
@Column(name = "smart_plug_on", nullable = false)
|
||||
@NotNull
|
||||
private boolean on;
|
||||
|
||||
public BigDecimal getTotalConsumption() {
|
||||
|
|
|
@ -64,7 +64,7 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
|
|||
}
|
||||
|
||||
/** Temperature to be reached */
|
||||
@Column @NotNull private BigDecimal targetTemperature;
|
||||
@Column private BigDecimal targetTemperature;
|
||||
|
||||
/** The temperature detected by the embedded sensor */
|
||||
@Column(nullable = false, precision = 4, scale = 1)
|
||||
|
|
|
@ -37,7 +37,6 @@ public abstract class Trigger<D extends Device> {
|
|||
* from a REST call.
|
||||
*/
|
||||
@Column(name = "device_id", nullable = false)
|
||||
@NotNull
|
||||
private Long deviceId;
|
||||
|
||||
@ManyToOne
|
||||
|
|
|
@ -48,7 +48,11 @@ public class DeviceService {
|
|||
}
|
||||
|
||||
public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException {
|
||||
roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new);
|
||||
final Room r =
|
||||
roomRepository
|
||||
.findByIdAndUsername(roomId, username)
|
||||
.orElseThrow(NotFoundException::new);
|
||||
if (!r.getId().equals(roomId)) throw new IllegalStateException();
|
||||
}
|
||||
|
||||
private void triggerTriggers(Device device, final String username) {
|
||||
|
@ -140,9 +144,7 @@ public class DeviceService {
|
|||
User host = null;
|
||||
if (hostId == null) {
|
||||
if (roomId != null) {
|
||||
roomRepository
|
||||
.findByIdAndUsername(roomId, username)
|
||||
.orElseThrow(NotFoundException::new);
|
||||
throwIfRoomNotOwned(roomId, username);
|
||||
devices = deviceRepository.findByRoomId(roomId);
|
||||
} else {
|
||||
devices = deviceRepository.findAllByUsername(username);
|
||||
|
|
Loading…
Reference in a new issue