package ch.usi.inf.sa4.sanmarinoes.smarthut.service; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class SceneService { @Autowired private DeviceRepository deviceRepository; @Autowired private DevicePopulationService devicePopulationService; @Autowired private DevicePropagationService devicePropagationService; @Autowired private StateRepository> stateRepository; private List copyStatesToDevices(Scene fromScene) { final List updated = new ArrayList<>(fromScene.getStates().size()); for (final State s : fromScene.getStates()) { s.apply(); updated.add(s.getDevice()); } devicePopulationService.populateComputedFields(updated); return updated; } public List apply(Scene newScene, String username, boolean fromTrigger) { List updated = copyStatesToDevices(newScene); devicePropagationService.saveAllAsOwner(updated, username, true, fromTrigger); return updated; } public List applyAsGuest(Scene newScene, String username, Long hostId) { List updated = copyStatesToDevices(newScene); devicePropagationService.saveAllAsGuestSceneApplication(updated, username, hostId); 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; } }