Class: LD4L::OpenAnnotationRDF::Annotation

Inherits:
ActiveTriples::Resource
  • Object
show all
Defined in:
lib/ld4l/open_annotation_rdf/annotation.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.localname_prefixObject (readonly)

Returns the value of attribute localname_prefix.



5
6
7
# File 'lib/ld4l/open_annotation_rdf/annotation.rb', line 5

def localname_prefix
  @localname_prefix
end

Class Method Details

.find_by_target(target_uri) ⇒ Object

TODO:

What to return if annotation persists fine, but body fails to persist?

Find annotation by target.

Parameters:

  • uri (String, RDF::URI)

    for the work

Returns:

  • true if annotation successfully persisted; otherwise, false

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ld4l/open_annotation_rdf/annotation.rb', line 89

def self::find_by_target(target_uri)
  raise ArgumentError, 'target_uri argument must be a uri string or an instance of RDF::URI'  unless
      target_uri.kind_of?(String) && target_uri.size > 0 || target_uri.kind_of?(RDF::URI)

  # raise ArgumentError, 'repository argument must be an instance of RDF::Repository'  unless
  #     repository.kind_of?(RDF::Repository)

  target_uri = RDF::URI(target_uri) unless target_uri.kind_of?(RDF::URI)

  repo = ActiveTriples::Repositories.repositories[repository]
  query = RDF::Query.new({
                             :annotation => {
                                 RDF.type =>  RDFVocabularies::OA.Annotation,
                                 RDFVocabularies::OA.hasTarget => target_uri,
                             }
                         })
  annotations = []
  results = query.execute(repo)
  results.each { |r| annotations << r.to_hash[:annotation] }
  annotations
end

.resume(uri_or_str) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ld4l/open_annotation_rdf/annotation.rb', line 20

def self.resume(uri_or_str)
  # Let ActiveTriples::Resource validate uri_or_str when creating new Annotation
  a = new(uri_or_str)

  # get motivatedBy
  m = a.get_values(:motivatedBy)
  m = m.to_a if Object.const_defined?("ActiveTriples::Relation") && m.kind_of?(ActiveTriples::Relation)

  # TODO:  Should m's class be validated?  I've seen it be RDF::Vocabulary::Term and RDF::URI.  For now, removing the validation.
  return a    unless m.kind_of?(Array) && m.size > 0
  # return a    unless m.kind_of?(Array) && m.size > 0 && (m.first.kind_of?(RDF::Vocabulary::Term) || m.first.kind_of?(RDF::URI)

  # motivatedBy is set
  m_uri = m.first
  # currently only support commenting and tagging
  return LD4L::OpenAnnotationRDF::CommentAnnotation.new(uri_or_str) if m_uri == RDFVocabularies::OA.commenting
  return a                                                       unless m_uri == RDFVocabularies::OA.tagging

  # Tagging can be TagAnnotation or SemanticTagAnnotation.  Only way to tell is by checking type of body.
  sta = LD4L::OpenAnnotationRDF::SemanticTagAnnotation.new(uri_or_str)
  stb = sta.getBody
  return sta                          unless stb.type.include?(RDFVocabularies::OA.Tag) || !stb.type.include?(RDFVocabularies::OA.SemanticTag)

  ta = LD4L::OpenAnnotationRDF::TagAnnotation.new(uri_or_str)
  tb = ta.getBody
  return ta                           if tb.type.include?(RDFVocabularies::OA.Tag)

  # can't match to a known annotation type, so return as generic annotation
  return a
end

Instance Method Details

#getBodyObject

Get the ActiveTriples::Resource instance holding the body of this annotation.

Parameters:

  • tag (String)

    value

Returns:

  • instance of an annotation body if one is set; otherwise, nil



57
58
59
# File 'lib/ld4l/open_annotation_rdf/annotation.rb', line 57

def getBody
  @body
end

#persist!Object

TODO:

What to return if annotation persists fine, but body fails to persist?

Save all annotation and annotation body triples to the triple store.

Returns:

  • true if annotation successfully persisted; otherwise, false



75
76
77
78
79
# File 'lib/ld4l/open_annotation_rdf/annotation.rb', line 75

def persist!
  persisted = super                                               # persist annotation
  body_persisted = persisted && @body ? @body.persist! : false    # persist body
  persisted
end

#setAnnotatedAtNowObject

Set annotatedAt property to now.

Returns:

  • the value of annotatedAt property



65
66
67
# File 'lib/ld4l/open_annotation_rdf/annotation.rb', line 65

def setAnnotatedAtNow
  set_value(:annotatedAt, Time.now.utc.iso8601(0))
end