From 16d1af2b368c6dd05a856c48f3056baa1250f4f7 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Mon, 4 May 2020 16:10:17 +0200 Subject: [PATCH] Scene copy implemented --- .../smarthut/controller/SceneController.java | 18 ++++++++++++++++++ .../smarthut/models/DimmableState.java | 7 +++++++ .../sa4/sanmarinoes/smarthut/models/State.java | 10 ++++++++++ .../smarthut/models/SwitchableState.java | 7 +++++++ .../smarthut/service/SceneService.java | 14 ++++++++++---- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java index c795049..a11b40e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java @@ -72,6 +72,24 @@ public class SceneController { return sceneService.apply(newScene, principal.getName(), false); } + @PostMapping("/{id}/copyFrom/{copyId}") + public @ResponseBody List> copy( + @PathVariable("id") long id, + @PathVariable("copyId") long copyId, + final Principal principal) + throws NotFoundException { + final Scene scene = + sceneRepository + .findByIdAndUsername(id, principal.getName()) + .orElseThrow(NotFoundException::new); + final Scene copyFrom = + sceneRepository + .findByIdAndUsername(copyId, principal.getName()) + .orElseThrow(NotFoundException::new); + + return sceneService.copyStates(scene, copyFrom); + } + @PutMapping("/{id}") public @ResponseBody Scene update( @PathVariable("id") long id, @RequestBody SceneSaveRequest s, final Principal principal) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java index 7c0b8d2..dff0143 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java @@ -25,4 +25,11 @@ public class DimmableState extends State { public void apply() { getDevice().readStateAndSet(this); } + + @Override + protected State copy() { + final DimmableState d = new DimmableState<>(); + d.setIntensity(intensity); + return d; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java index 01398b8..31cd69c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java @@ -45,6 +45,16 @@ public abstract class State { /** Sets the state of the connected device to the state represented by this object. */ public abstract void apply(); + /** Creates a perfect copy of this state, except for the id field and the sceneId/scene */ + protected abstract State copy(); + + public State copyToSceneId(Long sceneId) { + final State s = copy(); + s.setDeviceId(this.deviceId); + s.setSceneId(sceneId); + return s; + } + public long getId() { return id; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java index 67b3118..be21b6e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java @@ -22,4 +22,11 @@ public class SwitchableState extends State { public void apply() { getDevice().readStateAndSet(this); } + + @Override + protected State copy() { + final SwitchableState d = new SwitchableState<>(); + d.setOn(on); + return d; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java index 04a8877..72abf08 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java @@ -1,9 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; -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.Scene; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -14,6 +11,7 @@ public class SceneService { @Autowired private DeviceRepository deviceRepository; @Autowired private DeviceService deviceService; + @Autowired private StateRepository> stateRepository; public List apply(Scene newScene, String username, boolean fromTrigger) { final List updated = new ArrayList<>(); @@ -26,4 +24,12 @@ public class SceneService { deviceService.populateComputedFields(updated); return updated; } + + public List> copyStates(Scene to, Scene from) { + final ArrayList> states = new ArrayList<>(); + for (final State s : from.getStates()) { + states.add(stateRepository.save(s.copyToSceneId(to.getId()))); + } + return states; + } }