Skip to content

Commit

Permalink
Use the FileMetadata#type not FileSet#original_file_id for use
Browse files Browse the repository at this point in the history
FileSet's use "relation" should be controlled exclusively by the related
FileMetadata's `#type` property. Move toward removing the `original_file_id` and
other `FileSet` attributes by reworking the existing queries.
  • Loading branch information
Tom Johnson committed Jan 31, 2020
1 parent 838cb01 commit cbed6f2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 55 deletions.
2 changes: 2 additions & 0 deletions app/services/hyrax/custom_queries/find_file_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def find_many_file_metadata_by_ids(ids:)
# @example
# Hyrax.query_service.find_file_metadata_by_use(use: ::RDF::URI("http://pcdm.org/ExtractedText"))
def find_many_file_metadata_by_use(resource:, use:)
return [] unless resource.try(:file_ids)

results = find_many_file_metadata_by_ids(ids: resource.file_ids)
results.select { |fm| fm.type.include?(use) }
end
Expand Down
62 changes: 35 additions & 27 deletions app/services/hyrax/custom_queries/navigators/find_files.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Hyrax
module CustomQueries
module Navigators
##
# @example
# Hyrax.custom_queries.find_files(file_set: file_set_resource)
# Hyrax.custom_queries.find_original_file(file_set: file_set_resource)
# Hyrax.custom_queries.find_extracted_text(file_set: file_set_resource)
# Hyrax.custom_queries.find_thumbnail(file_set: file_set_resource)
class FindFiles
# @example
# Hyrax.custom_queries.find_files(file_set: file_set_resource)
# Hyrax.custom_queries.find_original_file(file_set: file_set_resource)
# Hyrax.custom_queries.find_extracted_text(file_set: file_set_resource)
# Hyrax.custom_queries.find_thumbnail(file_set: file_set_resource)

def self.queries
[:find_files,
:find_original_file,
Expand Down Expand Up @@ -39,40 +39,48 @@ def find_files(file_set:)
# @param file_set [Hyrax::FileSet]
# @return [Hyrax::FileMetadata]
def find_original_file(file_set:)
if file_set.respond_to?(:original_file_id)
raise ::Valkyrie::Persistence::ObjectNotFoundError, "File set's original file is blank" if file_set.original_file_id.blank?
query_service.custom_queries.find_file_metadata_by(id: file_set.original_file_id)
else
raise ::Valkyrie::Persistence::ObjectNotFoundError,
"#{file_set.internal_resource} is not a `Hydra::FileSet` implementer"
end
find_exactly_one_file_by_use(
file_set: file_set,
use: Hyrax::FileMetadata::Use::ORIGINAL_FILE
)
end

# Find extracted text id of a given file set resource, and map to file metadata resource
# @param file_set [Hyrax::FileSet]
# @return [Hyrax::FileMetadata]
def find_extracted_text(file_set:)
if file_set.respond_to?(:extracted_text_id)
raise ::Valkyrie::Persistence::ObjectNotFoundError, "File set's extracted text is blank" if file_set.extracted_text_id.blank?
query_service.custom_queries.find_file_metadata_by(id: file_set.extracted_text_id)
else
raise ::Valkyrie::Persistence::ObjectNotFoundError,
"#{file_set.internal_resource} is not a `Hydra::FileSet` implementer"
end
find_exactly_one_file_by_use(
file_set: file_set,
use: Hyrax::FileMetadata::Use::EXTRACTED_TEXT
)
end

# Find thumbnail id of a given file set resource, and map to file metadata resource
# @param file_set [Hyrax::FileSet]
# @return [Hyrax::FileMetadata]
def find_thumbnail(file_set:)
if file_set.respond_to?(:thumbnail_id)
raise ::Valkyrie::Persistence::ObjectNotFoundError, "File set's thumbnail is blank" if file_set.thumbnail_id.blank?
query_service.custom_queries.find_file_metadata_by(id: file_set.thumbnail_id)
else
raise ::Valkyrie::Persistence::ObjectNotFoundError,
"#{file_set.internal_resource} is not a `Hydra::FileSet` implementer"
end
find_exactly_one_file_by_use(
file_set: file_set,
use: Hyrax::FileMetadata::Use::THUMBNAIL
)
end

private

##
# @api private
#
# @return [Hyrax::FileMetadata]
# @raise [Valkyrie::Persistence::ObjectNotFoundError]
def find_exactly_one_file_by_use(file_set:, use:)
files =
query_service.custom_queries.find_many_file_metadata_by_use(resource: file_set, use: use)

raise Valkyrie::Persistence::ObjectNotFoundError, "FileSet #{file_set.id}'s #{use.fragment} is missing." if
files.blank?

files.first
end
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/wings/services/file_metadata_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ def attach_file_metadata_to_valkyrie_file_set(file_metadata, file_set)
# @api private
# @return [Hyrax::FileMetadata, nil]
def current_original_file(file_set)
Hyrax.query_service
.custom_queries
.find_many_file_metadata_by_use(resource: file_set, use: Hyrax::FileMetadata::Use::ORIGINAL_FILE)
.first
Hyrax.custom_queries.find_original_file(file_set: file_set)
rescue ::Valkyrie::Persistence::ObjectNotFoundError
nil
end

# Class for wrapping the file being ingested
Expand Down
66 changes: 42 additions & 24 deletions spec/services/hyrax/custom_queries/navigators/find_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

describe '#find_files' do
context 'when files exist' do
let!(:file_metadata1) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let!(:file_metadata2) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [file_metadata1, file_metadata2]) }
let(:file_metadata1) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let(:file_metadata2) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [file_metadata1, file_metadata2]) }

