<commit_before>class CatalogController < ApplicationController
  include AutomateTreeHelper

  before_filter :check_privileges
  before_filter :get_session_data
  after_filter :cleanup_action
  after_filter :set_session_data

  def self.model
    @model ||= ServiceTemplate
  end

  def self.table_name
    @table_name ||= "service_template"
  end

  CATALOG_X_BUTTON_ALLOWED_ACTIONS = {
    'ab_button_new'                 => :ab_button_new,
    'ab_button_edit'                => :ab_button_edit,
    'ab_button_delete'              => :ab_button_delete,
    'ab_group_delete'               => :ab_group_delete,
    'ab_group_edit'                 => :ab_group_edit,
    'ab_group_new'                  => :ab_group_new,
    'ab_group_reorder'              => :ab_group_reorder,
    'svc_catalog_provision'         => :svc_catalog_provision,
    'st_catalog_delete'             => :st_catalog_delete,

    'atomic_catalogitem_edit'       => :servicetemplate_edit,
    'atomic_catalogitem_new'        => :servicetemplate_edit,
    'catalogitem_edit'              => :servicetemplate_edit,
    'catalogitem_new'               => :servicetemplate_edit,

    'catalogitem_delete'            => :st_delete,
    'catalogitem_tag'               => :st_tags_edit,

    'orchestration_template_add'    => :ot_add,
    'orchestration_template_edit'   => :ot_edit,
    'orchestration_template_copy'   => :ot_copy,
    'orchestration_template_remove' => :ot_remove_submit,
    'orchestration_template_tag'    => :ot_tags_edit,
    'service_dialog_from_ot'        => :service_dialog_from_ot,
    'st_catalog_edit'               => :st_catalog_edit,
    'st_catalog_new'                => :st_catalog_edit,
  }.freeze

  def x_button
    # setting this here so it can be used in the common code
    @sb[:action] = action = params[:pressed]
    @sb[:applies_to_class] = 'ServiceTemplate'

    # guard this 'router' by matching against a list of allowed actions
    raise ActionController::RoutingError.new('invalid button action') unless
      CATALOG_X_BUTTON_ALLOWED_ACTIONS.key?(action)

    self.send(CATALOG_X_BUTTON_ALLOWED_ACTIONS[action])
  end

  def servicetemplate_edit
    assert_privileges(params[:pressed]) if params[:pressed]
    checked = find_checked_items
    @sb[:cached_waypoint_ids] = MiqAeClass.waypoint_ids_for_state_machines
    checked[0] = params[:id] if checked.blank? && params[:id]
    @record = checked[0] ? find_by_id_filtered(ServiceTemplate, checked[0]) : ServiceTemplate.new
    @sb[:st_form_active_tab] = "basic"
    if checked[0]
      if @record.service_type == "composite"
        st_edit
      else
        atomic_st_edit
      end
    else
      #check for service_type incase add/cancel button was pressed to direct to correct method
      if params[:pressed] == "atomic_catalogitem_new" || (params[:button] && session[:edit][:new][:service_type] == "atomic")
        atomic_st_edit
      else
        st_edit
      end
    end
  end

  def atomic_st_edit
    #reset the active tree back to :sandt_tree, it was changed temporairly to display automate entry point tree in a popup div
    self.x_active_tree = 'sandt_tree'
    case params[:button]
    when "cancel"
      if session[:edit][:rec_id]
        add_flash(_("Edit of %{model} \"%{name}\" was cancelled by the user") % {:model=>"#{ui_lookup(:model=>'ServiceTemplate')}", :name=>session[:edit][:new][:description]})
      else
        add_flash(_("Add of new %s was cancelled by the user") % "#{ui_lookup(:model=>'ServiceTemplate')}")
      end
      @edit = @record = nil
      @in_a_form = false
      replace_right_cell
    when "save","add"
      assert_privileges("atomic_catalogitem_#{params[:button] == "save" ? "edit" : "new"}")
      atomic_req_submit
    when "reset", nil  # Reset or first time in
      @_params[:org_controller] = "service_template"
      if params[:button] == "reset"
        add_flash(_("All changes have been reset"), :warning)
      end
      if !@record.id.nil? && need_prov_dialogs?(@record.prov_type)
        prov_set_form_vars(MiqRequest.find(@record.service_resources[0].resource_id))      # Set vars from existing request
        @edit[:new][:st_prov_type] = @record.prov_type
      else
        #prov_set_form_vars
        @edit ||= Hash.new                                    # Set default vars
        @edit[:new] ||= Hash.new
        @edit[:current] ||= Hash.new
        @edit[:key] = "prov_edit__new"
        @edit[:new][:st_prov_type] = @record.prov_type if @record.try(:id)
        @edit[:st_prov_types] = {
          "amazon"                => "Amazon",
          "openstack"             => "OpenStack",
          "generic_orchestration" => "Orchestration",
          "redhat"                => "RHEV",
          "generic"               => "Generic",
          "vmware"                => "VMware"
        }
      end

      #set name and description for ServiceTemplate record
      set_form_vars
      @edit[:new][:service_type] = "atomic"
      @edit[:rec_id] = @record.try(:id)
      @edit[:current] = copy_hash(@edit[:new])

      @tabactive = "#{@edit[:new][:current_tab_key]}_div"
      @in_a_form = true
      session[:changed] = false
      replace_right_cell("at_st_new")
    end
  end
