package com.aemreunal.service;

/*
 ***************************
 * Copyright (c) 2014      *
 *                         *
 * This code belongs to:   *
 *                         *
 * @author Ahmet Emre Ünal *
 * S001974                 *
 *                         *
 * aemreunal@gmail.com     *
 * emre.unal@ozu.edu.tr    *
 *                         *
 * aemreunal.com           *
 ***************************
 */

import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.aemreunal.config.GlobalSettings;
import com.aemreunal.domain.Project;
import com.aemreunal.domain.Scenario;
import com.aemreunal.exception.project.ProjectNotFoundException;
import com.aemreunal.exception.scenario.ScenarioNotFoundException;
import com.aemreunal.repository.scenario.ScenarioRepo;

@Transactional
@Service
public class ScenarioService {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private ScenarioRepo scenarioRepo;

    public Scenario save(String username, Long projectId, Scenario scenario) throws ConstraintViolationException {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Saving scenario with ID = \'" + scenario.getScenarioId() + "\'");
        }
        // Even though the 'project' variable is only used inside the if-clause,
        // the Project is found no matter what to ensure it exists and legitimate.
        Project project = projectService.findProjectById(username, projectId);
        if (scenario.getProject() == null) {
            // This means it hasn't been saved yet
            scenario.setProject(project);
        }
        return scenarioRepo.save(scenario);
    }

    public Scenario getScenario(String username, Long projectId, Long scenarioId) throws ScenarioNotFoundException, ProjectNotFoundException {
        if (GlobalSettings.DEBUGGING) {
            System.out.println("Finding scenario with ID = \'" + scenarioId + "\' in project = \'" + projectId + "\'");
        }
        Project project = projectService.findProjectById(username, projectId);
        Scenario scenario = scenarioRepo.findByScenarioIdAndProject(scenarioId, project);
        if(scenario == null) {
            throw new ScenarioNotFoundException(scenarioId);
        }
        return scenario;
    }

}

@@ -410,16 +410,67 @@
 **%0A */%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -511,24 +511,24 @@
 nException;%0A
-
 import org.s
@@ -734,16 +734,93 @@
 ttings;%0A
+import com.aemreunal.domain.Beacon;%0Aimport com.aemreunal.domain.BeaconGroup;%0A
 import c
@@ -890,73 +890,8 @@
 io;%0A
-import com.aemreunal.exception.project.ProjectNotFoundException;%0A
 impo
@@ -2001,34 +2001,8 @@
 tion
-, ProjectNotFoundException
  %7B%0A 
@@ -2473,10 +2473,1149 @@
 %0A    %7D%0A%0A
+    public List%3CBeacon%3E getBeaconsInScenario(String username, Long projectId, Long scenarioId) %7B%0A        Scenario scenario = this.getScenario(username, projectId, scenarioId);%0A        List%3CBeacon%3E beacons = new ArrayList%3CBeacon%3E();%0A        for (Beacon beacon : scenario.getBeacons()) %7B%0A            beacons.add(beacon);%0A        %7D%0A        return beacons;%0A    %7D%0A%0A    public List%3CBeaconGroup%3E getBeaconGroupsInScenario(String username, Long projectId, Long scenarioId) %7B%0A        Scenario scenario = this.getScenario(username, projectId, scenarioId);%0A        List%3CBeaconGroup%3E beaconGroups = new ArrayList%3CBeaconGroup%3E();%0A        for (BeaconGroup beaconGroup : scenario.getBeaconGroups()) %7B%0A            beaconGroups.add(beaconGroup);%0A        %7D%0A        return beaconGroups;%0A    %7D%0A%0A    public Scenario delete(String username, Long projectId, Long scenarioId) %7B%0A        if (GlobalSettings.DEBUGGING) %7B%0A            System.out.println(%22Deleting scenario with ID = %5C'%22 + scenarioId + %22%5C'%22);%0A        %7D%0A        Scenario scenario = this.getScenario(username, projectId, scenarioId);%0A        scenarioRepo.delete(scenario);%0A        return scenario;%0A    %7D%0A
 %7D%0A

Add methods