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.
-
#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:) ⇒ 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:) ⇒ 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
101 102 103 104 105 106 107 108 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 101 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 |
#custom_queries ⇒ Valkyrie::Persistence::CustomQueryContainer
Get the set of custom queries configured for this query service.
126 127 128 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 126 def custom_queries @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self) end |
#find_all ⇒ Array<Valkyrie::Resource>
Get all objects.
72 73 74 75 76 77 78 79 80 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 72 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.
83 84 85 86 87 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 83 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`.
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:) ⇒ 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*
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 114 def find_inverse_references_by(resource: nil, id: nil, property:) raise ArgumentError, "Provide resource or id" unless resource || id ensure_persisted(resource) if resource resource ||= find_by(id: id) if ordered_property?(resource: resource, property: property) find_inverse_references_by_ordered(resource: resource, property: property) else find_inverse_references_by_unordered(resource: resource, property: property) end end |
#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>
Get a batch of resources by ID.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 31 def find_many_by_ids(ids:) ids = ids.uniq if adapter.standardize_query_result? ids.map do |id| begin find_by(id: id) rescue ::Valkyrie::Persistence::ObjectNotFoundError nil end end.reject(&:nil?) end |
#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>
Get all members of a given resource.
62 63 64 65 66 67 68 69 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 62 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.
43 44 45 46 47 48 49 50 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 43 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! if adapter.standardize_query_result? parent_ids.lazy.map do |id| find_by(id: id) end end |
#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>
Get all resources referenced from a resource with a given property.
90 91 92 93 94 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 90 def find_references_by(resource:, property:) (resource[property] || []).select { |x| x.is_a?(Valkyrie::ID) }.lazy.map do |id| find_by(id: id) end end |
#include_uris ⇒ Array<RDF::URI>
Specify the URIs used in triples directly related to the requested resource
55 56 57 58 59 |
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 55 def include_uris [ adapter.fedora_version == 5 ? "http://fedora.info/definitions/fcrepo#PreferInboundReferences" : ::RDF::Vocab::Fcrepo4.InboundReferences ] end |