<commit_msg>Added support for SCVMM Catalog Item type.<commit_after>class CatalogController < ApplicationController
  include AutomateTreeHelper

  before_filter :check_privileges
  before_filter :get_session_data
  after_filter :cleanup_action
  after_filter :set_session_data

  def self.model
    @model ||= ServiceTemplate
  end

  def self.table_name
    @table_name ||= "service_template"
  end

  CATALOG_X_BUTTON_ALLOWED_ACTIONS = {
    'ab_button_new'                 => :ab_button_new,
    'ab_button_edit'                => :ab_button_edit,
    'ab_button_delete'              => :ab_button_delete,
    'ab_group_delete'               => :ab_group_delete,
    'ab_group_edit'                 => :ab_group_edit,
    'ab_group_new'                  => :ab_group_new,
    'ab_group_reorder'              => :ab_group_reorder,
    'svc_catalog_provision'         => :svc_catalog_provision,
    'st_catalog_delete'             => :st_catalog_delete,

    'atomic_catalogitem_edit'       => :servicetemplate_edit,
    'atomic_catalogitem_new'        => :servicetemplate_edit,
    'catalogitem_edit'              => :servicetemplate_edit,
    'catalogitem_new'               => :servicetemplate_edit,

    'catalogitem_delete'            => :st_delete,
    'catalogitem_tag'               => :st_tags_edit,

    'orchestration_template_add'    => :ot_add,
    'orchestration_template_edit'   => :ot_edit,
    'orchestration_template_copy'   => :ot_copy,
    'orchestration_template_remove' => :ot_remove_submit,
    'orchestration_template_tag'    => :ot_tags_edit,
    'service_dialog_from_ot'        => :service_dialog_from_ot,
    'st_catalog_edit'               => :st_catalog_edit,
    'st_catalog_new'                => :st_catalog_edit,
  }.freeze

  def x_button
    # setting this here so it can be used in the common code
    @sb[:action] = action = params[:pressed]
    @sb[:applies_to_class] = 'ServiceTemplate'

    # guard this 'router' by matching against a list of allowed actions
    raise ActionController::RoutingError.new('invalid button action') unless
      CATALOG_X_BUTTON_ALLOWED_ACTIONS.key?(action)

    self.send(CATALOG_X_BUTTON_ALLOWED_ACTIONS[action])
  end

  def servicetemplate_edit
    assert_privileges(params[:pressed]) if params[:pressed]
    checked = find_checked_items
    @sb[:cached_waypoint_ids] = MiqAeClass.waypoint_ids_for_state_machines
    checked[0] = params[:id] if checked.blank? && params[:id]
    @record = checked[0] ? find_by_id_filtered(ServiceTemplate, checked[0]) : ServiceTemplate.new
    @sb[:st_form_active_tab] = "basic"
    if checked[0]
      if @record.service_type == "composite"
        st_edit
      else
        atomic_st_edit
      end
    else
      #check for service_type incase add/cancel button was pressed to direct to correct method
      if params[:pressed] == "atomic_catalogitem_new" || (params[:button] && session[:edit][:new][:service_type] == "atomic")
        atomic_st_edit
      else
        st_edit
      end
    end
  end

  def atomic_st_edit
    #reset the active tree back to :sandt_tree, it was changed temporairly to display automate entry point tree in a popup div
    self.x_active_tree = 'sandt_tree'
    case params[:button]
    when "cancel"
      if session[:edit][:rec_id]
        add_flash(_("Edit of %{model} \"%{name}\" was cancelled by the user") % {:model=>"#{ui_lookup(:model=>'ServiceTemplate')}", :name=>session[:edit][:new][:description]})
      else
        add_flash(_("Add of new %s was cancelled by the user") % "#{ui_lookup(:model=>'ServiceTemplate')}")
      end
      @edit = @record = nil
      @in_a_form = false
      replace_right_cell
    when "save","add"
      assert_privileges("atomic_catalogitem_#{params[:button] == "save" ? "edit" : "new"}")
      atomic_req_submit
    when "reset", nil  # Reset or first time in
      @_params[:org_controller] = "service_template"
      if params[:button] == "reset"
        add_flash(_("All changes have been reset"), :warning)
      end
      if !@record.id.nil? && need_prov_dialogs?(@record.prov_type)
        prov_set_form_vars(MiqRequest.find(@record.service_resources[0].resource_id))      # Set vars from existing request
        @edit[:new][:st_prov_type] = @record.prov_type
      else
        #prov_set_form_vars
        @edit ||= Hash.new                                    # Set default vars
        @edit[:new] ||= Hash.new
        @edit[:current] ||= Hash.new
        @edit[:key] = "prov_edit__new"
        @edit[:new][:st_prov_type] = @record.prov_type if @record.try(:id)
        @edit[:st_prov_types] = catalog_item_types
      end

      #set name and description for ServiceTemplate record
      set_form_vars
      @edit[:new][:service_type] = "atomic"
      @edit[:rec_id] = @record.try(:id)
      @edit[:current] = copy_hash(@edit[:new])

      @tabactive = "#{@edit[:new][:current_tab_key]}_div"
      @in_a_form = true
      session[:changed] = false
      replace_right_cell("at_st_new")
    end
  end

  def catalog_item_types
    {
      "amazon"                => "Amazon",
      "generic"               => "Generic",
      "generic_orchestration" => "Orchestration",
      "microsoft"             => "SCVMM",
      "openstack"             => "OpenStack",
      "redhat"                => "RHEV",
      "vmware"                => "VMware"
    }
  end

{'fraction_non_alphanumeric': 0.1157337367624811, 'fraction_numerical': 0.0009455370650529501, 'mean_word_length': 2.782904148783977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3095504', 'n_tokens_mistral': 3406, 'n_tokens_neox': 3372, 'n_words': 646}