Skip to content

Commit

Permalink
Merge pull request #4215 from samvera/valk-char
Browse files Browse the repository at this point in the history
Create `FileSetDescription` to find characterization for FileSets
  • Loading branch information
elrayle committed Jan 30, 2020
2 parents 226e010 + a749f23 commit 1d5c2f2
Show file tree
Hide file tree
Showing 36 changed files with 233 additions and 118 deletions.
35 changes: 17 additions & 18 deletions app/actors/hyrax/actors/file_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,28 @@ def normalize_relation(relation)
end

def normalize_relation_for_active_fedora(relation)
return relation if relation.is_a? Symbol
return relation.to_sym if relation.respond_to? :to_sym

# TODO: whereever these are set, they should use FileSet.*_use... making the casecmp unnecessary
return :original_file if relation.to_s.casecmp(Hyrax::FileSet::ORIGINAL_FILE_USE.to_s)
return :extracted_file if relation.to_s.casecmp(Hyrax::FileSet::EXTRACTED_TEXT_USE.to_s)
return :thumbnail_file if relation.to_s.casecmp(Hyrax::FileSet::THUMBNAIL_USE.to_s)
:original_file
case relation
when Hyrax::FileMetadata::Use::ORIGINAL_FILE
:original_file
when Hyrax::FileMetadata::Use::EXTRACTED_TEXT
:extracted_file
when Hyrax::FileMetadata::Use::THUMBNAIL
:thumbnail_file
else
:original_file
end
end

##
# @return [RDF::URI]
def normalize_relation_for_valkyrie(relation)
# TODO: When this is fully switched to valkyrie, this should probably be removed and relation should always be passed
# in as a valid URI already set to the file's use
case relation.to_s.to_sym
when :original_file
Hyrax::FileSet::ORIGINAL_FILE_USE
when :extracted_file
Hyrax::FileSet.EXTRACTED_TEXT_USE
when :thumbnail_file
Hyrax::FileSet::THUMBNAIL_USE
else
Hyrax::FileSet::ORIGINAL_FILE_USE
end
return relation if relation.is_a?(RDF::URI)

Hyrax::FileMetadata::Use.uri_for(use: relation.to_sym)
rescue ArgumentError
Hyrax::FileMetadata::Use::ORIGINAL_FILE
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def update
filter_docs_with_edit_access!
copy_visibility = []
copy_visibility = params[:embargoes].values.map { |h| h[:copy_visibility] } if params[:embargoes]
af_objects = Hyrax.query_service.custom_queries.find_many_by_alternate_ids(alternate_ids: batch, use_valkyrie: false)
af_objects = Hyrax.custom_queries.find_many_by_alternate_ids(alternate_ids: batch, use_valkyrie: false)
af_objects.each do |curation_concern|
Hyrax::Actors::EmbargoActor.new(curation_concern).destroy
# if the concern is a FileSet, set its visibility and visibility propagation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def update
filter_docs_with_edit_access!
copy_visibility = []
copy_visibility = params[:leases].values.map { |h| h[:copy_visibility] } if params[:leases]
af_objects = Hyrax.query_service.custom_queries.find_many_by_alternate_ids(alternate_ids: batch, use_valkyrie: false)
af_objects = Hyrax.custom_queries.find_many_by_alternate_ids(alternate_ids: batch, use_valkyrie: false)
af_objects.each do |curation_concern|
Hyrax::Actors::LeaseActor.new(curation_concern).destroy
Hyrax::VisibilityPropagator.for(source: curation_concern).propagate if
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/concerns/hyrax/members_permission_job_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def file_set_ids(work)
when ActiveFedora::Base
::FileSet.search_with_conditions(id: work.member_ids).map(&:id)
when Valkyrie::Resource
Hyrax.query_service.custom_queries.find_child_fileset_ids(resource: work)
Hyrax.custom_queries.find_child_fileset_ids(resource: work)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/hyrax/collection_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def collection_type=(new_collection_type)
# add_member_objects using the member_of_collections relationship. Deprecate?
def add_members(new_member_ids)
return if new_member_ids.blank?
members << Hyrax.query_service.custom_queries.find_many_by_alternate_ids(alternate_ids: new_member_ids, use_valkyrie: false)
members << Hyrax.custom_queries.find_many_by_alternate_ids(alternate_ids: new_member_ids, use_valkyrie: false)
end

