<commit_before>class ServiceController < ApplicationController
  include Mixins::GenericSessionMixin
  include Mixins::GenericShowMixin

  before_action :check_privileges
  before_action :get_session_data
  after_action :cleanup_action
  after_action :set_session_data

  SERVICE_X_BUTTON_ALLOWED_ACTIONS = {
    'service_delete'      => :service_delete,
    'service_edit'        => :service_edit,
    'service_ownership'   => :service_ownership,
    'service_tag'         => :service_tag_edit,
    'service_retire'      => :service_retire,
    'service_retire_now'  => :service_retire_now,
    'service_reconfigure' => :service_reconfigure
  }

  def button
    if params[:pressed] == "custom_button"
      ids = if @display == 'generic_objects'
              @lastaction == 'generic_object' ? @sb[:rec_id] : 'LIST'
            end
      custom_buttons(ids)
      return
    end
    tag(GenericObject) if @display == 'generic_objects' && params[:pressed] == 'generic_object_tag'
  end

  def x_button
    generic_x_button(SERVICE_X_BUTTON_ALLOWED_ACTIONS)
  end

  def title
    _("My Services")
  end

  # Service show selected, redirect to proper controller
  def show
    @record = Service.find(from_cid(params[:id]))
    @lastaction = "show"

    @gtl_url = "/show"
    @display = params[:display] if params[:display]

    if self.class.display_methods.include?(@display)
      params[:display] = @display
      display_nested_list(@display)
      return
    end

    unless @explorer
      tree_node_id = TreeBuilder.build_node_id(@record)
      redirect_to :controller => "service",
                  :action     => "explorer",
                  :id         => tree_node_id
      return
    end
    redirect_to(:action => 'show', :controller => @record.class.base_model.to_s.underscore, :id => @record.id)
  end

  def show_list
    redirect_to :action => 'explorer', :flash_msg => @flash_array.try(:fetch_path, 0, :message)
  end

  def explorer
    @explorer   = true
    @lastaction = "explorer"

    # if AJAX request, replace right cell, and return
    if request.xml_http_request?
      replace_right_cell
      return
    end

    x_node_to_set = nil

    if params[:id] # Tree node id came in, show it in the tree.
      @find_with_aggregates = true
      nodetype, id = params[:id].split("-")
      x_node_to_set = "#{nodetype}-#{to_cid(id)}"
    end

    build_accordions_and_trees(x_node_to_set)

    params.instance_variable_get(:@parameters).merge!(session[:exp_parms]) if session[:exp_parms]  # Grab any explorer parm overrides
    session.delete(:exp_parms)
    @in_a_form = false

    render :layout => "application"
  end

  def identify_service(id = nil)
    @st = @record = identify_record(id || params[:id])
  end

  # ST clicked on in the explorer right cell
  def x_show
    identify_service(from_cid(params[:id]))
    generic_x_show(x_tree(:svcs_tree))
  end

  def service_edit
    assert_privileges("service_edit")
    @explorer = true
    case params[:button]
    when "cancel"
      service = find_record_with_rbac(Service, params[:id])
      add_flash(_("Edit of Service \"%{name}\" was cancelled by the user") % {:name => service.description})
      replace_right_cell
    when "save", "add"
      service = find_record_with_rbac(Service, params[:id])
      service_set_record_vars(service)

      begin
        service.save
      rescue => bang
        add_flash(_("Error during 'Service Edit': %{message}") % {:message => bang.message}, :error)
      else
        add_flash(_("Service \"%{name}\" was saved") % {:name => service.name})
      end
      replace_right_cell(:replace_trees => [:svcs])
    when "reset", nil # Reset or first time in
      checked = find_checked_items
      checked[0] = params[:id] if checked.blank? && params[:id]
      @service = find_record_with_rbac(Service, checked[0])
      @in_a_form = true
      replace_right_cell(:action => "service_edit")
      return
    end
  end

  def service_reconfigure
    @explorer = true
    s = Service.find_by_id(from_cid(params[:id]))
    st = s.service_template
    ra = st.resource_actions.find_by_action('Reconfigure') if st
    if ra && ra.dialog_id
      @right_cell_text = _("Reconfigure Service \"%{name}\"") % {:name => st.name}
      options = {
        :header      => @right_cell_text,
        :target_id   => s.id,
        :target_kls  => s.class.name,
        :dialog      => s.options[:dialog],
        :dialog_mode => :reconfigure
      }
      dialog_initialize(ra, options)
    end
  end

  def service_form_fields
    service = Service.find_by_id(params[:id])

    render :json => {
      :name        => service.name,
      :description => service.description
    }
  end

  def generic_object
    return unless init_show_variables

    id = params[:show] || params[:x_show]
    if id.present?
      @lastaction = "generic_object"
      @item = @record.generic_objects.find(from_cid(id)).first
      drop_breadcrumb(:name => _("%{name} (All Generic Objects)") % {:name => @record.name},
                      :url  => show_link(@record, :display => @display))
      drop_breadcrumb(:name => @item.name, :url => "/#{controller_name}/show/#{@record.id}?display=generic_objects/show=#{id}")
      @view = get_db_view(GenericObject)
      @sb[:rec_id] = id
      show_item
    end
  end

  def self.display_methods
    %w(generic_objects)
  end

  def display_generic_objects
    nested_list(GenericObject)
  end

  private
