Class: Swicky::Notebook

Inherits:
Object show all
Extended by:
TaliaUtil::UriHelper
Includes:
ActiveRDF::ResourceLike, TaliaUtil::UriHelper
Defined in:
lib/swicky/notebook.rb

Overview

Represents a SWicky Notebook in the RDF store. This wraps the queries to handle the SWicky annotations and user notebooks.

A notebook is an RDF subgraph that is store in its own context.

All parameters for this class that end up in RDF queries will be sanitized automatically

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TaliaUtil::UriHelper

irify, sanitize_sparql

Constructor Details

#initialize(user_name_or_uri, notebook_name = nil) ⇒ Notebook

Returns a new instance of Notebook.



22
23
24
25
26
27
28
29
# File 'lib/swicky/notebook.rb', line 22

def initialize(user_name_or_uri, notebook_name = nil)
  if(notebook_name)
    @user_url = self.class.user_url(user_name_or_uri)
    @url = self.class.notebook_url(user_name_or_uri, notebook_name)
  else
    @url = sanitize_sparql(user_name_or_uri).to_uri
  end
end

Instance Attribute Details

#urlObject (readonly) Also known as: uri

Returns the value of attribute url.



18
19
20
# File 'lib/swicky/notebook.rb', line 18

def url
  @url
end

#user_urlObject (readonly)

Returns the value of attribute user_url.



18
19
20
# File 'lib/swicky/notebook.rb', line 18

def user_url
  @user_url
end

Class Method Details

.annotation_list_for_url(url) ⇒ Object



115
116
117
# File 'lib/swicky/notebook.rb', line 115

def annotation_list_for_url(url)
  qry = ActiveRDF::Query.new(N::URI).distinct.select(:note).where(:fragment, N::DISCOVERY.isPartOf, url.to_uri).where(:note, N::SWICKY.refersTo, :fragment).execute
end

.annotations_for_image(url) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/swicky/notebook.rb', line 126

def annotations_for_image(url)
  url = sanitize_sparql(url).to_uri
  select_annotations([:note, N::SWICKY.refersTo, :fragment], [:fragment, N::DISCOVERY.isPartOf, url])
  # result = []
  # url = sanitize_sparql(url).to_uri
  # q = ActiveRDF::Query.new(N::URI).select(:fragment).distinct.where(:fragment, N::DISCOVERY.isPartOf, url)
  # q.execute.each do |fragment|
  #   result = {fragment.uri.to_s => {}}
  #   q2 = ActiveRDF::Query.new(N::URI).select(:predicate, :object).distinct
  #   q2.where fragment, :predicate, :object
  #   q2.execute.each do |predicate, object|
  #     result[fragment.uri.to_s][predicate.to_s] = object.to_s
  #   end
  # end
  # result
end

.annotations_for_url(url) ⇒ Object

Select all the triples for all the annotations (notes) that refer to the given URL



121
122
123
124
# File 'lib/swicky/notebook.rb', line 121

def annotations_for_url(url)
  url = sanitize_sparql(url).to_uri
  select_annotations([:note, N::SWICKY.refersTo, url])
end

.annotations_for_xpointer(xpointer) ⇒ Object

Select all the annotations on the note that uses the fragment identified by the given XPOINTER string



145
146
147
148
# File 'lib/swicky/notebook.rb', line 145

def annotations_for_xpointer(xpointer)
  xpointer = sanitize_sparql(xpointer).to_uri
  select_annotations([:note, N::SWICKY.refersTo, :fragment], [:fragment, N::SWICKY.hasCoordinates, xpointer])
end

.coordinates_for(url) ⇒ Object

Get the “coordinates” (an xpointer in the case of HTML fragments) for all the fragments that are part of the element with the given url.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/swicky/notebook.rb', line 102

