Class: Bulkrax::GuidedImportsController

Inherits:
ApplicationController show all
Includes:
GuidedImportDemoScenarios, ImporterFileHandler, Hyrax::ThemedLayoutController
Defined in:
app/controllers/bulkrax/guided_imports_controller.rb

Instance Method Summary collapse

Methods included from GuidedImportDemoScenarios

#demo_scenarios

Instance Method Details

#createObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/bulkrax/guided_imports_controller.rb', line 67

def create
  files = nil
  files = resolve_create_files
  return render_invalid_uploaded_files_response if params[:uploaded_files].present? && files.empty?

  @importer = Importer.new(importer_params)
  @importer.parser_klass = 'Bulkrax::CsvParser'
  @importer.user = current_user if respond_to?(:current_user) && current_user.present?
  apply_field_mapping

  if @importer.save
    write_files(files)
    Bulkrax::ImporterJob.perform_later(@importer.id)

    respond_to do |format|
      format.html { redirect_to bulkrax.importers_path, notice: I18n.t('bulkrax.importer.guided_import.flash.import_started') }
      format.json { render json: { success: true, importer_id: @importer.id }, status: :created }
    end
  else
    respond_to do |format|
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: { errors: @importer.errors.full_messages }, status: :unprocessable_entity }
    end
  end
ensure
  close_file_handles(files)
end

#download_validation_errorsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/bulkrax/guided_imports_controller.rb', line 50

def download_validation_errors
  cache_key = params[:key].to_s
  expected_prefix = "guided_import_errors:#{session.id}:"
  return head :not_found unless cache_key.start_with?(expected_prefix)

  cached = Rails.cache.read(cache_key)
  return head :not_found unless cached

  csv = ValidationErrorCsvBuilder.build(
    headers: cached[:headers],
    csv_data: cached[:csv_data],
    row_errors: cached[:row_errors],
    file_errors: cached[:file_errors]
  )
  send_data csv, filename: error_csv_filename(cached[:original_filename]), type: 'text/csv', disposition: 'attachment'
end

#newObject

trigger form to allow upload



15
16
17
18
19
20
# File 'app/controllers/bulkrax/guided_imports_controller.rb', line 15

def new
  @importer = Importer.new
  return unless defined?(::Hyrax)
  add_importer_breadcrumbs
  add_breadcrumb I18n.t('bulkrax.importer.guided_import.breadcrumb')
end

#validateObject

AJAX endpoint to validate uploaded files



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/bulkrax/guided_imports_controller.rb', line 23

def validate
  set_locale_from_params

  files, error = resolve_validation_files
  return render json: error, status: :ok if error
  return render json: StepperResponseFormatter.error(message: I18n.t('bulkrax.importer.guided_import.validation.no_files_uploaded')), status: :ok unless files.any?

  csv_file, zip_file = select_csv_and_zip(files)

  unless csv_file
    return render json: StepperResponseFormatter.error(message: I18n.t('bulkrax.importer.guided_import.validation.no_csv_uploaded')), status: :ok unless zip_file

    csv_file, error = extract_csv_from_zip(zip_file)
    return render json: error, status: :ok if error
  end

  admin_set_id = params[:importer]&.[](:admin_set_id)
  validation_result = run_validation(csv_file, zip_file, admin_set_id: admin_set_id)
  raw_csv_data = validation_result.delete(:raw_csv_data)
  cache_key = cache_validation_errors(validation_result, raw_csv_data, csv_file)
  formatted = StepperResponseFormatter.format(validation_result)
  formatted[:validationErrorsCacheKey] = cache_key
  render json: formatted, status: :ok
ensure
  close_file_handles(files)
end