Skip to content

Commit

Permalink
Accepts "file" as object_type, does not actually process file
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel committed Sep 28, 2021
1 parent 6664183 commit ef3d21e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
30 changes: 29 additions & 1 deletion app/uploaders/zizia/csv_manifest_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,45 @@ 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)
@errors << "Missing required column: \"#{header.titleize}\". Your spreadsheet must have this column."
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
Expand Down Expand Up @@ -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
Expand Down
47 changes: 30 additions & 17 deletions lib/zizia/hyrax/hyrax_record_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions spec/uploaders/zizia/csv_manfest_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']
Expand Down

0 comments on commit ef3d21e

Please sign in to comment.