<commit_before>class ModelsController < ApplicationController    
  
  include WhiteListHelper
  include IndexPager
  include DotGenerator
  include Seek::AssetsCommon
  
  before_filter :login_required
  
  before_filter :pal_or_admin_required,:only=> [:create_model_metadata,:update_model_metadata,:delete_model_metadata ]
  
  before_filter :find_assets, :only => [ :index ]
  before_filter :find_and_auth, :except => [ :build,:index, :new, :create,:create_model_metadata,:update_model_metadata,:delete_model_metadata,:request_resource,:preview,:test_asset_url, :update_tags_ajax]
  before_filter :find_display_model, :only=>[:show,:download,:execute,:builder,:simulate,:submit_to_jws]
    
  before_filter :jws_enabled,:only=>[:builder,:simulate,:submit_to_jws]
  
  @@model_builder = Seek::JWS::OneStop.new
  
  # GET /models
  # GET /models.xml
  
  def new_version
    if (handle_data nil)
      
      comments = params[:revision_comment]
      @model.content_blob = ContentBlob.new(:tmp_io_object => @tmp_io_object, :url=>@data_url)
      @model.content_type = params[:model][:content_type]
      @model.original_filename = params[:model][:original_filename]
      
      respond_to do |format|
        create_new_version comments
        format.html {redirect_to @model }
      end
    else
      flash[:error]=flash.now[:error]
      redirect_to @model
    end
  end    
  
  def delete_model_metadata
    attribute=params[:attribute]
    if attribute=="model_type"
      delete_model_type params
    elsif attribute=="model_format"
      delete_model_format params
    end
  end
  
  def builder
    saved_file=params[:saved_file]
    error=nil
    begin
      if saved_file
        supported=true
        @params_hash,@attribution_annotations,@saved_file,@objects_hash,@error_keys = @@model_builder.saved_file_builder_content saved_file
      else
        supported = @@model_builder.is_supported?(@display_model)
        @params_hash,@attribution_annotations,@saved_file,@objects_hash,@error_keys = @@model_builder.builder_content @display_model if supported
      end
    rescue Exception=>e
      error=e
      logger.error "Error submitting to JWS Online OneStop - #{e.message}"
    end
    
    respond_to do |format|
      if error
        flash[:error]="JWS Online encountered a problem processing this model."
        format.html { redirect_to model_path(@model,:version=>@display_model.version)}
      elsif !supported
        flash[:error]="This model is of neither SBML or JWS Online (Dat) format so cannot be used with JWS Online"
        format.html { redirect_to model_path(@model,:version=>@display_model.version)}        
      else
        format.html
      end
    end
  end    

