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

Returns a new instance of QueryService.



7
8
9
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 7

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:) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 93

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_queriesObject



113
114
115
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 113

def custom_queries
  @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self)
end

#find_allArray<Valkyrie::Resource>

Note:

This requires iterating over every resource in Fedora.

Returns All objects in the persistence backend.

Returns:



67
68
69
70
71
72
73
74
75
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 67

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>

Note:

This requires iterating over every resource in Fedora.

Returns All objects in the persistence backend with the given class.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects in the persistence backend with the given class.



80
81
82
83
84
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 80

def find_all_of_model(model:)
  find_all.select do |m|
    m.is_a?(model)
  end
end

#find_by(id:) ⇒ Valkyrie::Resource

Returns The object being searched for.

Parameters:

Returns:

Raises:



12
13
14
15
16
17
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 12

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

Returns The object being searched for.

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



20
21
22
23
24
25
26
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 20

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:, property:) ⇒ Array<Valkyrie::Resource>

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

Parameters:

  • resource (Valkyrie::Resource)

    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.



103
104
105
106
107
108
109
110
111
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 103

def find_inverse_references_by(resource:, property:)
  ensure_persisted(resource)
  content = content_with_inbound(id: resource.id)
  property_uri =  adapter.schema.predicate_for(property: property, resource: nil)
  ids = content.graph.query([nil, property_uri, nil]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) }
  ids.lazy.map do |id|
    find_by(id: id)
  end
end

#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>

Returns All requested objects that were found.

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



29
30
31
32
33
34
35
36
37
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 29

def find_many_by_ids(ids:)
  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>

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

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.



55
56
57
58
59
60
61
62
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 55

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>

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

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.



40
41
42
43
44
45
46
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 40

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.lazy.map do |id|
    find_by(id: id)
  end
end

#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>

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

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.



87
88
89
90
91
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 87

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_urisObject



48
49
50
51
52
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 48

def include_uris
  [
    ::RDF::Vocab::Fcrepo4.InboundReferences
  ]
end