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



89
90
91
92
93
94
95
96
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 89

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



109
110
111
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 109

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:



63
64
65
66
67
68
69
70
71
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 63

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.



76
77
78
79
80
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 76

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
18
19
20
21
22
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 12

def find_by(id:)
  id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
  validate_id(id)
  uri = adapter.id_to_uri(id)
  begin
    resource = Ldp::Resource.for(connection, uri, connection.get(uri))
    resource_factory.to_resource(object: resource)
  rescue ::Ldp::Gone, ::Ldp::NotFound
    raise ::Valkyrie::Persistence::ObjectNotFoundError
  end
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.



99
100
101
102
103
104
105
106
107
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 99

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



25
26
27
28
29
30
31
32
33
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 25

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.



51
52
53
54
55
56
57
58
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 51

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.



36
37
38
39
40
41
42
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 36

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.



83
84
85
86
87
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 83

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



44
45
46
47
48
# File 'lib/valkyrie/persistence/fedora/query_service.rb', line 44

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