Class: Bulkrax::ImportersController

Inherits:
ApplicationController show all
Includes:
API, DatatablesBehavior, DownloadBehavior, ImporterFileHandler, ValidationHelper, Hyrax::ThemedLayoutController
Defined in:
app/controllers/bulkrax/importers_controller.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Methods included from ValidationHelper

#check_admin_set, #check_user, #return_json_response, #return_value, #valid_bagit?, #valid_commit?, #valid_commit_message?, #valid_create_params?, #valid_csv?, #valid_importer?, #valid_name?, #valid_oai?, #valid_parser_fields?, #valid_parser_klass?, #valid_update_params?

Methods included from DatatablesBehavior

#download_zip, #entry_table_search, #entry_util_links, #exporter_table_search, #exporter_util_links, #format_entries, #format_exporters, #format_importers, #importer_table_search, #importer_util_links, #order_value, #status_message_for, #table_order, #table_page, #table_per_page

Methods included from DownloadBehavior

#content_head, #content_options, #file, #file_name, #prepare_file_headers, #send_content, #send_file_contents

Instance Method Details

#continueObject

PUT /importers/1



179
180
181
182
183
184
# File 'app/controllers/bulkrax/importers_controller.rb', line 179

def continue
  @importer = Importer.find(params[:importer_id])
  params[:importer] = { name: @importer.name }
  @importer.validate_only = false
  update
end

#createObject

POST /importers rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/bulkrax/importers_controller.rb', line 94

def create
  # rubocop:disable Style/IfInsideElse
  if api_request?
    return return_json_response unless valid_create_params?
  end
  uploads = uploaded_files_scope
  file = file_param
  cloud_files = cloud_params

  @importer = Importer.new(importer_params)
  field_mapping_params
  @importer.validate_only = true if params[:commit] == 'Create and Validate'
  # the following line is needed to handle updating remote files of a FileSet
  # on a new import otherwise it only gets updated during the update path
  @importer.parser_fields['update_files'] = true if params[:commit] == 'Create and Import'
  if @importer.save
    files_for_import(file, cloud_files, uploads)
    if params[:commit] == 'Create and Import'
      Bulkrax::ImporterJob.send(@importer.parser.perform_method, @importer.id)
      render_request('Importer was successfully created and import has been queued.')
    elsif params[:commit] == 'Create and Validate'
      Bulkrax::ImporterJob.send(@importer.parser.perform_method, @importer.id)
      render_request('Importer validation completed. Please review and choose to either Continue with or Discard the import.', true)
    else
      render_request('Importer was successfully created.')
    end
  else
    if api_request?
      json_response('create', :unprocessable_entity)
    else
      render :new
    end
  end
  # rubocop:enable Style/IfInsideElse
end

#destroyObject

DELETE /importers/1



169
170
171
172
173
174
175
176
# File 'app/controllers/bulkrax/importers_controller.rb', line 169

def destroy
  @importer.destroy
  if api_request?
    json_response('destroy', :ok, notice: 'Importer was successfully destroyed.')
  else
    redirect_to importers_url, notice: 'Importer was successfully destroyed.'
  end
end

#editObject

GET /importers/1/edit



81
82
83
84
85
86
87
88
89
# File 'app/controllers/bulkrax/importers_controller.rb', line 81

def edit
  if api_request?
    json_response('edit')
  elsif defined?(::Hyrax)
    add_importer_breadcrumbs
    add_breadcrumb @importer.name, bulkrax.importer_path(@importer.id)
    add_breadcrumb 'Edit'
  end
end

#entry_tableObject



51
52
53
54
55
56
57
# File 'app/controllers/bulkrax/importers_controller.rb', line 51

def entry_table
  @entries = @importer.entries.order(table_order).page(table_page).per(table_per_page)
  @entries = @entries.where(entry_table_search) if entry_table_search.present?
  respond_to do |format|
    format.json { render json: format_entries(@entries, @importer) }
  end
end

#export_errorsObject

GET /importers/1/export_errors



244
245
246
247
248
# File 'app/controllers/bulkrax/importers_controller.rb', line 244

def export_errors
  @importer = Importer.find(params[:importer_id])
  @importer.write_errored_entries_file
  send_content
end

#external_setsObject



211
212
213
214
215
216
217
# File 'app/controllers/bulkrax/importers_controller.rb', line 211

def external_sets
  if list_external_sets
    render json: { base_url: params[:base_url], sets: @sets }
  else
    render json: { base_url: params[:base_url], error: "unable to pull data from #{params[:base_url]}" }
  end
end

#importer_tableObject



31
32
33
34
35
36
37
38
# File 'app/controllers/bulkrax/importers_controller.rb', line 31

def importer_table
  order = table_order.presence || Arel.sql('last_imported_at DESC NULLS LAST')
  @importers = Importer.order(order).page(table_page).per(table_per_page)
  @importers = @importers.where(importer_table_search) if importer_table_search.present?
  respond_to do |format|
    format.json { render json: format_importers(@importers) }
  end
