Class: Valkyrie::Persistence::Fedora::Persister::OrmConverter

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

Overview

Responsible for converting LDP::Container::Basic to Resource

Defined Under Namespace

Classes: GraphToAttributes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object:, adapter:) ⇒ OrmConverter

Returns a new instance of OrmConverter.

Parameters:



11
12
13
14
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 11

def initialize(object:, adapter:)
  @object = object
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 6

def adapter
  @adapter
end

#objectObject (readonly)

Returns the value of attribute object.



6
7
8
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 6

def object
  @object
end

Instance Method Details

#attributesHash

Generate a Hash resulting from the mapping of Fedora LDP graphs to Valkyrie Resource attributes For example:

{
  :internal_resource => "CustomResource",
  :created_at => 2018-08-08 15:45:03 UTC,
  [...]
  id => #<Valkyrie::ID:0x00007ff821e9cb38 @id="9e5aef75-f4cc-42f4-b050-0eadeeab908d",
  new_record => false
}

Returns:

  • (Hash)


32
33
34
35
36
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 32

def attributes
  GraphToAttributes.new(graph: graph, adapter: adapter)
                   .convert
                   .merge(id: id, new_record: false)
end

#convertValkyrie::Resource

Convert a Fedora LDP container to a Valkyrie Resource

Returns:



18
19
20
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 18

def convert
  populate_native_lock(Valkyrie::Types::Anything[attributes])
end

#idValkyrie::ID

Generate the Valkyrie ID from the existing Fedora LDP resource property Should no such property exist, the URI for the LDP resource is used to mint a new one

Returns:



52
53
54
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 52

def id
  id_property.present? ? Valkyrie::ID.new(id_property) : adapter.uri_to_id(object.subject_uri)
end

#id_propertyString

Retrieve the RDF property for the Valkyrie ID

Returns:

  • (String)

See Also:

  • Valkyrie::Persistence::Fedora::Persister::OrmConverter.{Valkyrie{Valkyrie::Persistence{Valkyrie::Persistence::Fedora{Valkyrie::Persistence::Fedora::Persister{Valkyrie::Persistence::Fedora::Persister::ModelConverter{Valkyrie::Persistence::Fedora::Persister::ModelConverter::NestedInternalValkyrieID}


59
60
61
62
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 59

def id_property
  return unless object.subject_uri.to_s.include?("#")
  object.graph.query([RDF::URI(""), PermissiveSchema.id, nil]).to_a.first.try(:object).to_s
end

#populate_native_lock(resource) ⇒ Object

Get Fedora’s lastModified value from the LDP response



39
40
41
42
43
44
45
46
47
# File 'lib/valkyrie/persistence/fedora/persister/orm_converter.rb', line 39

def populate_native_lock(resource)
  return resource unless resource.respond_to?(Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK)
  lastmod = object.response_graph.first_object([nil, RDF::URI("http://fedora.info/definitions/v4/repository#lastModified"), nil])
  return resource unless lastmod

  token = Valkyrie::Persistence::OptimisticLockToken.new(adapter_id: "native-#{adapter.id}", token: DateTime.parse(lastmod.to_s).httpdate)
  resource.send(Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK) << token
  resource
end