Merge branch 'sonar-fix' into 'dev'

Degenerified Conditions

See merge request sa4-2020/the-sanmarinoes/backend!135
This commit is contained in:
Claudio Maggioni 2020-05-10 21:49:14 +02:00
commit 6e207a34c9
8 changed files with 24 additions and 36 deletions

View file

@ -25,11 +25,11 @@ public class BooleanConditionController {
@Autowired BooleanConditionRepository booleanConditionRepository; @Autowired BooleanConditionRepository booleanConditionRepository;
@GetMapping("/{automationId}") @GetMapping("/{automationId}")
public List<BooleanCondition<?>> getAll(@PathVariable long automationId) { public List<BooleanCondition> getAll(@PathVariable long automationId) {
return booleanConditionRepository.findAllByAutomationId(automationId); return booleanConditionRepository.findAllByAutomationId(automationId);
} }
private BooleanCondition<?> save(BooleanCondition<?> newRL, BooleanConditionSaveRequest s) { private BooleanCondition save(BooleanCondition newRL, BooleanConditionSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOn(s.isOn()); newRL.setOn(s.isOn());
@ -38,13 +38,13 @@ public class BooleanConditionController {
} }
@PostMapping @PostMapping
public BooleanCondition<?> create( public BooleanCondition create(
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) {
return save(new BooleanCondition<>(), booleanTriggerSaveRequest); return save(new BooleanCondition(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping
public BooleanCondition<?> update( public BooleanCondition update(
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) @Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest)
throws NotFoundException { throws NotFoundException {
return save( return save(

View file

@ -25,11 +25,11 @@ public class RangeConditionController {
@Autowired RangeConditionRepository rangeConditionRepository; @Autowired RangeConditionRepository rangeConditionRepository;
@GetMapping("/{automationId}") @GetMapping("/{automationId}")
public List<RangeCondition<?>> getAll(@PathVariable long automationId) { public List<RangeCondition> getAll(@PathVariable long automationId) {
return rangeConditionRepository.findAllByAutomationId(automationId); return rangeConditionRepository.findAllByAutomationId(automationId);
} }
private RangeCondition<?> save(RangeCondition<?> newRL, RangeConditionSaveRequest s) { private RangeCondition save(RangeCondition newRL, RangeConditionSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator()); newRL.setOperator(s.getOperator());
@ -39,13 +39,13 @@ public class RangeConditionController {
} }
@PostMapping @PostMapping
public RangeCondition<?> create( public RangeCondition create(
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) {
return save(new RangeCondition<>(), booleanTriggerSaveRequest); return save(new RangeCondition(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping
public RangeCondition<?> update( public RangeCondition update(
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) @Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest)
throws NotFoundException { throws NotFoundException {
return save( return save(

View file

@ -62,33 +62,33 @@ public class AutomationFastUpdateRequest {
} }
} }
public abstract class ConditionDTO { public abstract static class ConditionDTO {
@NotNull public long deviceId; @NotNull public long deviceId;
public abstract Condition<?> toModel(); public abstract Condition<?> toModel();
} }
public class BooleanConditionDTO extends ConditionDTO { public static class BooleanConditionDTO extends ConditionDTO {
@NotNull public boolean on; @NotNull public boolean on;
@Override @Override
public Condition<?> toModel() { public Condition<?> toModel() {
BooleanCondition<?> t = new BooleanCondition<>(); BooleanCondition t = new BooleanCondition();
t.setDeviceId(this.deviceId); t.setDeviceId(this.deviceId);
t.setOn(this.on); t.setOn(this.on);
return t; return t;
} }
} }
public class RangeConditionDTO extends ConditionDTO { public static class RangeConditionDTO extends ConditionDTO {
@NotNull RangeCondition.Operator operator; @NotNull RangeCondition.Operator operator;
@NotNull double range; @NotNull double range;
@Override @Override
public Condition<?> toModel() { public Condition<?> toModel() {
RangeCondition<?> t = new RangeCondition<>(); RangeCondition t = new RangeCondition();
t.setDeviceId(this.deviceId); t.setDeviceId(this.deviceId);
t.setOperator(this.operator); t.setOperator(this.operator);
t.setRange(this.range); t.setRange(this.range);
@ -96,7 +96,7 @@ public class AutomationFastUpdateRequest {
} }
} }
public class ThermostatConditionDTO extends ConditionDTO { public static class ThermostatConditionDTO extends ConditionDTO {
@NotNull ThermostatCondition.Operator operator; @NotNull ThermostatCondition.Operator operator;
@NotNull private Thermostat.Mode mode; @NotNull private Thermostat.Mode mode;

View file

@ -4,8 +4,7 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
@Entity @Entity
public class BooleanCondition<D extends Device & BooleanTriggerable> public class BooleanCondition extends Condition<BooleanTriggerable> {
extends Condition<D> { // TODO add interface to type constraints
@Column(name = "switchable_on") @Column(name = "switchable_on")
private boolean on; private boolean on;
@ -22,10 +21,6 @@ public class BooleanCondition<D extends Device & BooleanTriggerable>
this.on = on; this.on = on;
} }
public boolean check(boolean on) {
return this.on == on;
}
@Override @Override
public boolean triggered() { public boolean triggered() {
return this.getDevice().readTriggerState() == isOn(); return this.getDevice().readTriggerState() == isOn();

View file

@ -3,8 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List; import java.util.List;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
public interface BooleanConditionRepository public interface BooleanConditionRepository extends ConditionRepository<BooleanCondition> {
extends ConditionRepository<BooleanCondition<? extends Device>> {
List<BooleanCondition<?>> findAllByAutomationId(@Param("automationId") long automationId); List<BooleanCondition> findAllByAutomationId(@Param("automationId") long automationId);
} }

View file

@ -10,10 +10,9 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
@Entity @Entity
public abstract class Condition<D extends Device> { public abstract class Condition<D> {
@Transient private String kind; @Transient private String kind;
@ -41,7 +40,6 @@ public abstract class Condition<D extends Device> {
* from a REST call. * from a REST call.
*/ */
@Column(name = "device_id", nullable = false) @Column(name = "device_id", nullable = false)
@NotNull
private Long deviceId; private Long deviceId;
@ManyToOne @ManyToOne
@ -50,7 +48,6 @@ public abstract class Condition<D extends Device> {
private Automation automation; private Automation automation;
@Column(name = "automation_id", nullable = false) @Column(name = "automation_id", nullable = false)
@NotNull
private Long automationId; private Long automationId;
public abstract boolean triggered(); public abstract boolean triggered();

View file

@ -7,10 +7,10 @@ import javax.persistence.Entity;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@Entity @Entity
public class RangeCondition<D extends Device & RangeTriggerable> extends Condition<D> { public class RangeCondition extends Condition<RangeTriggerable> {
public RangeCondition() { public RangeCondition() {
super("booleanCondition"); super("rangeCondition");
} }
public enum Operator { public enum Operator {
@ -26,7 +26,6 @@ public class RangeCondition<D extends Device & RangeTriggerable> extends Conditi
GREATER_EQUAL GREATER_EQUAL
} }
@NotNull
@Column(nullable = false) @Column(nullable = false)
private RangeCondition.Operator operator; private RangeCondition.Operator operator;

View file

@ -3,8 +3,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List; import java.util.List;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
public interface RangeConditionRepository public interface RangeConditionRepository extends ConditionRepository<RangeCondition> {
extends ConditionRepository<RangeCondition<? extends Device>> { List<RangeCondition> findAllByAutomationId(@Param("automationId") long automationId);
List<RangeCondition<?>> findAllByAutomationId(@Param("automationId") long automationId);
} }