Class: Swicky::Notebook

Inherits:
Object
  • Object
show all
Extended by:
TaliaUtil::UriHelper
Includes:
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, notebook_name) ⇒ Notebook

Returns a new instance of Notebook.



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

def initialize(user_name, notebook_name)
  @user_url = self.class.user_url(user_name)
  @url = self.class.notebook_url(user_name, notebook_name)
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#user_urlObject (readonly)

Returns the value of attribute user_url.



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

def user_url
  @user_url
end

Class Method Details

.annotations_for_url(url) ⇒ Object



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

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

.annotations_for_xpointer(xpointer) ⇒ Object



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

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



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

def coordinates_for(url)
  url = sanitize_sparql(url).to_uri
  frag_qry = ActiveRDF::Query.new(N::URI).select(: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.collect { |coord| coord.to_s }
end

.find_all(user_name = nil) ⇒ Object



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

def find_all(user_name = nil)
  nb_query = ActiveRDF::Query.new(N::URI).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



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

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



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

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

Instance Method Details

#create(xml_data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/swicky/notebook.rb', line 50

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



24
25
26
# File 'lib/swicky/notebook.rb', line 24

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

#deleteObject



32
33
34
35
36
# File 'lib/swicky/notebook.rb', line 32

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)


61
62
63
# File 'lib/swicky/notebook.rb', line 61

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

#load(xml_file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/swicky/notebook.rb', line 38

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

#xml_dataObject



28
29
30
# File 'lib/swicky/notebook.rb', line 28

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