From b72f527b0de4023225ca31e7e48e5d1cc8f5ccd0 Mon Sep 17 00:00:00 2001 From: omenem Date: Fri, 8 May 2020 17:44:30 +0200 Subject: [PATCH] Added conditions in PUT automation/fast --- .../controller/AutomationController.java | 12 ++++ .../dto/AutomationFastUpdateRequest.java | 63 +++++++++++++++++++ .../smarthut/models/Condition.java | 29 +-------- .../smarthut/models/ConditionRepository.java | 4 ++ 4 files changed, 81 insertions(+), 27 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java index cfad787..19d828a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java @@ -28,6 +28,7 @@ public class AutomationController { @Autowired private AutomationRepository automationRepository; @Autowired private ScenePriorityRepository sceneRepository; @Autowired private TriggerRepository> triggerRepository; + @Autowired private ConditionRepository> conditionRepository; @Autowired private UserRepository userService; @GetMapping @@ -87,6 +88,7 @@ public class AutomationController { triggerRepository.deleteAllByAutomationId(a.getId()); sceneRepository.deleteAllByAutomationId(a.getId()); + conditionRepository.deleteAllByAutomationId(a.getId()); Iterable> tt = triggerRepository.saveAll( @@ -103,10 +105,20 @@ public class AutomationController { .map(t -> t.setAutomationId(a.getId())) .collect(Collectors.toList())); + Iterable> cc = + conditionRepository.saveAll( + req.getConditions() + .stream() + .map(AutomationFastUpdateRequest.ConditionDTO::toModel) + .map(t -> t.setAutomationId(a.getId())) + .collect(Collectors.toList())); + a.getScenes().clear(); a.getTriggers().clear(); + a.getConditions().clear(); ss.forEach(t -> a.getScenes().add(t)); tt.forEach(t -> a.getTriggers().add(t)); + cc.forEach(t -> a.getConditions().add(t)); return a; } 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 58ce46c..61ced2a 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 @@ -1,8 +1,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Condition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger; import java.util.List; import javax.validation.constraints.Min; @@ -57,8 +62,58 @@ public class AutomationFastUpdateRequest { } } + public abstract class ConditionDTO { + @NotNull public long deviceId; + + public abstract Condition toModel(); + } + + public class BooleanConditionDTO extends ConditionDTO { + + @NotNull public boolean on; + + @Override + public Condition toModel() { + BooleanCondition t = new BooleanCondition<>(); + t.setDeviceId(this.deviceId); + t.setOn(this.on); + return t; + } + } + + public class RangeConditionDTO extends ConditionDTO { + + @NotNull RangeCondition.Operator operator; + @NotNull double range; + + @Override + public Condition toModel() { + RangeCondition t = new RangeCondition<>(); + t.setDeviceId(this.deviceId); + t.setOperator(this.operator); + t.setRange(this.range); + return t; + } + } + + public class ThermostatConditionDTO extends ConditionDTO { + + @NotNull ThermostatCondition.Operator operator; + @NotNull private Thermostat.Mode mode; + + @Override + public Condition toModel() { + ThermostatCondition t = new ThermostatCondition(); + t.setDeviceId(this.deviceId); + t.setOperator(this.operator); + t.setMode(this.mode); + return t; + } + } + @NotNull private List scenes; @NotNull private List triggers; + @NotNull private List conditions; @NotNull private long id; @NotNull @NotEmpty private String name; @@ -94,4 +149,12 @@ public class AutomationFastUpdateRequest { public void setId(long id) { this.id = id; } + + public List getConditions() { + return conditions; + } + + public void setConditions(List conditions) { + this.conditions = conditions; + } } 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 be917b8..859730b 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 @@ -31,16 +31,6 @@ public abstract class Condition { @ApiModelProperty(hidden = true) private long id; - @ManyToOne - @JoinColumn(name = "user_id", updatable = false, insertable = false) - @GsonExclude - private User user; - - @NotNull - @Column(name = "user_id", nullable = false) - @GsonExclude - private Long userId; - @ManyToOne(targetEntity = Device.class) @JoinColumn(name = "device_id", updatable = false, insertable = false) @GsonExclude @@ -73,22 +63,6 @@ public abstract class Condition { this.id = id; } - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - - public Long getUserId() { - return userId; - } - - public void setUserId(Long userId) { - this.userId = userId; - } - public D getDevice() { return device; } @@ -117,7 +91,8 @@ public abstract class Condition { return automationId; } - public void setAutomationId(Long automationId) { + public Condition setAutomationId(Long automationId) { this.automationId = automationId; + return this; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java index cb6c1c4..2f7a1cc 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import java.util.List; +import javax.transaction.Transactional; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; @@ -9,4 +10,7 @@ public interface ConditionRepository> extends CrudReposit List findAllByDeviceId(@Param("deviceId") long deviceId); List findAllByAutomationId(@Param("automationId") long automationId); + + @Transactional + void deleteAllByAutomationId(@Param("automationId") long automationId); }