# Add member objects by adding this collection to the objects' member_of_collection association.
Expand Down
49 changes: 42 additions & 7 deletions app/models/hyrax/file_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@

module Hyrax
class FileMetadata < Valkyrie::Resource
GENERIC_MIME_TYPE = 'application/octet-stream'

##
# Constants for PCDM Use URIs; use these constants in place of hard-coded
# URIs in the `::Valkyrie::Vocab::PCDMUse` vocabulary.
module Use
ORIGINAL_FILE = ::Valkyrie::Vocab::PCDMUse.OriginalFile
EXTRACTED_TEXT = ::Valkyrie::Vocab::PCDMUse.ExtractedText
THUMBNAIL = ::Valkyrie::Vocab::PCDMUse.ThumbnailImage

##
# @param use [Symbol]
#
# @return [RDF::URI]
# @raise [ArgumentError] if no use is known for the argument
def uri_for(use:)
case use
when :original_file
ORIGINAL_FILE
when :extracted_file
EXTRACTED_TEXT
when :thumbnail_file
THUMBNAIL
else
raise ArgumentError, "No PCDM use is recognized for #{use}"
end
end
module_function :uri_for
end

attribute :file_identifiers, ::Valkyrie::Types::Set # id of the file stored by the storage adapter
attribute :alternate_ids, Valkyrie::Types::Set.of(Valkyrie::Types::ID) # id of the Hydra::PCDM::File which holds metadata and the file in ActiveFedora
attribute :file_set_id, ::Valkyrie::Types::ID # id of parent file set resource

# all remaining attributes are on AF::File metadata_node unless otherwise noted
attribute :label, ::Valkyrie::Types::Set
attribute :original_filename, ::Valkyrie::Types::Set
attribute :mime_type, ::Valkyrie::Types::Set
attribute :type, ::Valkyrie::Types::Set # AF::File type
attribute :mime_type, ::Valkyrie::Types::String.default(GENERIC_MIME_TYPE)
attribute :type, ::Valkyrie::Types::Set.default([Use::ORIGINAL_FILE])
attribute :content, ::Valkyrie::Types::Set

# attributes set by fits
Expand Down Expand Up @@ -75,20 +105,25 @@ class FileMetadata < Valkyrie::Resource
def self.for(file:)
new(label: file.original_filename,
original_filename: file.original_filename,
mime_type: file.content_type,
type: file.try(:type) || [Hyrax::FileSet::ORIGINAL_FILE_USE])
mime_type: file.content_type)
end

##
# @return [Boolean]
def original_file?
type.include?(Hyrax::FileSet::ORIGINAL_FILE_USE)
type.include?(Use::ORIGINAL_FILE)
end

##
# @return [Boolean]
def thumbnail_file?
type.include?(Hyrax::FileSet::THUMBNAIL_USE)
type.include?(Use::THUMBNAIL)
end

##
# @return [Boolean]
def extracted_file?
type.include?(Hyrax::FileSet::EXTRACTED_TEXT_USE)
type.include?(Use::EXTRACTED_TEXT)
end

def title
Expand Down
35 changes: 0 additions & 35 deletions app/models/hyrax/file_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ module Hyrax
class FileSet < Hyrax::Resource
include Hyrax::Schema(:core_metadata)

ORIGINAL_FILE_USE = ::Valkyrie::Vocab::PCDMUse.OriginalFile
EXTRACTED_TEXT_USE = ::Valkyrie::Vocab::PCDMUse.ExtractedText
THUMBNAIL_USE = ::Valkyrie::Vocab::PCDMUse.Thumbnail

attribute :file_ids, Valkyrie::Types::Array.of(Valkyrie::Types::ID) # id for FileMetadata resources
attribute :original_file_id, Valkyrie::Types::ID # id for FileMetadata resource
attribute :thumbnail_id, Valkyrie::Types::ID # id for FileMetadata resource
Expand All @@ -28,36 +24,5 @@ def pcdm_object?
def file_set?
true
end

##
# Gives file metadata for the file filling the http://pcdm.org/OriginalFile use
# @return [FileMetadata] the FileMetadata resource of the original file
def original_file
filter_files_by_type(Hyrax::FileSet::ORIGINAL_FILE_USE).first
end

