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:



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

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:



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

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:, external_resource: false) ⇒ Valkyrie::Resource

Save a single resource.

Parameters:

  • resource (Valkyrie::Resource)

    The resource to save.

  • external_resource (Boolean) (defaults to: false)

    Whether the resource to be saved comes from a different metadata store. Allows a resource to be saved even if it’s not already in the store. For example, if you’re indexing a resource into Solr - it’s saved in your primary metadata store, but not in Solr, so it’s okay if it doesn’t exist in Solr but is marked as persisted.

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
39
40
# File 'lib/valkyrie/persistence/fedora/persister.rb', line 16

def save(resource:, external_resource: false)
  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."
rescue Ldp::Gone
  raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time."
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:



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

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



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

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