<commit_msg>Adjustments for nested display of GO list and GO items<commit_after>class ServiceController < ApplicationController
  include Mixins::GenericSessionMixin
  include Mixins::GenericShowMixin

  before_action :check_privileges
  before_action :get_session_data
  after_action :cleanup_action
  after_action :set_session_data

  SERVICE_X_BUTTON_ALLOWED_ACTIONS = {
    'service_delete'      => :service_delete,
    'service_edit'        => :service_edit,
    'service_ownership'   => :service_ownership,
    'service_tag'         => :service_tag_edit,
    'service_retire'      => :service_retire,
    'service_retire_now'  => :service_retire_now,
    'service_reconfigure' => :service_reconfigure
  }

  def button
    if params[:pressed] == "custom_button"
      ids = if @display == 'generic_objects'
              @lastaction == 'generic_object' ? @sb[:rec_id] : 'LIST'
            end
      custom_buttons(ids)
      return
    end
    tag(GenericObject) if @display == 'generic_objects' && params[:pressed] == 'generic_object_tag'
  end

  def x_button
    generic_x_button(SERVICE_X_BUTTON_ALLOWED_ACTIONS)
  end

  def title
    _("My Services")
  end

  # Service show selected, redirect to proper controller
  def show
    @record = Service.find(from_cid(params[:id]))
    @lastaction = "show"

    @gtl_url = "/show"

    @display = params[:display]
    @display ||= default_display unless pagination_or_gtl_request?

    if self.class.display_methods.include?(@display)
      nested_list_show
      return
    end

    unless @explorer
      tree_node_id = TreeBuilder.build_node_id(@record)
      redirect_to :controller => "service",
                  :action     => "explorer",
                  :id         => tree_node_id
      return
    end
    redirect_to(:action => 'show', :controller => @record.class.base_model.to_s.underscore, :id => @record.id)
  end

  def nested_list_show
    params[:generic_object_id] ? generic_object : display_nested_list(@display)
  end

  def show_list
    redirect_to :action => 'explorer', :flash_msg => @flash_array.try(:fetch_path, 0, :message)
  end

  def explorer
    @explorer   = true
    @lastaction = "explorer"

    # if AJAX request, replace right cell, and return
    if request.xml_http_request?
      replace_right_cell
      return
    end

    x_node_to_set = nil

    if params[:id] # Tree node id came in, show it in the tree.
      @find_with_aggregates = true
      nodetype, id = params[:id].split("-")
      x_node_to_set = "#{nodetype}-#{to_cid(id)}"
    end

    build_accordions_and_trees(x_node_to_set)

    params.instance_variable_get(:@parameters).merge!(session[:exp_parms]) if session[:exp_parms]  # Grab any explorer parm overrides
    session.delete(:exp_parms)
    @in_a_form = false

    render :layout => "application"
  end

  def identify_service(id = nil)
    @st = @record = identify_record(id || params[:id])
  end

  # ST clicked on in the explorer right cell
  def x_show
    identify_service(from_cid(params[:id]))
    generic_x_show(x_tree(:svcs_tree))
  end

  def service_edit
    assert_privileges("service_edit")
    @explorer = true
    case params[:button]
    when "cancel"
      service = find_record_with_rbac(Service, params[:id])
      add_flash(_("Edit of Service \"%{name}\" was cancelled by the user") % {:name => service.description})
      replace_right_cell
    when "save", "add"
      service = find_record_with_rbac(Service, params[:id])
      service_set_record_vars(service)

      begin
        service.save
      rescue => bang
        add_flash(_("Error during 'Service Edit': %{message}") % {:message => bang.message}, :error)
      else
        add_flash(_("Service \"%{name}\" was saved") % {:name => service.name})
      end
      replace_right_cell(:replace_trees => [:svcs])
    when "reset", nil # Reset or first time in
      checked = find_checked_items
      checked[0] = params[:id] if checked.blank? && params[:id]
      @service = find_record_with_rbac(Service, checked[0])
      @in_a_form = true
      replace_right_cell(:action => "service_edit")
      return
    end
  end

  def service_reconfigure
    @explorer = true
    s = Service.find_by_id(from_cid(params[:id]))
    st = s.service_template
    ra = st.resource_actions.find_by_action('Reconfigure') if st
    if ra && ra.dialog_id
      @right_cell_text = _("Reconfigure Service \"%{name}\"") % {:name => st.name}
      options = {
        :header      => @right_cell_text,
        :target_id   => s.id,
        :target_kls  => s.class.name,
        :dialog      => s.options[:dialog],
        :dialog_mode => :reconfigure
      }
      dialog_initialize(ra, options)
    end
  end

  def service_form_fields
    service = Service.find_by_id(params[:id])

    render :json => {
      :name        => service.name,
      :description => service.description
    }
  end

  def generic_object
    return unless init_show_variables

    @lastaction = 'generic_object'
    @item ||= @record.generic_objects.find(params[:generic_object_id]).first
    drop_breadcrumb(:name => _("%{name} (All Generic Objects)") % {:name => @record.name},
                    :url  => show_link(@record, :display => @display))
    drop_breadcrumb(:name => @item.name, :url => "/#{controller_name}/show/#{@record.id}?display=generic_objects&generic_object_id=#{params[:generic_object_id]}")
    @view = get_db_view(GenericObject)
    @sb[:rec_id] = params[:generic_object_id]
    show_item
  end

  def self.display_methods
    %w(generic_objects)
  end

  def display_generic_objects
    nested_list(GenericObject)
  end

  private

{'fraction_non_alphanumeric': 0.11749819233550253, 'fraction_numerical': 0.0005422993492407809, 'mean_word_length': 3.134902840059791, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '508546', 'n_tokens_mistral': 3602, 'n_tokens_neox': 3519, 'n_words': 743}