<commit_before>class TableauPublisher

  attr_accessor :current_user

  def initialize(user)
    @current_user = user
  end

  def publish_workbook(dataset, workspace, params)
    workbook = build_workbook(dataset, params[:tableau_workbook][:name],
                                   params[:tableau_workbook][:tableau_username],
                                   params[:tableau_workbook][:tableau_password])

    workfile = create_workfile(params, workspace)

    if (!workfile || workfile.validate_name_uniqueness) && workbook.save
      publication = create_publication(dataset, params)
      if workfile
        workfile.tableau_workbook_publication = publication
        workfile.save!
      end
      publication
    else
      handle_errors(workbook, workfile)
    end
  end

  private


  def handle_errors(workbook, workfile)
    messages = workbook.errors.full_messages
    messages = messages + workfile.errors.full_messages if workfile
    raise ModelNotCreated.new(messages.join(". "))
  end

  def create_publication(dataset, params)
    TableauWorkbookPublication.create!(
        :name => params[:tableau_workbook][:name],
        :dataset_id => dataset.id,
        :workspace_id => params[:workspace_id],
        :project_name => "Default"
    )
  end

  def create_workfile(params, workspace)
    if params[:tableau_workbook][:create_work_file] == "true"
      workfile = LinkedTableauWorkfile.new(file_name: "#{params[:tableau_workbook][:name]}.twb")
      workfile.owner = current_user
      workfile.workspace = workspace
    else
      workfile = nil
    end
    workfile
  end

  def build_workbook(dataset, workbook_name, username, password)
    login_params = {
        :name => workbook_name,
        :server => ChorusConfig.instance['tableau.url'],<commit_msg>[#49599699] Refactor TableauPublisher<commit_after>class TableauPublisher

  attr_accessor :current_user

  def initialize(user)
    @current_user = user
  end

  def publish_workbook(dataset, workspace, params)
    workbook = build_workbook(dataset, params[:tableau_workbook][:name],
                                   params[:tableau_workbook][:tableau_username],
                                   params[:tableau_workbook][:tableau_password])

    workfile = build_workfile(params, workspace)
    handle_errors(workbook, workfile) unless workfile_unique?(workfile) && workbook.save

    publication = create_publication(dataset, params)
    if workfile
      workfile.tableau_workbook_publication = publication
      workfile.save!
    end
    publication
  end

  private

  def workfile_unique?(workfile)
    (!workfile || workfile.validate_name_uniqueness)
  end

  def handle_errors(workbook, workfile)
    messages = workbook.errors.full_messages
    messages = messages + workfile.errors.full_messages if workfile
    raise ModelNotCreated.new(messages.join(". "))
  end

  def create_publication(dataset, params)
    TableauWorkbookPublication.create!(
        :name => params[:tableau_workbook][:name],
        :dataset_id => dataset.id,
        :workspace_id => params[:workspace_id],
        :project_name => "Default"
    )
  end

  def build_workfile(params, workspace)
    if params[:tableau_workbook][:create_work_file] == "true"
      workfile = LinkedTableauWorkfile.new(file_name: "#{params[:tableau_workbook][:name]}.twb")
      workfile.owner = current_user
      workfile.workspace = workspace
    else
      workfile = nil
    end
    workfile
  end

  def build_workbook(dataset, workbook_name, username, password)
    login_params = {
        :name => workbook_name,
        :server => ChorusConfig.instance['tableau.url'],
{'fraction_non_alphanumeric': 0.09535586699642759, 'fraction_numerical': 0.0021984061555372354, 'mean_word_length': 3.31791221826809, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '794412', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1065, 'n_words': 195}