Class: Valkyrie::Persistence::Fedora::QueryService
- Inherits:
-
Object
- Object
- Valkyrie::Persistence::Fedora::QueryService
- Defined in:
- lib/valkyrie/persistence/fedora/query_service.rb
Overview
Query Service for Fedora MetadataAdapter
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
-
#content_with_inbound(id:) ⇒ Faraday::Response
Retrieves the RDF graph for the LDP container for a resource This includes inbound links.
-
#count_all_of_model(model:) ⇒ Object
Count all objects of a given model.
-
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Get the set of custom queries configured for this query service.
-
#find_all ⇒ Array<Valkyrie::Resource>
Get all objects.
-
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
Get all objects of a given model.
-
#find_by(id:) ⇒ Valkyrie::Resource
Get a single resource by ID.
-
#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource
Get a single resource by
alternate_identifier. -
#find_inverse_references_by(resource: nil, id: nil, property:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all resources which link to a resource with a given property.
-
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Get a batch of resources by ID.
-
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all members of a given resource.
-
#find_parents(resource:) ⇒ Array<Valkyrie::Resource>
Find all parents of a given resource.
-
#find_references_by(resource:, property:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all resources referenced from a resource with a given property.
-
#include_uris ⇒ Array<RDF::URI>
Specify the URIs used in triples directly related to the requested resource.
-
#initialize(adapter:) ⇒ QueryService
constructor
A new instance of QueryService.
Constructor Details
#initialize(adapter:) ⇒ QueryService
Many query service methods are part of Valkyrie's public API, but instantiation itself is not
Returns a new instance of QueryService.
9 10 11 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 9 def initialize(adapter:) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
5 6 7 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 5 def adapter @adapter end |
Instance Method Details
#content_with_inbound(id:) ⇒ Faraday::Response
Retrieves the RDF graph for the LDP container for a resource This includes inbound links
106 107 108 109 110 111 112 113 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 106 def content_with_inbound(id:) uri = adapter.id_to_uri(id) connection.get(uri) do |req| prefer_headers = Ldp::PreferHeaders.new(req.headers["Prefer"]) prefer_headers.include = prefer_headers.include | include_uris req.headers["Prefer"] = prefer_headers.to_s end end |
#count_all_of_model(model:) ⇒ Object
Count all objects of a given model.
88 89 90 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 88 def count_all_of_model(model:) find_all_of_model(model: model).count end |
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Get the set of custom queries configured for this query service.
132 133 134 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 132 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Array<Valkyrie::Resource>
Get all objects.
70 71 72 73 74 75 76 77 78 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 70 def find_all resource = Ldp::Resource.for(connection, adapter.base_path, connection.get(adapter.base_path)) ids = resource.graph.query([nil, RDF::Vocab::LDP.contains, nil]).map(&:object).map { |x| adapter.uri_to_id(x) } ids.lazy.map do |id| find_by(id: id) end rescue ::Ldp::NotFound [] end |
#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>
Get all objects of a given model.
81 82 83 84 85 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 81 def find_all_of_model(model:) find_all.select do |m| m.is_a?(model) end end |
#find_by(id:) ⇒ Valkyrie::Resource
Get a single resource by ID.
14 15 16 17 18 19 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 14 def find_by(id:) validate_id(id) uri = adapter.id_to_uri(id) resource_from_uri(uri) end |
#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource
Get a single resource by alternate_identifier. Alternate identifiers are identifiers (like NOIDs,
DOIs, ARKs, etc.) that are not the system-generated ID, but might be used to identify a resource in an
application (e.g., to make shorter URLs).
22 23 24 25 26 27 28 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 22 def find_by_alternate_identifier(alternate_identifier:) validate_id(alternate_identifier) uri = adapter.id_to_uri(alternate_identifier) alternate_id = resource_from_uri(uri).references find_by(id: alternate_id) end |
#find_inverse_references_by(resource: nil, id: nil, property:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all resources which link to a resource with a given property. Find all resources referencing a given resource (e. g. parents) This is done by iterating through the ID of each resource referencing the resource in the query, and requesting each resource over the HTTP Also, an initial request is made to find the URIs of the resources referencing the resource in the query
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 119 def find_inverse_references_by(resource: nil, id: nil, property:, model: nil) raise ArgumentError, "Provide resource or id" unless resource || id ensure_persisted(resource) if resource resource ||= find_by(id: id) ids = find_inverse_reference_ids_by_unordered(resource: resource, property: property).uniq objects_from_unordered = ids.lazy.map { |ref_id| find_by(id: ref_id) } objects_from_ordered = find_inverse_references_by_ordered(resource: resource, property: property, ignore_ids: ids) objects = [objects_from_unordered, objects_from_ordered].lazy.flat_map(&:lazy) return objects unless model objects.select { |obj| obj.is_a?(model) } end |
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Get a batch of resources by ID.
31 32 33 34 35 36 37 38 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 31 def find_many_by_ids(ids:) ids = ids.uniq ids.map do |id| find_by(id: id) rescue ::Valkyrie::Persistence::ObjectNotFoundError nil end.reject(&:nil?) end |
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all members of a given resource.
60 61 62 63 64 65 66 67 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 60 def find_members(resource:, model: nil) return [] unless resource.respond_to? :member_ids result = Array(resource.member_ids).lazy.map do |id| find_by(id: id) end.select(&:present?) return result unless model result.select { |obj| obj.is_a?(model) } end |
#find_parents(resource:) ⇒ Array<Valkyrie::Resource>
Find all parents of a given resource.
41 42 43 44 45 46 47 48 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 41 def find_parents(resource:) content = content_with_inbound(id: resource.id) parent_ids = content.graph.query([nil, RDF::Vocab::ORE.proxyFor, nil]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) } parent_ids.uniq! parent_ids.lazy.map do |id| find_by(id: id) end end |
#find_references_by(resource:, property:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all resources referenced from a resource with a given property.
93 94 95 96 97 98 99 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 93 def find_references_by(resource:, property:, model: nil) objects = (resource[property] || []).select { |x| x.is_a?(Valkyrie::ID) }.lazy.map do |id| find_by(id: id) end return objects unless model objects.select { |obj| obj.is_a?(model) } end |
#include_uris ⇒ Array<RDF::URI>
Specify the URIs used in triples directly related to the requested resource
53 54 55 56 57 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 53 def include_uris [ adapter.fedora_version >= 5 ? "http://fedora.info/definitions/fcrepo#PreferInboundReferences" : ::RDF::Vocab::Fcrepo4.InboundReferences ] end |