Class: Bulkrax::ImportersController

Inherits:
ApplicationController show all
Includes:
API, DatatablesBehavior, DownloadBehavior, 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



164
165
166
167
168
169
# File 'app/controllers/bulkrax/importers_controller.rb', line 164

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/bulkrax/importers_controller.rb', line 81

def create
  # rubocop:disable Style/IfInsideElse
  if api_request?
    return return_json_response unless valid_create_params?
  end
  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)
    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



154
155
156
157
158
159
160
161
# File 'app/controllers/bulkrax/importers_controller.rb', line 154

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



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

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



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

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



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

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

#external_setsObject



196
197
198
199
200
201
202
# File 'app/controllers/bulkrax/importers_controller.rb', line 196

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



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

def importer_table
  @importers = Importer.order(table_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



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

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



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

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

#original_fileObject



204
205
206
207
208
209
210
# File 'app/controllers/bulkrax/importers_controller.rb', line 204

def original_file
  if @importer.original_file?
    send_file @importer.original_file
  else
    redirect_to @importer, alert: 'Importer does not support file re-download or the imported file is not found on the server.'
  end
end

#showObject

GET /importers/1



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

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/bulkrax/importers_controller.rb', line 119

def update
  if api_request?
    return return_json_response unless valid_update_params?
  end

  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) unless file.nil? && cloud_files.nil?
    # 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



172
173
174
175
176
177
178
179
180
# File 'app/controllers/bulkrax/importers_controller.rb', line 172

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



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

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