#  def annotator
#    respond_to do |format|
#      format.html
#    end
#  end

  def submit_to_jws
    following_action=params.delete("following_action")    
    error=nil
    begin
      if following_action == "annotate"
        @params_hash,@attribution_annotations,@species_annotations,@reaction_annotations,@search_results,@cached_annotations,@saved_file,@error_keys = @@model_builder.annotate params
      else
        @params_hash,@attribution_annotations,@saved_file,@objects_hash,@error_keys = @@model_builder.construct params
      end
    rescue Exception => e
      error=e
    end

    if (!error && @error_keys.empty?)

      if following_action == "simulate"
        begin
          @applet=@@model_builder.simulate @saved_file
        rescue Exception => e
          error=e
        end
      elsif following_action == "save_new_version"
        model_format=params.delete("saved_model_format") #only used for saving as a new version
        new_version_filename=params.delete("new_version_filename")
        new_version_comments=params.delete("new_version_comments")
        if model_format == "dat"
          url=@@model_builder.saved_dat_download_url @saved_file
        elsif model_format == "sbml"
          url=@@model_builder.sbml_download_url @saved_file
        end
        if url
          downloader=Seek::RemoteDownloader.new
          data_hash = downloader.get_remote_data url
          File.open(data_hash[:data_tmp_path],"r") do |f|
            @model.content_blob=ContentBlob.new(:data=>f.read)
          end
          @model.content_type=model_format=="sbml" ? "text/xml" : "text/plain"
          @model.original_filename=new_version_filename
        end
      end
    end


    respond_to do |format|
      if error
        flash[:error]="JWS Online encountered a problem processing this model."
        format.html { render :action=>"builder" }
      elsif @error_keys.empty? && following_action == "simulate"
        format.html {render :action=>"simulate",:layout=>"no_sidebar"}
      elsif @error_keys.empty? && following_action == "annotate"
        format.html {render :action=>"annotator"}
      elsif @error_keys.empty? && following_action == "save_new_version"
        create_new_version(new_version_comments)
        format.html {redirect_to  model_path(@model,:version=>@model.version) }
      else
        format.html { render :action=>"builder" }
      end      
    end
    
  end
  
  def simulate
    error=nil
    begin
      supported = @@model_builder.is_supported?(@display_model)
      if supported
        @data_script_hash,attribution_annotations,saved_file,@objects_hash = @@model_builder.builder_content @display_model    
        @applet=@@model_builder.simulate saved_file
      end
    rescue Exception=>e
      error=e
    end
    
    respond_to do |format|
      if error
        flash[:error]="JWS Online encountered a problem processing this model."
        format.html { redirect_to(@model,:version=>@display_model.version)}                      
      elsif !supported
        flash[:error]="This model is of neither SBML or JWS Online (Dat) format so cannot be used with JWS Online"
        format.html { redirect_to(@model,:version=>@display_model.version)}        
      else
        format.html {render :layout=>"no_sidebar"}
      end
    end
    
  end
  
  def update_model_metadata
    attribute=params[:attribute]
    if attribute=="model_type"
      update_model_type params
    elsif attribute=="model_format"
      update_model_format params
    end
  end<commit_msg>fixed flash[:error] to be flash.now[:error]<commit_after>class ModelsController < ApplicationController    
  
  include WhiteListHelper
  include IndexPager
  include DotGenerator
  include Seek::AssetsCommon
  
  before_filter :login_required
  
  before_filter :pal_or_admin_required,:only=> [:create_model_metadata,:update_model_metadata,:delete_model_metadata ]
  
  before_filter :find_assets, :only => [ :index ]
  before_filter :find_and_auth, :except => [ :build,:index, :new, :create,:create_model_metadata,:update_model_metadata,:delete_model_metadata,:request_resource,:preview,:test_asset_url, :update_tags_ajax]
  before_filter :find_display_model, :only=>[:show,:download,:execute,:builder,:simulate,:submit_to_jws]
    
  before_filter :jws_enabled,:only=>[:builder,:simulate,:submit_to_jws]
  
  @@model_builder = Seek::JWS::OneStop.new
  
  # GET /models
  # GET /models.xml
  
  def new_version
    if (handle_data nil)
      
      comments = params[:revision_comment]
      @model.content_blob = ContentBlob.new(:tmp_io_object => @tmp_io_object, :url=>@data_url)
      @model.content_type = params[:model][:content_type]
      @model.original_filename = params[:model][:original_filename]
      
      respond_to do |format|
        create_new_version comments
        format.html {redirect_to @model }
      end
    else
      flash[:error]=flash.now[:error]
      redirect_to @model
    end
  end    
  
  def delete_model_metadata
    attribute=params[:attribute]
    if attribute=="model_type"
      delete_model_type params
    elsif attribute=="model_format"
      delete_model_format params
    end
  end
  
  def builder
    saved_file=params[:saved_file]
    error=nil
    begin
      if saved_file
        supported=true
        @params_hash,@attribution_annotations,@saved_file,@objects_hash,@error_keys = @@model_builder.saved_file_builder_content saved_file
      else
        supported = @@model_builder.is_supported?(@display_model)
        @params_hash,@attribution_annotations,@saved_file,@objects_hash,@error_keys = @@model_builder.builder_content @display_model if supported
      end
    rescue Exception=>e
      error=e
      logger.error "Error submitting to JWS Online OneStop - #{e.message}"
    end
    
    respond_to do |format|
      if error
        flash.now[:error]="JWS Online encountered a problem processing this model."
        format.html { redirect_to model_path(@model,:version=>@display_model.version)}
      elsif !supported
        flash[:error]="This model is of neither SBML or JWS Online (Dat) format so cannot be used with JWS Online"
        format.html { redirect_to model_path(@model,:version=>@display_model.version)}        
      else
        format.html
      end
    end
  end    

