From 61604399e3b156c629a68d7b133c913cc5d4ada3 Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Fri, 20 May 2022 14:49:27 -0700 Subject: [PATCH] added missing method for BagIt parser (#516) * added missing method for BagIt parser * added a conditional for rdf entries * WIP bagit rdfentry --- app/models/bulkrax/csv_file_set_entry.rb | 27 +--------------- app/models/bulkrax/rdf_file_set_entry.rb | 7 ++++ .../bulkrax/file_set_entry_behavior.rb | 32 +++++++++++++++++++ app/parsers/bulkrax/bagit_parser.rb | 5 +++ 4 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 app/models/bulkrax/rdf_file_set_entry.rb create mode 100644 app/models/concerns/bulkrax/file_set_entry_behavior.rb diff --git a/app/models/bulkrax/csv_file_set_entry.rb b/app/models/bulkrax/csv_file_set_entry.rb index af5dcdd5..8e722a03 100644 --- a/app/models/bulkrax/csv_file_set_entry.rb +++ b/app/models/bulkrax/csv_file_set_entry.rb @@ -2,31 +2,6 @@ module Bulkrax class CsvFileSetEntry < CsvEntry - def factory_class - ::FileSet - end - - def add_path_to_file - parsed_metadata['file'].each_with_index do |filename, i| - path_to_file = ::File.join(parser.path_to_files, filename) - - parsed_metadata['file'][i] = path_to_file - end - raise ::StandardError, "one or more file paths are invalid: #{parsed_metadata['file'].join(', ')}" unless parsed_metadata['file'].map { |file_path| ::File.file?(file_path) }.all? - - parsed_metadata['file'] - end - - def validate_presence_of_filename! - return if parsed_metadata&.[]('file')&.map(&:present?)&.any? - - raise StandardError, 'File set must have a filename' - end - - def validate_presence_of_parent! - return if parsed_metadata[related_parents_parsed_mapping]&.map(&:present?)&.any? - - raise StandardError, 'File set must be related to at least one work' - end + include FileSetEntryBehavior end end diff --git a/app/models/bulkrax/rdf_file_set_entry.rb b/app/models/bulkrax/rdf_file_set_entry.rb new file mode 100644 index 00000000..09c0bdee --- /dev/null +++ b/app/models/bulkrax/rdf_file_set_entry.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Bulkrax + class RdfFileSetEntry < RdfEntry + include FileSetEntryBehavior + end +end diff --git a/app/models/concerns/bulkrax/file_set_entry_behavior.rb b/app/models/concerns/bulkrax/file_set_entry_behavior.rb new file mode 100644 index 00000000..7e99cc33 --- /dev/null +++ b/app/models/concerns/bulkrax/file_set_entry_behavior.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module Bulkrax + module FileSetEntryBehavior + def factory_class + ::FileSet + end + + def add_path_to_file + parsed_metadata['file'].each_with_index do |filename, i| + path_to_file = ::File.join(parser.path_to_files, filename) + + parsed_metadata['file'][i] = path_to_file + end + raise ::StandardError, "one or more file paths are invalid: #{parsed_metadata['file'].join(', ')}" unless parsed_metadata['file'].map { |file_path| ::File.file?(file_path) }.all? + + parsed_metadata['file'] + end + + def validate_presence_of_filename! + return if parsed_metadata&.[]('file')&.map(&:present?)&.any? + + raise StandardError, 'File set must have a filename' + end + + def validate_presence_of_parent! + return if parsed_metadata[related_parents_parsed_mapping]&.map(&:present?)&.any? + + raise StandardError, 'File set must be related to at least one work' + end + end +end diff --git a/app/parsers/bulkrax/bagit_parser.rb b/app/parsers/bulkrax/bagit_parser.rb index 9391e8e1..bbbe4be0 100644 --- a/app/parsers/bulkrax/bagit_parser.rb +++ b/app/parsers/bulkrax/bagit_parser.rb @@ -23,6 +23,11 @@ def collection_entry_class Entry end + def file_set_entry_class + csv_format = Bulkrax::Importer.last.parser_fields['metadata_format'] == "Bulkrax::CsvEntry" + csv_format ? CsvFileSetEntry : RdfFileSetEntry + end + # Take a random sample of 10 metadata_paths and work out the import fields from that def import_fields raise StandardError, 'No metadata files were found' if metadata_paths.blank?