<commit_before>require 'zip'

class SimulationScenariosController < ApplicationController
  before_filter :load_simulation_scenario, except: [ :index, :create ]

  def index

  end

  def edit
    if @simulation_scenario.blank? or @simulation_scenario.user_id != @current_user.id
      flash[:error] = t('simulation_scenarios.not_owned_by', id: params[:id], user: @current_user.login)
      redirect_to simulations_path
    else
      @input_writers = SimulationInputWriter.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
      @executors = SimulationExecutor.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
      @output_readers = SimulationOutputReader.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
      @progress_monitors = SimulationProgressMonitor.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
    end
  end

  def update
    # TODO if necessary - validate simulation_input and simulation_binaries somehow

    if @simulation_scenario.blank? or @simulation_scenario.user_id != @current_user.id
      flash[:error] = t('simulation_scenarios.not_owned_by', id: params[:id], user: @current_user.login)
    else
      simulation_input =  params.include?(:simulation_input) ? Utils.parse_json_if_string(Utils.read_if_file(params[:simulation_input])) : nil
      simulation_scenario_params_validation(simulation_input)

      # simulation update
      if flash[:error].nil?
        @simulation_scenario.name = params[:simulation_name]
        @simulation_scenario.description = params[:simulation_description]
        @simulation_scenario.input_specification = simulation_input unless simulation_input.blank?
        @simulation_scenario.created_at = Time.now

        begin
          set_up_adapter('input_writer', @simulation_scenario, false)
          set_up_adapter('executor', @simulation_scenario)
          set_up_adapter('output_reader', @simulation_scenario, false)
          set_up_adapter('progress_monitor', @simulation_scenario, false)

          unless (binaries = params[:simulation_binaries]).blank?
            @simulation_scenario.set_simulation_binaries(binaries.original_filename, binaries.read)
          end

          @simulation_scenario.save

          flash[:notice] = t('simulation_scenarios.update.success', name: @simulation_scenario.name) if flash[:error].nil?
        rescue Exception => e
          flash[:error] = t('simulations.create.internal_error') unless flash[:error]
          Rails.logger.error("Exception occurred when setting up adapters or binaries: #{e}\n#{e.backtrace.join("\n")}")
        end
      end
    end

    respond_to do |format|
      format.json { render json: { status: (flash[:error].nil? ? 'ok' : 'error'), simulation_id: simulation.id.to_s } }
      format.html { redirect_to simulations_path }
    end
  end

  def create
  end

  def show
    render json: (
        if @simulation_scenario
          {status: 'ok', data: @simulation_scenario.to_h }
        else<commit_msg>SCAL-633 changed SecurityError handling manner<commit_after>require 'zip'

class SimulationScenariosController < ApplicationController
  before_filter :load_simulation_scenario, except: [ :index, :create ]

  def index

  end

  def edit
    if @simulation_scenario.blank? or @simulation_scenario.user_id != @current_user.id
      flash[:error] = t('simulation_scenarios.not_owned_by', id: params[:id], user: @current_user.login)
      redirect_to simulations_path
    else
      @input_writers = SimulationInputWriter.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
      @executors = SimulationExecutor.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
      @output_readers = SimulationOutputReader.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
      @progress_monitors = SimulationProgressMonitor.find_all_by_user_id(@current_user.id).map{|ex| [ex.name, ex._id]}.unshift(["None", nil])
    end
  end

  def update
    # TODO if necessary - validate simulation_input and simulation_binaries somehow

    if @simulation_scenario.blank? or @simulation_scenario.user_id != @current_user.id
      flash[:error] = t('simulation_scenarios.not_owned_by', id: params[:id], user: @current_user.login)
    else
      simulation_input =  params.include?(:simulation_input) ? Utils.parse_json_if_string(Utils.read_if_file(params[:simulation_input])) : nil
      simulation_scenario_params_validation(simulation_input)

      # simulation update
      if flash[:error].nil?
        @simulation_scenario.name = params[:simulation_name]
        @simulation_scenario.description = params[:simulation_description]
        @simulation_scenario.input_specification = simulation_input unless simulation_input.blank?
        @simulation_scenario.created_at = Time.now

        begin
          set_up_adapter('input_writer', @simulation_scenario, false)
          set_up_adapter('executor', @simulation_scenario)
          set_up_adapter('output_reader', @simulation_scenario, false)
          set_up_adapter('progress_monitor', @simulation_scenario, false)

          unless (binaries = params[:simulation_binaries]).blank?
            @simulation_scenario.set_simulation_binaries(binaries.original_filename, binaries.read)
          end

          @simulation_scenario.save

          flash[:notice] = t('simulation_scenarios.update.success', name: @simulation_scenario.name) if flash[:error].nil?
        rescue SecurityError => e
          raise e
        rescue Exception => e
          flash[:error] = t('simulations.create.internal_error') unless flash[:error]
          Rails.logger.error("Exception occurred when setting up adapters or binaries: #{e}\n#{e.backtrace.join("\n")}")
        end
      end
    end

    respond_to do |format|
      format.json { render json: { status: (flash[:error].nil? ? 'ok' : 'error'), simulation_id: simulation.id.to_s } }
      format.html { redirect_to simulations_path }
    end
  end

  def create
  end

  def show
    render json: (
        if @simulation_scenario
          {status: 'ok', data: @simulation_scenario.to_h }
        else
{'fraction_non_alphanumeric': 0.11358220810166798, 'fraction_numerical': 0.0004765687053216839, 'mean_word_length': 3.9419152276295133, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4369990', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1940, 'n_words': 342}