##
# Gives file metadata for the file filling the http://pcdm.org/ExtractedText use
# @return [FileMetadata] the FileMetadata resource of the extracted text
def extracted_text
filter_files_by_type(Hyrax::FileSet::EXTRACTED_TEXT_USE).first
end

##
# Gives file metadata for the file filling the http://pcdm.org/Thumbnail use
# @return [FileMetadata] the FileMetadata resource of the thumbnail
def thumbnail
filter_files_by_type(Hyrax::FileSet::THUMBNAIL_USE).first
end

##
# Gives file metadata for files that have the requested RDF Type for use
# @param [RDF::URI] uri for the desired Type
# @return [Enumerable<FileMetadata>] the FileMetadata resources
# @example
# filter_files_by_type(::RDF::URI("http://pcdm.org/ExtractedText"))
def filter_files_by_type(uri)
Hyrax.query_service.custom_queries.find_many_file_metadata_by_use(resource: self, use: uri)
end
end
end
2 changes: 1 addition & 1 deletion app/models/job_io_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def to_file_metadata
Hyrax::FileMetadata.new(label: original_name,
original_filename: original_name,
mime_type: mime_type,
use: [Hyrax::FileSet::ORIGINAL_FILE_USE])
use: [Hyrax::FileMetadata::Use::ORIGINAL_FILE])
end

# The magic that switches *once* between local filepath and CarrierWave file
Expand Down
45 changes: 45 additions & 0 deletions app/services/hyrax/characterization/file_set_description.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Hyrax
module Characterization
##
# @api public
class FileSetDescription
include Hydra::Works::MimeTypes

##
# @!attribute [rw] file_set
# @return [Hyrax::FileSet]
attr_accessor :file_set

delegate :mime_type, to: :primary_file

##
# @param [Hyrax::FileSet] file_set
# @param [Symbol] primary_file a symbol mapping to the file_set member
# used for characterization
def initialize(file_set:, primary_file: :original_file)
self.file_set = file_set

@primary_file_type_uri =
Hyrax::FileMetadata::Use.uri_for(use: primary_file)
end

##
# @api public
# @return [Hyrax::FileMetadata] the member file to use for characterization
def primary_file
queries.find_many_file_metadata_by_use(resource: file_set, use: @primary_file_type_uri).first ||
Hyrax::FileMetadata.new
end

private

##
# @api private
def queries
Hyrax.custom_queries
end
end
end
end
Empty file.
2 changes: 1 addition & 1 deletion app/services/hyrax/custom_queries/find_access_control.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Hyrax
module CustomQueries
# @example
# Hyrax.query_service.custom_queries.find_access_control_for(resource: resource)
# Hyrax.custom_queries.find_access_control_for(resource: resource)
class FindAccessControl
def self.queries
[:find_access_control_for]
Expand Down
9 changes: 5 additions & 4 deletions app/services/hyrax/custom_queries/find_file_metadata.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Provide custom queries for finding Hyrax::FileMetadata
# @example
# Hyrax.query_service.custom_queries.find_file_metadata_by(id: valkyrie_id)
# Hyrax.query_service.custom_queries.find_file_metadata_by_alternate_identifier(alternate_identifier: alt_id)
# Hyrax.query_service.custom_queries.find_many_file_metadata_by_ids(ids: [valkyrie_id, valkyrie_id])
# Hyrax.custom_queries.find_file_metadata_by(id: valkyrie_id)
# Hyrax.custom_queries.find_file_metadata_by_alternate_identifier(alternate_identifier: alt_id)
# Hyrax.custom_queries.find_many_file_metadata_by_ids(ids: [valkyrie_id, valkyrie_id])
module Hyrax
module CustomQueries
class FindFileMetadata
def self.queries
[:find_file_metadata_by,
:find_file_metadata_by_alternate_identifier,
:find_many_file_metadata_by_ids]
:find_many_file_metadata_by_ids,
:find_many_file_metadata_by_use]
end

