Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the FileMetadata#type not FileSet#original_file_id for use #4236

Merged
merged 2 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
no-reply marked this conversation as resolved.
Show resolved Hide resolved
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.empty?

files.first
end
end
end
end
Expand Down
69 changes: 0 additions & 69 deletions lib/hyrax/specs/shared_specs/hydra_works.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,73 +215,4 @@
end
end
end

describe 'original file' do
it 'has nil original_file_id by default' do
expect(fileset.original_file_id).to be_nil
end

context 'with original file' do
let(:file_class) { Hyrax::FileMetadata }
let(:original_file) { persister.save(resource: file_class.new) }
let(:original_file_id) { original_file.id }
let(:second_file) { persister.save(resource: file_class.new) }

before { fileset.original_file_id = original_file_id }

it 'has original_file_id' do
expect(fileset.original_file_id).to eq original_file_id
end

it 'can query original file' do
expect(query_service.custom_queries.find_original_file(file_set: fileset)).to eq original_file
end
end
end

describe 'extracted text' do
it 'has nil extracted_text_id by default' do
expect(fileset.extracted_text_id).to be_nil
end

context 'with extracted text' do
let(:file_class) { Hyrax::FileMetadata }
let(:extracted_text) { persister.save(resource: file_class.new) }
let(:extracted_text_id) { extracted_text.id }
let(:second_file) { persister.save(resource: file_class.new) }

before { fileset.extracted_text_id = extracted_text_id }

it 'has extracted_text_id' do
expect(fileset.extracted_text_id).to eq extracted_text_id
end

it 'can query extracted text' do
expect(query_service.custom_queries.find_extracted_text(file_set: fileset)).to eq extracted_text
end
end
end

describe 'thumbnail' do
it 'has nil thumbnail_id by default' do
expect(fileset.thumbnail_id).to be_nil
end

context 'with thumbnail' do
let(:file_class) { Hyrax::FileMetadata }
let(:thumbnail) { persister.save(resource: file_class.new) }
let(:thumbnail_id) { thumbnail.id }
let(:second_file) { persister.save(resource: file_class.new) }

before { fileset.thumbnail_id = thumbnail_id }

it 'has thumbnail_id' do
expect(fileset.thumbnail_id).to eq thumbnail_id
end

it 'can query thumbnail' do
expect(query_service.custom_queries.find_thumbnail(file_set: fileset)).to eq thumbnail
end
end
end
end
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
77 changes: 48 additions & 29 deletions spec/services/hyrax/custom_queries/navigators/find_files_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
RSpec.describe Hyrax::CustomQueries::Navigators::FindFiles do
let(:query_service) { Valkyrie::MetadataAdapter.find(:test_adapter).query_service }
subject(:query_handler) { described_class.new(query_service: query_service) }
RSpec.describe Hyrax::CustomQueries::Navigators::FindFiles, valkyrie_adapter: :test_adapter do
subject(:query_handler) do
described_class.new(query_service: Hyrax.query_service)
end

describe '#find_files' do
context 'when files exist' do
let!(:file_metadata1) { FactoryBot.create_using_test_adapter(:hyrax_file_metadata) }
let!(:file_metadata2) { FactoryBot.create_using_test_adapter(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.create_using_test_adapter(: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))
no-reply marked this conversation as resolved.
Show resolved Hide resolved
.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 @@ -22,78 +26,93 @@

describe '#find_original_file' do
context 'when original file exists' do
let!(:original_file) { FactoryBot.create_using_test_adapter(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.create_using_test_adapter(: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
no-reply marked this conversation as resolved.
Show resolved Hide resolved
end
end

context 'when file_set does not respond to original file' do
let!(:fileset) { FactoryBot.build(:hyrax_resource) }
context 'when resource does not respond file_ids' do
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.create_using_test_adapter(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.create_using_test_adapter(: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 resource does not respond file_ids' 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.create_using_test_adapter(:hyrax_file_metadata) }
let!(:fileset) { FactoryBot.create_using_test_adapter(: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) }
context 'when resource does not respond file_ids' do
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