#  def annotator
#    respond_to do |format|
#      format.html
#    end
#  end

  def submit_to_jws
    following_action=params.delete("following_action")    
    error=nil
    begin
      if following_action == "annotate"
        @params_hash,@attribution_annotations,@species_annotations,@reaction_annotations,@search_results,@cached_annotations,@saved_file,@error_keys = @@model_builder.annotate params
      else
        @params_hash,@attribution_annotations,@saved_file,@objects_hash,@error_keys = @@model_builder.construct params
      end
    rescue Exception => e
      error=e
    end

    if (!error && @error_keys.empty?)

      if following_action == "simulate"
        begin
          @applet=@@model_builder.simulate @saved_file
        rescue Exception => e
          error=e
        end
      elsif following_action == "save_new_version"
        model_format=params.delete("saved_model_format") #only used for saving as a new version
        new_version_filename=params.delete("new_version_filename")
        new_version_comments=params.delete("new_version_comments")
        if model_format == "dat"
          url=@@model_builder.saved_dat_download_url @saved_file
        elsif model_format == "sbml"
          url=@@model_builder.sbml_download_url @saved_file
        end
        if url
          downloader=Seek::RemoteDownloader.new
          data_hash = downloader.get_remote_data url
          File.open(data_hash[:data_tmp_path],"r") do |f|
            @model.content_blob=ContentBlob.new(:data=>f.read)
          end
          @model.content_type=model_format=="sbml" ? "text/xml" : "text/plain"
          @model.original_filename=new_version_filename
        end
      end
    end


    respond_to do |format|
      if error
        flash.now[:error]="JWS Online encountered a problem processing this model."
        format.html { render :action=>"builder" }
      elsif @error_keys.empty? && following_action == "simulate"
        format.html {render :action=>"simulate",:layout=>"no_sidebar"}
      elsif @error_keys.empty? && following_action == "annotate"
        format.html {render :action=>"annotator"}
      elsif @error_keys.empty? && following_action == "save_new_version"
        create_new_version(new_version_comments)
        format.html {redirect_to  model_path(@model,:version=>@model.version) }
      else
        format.html { render :action=>"builder" }
      end      
    end
    
  end
  
  def simulate
    error=nil
    begin
      supported = @@model_builder.is_supported?(@display_model)
      if supported
        @data_script_hash,attribution_annotations,saved_file,@objects_hash = @@model_builder.builder_content @display_model    
        @applet=@@model_builder.simulate saved_file
      end
    rescue Exception=>e
      error=e
    end
    
    respond_to do |format|
      if error
        flash.now[:error]="JWS Online encountered a problem processing this model."
        format.html { redirect_to(@model,:version=>@display_model.version)}                      
      elsif !supported
        flash[:error]="This model is of neither SBML or JWS Online (Dat) format so cannot be used with JWS Online"
        format.html { redirect_to(@model,:version=>@display_model.version)}        
      else
        format.html {render :layout=>"no_sidebar"}
      end
    end
    
  end
  
  def update_model_metadata
    attribute=params[:attribute]
    if attribute=="model_type"
      update_model_type params
    elsif attribute=="model_format"
      update_model_format params
    end
  end
{'fraction_non_alphanumeric': 0.09582935370900987, 'fraction_numerical': 0.0, 'mean_word_length': 3.217858341725411, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1867228', 'n_tokens_mistral': 3796, 'n_tokens_neox': 3749, 'n_words': 733}