def initialize(query_service:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Hyrax
module CustomQueries
class FindManyByAlternateIds
# Use:
# Hyrax.query_service.custom_queries.find_many_by_alternate_ids(alternate_ids: ids)
# Hyrax.custom_queries.find_many_by_alternate_ids(alternate_ids: ids)

def self.queries
[:find_many_by_alternate_ids]
Expand Down
8 changes: 4 additions & 4 deletions app/services/hyrax/custom_queries/navigators/find_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module CustomQueries
module Navigators
class FindFiles
# @example
# Hyrax.query_service.custom_queries.find_files(file_set: file_set_resource)
# Hyrax.query_service.custom_queries.find_original_file(file_set: file_set_resource)
# Hyrax.query_service.custom_queries.find_extracted_text(file_set: file_set_resource)
# Hyrax.query_service.custom_queries.find_thumbnail(file_set: file_set_resource)
# 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,
Expand Down
2 changes: 1 addition & 1 deletion app/services/hyrax/resource_visibility_propagator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(source:,
embargo_manager: Hyrax::EmbargoManager,
lease_manager: Hyrax::LeaseManager,
persister: Hyrax.persister,
queries: Hyrax.query_service.custom_queries)
queries: Hyrax.custom_queries)
@persister = persister
@queries = queries
self.source = source
Expand Down
6 changes: 6 additions & 0 deletions lib/hyrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ def self.storage_adapter
def self.query_service
metadata_adapter.query_service
end

##
# The custom queries common to Hyrax
def self.custom_queries
query_service.custom_queries
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def parent_collection_ids(valkyrie: false)
# @return [Enumerable<ActiveFedora::Base> | Enumerable<Valkyrie::Resource>] an enumerable over the child collections
# @todo There is no guarantee to collection ordering until Hyrax is fully valkyrie-native, see issue 3784
def child_collections(valkyrie: false)
resources = Hyrax.query_service.custom_queries.find_child_collections(resource: self)
resources = Hyrax.custom_queries.find_child_collections(resource: self)
return resources if valkyrie
resources.map { |r| Wings::ActiveFedoraConverter.new(resource: r).convert }
end
Expand Down
11 changes: 8 additions & 3 deletions lib/wings/hydra/works/services/add_file_to_file_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ def association_type(type)
end

def type_to_association_type(type)
return :original_file if type.to_s.casecmp?(Hyrax::FileSet::ORIGINAL_FILE_USE.to_s)
return :extracted_text if type.to_s.casecmp?(Hyrax::FileSet::EXTRACTED_TEXT_USE.to_s)
return :thumbnail if type.to_s.casecmp?(Hyrax::FileSet::THUMBNAIL_USE.to_s)
case type
when Hyrax::FileMetadata::Use::ORIGINAL_FILE
:original_file
when Hyrax::FileMetadata::Use::EXTRACTED_TEXT
:extracted_text
when Hyrax::FileMetadata::Use::THUMBNAIL
:thumbnail
end
end

def type_to_rdf_uri(type)
Expand Down
2 changes: 1 addition & 1 deletion lib/wings/services/custom_queries/find_access_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module CustomQueries
class FindAccessControl
# Custom query override specific to Wings
# Use:
# Hyrax.query_service.custom_queries.find_access_control_for(resource: resource)
# Hyrax.custom_queries.find_access_control_for(resource: resource)

def self.queries
[:find_access_control_for]
Expand Down
4 changes: 2 additions & 2 deletions lib/wings/services/custom_queries/find_file_metadata.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Custom query override specific to Wings for finding Hydra::PCDM::File and converting to Hyrax::FileMetadata.
# @example
# Hyrax.query_service.custom_queries.find_file_metadata_by(id: valkyrie_id, use_valkyrie: true)
# Hyrax.query_service.custom_queries.find_file_metadata_by_alternate_identifier(alternate_identifier: id, use_valkyrie: true)
# Hyrax.custom_queries.find_file_metadata_by(id: valkyrie_id, use_valkyrie: true)
# Hyrax.custom_queries.find_file_metadata_by_alternate_identifier(alternate_identifier: id, use_valkyrie: true)
require 'wings/services/file_converter_service'
module Wings
module CustomQueries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module CustomQueries
class FindManyByAlternateIds
# Custom query override specific to Wings
# Use:
# Hyrax.query_service.custom_queries.find_many_by_alternate_ids(alternate_ids: ids, use_valkyrie: true)
# Hyrax.custom_queries.find_many_by_alternate_ids(alternate_ids: ids, use_valkyrie: true)

def self.queries
[:find_many_by_alternate_ids]
Expand Down
Loading

0 comments on commit 1d5c2f2

Please sign in to comment.