Class: Valkyrie::Persistence::Fedora::QueryService

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/fedora/query_service.rb

Overview

Query Service for Fedora MetadataAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ QueryService

Note:

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

#adapterObject (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_queriesValkyrie::Persistence::CustomQueryContainer

Get the set of custom queries configured for this query service.

Returns:



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_allArray<Valkyrie::Resource>

Get all objects.

Returns:



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.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects in the persistence backend with the given class.



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.

Parameters:

Returns:

Raises:



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`.

Parameters:

  • alternate_identifier (Valkyrie::ID)

    The alternate identifier to query for.

Returns:

Raises:

  • (Valkyrie::Persistence::ObjectNotFoundError)

    Raised when the alternate identifier isn’t in the persistence backend.

  • (ArgumentError)

    Raised when alternate identifier is not a String or a Valkyrie::ID



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*

Parameters:

  • resource (Valkyrie::Resource) (defaults to: nil)

    The resource which is being referenced by other resources.

  • property (Symbol)

    The property which, on other resources, is referencing the given ‘resource`

Returns:

  • (Array<Valkyrie::Resource>)

    All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.

Raises:

  • (ArgumentError)

    Raised when the ID is not in the persistence backend.



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.

Parameters:

  • ids (Array<Valkyrie::ID, String>)

    The IDs to query for.

Returns:

Raises:

  • (ArgumentError)

    Raised when any ID is not a String or a Valkyrie::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.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose members are being searched for.

  • model (Class) (defaults to: nil)

    Class to query for. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    child objects of type ‘model` referenced by `resource`’s ‘member_ids` method. Returned in order.



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.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    All resources which are parents of the given ‘resource`. This means the resource’s ‘id` appears in their `member_ids` array.



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.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose property is being searched.

  • property (Symbol)

    Property which, on the ‘resource`, contains IDs which are to be de-referenced.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects which are referenced by the ‘property` property on `resource`. Not necessarily in order.



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_urisArray<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