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 445bb83..7abd499 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 @@ -6,10 +6,9 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import java.security.Principal; -import java.util.Collection; import java.util.List; -import java.util.stream.Collectors; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -31,24 +30,15 @@ public class AutomationController { @Autowired private AutomationRepository automationRepository; @Autowired private SceneRepository sceneRepository; @Autowired private ScenePriorityRepository scenePriorityRepository; + @Autowired private UserRepository userService; @GetMapping public List getAll( - @RequestParam(value = "hostId", required = false) Long hostId, final Principal user) + @RequestParam(value = "hostId", required = false) Long hostId, + final Principal principal) throws NotFoundException { - return sceneRepository - .findByUsername(user.getName()) - .stream() - .map(s -> scenePriorityRepository.findAllBySceneId(s.getId())) - .flatMap(Collection::stream) - .distinct() - .map( - t -> - automationRepository - .findById(t.getAutomationId()) - .orElseThrow(IllegalStateException::new)) - .distinct() - .collect(Collectors.toList()); + final Long userId = userService.findByUsername(principal.getName()).getId(); + return automationRepository.findAllByUserId(userId); } @GetMapping("/{id}") @@ -56,25 +46,31 @@ public class AutomationController { return automationRepository.findById(id).orElseThrow(NotFoundException::new); } - private Automation save(Automation newRL, AutomationSaveRequest s) { + private Automation save(Automation newRL, AutomationSaveRequest s, Principal principal) { + + final Long userId = userService.findByUsername(principal.getName()).getId(); newRL.setName(s.getName()); + newRL.setUserId(userId); return automationRepository.save(newRL); } @PostMapping - public Automation create(@Valid @RequestBody AutomationSaveRequest automationSaveRequest) { - return save(new Automation(), automationSaveRequest); + public Automation create( + @Valid @RequestBody AutomationSaveRequest automationSaveRequest, Principal principal) { + return save(new Automation(), automationSaveRequest, principal); } @PutMapping - public Automation update(@Valid @RequestBody AutomationSaveRequest automation) + public Automation update( + @Valid @RequestBody AutomationSaveRequest automation, Principal principal) throws NotFoundException { return save( automationRepository .findById(automation.getId()) .orElseThrow(NotFoundException::new), - automation); + automation, + principal); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java index 7043566..655ce4b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java @@ -29,7 +29,7 @@ public class RangeTriggerController { return rangeTriggerRepository.findAllByAutomationId(automationId); } - private RangeTrigger save(RangeTrigger newRL, RangeTriggerSaveRequest s) { + private RangeTrigger save(RangeTrigger newRL, RangeTriggerSaveRequest s) { newRL.setDeviceId(s.getDeviceId()); newRL.setAutomationId(s.getAutomationId()); newRL.setOperator(s.getOperator()); @@ -41,7 +41,7 @@ public class RangeTriggerController { @PostMapping public RangeTrigger create( @Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) { - return save(new RangeTrigger(), booleanTriggerSaveRequest); + return save(new RangeTrigger<>(), booleanTriggerSaveRequest); } @PutMapping diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java index d0bbb5a..1449727 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java @@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ScenePrioritySaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; +import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -23,9 +24,10 @@ public class ScenePriorityController { @Autowired ScenePriorityRepository scenePriorityRepository; - @GetMapping("/{id}") - public ScenePriority get(@PathVariable long id) throws NotFoundException { - return scenePriorityRepository.findById(id).orElseThrow(NotFoundException::new); + @GetMapping("/{automationId}") + public List getByAutomationId(@PathVariable long automationId) + throws NotFoundException { + return scenePriorityRepository.findAllByAutomationId(automationId); } private ScenePriority save(ScenePriority newRL, ScenePrioritySaveRequest s) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java index 27bda84..17d5bfc 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java @@ -17,6 +17,16 @@ public class Automation { @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; + @OneToMany(mappedBy = "automation", orphanRemoval = true) @GsonExclude private Set> triggers = new HashSet<>(); @@ -50,4 +60,20 @@ public class Automation { public void setName(String name) { this.name = name; } + + 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; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java index b8b2ac2..a54a9ee 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java @@ -1,5 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.List; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; -public interface AutomationRepository extends CrudRepository {} +public interface AutomationRepository extends CrudRepository { + + List findAllByUserId(@Param("userId") long userId); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java index f52e5ca..fde9b3b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java @@ -2,11 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; @Entity -@Inheritance(strategy = InheritanceType.JOINED) public class BooleanTrigger extends Trigger { @Column(name = "switchable_on") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java index ab49dd0..6ae3150 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import java.util.Set; import javax.persistence.*; import javax.validation.constraints.Max; @@ -23,6 +24,7 @@ public class Dimmable extends Switchable implements RangeTriggerable { @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) @GsonExclude + @SocketGsonExclude private Set dimmers; /** The light intensity value. Goes from 0 (off) to 100 (on) */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java index 9b3d627..e920d2f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import java.util.HashSet; import java.util.Set; import javax.persistence.*; @@ -15,6 +16,7 @@ public abstract class Dimmer extends InputDevice { @ManyToMany(cascade = CascadeType.DETACH) @GsonExclude + @SocketGsonExclude @JoinTable( name = "dimmer_dimmable", joinColumns = @JoinColumn(name = "dimmer_id"), diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java index a03781d..db94966 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java @@ -3,12 +3,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import com.google.gson.annotations.SerializedName; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; import javax.validation.constraints.NotNull; @Entity -@Inheritance(strategy = InheritanceType.JOINED) public class RangeTrigger extends Trigger { @Override diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java index 0652e5e..04586d9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java @@ -1,8 +1,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import io.swagger.annotations.ApiModelProperty; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @@ -13,6 +16,12 @@ import javax.validation.constraints.NotNull; @Entity public class ScenePriority { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id", updatable = false, nullable = false, unique = true) + @ApiModelProperty(hidden = true) + private long id; + @ManyToOne @JoinColumn(name = "automation_id", updatable = false, insertable = false) @GsonExclude @@ -32,8 +41,7 @@ public class ScenePriority { @GsonExclude private Scene scene; - @Id - @Column(name = "scene_id", nullable = false, updatable = false, unique = true) + @Column(name = "scene_id", nullable = false, updatable = false) @NotNull private Long sceneId; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java index 020a369..1d9b3c6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java @@ -9,4 +9,6 @@ public interface ScenePriorityRepository extends CrudRepository findAllBySceneId(@Param("sceneId") long sceneId); void deleteBySceneId(@Param("sceneId") long sceneId); + + List findAllByAutomationId(@Param("automationId") long automationId); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index 0768d6b..4b9ef3a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import java.util.HashSet; import java.util.Set; import javax.persistence.*; @@ -11,6 +12,7 @@ public class Switch extends InputDevice implements BooleanTriggerable { @ManyToMany(cascade = CascadeType.DETACH) @GsonExclude + @SocketGsonExclude @JoinTable( name = "switch_switchable", joinColumns = @JoinColumn(name = "switch_id"), diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index 7fbc64a..c7abc7e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import java.util.HashSet; import java.util.Set; import javax.persistence.*; @@ -15,6 +16,7 @@ public abstract class Switchable extends OutputDevice { @ManyToMany(mappedBy = "switchables", cascade = CascadeType.DETACH) @GsonExclude + @SocketGsonExclude private Set inputs = new HashSet<>(); protected Switchable(String kind) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java index bb4bef1..689c7ee 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java @@ -1,9 +1,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude; import com.google.gson.annotations.SerializedName; import java.math.BigDecimal; +import java.util.HashSet; +import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.OneToMany; import javax.persistence.Transient; import javax.validation.constraints.NotNull; @@ -34,10 +39,6 @@ public class Thermostat extends Switchable implements BooleanTriggerable { BigDecimal measured = this.getMeasuredTemperature(); BigDecimal target = this.getTargetTemperature(); - System.out.println("measured is"); - // System.out.println(measured.getClass().getName()); - System.out.println("target is"); - System.out.println(target.getClass().getName()); BigDecimal delta = target.subtract(measured); if (delta.abs().doubleValue() < 0.25) { @@ -85,9 +86,10 @@ public class Thermostat extends Switchable implements BooleanTriggerable { @Column private boolean useExternalSensors = false; - // @OneToMany(mappedBy = "scene", orphanRemoval = true) - // @GsonExclude - // private Set> triggers = new HashSet<>() + @OneToMany(mappedBy = "scene", orphanRemoval = true) + @GsonExclude + @SocketGsonExclude + private Set> triggers = new HashSet<>(); /** Creates a thermostat with a temperature sensor and its initial OFF state */ public Thermostat() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java index 1ef68f0..4e74b11 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java @@ -15,7 +15,7 @@ import javax.persistence.PreRemove; import javax.validation.constraints.NotNull; @Entity -@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class Trigger { public abstract boolean triggered(); @@ -26,7 +26,7 @@ public abstract class Trigger { @ApiModelProperty(hidden = true) private long id; - @ManyToOne(targetEntity = OutputDevice.class) + @ManyToOne(targetEntity = Device.class) @JoinColumn(name = "device_id", updatable = false, insertable = false) @GsonExclude private D device;