From 8f5c0e4df77dff3156210ad12765eebd9f4345ec Mon Sep 17 00:00:00 2001 From: Rob Kaufman Date: Fri, 9 Feb 2024 12:40:51 -0800 Subject: [PATCH] Select existing entries for update (#924) * We may want to rebuild entries without getting a new file, this allows for that * call the cops --- app/assets/javascripts/bulkrax/importers.js.erb | 16 +++++++++++++++- .../stylesheets/bulkrax/import_export.scss | 7 ++++++- app/controllers/bulkrax/importers_controller.rb | 2 +- app/models/bulkrax/importer.rb | 6 +++++- app/parsers/bulkrax/application_parser.rb | 14 ++++++++++++++ app/views/bulkrax/importers/_csv_fields.html.erb | 6 +++++- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/bulkrax/importers.js.erb b/app/assets/javascripts/bulkrax/importers.js.erb index 87c0f7b1d..e254c60e2 100644 --- a/app/assets/javascripts/bulkrax/importers.js.erb +++ b/app/assets/javascripts/bulkrax/importers.js.erb @@ -74,12 +74,14 @@ function handleFileToggle(file_path) { $('#file_path').hide() $('#file_upload').hide() $('#cloud').hide() + $('#existing_options').hide() $('#file_path input').attr('required', null) $('#file_upload input').attr('required', null) } else { $('#file_path').show() $('#file_upload').hide() $('#cloud').hide() + $('#existing_options').hide() $('#file_path input').attr('required', 'required') $('#file_upload input').attr('required', null) $('#importer_parser_fields_file_style_specify_a_path_on_the_server').attr('checked', true) @@ -89,6 +91,7 @@ function handleFileToggle(file_path) { $('#file_path').hide() $('#file_upload').show() $('#cloud').hide() + $('#existing_options').hide() $('#file_path input').attr('required', null) $('#file_upload input').attr('required', 'required') }) @@ -96,6 +99,7 @@ function handleFileToggle(file_path) { $('#file_path').show() $('#file_upload').hide() $('#cloud').hide() + $('#existing_options').hide() $('#file_path input').attr('required', 'required') $('#file_upload input').attr('required', null) }) @@ -103,9 +107,19 @@ function handleFileToggle(file_path) { $('#file_path').hide() $('#file_upload').hide() $('#cloud').show() + $('#existing_options').hide() $('#file_path input').attr('required', null) $('#file_upload input').attr('required', null) }) + $('#importer_parser_fields_file_style_existing_entries').click(function(e){ + $('#file_path').hide() + $('#file_upload').hide() + $('#cloud').hide() + $('#existing_options').show() + $('#file_path input').attr('required', null) + $('#file_upload input').attr('required', null) + }) + } function handleParserKlass() { @@ -189,4 +203,4 @@ function setError(selector, error) { selector.attr('disabled', true) } -$(document).on({'ready': prepBulkrax, 'turbolinks:load': prepBulkrax}) \ No newline at end of file +$(document).on({'ready': prepBulkrax, 'turbolinks:load': prepBulkrax}) diff --git a/app/assets/stylesheets/bulkrax/import_export.scss b/app/assets/stylesheets/bulkrax/import_export.scss index 1834840ac..0e182842f 100644 --- a/app/assets/stylesheets/bulkrax/import_export.scss +++ b/app/assets/stylesheets/bulkrax/import_export.scss @@ -34,4 +34,9 @@ div#s2id_exporter_export_source_collection { .bulkrax-clear-toggles { clear: both; -} \ No newline at end of file +} + +#existing_options .collection_check_boxes { + margin-left: 10px; + margin-right: 10px; +} diff --git a/app/controllers/bulkrax/importers_controller.rb b/app/controllers/bulkrax/importers_controller.rb index 8248975cd..dd58d8970 100644 --- a/app/controllers/bulkrax/importers_controller.rb +++ b/app/controllers/bulkrax/importers_controller.rb @@ -218,7 +218,7 @@ def importable_params end def importable_parser_fields - params&.[](:importer)&.[](:parser_fields)&.except(:file)&.keys + params&.[](:importer)&.[](:parser_fields)&.except(:file, :entry_statuses)&.keys&. + [{ "entry_statuses" => [] }] end # Only allow a trusted parameters through. diff --git a/app/models/bulkrax/importer.rb b/app/models/bulkrax/importer.rb index 01f0a3258..7618de90f 100644 --- a/app/models/bulkrax/importer.rb +++ b/app/models/bulkrax/importer.rb @@ -167,6 +167,10 @@ def metadata_only? parser.parser_fields['metadata_only'] == true end + def existing_entries? + parser.parser_fields['file_style']&.match(/Existing Entries/) + end + def import_works import_objects(['work']) end @@ -189,7 +193,7 @@ def import_objects(types_array = nil) self.only_updates ||= false self.save if self.new_record? # Object needs to be saved for statuses types = types_array || DEFAULT_OBJECT_TYPES - parser.create_objects(types) + existing_entries? ? parser.rebuild_entries(types) : parser.create_objects(types) mark_unseen_as_skipped rescue StandardError => e set_status_info(e) diff --git a/app/parsers/bulkrax/application_parser.rb b/app/parsers/bulkrax/application_parser.rb index a095b129d..ad42ca618 100644 --- a/app/parsers/bulkrax/application_parser.rb +++ b/app/parsers/bulkrax/application_parser.rb @@ -209,6 +209,20 @@ def create_objects(types_array = nil) set_status_info(e) end + def rebuild_entries(_types_array = nil) + index = 0 + importer.entries.where(status_message: parser_fields['entry_statuses']).find_each do |e| + seen[e.identifier] = true + if remove_and_rerun + "Bulkrax::DeleteAndImport#{type.camelize}Job".constantize.send(perform_method, e, current_run) + else + "Bulkrax::Import#{type.camelize}Job".constantize.send(perform_method, e.id, current_run.id) + end + increment_counters(index) + index += 1 + end + end + def create_entry_and_job(current_record, type, identifier = nil) identifier ||= current_record[source_identifier] new_entry = find_or_create_entry(send("#{type}_entry_class"), diff --git a/app/views/bulkrax/importers/_csv_fields.html.erb b/app/views/bulkrax/importers/_csv_fields.html.erb index 758766e73..88dcd61ba 100644 --- a/app/views/bulkrax/importers/_csv_fields.html.erb +++ b/app/views/bulkrax/importers/_csv_fields.html.erb @@ -25,13 +25,17 @@

Add CSV File to Import:

<%# accept a single file upload; data files and bags will need to be added another way %> - <%= fi.input :file_style, collection: ['Upload a File', 'Specify a Path on the Server'], as: :radio_buttons, label: false %> + <%= fi.input :file_style, collection: ['Upload a File', 'Specify a Path on the Server', 'Existing Entries'], as: :radio_buttons, label: false %>
<%= fi.input 'file', as: :file, input_html: { accept: 'text/csv,application/zip' } %>
<%= fi.input :import_file_path, as: :string, input_html: { value: importer.parser_fields['import_file_path'] } %>
+
+ <%= fi.collection_check_boxes :entry_statuses, [['Failed'], ['Pending'], ['Skipped'], ['Deleted'], ['Complete']], :first, :first %> +
+ <% if defined?(::Hyrax) && Hyrax.config.browse_everything? %>

Add Files to Import:

Choose files to upload. The filenames must be unique, and the filenames must be referenced in a column called 'file' in the accompanying CSV file.