Merge branch '71-when-creating-a-scene-users-can-t-copy-the-configuration-from-another-scene' into 'dev'

Resolve "When creating a scene, users can't copy the configuration from another scene."

Closes #71

See merge request sa4-2020/the-sanmarinoes/backend!111
This commit is contained in:
Claudio Maggioni 2020-05-04 16:14:22 +02:00
commit 1ddc8f5ba6
5 changed files with 52 additions and 4 deletions

View file

@ -72,6 +72,24 @@ public class SceneController {
return sceneService.apply(newScene, principal.getName(), false);
}
@PostMapping("/{id}/copyFrom/{copyId}")
public @ResponseBody List<State<?>> 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)

View file

@ -25,4 +25,11 @@ public class DimmableState<T extends Dimmable> extends State<T> {
public void apply() {
getDevice().readStateAndSet(this);
}
@Override
protected State<T> copy() {
final DimmableState<T> d = new DimmableState<>();
d.setIntensity(intensity);
return d;
}
}

View file

@ -45,6 +45,16 @@ public abstract class State<D extends OutputDevice> {
/** 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<D> copy();
public State<D> copyToSceneId(Long sceneId) {
final State<D> s = copy();
s.setDeviceId(this.deviceId);
s.setSceneId(sceneId);
return s;
}
public long getId() {
return id;
}

View file

@ -22,4 +22,11 @@ public class SwitchableState<T extends Switchable> extends State<T> {
public void apply() {
getDevice().readStateAndSet(this);
}
@Override
protected State<T> copy() {
final SwitchableState<T> d = new SwitchableState<>();
d.setOn(on);
return d;
}
}

View file

@ -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<Device> deviceRepository;
@Autowired private DeviceService deviceService;
@Autowired private StateRepository<State<?>> stateRepository;
public List<Device> apply(Scene newScene, String username, boolean fromTrigger) {
final List<Device> updated = new ArrayList<>();
@ -26,4 +24,12 @@ public class SceneService {
deviceService.populateComputedFields(updated);
return updated;
}
public List<State<?>> copyStates(Scene to, Scene from) {
final ArrayList<State<?>> states = new ArrayList<>();
for (final State<?> s : from.getStates()) {
states.add(stateRepository.save(s.copyToSceneId(to.getId())));
}
return states;
}
}