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



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.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • integer. Count objects in the persistence backend with the given class.



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

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

Returns:



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

Get all objects.

Returns:



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.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects in the persistence backend with the given class.



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.

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

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:, 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*

Parameters:

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

    The resource which is being referenced by other resources. Requires either resource or id parameter to be specified.

  • id (Valkyrie::ID) (defaults to: nil)

    ID of the resource which is being reference by other resources. Requires either resource or id parameter to be specified.

  • property (Symbol)

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

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

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.



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.

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

Parameters:

  • resource (Valkyrie::Resource)

    Model whose members are being searched for.

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

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



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.

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.



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.

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.

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

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



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