def coordinates_for(url)
  result = []
  url = sanitize_sparql(url).to_uri
  frag_qry = ActiveRDF::Query.new(N::URI).select(:fragment, :coordinates).distinct
  frag_qry.where(:fragment, N::DISCOVERY.isPartOf, url)
  frag_qry.where(:fragment, N::SWICKY.hasCoordinates, :coordinates)
  frag_qry.where(:note, N::SWICKY.refersTo, :fragment)
  frag_qry.execute.each do |fragment, coordinates|
    result << {'fragment' => fragment.to_s, 'coordinates' => coordinates.to_s}
  end
  result
end

.find_all(user_name = nil) ⇒ Object

Find all notebooks for the given user



83
84
85
86
87
88
# File 'lib/swicky/notebook.rb', line 83

def find_all(user_name = nil)
  nb_query = ActiveRDF::Query.new(Notebook).select(:notebook).distinct
  nb_query.where(:notebook, N::RDF.type, N::TALIA.SwickyNotebook)
  nb_query.where(user_url(user_name), N::TALIA.hasSwickyNotebook, :notebook) if(user_name)
  nb_query.execute
end

.notebook_url(user_name, notebook_name) ⇒ Object

Construct the URL for a notebook from the user and notebook name



96
97
98
# File 'lib/swicky/notebook.rb', line 96

def notebook_url(user_name, notebook_name)
  sanitize_sparql(user_url(user_name) + '/swicky_notebooks/' + notebook_name).to_uri
end

.user_url(user_name) ⇒ Object

Construct the “user” url for the given user name



91
92
93
# File 'lib/swicky/notebook.rb', line 91

def user_url(user_name)
  sanitize_sparql(N::LOCAL + "users/#{user_name}").to_uri
end

Instance Method Details

#==(value) ⇒ Object



76
77
78
# File 'lib/swicky/notebook.rb', line 76

def ==(value)
  (value.class == self.class) && (value.uri == self.uri)
end

#create(xml_data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/swicky/notebook.rb', line 57

def create(xml_data)
  # Make a temp file for the data
  tmpfile = Tempfile.new('xml_notebook')
  tmpfile << xml_data
  tmpfile.close
  # Load into store
  load(tmpfile.path)
  # remove the temp file
  tmpfile.unlink
end

#dataObject



31
32
33
# File 'lib/swicky/notebook.rb', line 31

def data
  @data ||= ActiveRDF::Query.new(N::URI).select(:s, :p, :o).distinct.where(:s, :p, :o, url).execute
end

#deleteObject



39
40
41
42
43
# File 'lib/swicky/notebook.rb', line 39

def delete
  ActiveRDF::FederationManager.delete(nil, nil, nil, url)
  ActiveRDF::FederationManager.delete(user_url, N::TALIA.hasSwickyNotebook, url)
  ActiveRDF::FederationManager.delete(url, N::RDF.type, N::TALIA.SwickyNotebook)
end

#exist?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/swicky/notebook.rb', line 68

def exist?
  ActiveRDF::Query.new(N::URI).select(:user).where(:user, N::TALIA.hasSwickyNotebook, url).execute.size > 0
end

#load(xml_file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/swicky/notebook.rb', line 45

def load(xml_file)
  @data = nil
  begin
    ActiveRDF::ConnectionPool.write_adapter.load(xml_file, 'rdfxml', url)
  rescue Exception => e
    puts "\tProblem loading #{xml_file.to_s}: (#{e.message}) File not loaded!"
    puts e.backtrace
  end
  ActiveRDF::FederationManager.add(user_url, N::TALIA.hasSwickyNotebook, url)
  ActiveRDF::FederationManager.add(url, N::RDF.type, N::TALIA.SwickyNotebook)
end

#to_uriObject



72
73
74
# File 'lib/swicky/notebook.rb', line 72

def to_uri
  N::URI.new(uri)
end

#xml_dataObject



35
36
37
# File 'lib/swicky/notebook.rb', line 35

def xml_data
  TaliaUtil::Xml::RdfBuilder.xml_string_for_triples(data)
end