/**
 * Controller for scenario editor
 */
angular.module('lcaApp.scenario.edit',
    ['ui.router', 'lcaApp.resources.service', 'lcaApp.status.service'])
    .controller('ScenarioEditController',
    ['$scope', '$state', '$stateParams', '$q', 'StatusService',
     'ScenarioService', 'FragmentService',
        function ($scope, $state, $stateParams, $q, StatusService,
                  ScenarioService, FragmentService) {
            var existingScenario = null;    // Existing scenario resource, updated on save.

            /**
             * Action for Save button. Create new scenario or update existing scenario.
             * Setting up a new scenario takes some time, so display the spinner while
             * waiting for a response.
             */
            $scope.save = function () {
                if ( $scope.form.$valid ) {
                    StatusService.startWaiting();
                    if (existingScenario) {
                        angular.copy($scope.scenario, existingScenario);
                        ScenarioService.update( {scenarioID: existingScenario.scenarioID}, existingScenario,
                                                handleSuccess, StatusService.handleFailure);
                    } else {
                        ScenarioService.create(null, $scope.scenario, handleSuccess, StatusService.handleFailure);
                    }
                }
            };

            /**
             * Action for Cancel button. Go back to previous state
             */
            $scope.revertChanges = function () {
                goBack();
            };

            function goBack() {
                if ( $stateParams.scenarioID) {
                    $state.go('^');
                } else {
                    $state.go('home');
                }
            }

            function handleSuccess() {
                StatusService.stopWaiting();
                goBack();
            }

            function setScope() {
                StatusService.stopWaiting();
                $scope.fragments = FragmentService.getAll();
                if ($stateParams.scenarioID) {
                    existingScenario = ScenarioService.get($stateParams.scenarioID);
                    if (existingScenario) {
                        $scope.scenario = angular.copy(existingScenario);
                    }
                }
                else {
                    // Create scenario
                    $scope.scenario = { name: "", activityLevel: 1, topLevelFragmentID: null};

                }
            }

            StatusService.startWaiting();
            $q.all([ScenarioService.load(), FragmentService.load()])
                .then(setScope, StatusService.handleFailure);

        }
     ]
);
@@ -2298,37 +2298,452 @@
 o = 
-angular.copy(existingScenario
+%7B%0A                            scenarioID: existingScenario.scenarioID,%0A                            name: existingScenario.name,%0A                            activityLevel: existingScenario.activityLevel,%0A                            topLevelFragmentID: existingScenario.topLevelFragmentID%0A                        %7D;%0A                    %7D else %7B%0A                        StatusService.handleFailure(%22Invalid scenarioID : %22 + $stateParams.scenarioID
 );%0A 

Update scenario: omit properties that are generated by back-end (#199)