<commit_before>class DesignController < ApplicationController
  before_filter :find_assembly
  before_filter :find_latest_release, :only => [:show, :extract, :load]
  before_filter :check_open_release, :only => [:load]

  def show
    respond_to do |format|
      format.html do
        platforms = Cms::DjRelation.all(:params => {:ciId            => @assembly.ciId,
                                                    :direction       => 'from',
                                                    :targetClassName => 'catalog.Platform',
                                                    :relationName    => 'base.ComposedOf'})
        @platforms = platforms.map(&:toCi)
        @diagram = prepare_platforms_diagram(platforms)

        render :action => :show
      end
    end
  end

  def extract
    respond_to do |format|
      format.json do
        render :json => export_design
      end

      format.yaml do
        render :text => export_design.to_yaml, :content_type => 'text/data_string'
      end
    end
  end

  def load
    if request.put?
      data = nil
      data_file = params[:data_file]
      @data_string = (data_file && data_file.read).presence || params[:data]
      begin
        data = YAML.load(@data_string)
      rescue
        begin
          data = JSON.parse(@data_string)
        rescue
        end
      end

      if data.present?
        import_data, @errors = prepare_import_design_data(data)

        if import_data
          ok, message = Transistor.import_design(@assembly, import_data)
          @errors = [message] unless ok
        end
      else
        ok = false
        @errors = ['Please specify valid design coonfguration in YAML or JSON format.']
      end

      respond_to do |format|
        format.html do
          # render :text => (import_data || errors).to_yaml, :content_type => 'text/data_string'
          if ok
            flash[:notice] = 'Successfully loaded design.'
            redirect_to assembly_design_url(@assembly)
          end
        end

        format.json {ok ? show : render_json_ci_response(false, nil, @errors)}
      end
    end
  end

  def diagram
    send_data(prepare_platforms_diagram, :type => 'image/svg+xml', :disposition => 'inline')
  end


  private

  def find_assembly
    @assembly = locate_assembly(params[:assembly_id])
  end

  def find_latest_release
    @release = Cms::Release.latest(:nsPath => assembly_ns_path(@assembly))
  end

  def check_open_release
    if @release && @release.releaseState == 'open'
      message = 'Design load is not allowed when there is an open release. Please commit or discard current release before proceeding with design loading.'
      respond_to do |format|
        format.html do
          flash.now[:error] = message
          show
        end

        format.json {render_json_ci_response(false, nil, [message])}
      end
    end
  end<commit_msg>Fixes for json responders for extract/load . - Issue #39.<commit_after>class DesignController < ApplicationController
  before_filter :find_assembly
  before_filter :find_latest_release, :only => [:show, :extract, :load]
  before_filter :check_open_release, :only => [:load]

  def show
    respond_to do |format|
      format.html do
        platforms = Cms::DjRelation.all(:params => {:ciId            => @assembly.ciId,
                                                    :direction       => 'from',
                                                    :targetClassName => 'catalog.Platform',
                                                    :relationName    => 'base.ComposedOf'})
        @platforms = platforms.map(&:toCi)
        @diagram = prepare_platforms_diagram(platforms)

        render :action => :show
      end
    end
  end

  def extract
    respond_to do |format|
      format.json do
        render :json => export_design
      end

      format.yaml do
        render :text => export_design.to_yaml, :content_type => 'text/data_string'
      end
    end
  end

  def load
    if request.put?
      data = nil
      data_file = params[:data_file]
      @data_string = (data_file && data_file.read).presence || params[:data]
      begin
        data = YAML.load(@data_string)
      rescue
        begin
          data = JSON.parse(@data_string)
        rescue
        end
      end

      if data.present?
        import_data, @errors = prepare_import_design_data(data)

        if import_data
          ok, message = Transistor.import_design(@assembly, import_data)
          @errors = [message] unless ok
        end
      else
        ok = false
        @errors = ['Please specify valid design coonfguration in YAML or JSON format.']
      end

      respond_to do |format|
        format.html do
          # render :text => (import_data || errors).to_yaml, :content_type => 'text/data_string'
          if ok
            flash[:notice] = 'Successfully loaded design.'
            redirect_to assembly_design_url(@assembly)
          end
        end

        format.json {ok ? render(:json => {}) : render(:json => {:errors => @errors}, :status => :unprocessable_entity)}
      end
    end
  end

  def diagram
    send_data(prepare_platforms_diagram, :type => 'image/svg+xml', :disposition => 'inline')
  end


  private

  def find_assembly
    @assembly = locate_assembly(params[:assembly_id])
  end

  def find_latest_release
    @release = Cms::Release.latest(:nsPath => assembly_ns_path(@assembly))
  end

  def check_open_release
    if @release && @release.releaseState == 'open'
      message = 'Design load is not allowed when there is an open release. Please commit or discard current release before proceeding with design loading.'
      respond_to do |format|
        format.html do
          flash.now[:error] = message
          show
        end

        format.json {render :json => {:errors => [message]}, :status => :unprocessable_entity}
      end
    end
  end
{'fraction_non_alphanumeric': 0.09328358208955224, 'fraction_numerical': 0.00033921302578018993, 'mean_word_length': 2.0665626625065, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501360', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1606, 'n_words': 433}