Skip to content

Commit

Permalink
add convenience methods
Browse files Browse the repository at this point in the history
* add Annotations.find_by_target
* add CommentAnnotation.getComment
* add TagAnnotation.getTag
  • Loading branch information
elrayle committed Feb 17, 2015
1 parent 5cb65c0 commit a407024
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
30 changes: 30 additions & 0 deletions lib/ld4l/open_annotation_rdf/annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ def persist!
body_persisted = persisted && @body ? @body.persist! : false # persist body
persisted
end

##
# Find annotation by target.
#
# @param [String, RDF::URI] uri for the work
#
# @return true if annotation successfully persisted; otherwise, false
#
# @todo What to return if annotation persists fine, but body fails to persist?
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
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/ld4l/open_annotation_rdf/comment_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CommentAnnotation < LD4L::OpenAnnotationRDF::Annotation
#
# @param [String]
#
# @return instance of SemanticTagBody
# @return instance of CommentAnnotation
def setComment(comment)
@body = LD4L::OpenAnnotationRDF::CommentBody.new(
ActiveTriples::LocalName::Minter.generate_local_name(
Expand All @@ -24,6 +24,15 @@ def setComment(comment)
@body
end

##
# Get the value of the comment stored in a comment annotation.
#
# @return text value of comment
def getComment
comments = @body.content
comments && comments.size > 0 ? comments.first : ""
end

##
# Special processing for new and resumed CommentAnnotations
#
Expand Down
4 changes: 2 additions & 2 deletions lib/ld4l/open_annotation_rdf/semantic_tag_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def self::annotations_using( term_uri )
term_uri = RDF::URI(term_uri) unless term_uri.kind_of?(RDF::URI)

# find usage by Annotations
graph = ActiveTriples::Repositories.repositories[repository]
repo = ActiveTriples::Repositories.repositories[repository]
query = RDF::Query.new({
:annotation => {
RDF.type => RDFVocabularies::OA.Annotation,
RDFVocabularies::OA.hasBody => term_uri,
}
})
annotations = []
results = query.execute(graph)
results = query.execute(repo)
results.each { |r| annotations << r.to_hash[:annotation] }
annotations
end
Expand Down
9 changes: 9 additions & 0 deletions lib/ld4l/open_annotation_rdf/tag_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ class TagAnnotation < LD4L::OpenAnnotationRDF::Annotation

# TODO: Should a tag be destroyed when the last annotation referencing the tag is destroyed?

##
# Get the value of the tag stored in a tag annotation.
#
# @return text value of tag
def getTag
tags = @body.tag
tags && tags.size > 0 ? tags.first : "" # TODO What is the appropriate default value for a tag?
end

##
# Set the hasBody property to the URI of the one and only TagBody holding the tag value. Create a new TagBody
# if one doesn't exist with this value.
Expand Down
8 changes: 4 additions & 4 deletions lib/ld4l/open_annotation_rdf/tag_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def self::annotations_using( tag_value )
tag_uri = tb.rdf_subject

# find usage by Annotations
graph = ActiveTriples::Repositories.repositories[repository]
repo = ActiveTriples::Repositories.repositories[repository]
query = RDF::Query.new({
:annotation => {
RDF.type => RDFVocabularies::OA.Annotation,
RDFVocabularies::OA.hasBody => tag_uri,
}
})
results = query.execute(graph)
results = query.execute(repo)

# process results
annotations = []
Expand All @@ -54,7 +54,7 @@ def self::fetch_by_tag_value( tag_value )
raise ArgumentError, 'Argument must be a string with at least one character' unless
tag_value.kind_of?(String) && tag_value.size > 0

graph = ActiveTriples::Repositories.repositories[repository]
repo = ActiveTriples::Repositories.repositories[repository]
query = RDF::Query.new({
:tagbody => {
RDF.type => RDFVocabularies::OA.Tag,
Expand All @@ -63,7 +63,7 @@ def self::fetch_by_tag_value( tag_value )
})

tagbody = nil
results = query.execute(graph)
results = query.execute(repo)
unless( results.empty? )
tagbody_uri = results[0].to_hash[:tagbody]
tagbody = LD4L::OpenAnnotationRDF::TagBody.new(tagbody_uri)
Expand Down

0 comments on commit a407024

Please sign in to comment.