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

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

Overview

Persister for Fedora MetadataAdapter.

Defined Under Namespace

Classes: ModelConverter, OrmConverter, ResourceFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ Persister

Note:

Many persister methods are part of Valkyrie’s public API, but instantiation itself is not

Returns a new instance of Persister.



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

def initialize(adapter:)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

Instance Method Details

#delete(resource:) ⇒ Object

Delete a resource.

Parameters:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/valkyrie/persistence/fedora/persister.rb', line 51

def delete(resource:)
  if resource.try(:alternate_ids)
    resource.alternate_ids.each do |alternate_identifier|
      adapter.persister.delete(resource: adapter.query_service.find_by(id: alternate_identifier))
    end
  end

  orm = resource_factory.from_resource(resource: resource)
  orm.delete

  resource
end

#initialize_repositoryLdp::Container::Basic

Creates the root LDP Container for the connection with Fedora

Returns:

  • (Ldp::Container::Basic)

See Also:



78
79
80
81
82
83
84
85
# File 'lib/valkyrie/persistence/fedora/persister.rb', line 78

def initialize_repository
  @initialized ||=
    begin
      resource = ::Ldp::Container::Basic.new(connection, base_path, nil, base_path)
      resource.save if resource.new?
      true
    end
end

#save(resource:) ⇒ Valkyrie::Resource

Save a single resource.

Parameters:

Returns:

  • (Valkyrie::Resource)

    The resource with an ‘#id` value generated by the persistence backend.

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/valkyrie/persistence/fedora/persister.rb', line 16

def save(resource:)
  initialize_repository
  internal_resource = resource.dup
  internal_resource.created_at ||= Time.current
  internal_resource.updated_at = Time.current
  validate_lock_token(internal_resource)
  native_lock = native_lock_token(internal_resource)
  generate_lock_token(internal_resource)
  orm = resource_factory.from_resource(resource: internal_resource)
  alternate_resources = find_or_create_alternate_ids(internal_resource)

  if !orm.new? || internal_resource.id
    cleanup_alternate_resources(internal_resource) if alternate_resources
    orm.update { |req| update_request_headers(req, native_lock) }
  else
    orm.create
  end
  persisted_resource = resource_factory.to_resource(object: orm)

  alternate_resources ? save_reference_to_resource(persisted_resource, alternate_resources) : persisted_resource
rescue Ldp::PreconditionFailed
  raise Valkyrie::Persistence::StaleObjectError, "The object #{internal_resource.id} has been updated by another process."
end

#save_all(resources:) ⇒ Array<Valkyrie::Resource>

Save a batch of resources.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    List of resources with an ‘#id` value generated by the persistence backend.

Raises:



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

def save_all(resources:)
  resources.map do |resource|
    save(resource: resource)
  end
rescue Valkyrie::Persistence::StaleObjectError
  # blank out the message / id
  raise Valkyrie::Persistence::StaleObjectError, "One or more resources have been updated by another process."
end

#wipe!Object

Removes all data from the persistence backend. Deletes Fedora repository resource and the tombstone resources which remain



68
69
70
71
72
73
# File 'lib/valkyrie/persistence/fedora/persister.rb', line 68

def wipe!
  connection.delete(base_path)
  connection.delete("#{base_path}/fcr:tombstone")
rescue => error
  Valkyrie.logger.debug("Failed to wipe Fedora for some reason: #{error}", logging_context: "Valkyrie::Persistence::Fedora::Persister#wipe") unless error.is_a?(::Ldp::NotFound)
end