From c6041e0e0f82a5b16cb0f9b464f6c4454817da39 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 10 May 2020 21:44:10 +0200 Subject: [PATCH] Degenerified Conditions --- .../controller/BooleanConditionController.java | 10 +++++----- .../controller/RangeConditionController.java | 10 +++++----- .../smarthut/dto/AutomationFastUpdateRequest.java | 12 ++++++------ .../smarthut/models/BooleanCondition.java | 7 +------ .../smarthut/models/BooleanConditionRepository.java | 5 ++--- .../sa4/sanmarinoes/smarthut/models/Condition.java | 5 +---- .../sanmarinoes/smarthut/models/RangeCondition.java | 5 ++--- .../smarthut/models/RangeConditionRepository.java | 6 ++---- 8 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java index fac99b5..ddcb880 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java @@ -25,11 +25,11 @@ public class BooleanConditionController { @Autowired BooleanConditionRepository booleanConditionRepository; @GetMapping("/{automationId}") - public List> getAll(@PathVariable long automationId) { + public List getAll(@PathVariable long automationId) { return booleanConditionRepository.findAllByAutomationId(automationId); } - private BooleanCondition save(BooleanCondition newRL, BooleanConditionSaveRequest s) { + private BooleanCondition save(BooleanCondition newRL, BooleanConditionSaveRequest s) { newRL.setDeviceId(s.getDeviceId()); newRL.setAutomationId(s.getAutomationId()); newRL.setOn(s.isOn()); @@ -38,13 +38,13 @@ public class BooleanConditionController { } @PostMapping - public BooleanCondition create( + public BooleanCondition create( @Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) { - return save(new BooleanCondition<>(), booleanTriggerSaveRequest); + return save(new BooleanCondition(), booleanTriggerSaveRequest); } @PutMapping - public BooleanCondition update( + public BooleanCondition update( @Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) throws NotFoundException { return save( diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java index 25491b0..5055c61 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java @@ -25,11 +25,11 @@ public class RangeConditionController { @Autowired RangeConditionRepository rangeConditionRepository; @GetMapping("/{automationId}") - public List> getAll(@PathVariable long automationId) { + public List getAll(@PathVariable long automationId) { return rangeConditionRepository.findAllByAutomationId(automationId); } - private RangeCondition save(RangeCondition newRL, RangeConditionSaveRequest s) { + private RangeCondition save(RangeCondition newRL, RangeConditionSaveRequest s) { newRL.setDeviceId(s.getDeviceId()); newRL.setAutomationId(s.getAutomationId()); newRL.setOperator(s.getOperator()); @@ -39,13 +39,13 @@ public class RangeConditionController { } @PostMapping - public RangeCondition create( + public RangeCondition create( @Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) { - return save(new RangeCondition<>(), booleanTriggerSaveRequest); + return save(new RangeCondition(), booleanTriggerSaveRequest); } @PutMapping - public RangeCondition update( + public RangeCondition update( @Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) throws NotFoundException { return save( diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java index 5283ec3..bb90896 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationFastUpdateRequest.java @@ -62,33 +62,33 @@ public class AutomationFastUpdateRequest { } } - public abstract class ConditionDTO { + public abstract static class ConditionDTO { @NotNull public long deviceId; public abstract Condition toModel(); } - public class BooleanConditionDTO extends ConditionDTO { + public static class BooleanConditionDTO extends ConditionDTO { @NotNull public boolean on; @Override public Condition toModel() { - BooleanCondition t = new BooleanCondition<>(); + BooleanCondition t = new BooleanCondition(); t.setDeviceId(this.deviceId); t.setOn(this.on); return t; } } - public class RangeConditionDTO extends ConditionDTO { + public static class RangeConditionDTO extends ConditionDTO { @NotNull RangeCondition.Operator operator; @NotNull double range; @Override public Condition toModel() { - RangeCondition t = new RangeCondition<>(); + RangeCondition t = new RangeCondition(); t.setDeviceId(this.deviceId); t.setOperator(this.operator); 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 private Thermostat.Mode mode; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java index 5b3f1e3..22ac7a2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java @@ -4,8 +4,7 @@ import javax.persistence.Column; import javax.persistence.Entity; @Entity -public class BooleanCondition - extends Condition { // TODO add interface to type constraints +public class BooleanCondition extends Condition { @Column(name = "switchable_on") private boolean on; @@ -22,10 +21,6 @@ public class BooleanCondition this.on = on; } - public boolean check(boolean on) { - return this.on == on; - } - @Override public boolean triggered() { return this.getDevice().readTriggerState() == isOn(); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java index c564c41..539eb6d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java @@ -3,8 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import java.util.List; import org.springframework.data.repository.query.Param; -public interface BooleanConditionRepository - extends ConditionRepository> { +public interface BooleanConditionRepository extends ConditionRepository { - List> findAllByAutomationId(@Param("automationId") long automationId); + List findAllByAutomationId(@Param("automationId") long automationId); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java index 859730b..08b2db8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java @@ -10,10 +10,9 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Transient; -import javax.validation.constraints.NotNull; @Entity -public abstract class Condition { +public abstract class Condition { @Transient private String kind; @@ -41,7 +40,6 @@ public abstract class Condition { * from a REST call. */ @Column(name = "device_id", nullable = false) - @NotNull private Long deviceId; @ManyToOne @@ -50,7 +48,6 @@ public abstract class Condition { private Automation automation; @Column(name = "automation_id", nullable = false) - @NotNull private Long automationId; public abstract boolean triggered(); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java index 7ad1a8d..f3e4bce 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java @@ -7,10 +7,10 @@ import javax.persistence.Entity; import javax.validation.constraints.NotNull; @Entity -public class RangeCondition extends Condition { +public class RangeCondition extends Condition { public RangeCondition() { - super("booleanCondition"); + super("rangeCondition"); } public enum Operator { @@ -26,7 +26,6 @@ public class RangeCondition extends Conditi GREATER_EQUAL } - @NotNull @Column(nullable = false) private RangeCondition.Operator operator; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java index 1ed6aa3..451d4a7 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java @@ -3,8 +3,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import java.util.List; import org.springframework.data.repository.query.Param; -public interface RangeConditionRepository - extends ConditionRepository> { - - List> findAllByAutomationId(@Param("automationId") long automationId); +public interface RangeConditionRepository extends ConditionRepository { + List findAllByAutomationId(@Param("automationId") long automationId); }