Class: Wings::CustomQueries::FindFileMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/wings/services/custom_queries/find_file_metadata.rb

Overview

Custom query override specific to Wings for finding Hydra::PCDM::File and converting to Hyrax::FileMetadata.

Examples:

Hyrax.custom_queries.(id: valkyrie_id, use_valkyrie: true)
Hyrax.custom_queries.(alternate_identifier: id, use_valkyrie: true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_service:) ⇒ FindFileMetadata

Returns a new instance of FindFileMetadata.



17
18
19
# File 'lib/wings/services/custom_queries/find_file_metadata.rb', line 17

def initialize(query_service:)
  @query_service = query_service
end

Instance Attribute Details

#query_serviceObject (readonly)

Returns the value of attribute query_service.



21
22
23
# File 'lib/wings/services/custom_queries/find_file_metadata.rb', line 21

def query_service
  @query_service
end

Class Method Details

.queriesObject



10
11
12
13
14
15
# File 'lib/wings/services/custom_queries/find_file_metadata.rb', line 10

def self.queries
  [:find_file_metadata_by,
   :find_file_metadata_by_alternate_identifier,
   :find_many_file_metadata_by_ids,
   :find_many_file_metadata_by_use]
end

Instance Method Details

#find_file_metadata_by(id:, use_valkyrie: true) ⇒ Hyrax::FileMetadata, Hydra::PCDM::File

Find a Hyrax::FileMetadata using a Valkyrie ID,

Parameters:

  • id (Valkyrie::ID, String)
  • use_valkyrie (boolean) (defaults to: true)

    defaults to true; optionally return ActiveFedora::File objects if false

Returns:

  • (Hyrax::FileMetadata, Hydra::PCDM::File)
    • when use_valkyrie is

    true, returns FileMetadata resource; otherwise, returns ActiveFedora PCDM::File

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wings/services/custom_queries/find_file_metadata.rb', line 35

def (id:, use_valkyrie: true)
  fcrepo_flag =
    begin
      ::Valkyrie::StorageAdapter.adapter_for(id: id).is_a?(::Valkyrie::Storage::Fedora)
    rescue ::Valkyrie::StorageAdapter::AdapterNotFoundError
      true # assume fcrepo if we can't find an adapter
    end

  if fcrepo_flag
    (alternate_identifier: id, use_valkyrie: use_valkyrie)
  else
    result = ActiveFedora::Base.where(file_identifier_ssim: id.to_s).first ||
             raise(Hyrax::ObjectNotFoundError)
    result.valkyrie_resource
  end
end

#find_file_metadata_by_alternate_identifier(alternate_identifier:, use_valkyrie: true) ⇒ Hyrax::FileMetadata, Hydra::PCDM::File

Find a Hyrax::FileMetadata using an alternate ID, and map it to a

Parameters:

  • alternate_identifier (Valkyrie::ID, String)
  • use_valkyrie (boolean) (defaults to: true)

    defaults to true; optionally return ActiveFedora::File objects if false

Returns:

  • (Hyrax::FileMetadata, Hydra::PCDM::File)
    • when use_valkyrie is

    true, returns FileMetadata resource; otherwise, returns ActiveFedora PCDM::File

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wings/services/custom_queries/find_file_metadata.rb', line 63

def (alternate_identifier:, use_valkyrie: true)
  alternate_identifier = ::Valkyrie::ID.new(alternate_identifier).to_s
  object = Hydra::PCDM::File.find(alternate_identifier)
  raise Hyrax::ObjectNotFoundError if object.new_record?

  if use_valkyrie == false
    warn_about_deprecation
    return object
  end

  object.valkyrie_resource
end

#find_many_file_metadata_by_ids(ids:, use_valkyrie: true) ⇒ Array<Hyrax::FileMetadata, Hydra::PCDM::File>

Note:

Ignores non-existent ids and ids for non-file metadata resources.

Find an array of file metadata using Valkyrie IDs, and map them to Hyrax::FileMetadata maintaining order based on given ids.

Parameters:

  • ids (Array<Valkyrie::ID, String>)
  • use_valkyrie (boolean) (defaults to: true)

    defaults to true; optionally return ActiveFedora::File objects if false

Returns:

  • (Array<Hyrax::FileMetadata, Hydra::PCDM::File>)

    or empty array if there are no ids or none of the ids map to Hyrax::FileMetadata



87
88
89
90
91
92
93
# File 'lib/wings/services/custom_queries/find_file_metadata.rb', line 87

def (ids:, use_valkyrie: true)
  ids.each_with_object([]) do |alt_id, results|
    results << (alternate_identifier: alt_id, use_valkyrie: use_valkyrie)
  rescue Hyrax::ObjectNotFoundError
    next
  end
end

#find_many_file_metadata_by_use(resource:, use:, use_valkyrie: true) ⇒ Array<Hyrax::FileMetadata, Hydra::PCDM::File>

Find file metadata for files within a resource that have the requested use.

Examples:

Hyrax.query_service.(use: ::RDF::URI("http://pcdm.org/ExtractedText"))

Parameters:

  • use (RDF::URI)

    uri for the desired use Type

  • use_valkyrie (boolean) (defaults to: true)

    defaults to true; optionally return ActiveFedora::File objects if false

Returns:

  • (Array<Hyrax::FileMetadata, Hydra::PCDM::File>)

    or empty array if there are no files with the requested use



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wings/services/custom_queries/find_file_metadata.rb', line 109

def (resource:, use:, use_valkyrie: true)
  pcdm_files = (ids: resource.file_ids, use_valkyrie: false)
  pcdm_files.select! { |pcdm_file| pcdm_file..type.include?(use) }

  if use_valkyrie == false
    warn_about_deprecation
    return pcdm_files
  end

  pcdm_files.map(&:valkyrie_resource)
end