end

#indexObject

GET /importers



21
22
23
24
25
26
27
28
29
# File 'app/controllers/bulkrax/importers_controller.rb', line 21

def index
  # NOTE: We're paginating this in the browser.
  if api_request?
    @importers = Importer.order(created_at: :desc).all
    json_response('index')
  elsif defined?(::Hyrax)
    add_importer_breadcrumbs
  end
end

#newObject

GET /importers/new



60
61
62
63
64
65
66
67
68
# File 'app/controllers/bulkrax/importers_controller.rb', line 60

def new
  @importer = Importer.new
  if api_request?
    json_response('new')
  elsif defined?(::Hyrax)
    add_importer_breadcrumbs
    add_breadcrumb 'New'
  end
end

#original_fileObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/bulkrax/importers_controller.rb', line 219

def original_file
  file_type = params[:file_type]&.to_sym

  files = @importer.original_files
  if files.empty?
    redirect_to @importer, alert: 'Importer does not support file re-download or the imported file is not found on the server.'
    return
  end

  # If file_type is specified, find that specific file
  if file_type
    file = files.find { |f| f[:type] == file_type }
    if file
      send_file file[:path], filename: file[:name], disposition: 'attachment'
    else
      redirect_to @importer, alert: "File type '#{file_type}' not found."
    end
  else
    # Default behavior: send the first file (CSV) for backward compatibility
    file = files.first
    send_file file[:path], filename: file[:name], disposition: 'attachment'
  end
end

#sample_csv_fileObject

GET /importers/sample_csv_file



71
72
73
74
75
76
77
78
# File 'app/controllers/bulkrax/importers_controller.rb', line 71

def sample_csv_file
  admin_set_id = params[:admin_set_id].presence
  sample = Bulkrax::CsvParser.generate_template(models: 'all', output: 'file', admin_set_id: admin_set_id)
  send_file sample, filename: File.basename(sample), type: 'text/csv', disposition: 'attachment'
rescue StandardError => e
  flash[:error] = "Unable to generate sample CSV file: #{e.message}"
  redirect_back fallback_location: bulkrax.importers_path
end

#showObject

GET /importers/1



41
42
43
44
45
46
47
48
49
# File 'app/controllers/bulkrax/importers_controller.rb', line 41

def show
  if api_request?
    json_response('show')
  elsif defined?(::Hyrax)
    add_importer_breadcrumbs
    add_breadcrumb @importer.name
  end
  @first_entry = @importer.entries.first
end

#updateObject

PATCH/PUT /importers/1 # @todo refactor so as to not need to disable rubocop rubocop:disable all



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/controllers/bulkrax/importers_controller.rb', line 134

def update
  if api_request?
    return return_json_response unless valid_update_params?
  end
  uploads = uploaded_files_scope
  file = file_param
  cloud_files = cloud_params

  # Skipped during a continue
  field_mapping_params if params[:importer][:parser_fields].present?

  if @importer.update(importer_params)
    files_for_import(file, cloud_files, uploads)
    # do not perform the import
    unless params[:commit] == 'Update Importer'
      set_files_parser_fields
      Bulkrax::ImporterJob.send(@importer.parser.perform_method, @importer.id, update_harvest)
    end
    if api_request?
      json_response('updated', :ok, 'Importer was successfully updated.')
    else
      redirect_to importers_path, notice: 'Importer was successfully updated.'
    end
  else
    if api_request?
      json_response('update', :unprocessable_entity, 'Something went wrong.')
    else
      render :edit
    end
  end
end

#upload_corrected_entriesObject

GET /importer/1/upload_corrected_entries



187
188
189
190
191
192
193
194
195
# File 'app/controllers/bulkrax/importers_controller.rb', line 187

def upload_corrected_entries
  @importer = Importer.find(params[:importer_id])
  return unless defined?(::Hyrax)
  add_breadcrumb t(:'hyrax.controls.home'), main_app.root_path
  add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path
  add_breadcrumb 'Importers', bulkrax.importers_path
  add_breadcrumb @importer.name, bulkrax.importer_path(@importer.id)
  add_breadcrumb 'Upload Corrected Entries'
end

#upload_corrected_entries_fileObject

POST /importer/1/upload_corrected_entries_file



198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/bulkrax/importers_controller.rb', line 198

def upload_corrected_entries_file
  file = params[:importer][:parser_fields].delete(:file)
  @importer = Importer.find(params[:importer_id])
  if file.present?
    @importer[:parser_fields]['partial_import_file_path'] = @importer.parser.write_partial_import_file(file)
    @importer.save
    Bulkrax::ImporterJob.perform_later(@importer.id, true)
    redirect_to importer_path(@importer), notice: 'Corrected entries uploaded successfully.'
  else
    redirect_to importer_upload_corrected_entries_path(@importer), alert: 'Importer failed to update with new file.'
  end
end