From ef3d21e6a13219251f467728d03a29773948bc43 Mon Sep 17 00:00:00 2001 From: Max Kadel Date: Tue, 28 Sep 2021 18:51:18 -0400 Subject: [PATCH] Accepts "file" as object_type, does not actually process file --- app/uploaders/zizia/csv_manifest_validator.rb | 30 +++++++++++- lib/zizia/hyrax/hyrax_record_importer.rb | 47 ++++++++++++------- .../csv_import/good/mix_of_object_types.csv | 1 + .../zizia/csv_manfest_validator_spec.rb | 12 +++++ 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/app/uploaders/zizia/csv_manifest_validator.rb b/app/uploaders/zizia/csv_manifest_validator.rb index c5ee77a..b72e778 100644 --- a/app/uploaders/zizia/csv_manifest_validator.rb +++ b/app/uploaders/zizia/csv_manifest_validator.rb @@ -64,6 +64,28 @@ def parse_csv @errors << 'We are unable to read this CSV file.' end + def object_types + return ["work"] unless @transformed_headers.include?("object type") + original_object_types = @rows.map { |row| row[@transformed_headers.find_index("object type")] } + original_object_types.map { |object_type| map_object_type(object_type) }.compact.uniq + end + + def map_object_type(orig_value) + case orig_value&.downcase + when "c", "collection" + "collection" + when "w", "work" + "work" + when "f", "file" + "file" + # Don't return an object type for the header + when "object type" + nil + else + "work" + end + end + def missing_headers required_headers.each do |header| next if @transformed_headers.include?(header) @@ -71,10 +93,16 @@ def missing_headers end end + def required_headers_for_sheet + object_types.flat_map { |object_type| required_headers(object_type) }.compact.uniq + end + def required_headers(object_type = "w") return work_headers if object_type.nil? if object_type.casecmp("c").zero? || object_type.casecmp("collection").zero? ['title', 'visibility'] + elsif object_type.casecmp("f").zero? || object_type.casecmp("file").zero? + ['files', 'parent'] else work_headers end @@ -158,7 +186,7 @@ def valid_rights_statements end def valid_object_types - @valid_object_types ||= ['c', 'collection', 'w', 'work'] + @valid_object_types ||= ['c', 'collection', 'w', 'work', 'f', 'file'] end # Make sure this column contains only valid values diff --git a/lib/zizia/hyrax/hyrax_record_importer.rb b/lib/zizia/hyrax/hyrax_record_importer.rb index 5628b49..ca3548a 100644 --- a/lib/zizia/hyrax/hyrax_record_importer.rb +++ b/lib/zizia/hyrax/hyrax_record_importer.rb @@ -120,6 +120,8 @@ def determine_object_type(object_type_string) Collection when "w", "work" Hyrax.config.curation_concerns.first + when "f", "file" + FileSet else raise "[zizia] Unrecognized object_type: #{object_type_string}" end @@ -232,30 +234,41 @@ def process_collection_attrs(record:) attrs end + def create_collection(record) + created = Collection.new + attrs = process_collection_attrs(record: record) + created.update(attrs) + created.save! + end + + def create_curation_concern(record, import_type) + created = import_type.new + attrs = process_attrs(record: record) + actor_env = Hyrax::Actors::Environment.new(created, + ::Ability.new(depositor), + attrs) + if Hyrax::CurationConcern.actor.create(actor_env) + Rails.logger.info "[zizia] event: record_created, batch_id: #{batch_id}, record_id: #{created.id}, collection_id: #{collection_id}, record_title: #{attrs[:title]&.first}" + csv_import_detail.success_count += 1 + else + created.errors.each do |attr, msg| + Rails.logger.error "[zizia] event: validation_failed, batch_id: #{batch_id}, collection_id: #{collection_id}, attribute: #{attr.capitalize}, message: #{msg}, record_title: record_title: #{attrs[:title] ? attrs[:title] : attrs}" + end + csv_import_detail.failure_count += 1 + end + end + # Create an object using the Hyrax actor stack # We assume the object was created as expected if the actor stack returns true. def create_for(record:) Rails.logger.info "[zizia] event: record_import_started, batch_id: #{batch_id}, collection_id: #{collection_id}, record_title: #{record.respond_to?(:title) ? record.title : record}" import_type = import_type(record) - created = import_type.new if import_type == Collection - attrs = process_collection_attrs(record: record) - created.update(attrs) - created.save! + create_collection(record) + elsif import_type == FileSet + Rails.logger.error "[zizia] event: Attempted to create a FileSet; however, this is not yet implemented" else - attrs = process_attrs(record: record) - actor_env = Hyrax::Actors::Environment.new(created, - ::Ability.new(depositor), - attrs) - if Hyrax::CurationConcern.actor.create(actor_env) - Rails.logger.info "[zizia] event: record_created, batch_id: #{batch_id}, record_id: #{created.id}, collection_id: #{collection_id}, record_title: #{attrs[:title]&.first}" - csv_import_detail.success_count += 1 - else - created.errors.each do |attr, msg| - Rails.logger.error "[zizia] event: validation_failed, batch_id: #{batch_id}, collection_id: #{collection_id}, attribute: #{attr.capitalize}, message: #{msg}, record_title: record_title: #{attrs[:title] ? attrs[:title] : attrs}" - end - csv_import_detail.failure_count += 1 - end + create_curation_concern(record, import_type) end csv_import_detail.save end diff --git a/spec/dummy/spec/fixtures/csv_import/good/mix_of_object_types.csv b/spec/dummy/spec/fixtures/csv_import/good/mix_of_object_types.csv index 0661f6c..cc22496 100644 --- a/spec/dummy/spec/fixtures/csv_import/good/mix_of_object_types.csv +++ b/spec/dummy/spec/fixtures/csv_import/good/mix_of_object_types.csv @@ -5,3 +5,4 @@ w,Work on tomatoes,"Tomato, Tommy",fruits|~|tomatoes,http://rightsstatements.org wOrk,Work on cabbages,"Cabbage, Carl",vegetables|~|cabbages,http://rightsstatements.org/vocab/InC/1.0/,public,dog.jpg,abs/234 coLLEction,Collection of legumes,,,,public,,def/345 ,Work without object type,"Cabbage, Carl",vegetables|~|cabbages,http://rightsstatements.org/vocab/InC/1.0/,public,dog.jpg,abc/345 +f,,,,,,dog.jpg,,abc/123 diff --git a/spec/uploaders/zizia/csv_manfest_validator_spec.rb b/spec/uploaders/zizia/csv_manfest_validator_spec.rb index 8a17d35..c31e999 100644 --- a/spec/uploaders/zizia/csv_manfest_validator_spec.rb +++ b/spec/uploaders/zizia/csv_manfest_validator_spec.rb @@ -46,6 +46,12 @@ expect(validator.errors).to eq([]) expect(validator.warnings).to eq([]) end + + it "collects all the object_types in the file" do + required_file_plus_work_headers = ['title', 'creator', 'keyword', 'rights statement', 'visibility', 'files', 'deduplication_key', 'parent'] + expect(validator.object_types).to match_array(["collection", "work", "file"]) + expect(validator.required_headers_for_sheet).to match_array(required_file_plus_work_headers) + end end context "with a object type column" do @@ -60,6 +66,12 @@ expect(validator.send(:valid_headers).sort).to eq(Zizia::HyraxBasicMetadataMapper.new.headers.map(&:to_s).sort) end + it "collects all the object_types in the file" do + required_work_headers = ['title', 'creator', 'keyword', 'rights statement', 'visibility', 'files', 'deduplication_key'] + expect(validator.object_types).to eq(["work", "collection"]) + expect(validator.required_headers_for_sheet).to match_array(required_work_headers) + end + it "returns required headers based on the object type" do required_work_headers = ['title', 'creator', 'keyword', 'rights statement', 'visibility', 'files', 'deduplication_key'] required_collection_headers = ['title', 'visibility']