Class: Triannon::LdpWriter

Inherits:
Object
  • Object
show all
Defined in:
app/services/triannon/ldp_writer.rb

Overview

writes data/objects to the LDP server; also does deletes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anno, id = nil) ⇒ LdpWriter

Returns a new instance of LdpWriter.

Parameters:

  • anno (Triannon::Annotation)

    a Triannon::Annotation object

  • id (String) (defaults to: nil)

    the unique id for the LDP container for the passed annotation; defaults to nil



49
50
51
52
53
# File 'app/services/triannon/ldp_writer.rb', line 49

def initialize(anno, id=nil)
  @anno = anno
  @id = id
  @base_uri = Triannon.config[:ldp_url]
end

Class Method Details

.create_anno(anno) ⇒ Object

use LDP protocol to create the OpenAnnotation.Annotation in an RDF store

Parameters:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/triannon/ldp_writer.rb', line 8

def self.create_anno(anno)
  if anno && anno.graph
    # TODO:  special case if the Annotation object already has an id --
    #  see https://github.com/sul-dlss/triannon/issues/84
    ldp_writer = Triannon::LdpWriter.new anno
    id = ldp_writer.create_base

    bodies_solns = anno.graph.query([nil, RDF::OpenAnnotation.hasBody, nil])
    if bodies_solns.size > 0
      ldp_writer.create_body_container
      ldp_writer.create_body_resources
    end

    targets_solns = anno.graph.query([nil, RDF::OpenAnnotation.hasTarget, nil])
    # NOTE:  Annotation is invalid if there are no target statements
    if targets_solns.size > 0
      ldp_writer.create_target_container
      ldp_writer.create_target_resources
    end

    id
  end
end

.delete_container(id) ⇒ Object Also known as: delete_anno

deletes the indicated container and all its child containers from the LDP store

Parameters:

  • id (String)

    the unique id for the LDP container for an annotation May be a compound id, such as uuid1/t/uuid2, in which case the LDP container object uuid2 and its children are deleted from the LDP store, but LDP containers uuid1/t and uuid1 are not deleted from the LDP store.



36
37
38
39
40
41
# File 'app/services/triannon/ldp_writer.rb', line 36

def self.delete_container id
  if id && id.size > 0
    ldpw = Triannon::LdpWriter.new nil
    ldpw.delete_containers id
  end
end

Instance Method Details

#create_baseString

creates a stored LDP container for this object’s Annotation, without its targets or bodies (as those are put in descendant containers)

SIDE EFFECT:  assigns the uuid of the container created to @id

Returns:

  • (String)

    the unique id for the LDP container created for this annotation



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/triannon/ldp_writer.rb', line 58

def create_base
  if @anno.graph.query([nil, RDF::Triannon.externalReference, nil]).count > 0
    raise Triannon::ExternalReferenceError, "Incoming annotations may not have http://triannon.stanford.edu/ns/externalReference as a predicate."
  end

  if @anno.graph.id_as_url && @anno.graph.id_as_url.size > 0
    raise Triannon::ExternalReferenceError, "Incoming new annotations may not have an existing id (yet)."
  end

  # TODO:  special case if the Annotation object already has an id --
  #  see https://github.com/sul-dlss/triannon/issues/84

  # we need to work with a copy of the graph so we don't change @anno.graph
  g = RDF::Graph.new
  @anno.graph.each { |s|
    g << s
  }
  g = Triannon::Graph.new(g)
  g.remove_non_base_statements
  g.make_null_relative_uri_out_of_blank_node

  @id = create_resource g.to_ttl
end

#create_body_containerObject

creates the LDP container for any and all bodies for this annotation



83
84
85
# File 'app/services/triannon/ldp_writer.rb', line 83

def create_body_container
  create_direct_container RDF::OpenAnnotation.hasBody
end

#create_body_resourcesObject

create the body resources inside the (already created) body container



93
94
95
# File 'app/services/triannon/ldp_writer.rb', line 93

def create_body_resources
  create_resources_in_container RDF::OpenAnnotation.hasBody
end

#create_target_containerObject

creates the LDP container for any and all targets for this annotation



88
89
90
# File 'app/services/triannon/ldp_writer.rb', line 88

def create_target_container
  create_direct_container RDF::OpenAnnotation.hasTarget
end

#create_target_resourcesObject

create the target resources inside the (already created) target container



98
99
100
# File 'app/services/triannon/ldp_writer.rb', line 98

def create_target_resources
  create_resources_in_container RDF::OpenAnnotation.hasTarget
end

#delete_containers(ldp_container_uris) ⇒ Object

Returns true if a resource was deleted; false otherwise.

Parameters:

  • ldp_container_uris (Array<String>)

    an Array of ids for LDP containers. (can also be a String) e.g. [@base_uri/(uuid1)/t/(uuid2), @base_uri/(uuid1)/t/(uuid3)] or [@base_uri/(uuid)] or (uuid)

Returns:

  • true if a resource was deleted; false otherwise



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/services/triannon/ldp_writer.rb', line 105

def delete_containers ldp_container_uris
  return false if !ldp_container_uris || ldp_container_uris.empty?
  if ldp_container_uris.kind_of? String
    ldp_container_uris = [ldp_container_uris]
  end
  something_deleted = false
  ldp_container_uris.each { |uri|
    ldp_id = uri.to_s.split(@base_uri + '/').last
    resp = conn.delete { |req| req.url ldp_id }
    if resp.status != 204
      raise Triannon::LDPStorageError.new("Unable to delete LDP container #{ldp_id}", resp.status, resp.body)
    end
    something_deleted = true
  }
  something_deleted
end