it 'returns file metadata resource' do
expect(query_handler.find_files(file_set: fileset).map(&:id).map(&:to_s)).to match_array [file_metadata1.id.to_s, file_metadata2.id.to_s]
expect(query_handler.find_files(file_set: fileset).map(&:id).map(&:to_s))
.to match_array [file_metadata1.id.to_s, file_metadata2.id.to_s]
end
end

context 'when files do not exist' do
let!(:fileset) { FactoryBot.build(:hyrax_file_set) }
let(:fileset) { FactoryBot.build(:hyrax_file_set) }

it 'returns an empty array' do
expect(query_handler.find_files(file_set: fileset)).to be_empty
end
Expand All @@ -23,78 +26,93 @@

describe '#find_original_file' do
context 'when original file exists' do
let!(:original_file) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [original_file], original_file: original_file) }
let(:original_file) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [original_file]) }

it 'returns file metadata resource' do
expect(query_handler.find_original_file(file_set: fileset).id.to_s).to eq original_file.id.to_s
end
end

context 'when files do not exist' do
let!(:fileset) { FactoryBot.build(:hyrax_file_set) }
let(:fileset) { FactoryBot.build(:hyrax_file_set) }

it 'raises error' do
expect { query_handler.find_original_file(file_set: fileset) }
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError, "File set's original file is blank"
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError
end
end

context 'when file_set does not respond to original file' do
let!(:fileset) { FactoryBot.build(:hyrax_resource) }
let(:fileset) { FactoryBot.build(:hyrax_resource) }

it 'raises error' do
expect { query_handler.find_original_file(file_set: fileset) }
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError, "Hyrax::Resource is not a `Hydra::FileSet` implementer"
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError
end
end
end

describe '#find_extracted_text' do
context 'when extracted text exists' do
let!(:extracted_text) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [extracted_text], extracted_text: extracted_text) }
let(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [extracted_text]) }

let(:extracted_text) do
FactoryBot.valkyrie_create(:hyrax_file_metadata, type: Hyrax::FileMetadata::Use::EXTRACTED_TEXT)
end

it 'returns file metadata resource' do
expect(query_handler.find_extracted_text(file_set: fileset).id.to_s).to eq extracted_text.id.to_s
end
end

context 'when files do not exist' do
let!(:fileset) { FactoryBot.build(:hyrax_file_set) }
let(:fileset) { FactoryBot.build(:hyrax_file_set) }

it 'raises error' do
expect { query_handler.find_extracted_text(file_set: fileset) }
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError, "File set's extracted text is blank"
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError
end
end

context 'when file_set does not respond to extracted text' do
let!(:fileset) { FactoryBot.build(:hyrax_resource) }
context 'when file_set class does not does not have any files' do
let(:fileset) { FactoryBot.build(:hyrax_resource) }

it 'raises error' do
expect { query_handler.find_extracted_text(file_set: fileset) }
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError, "Hyrax::Resource is not a `Hydra::FileSet` implementer"
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError
end
end
end

describe '#find_thumbnail' do
context 'when thumbnail exists' do
let!(:thumbnail) { FactoryBot.valkyrie_create(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [thumbnail], thumbnail: thumbnail) }
let(:fileset) { FactoryBot.valkyrie_create(:hyrax_file_set, files: [thumbnail], thumbnail: thumbnail) }

let(:thumbnail) do
FactoryBot.valkyrie_create(:hyrax_file_metadata, type: Hyrax::FileMetadata::Use::THUMBNAIL)
end

it 'returns file metadata resource' do
expect(query_handler.find_thumbnail(file_set: fileset).id.to_s).to eq thumbnail.id.to_s
end
end

context 'when files do not exist' do
let!(:fileset) { FactoryBot.build(:hyrax_file_set) }
let(:fileset) { FactoryBot.build(:hyrax_file_set) }

it 'raises error' do
expect { query_handler.find_thumbnail(file_set: fileset) }
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError, "File set's thumbnail is blank"
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError
end
end

context 'when file_set does not respond to thumbnail' do
let!(:fileset) { FactoryBot.build(:hyrax_resource) }
let(:fileset) { FactoryBot.build(:hyrax_resource) }

it 'raises error' do
expect { query_handler.find_thumbnail(file_set: fileset) }
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError, "Hyrax::Resource is not a `Hydra::FileSet` implementer"
.to raise_error ::Valkyrie::Persistence::ObjectNotFoundError
end
end
end
Expand Down

0 comments on commit cbed6f2

Please sign in to comment.