Permalink
Browse files

new rdf/virtuoso adapter

  • Loading branch information...
2 parents 72d6d03 + 6f50017 commit 046cb140b9fbf2fe99f906d54ae3c5757edd6bc5 @bensinober bensinober committed Aug 23, 2012
View
2 .rspec
@@ -1 +1 @@
---colour
+--colour --format d
View
2 .rvmrc
@@ -1,2 +0,0 @@
-rvm use --create ruby-1.9.3@booky
-export PATH=./bin:$PATH
View
7 Gemfile
@@ -16,11 +16,11 @@ gem 'coffee-rails', '~> 3.2.1'
gem 'jquery-rails'
-gem 'httparty', '0.8.2'
+gem 'httparty', '~> 0.8.2'
gem 'rest-client'
gem 'nokogiri'
gem 'rdf'
-gem 'sparql-client', git: 'git://github.com/xaop/sparql-client.git', branch: 'virtuoso_update'
+#gem 'sparql-client', git: 'git://github.com/xaop/sparql-client.git', branch: 'virtuoso_update'
gem 'friendly_id', '~> 4.0.0'
@@ -62,4 +62,5 @@ gem 'feedzirra', git: 'http://github.com/pkordel/feedzirra.git'
gem 'capistrano'
gem 'rvm-capistrano'
gem 'rocket_pants', '~> 1.0'
-gem 'rdf-virtuoso', git: 'https://github.com/digibib/rdf-virtuoso.git', branch: 'develop'
+#gem 'rdf/virtuoso'
+gem 'rdf-virtuoso', git: 'http://github.com/digibib/rdf-virtuoso.git'
View
162 Gemfile.lock
@@ -1,12 +1,15 @@
GIT
- remote: git://github.com/xaop/sparql-client.git
- revision: ed4db969c53cfe587d0d70445f6ed10d48feffdf
- branch: virtuoso_update
+ remote: http://github.com/digibib/rdf-virtuoso.git
+ revision: 161821a70ec4a17c51daf503a2b85ce0de2ee243
specs:
- sparql-client (0.0.11)
- json_pure (>= 1.4.2)
- net-http-persistent (~> 1.4.1)
- rdf (>= 0.3.4)
+ rdf-virtuoso (0.0.13)
+ active_attr
+ activesupport (~> 3.2.3)
+ api_smith (~> 1.2.0)
+ httparty (~> 0.8.2)
+ rdf (~> 0.3.7)
+ transaction-simple (~> 1.4.0)
+ uuid (~> 2.3.5)
GIT
remote: http://github.com/pkordel/feedzirra.git
@@ -24,20 +27,6 @@ GIT
sax-machine (~> 0.1.0)
GIT
- remote: https://github.com/digibib/rdf-virtuoso.git
- revision: 04e7a5b7237173e6af2e64f2f658aa8ca586709e
- branch: develop
- specs:
- rdf-virtuoso (0.0.10)
- active_attr
- activesupport (~> 3.2.3)
- api_smith
- httparty (= 0.8.2)
- rdf (~> 0.3.5)
- transaction-simple (~> 1.4.0)
- uuid (~> 2.3.5)
-
-GIT
remote: https://github.com/kytrinyx/faker.git
revision: da25441857ebd1028332dfb617ee3a6400f739e3
specs:
@@ -77,16 +66,16 @@ GEM
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
- addressable (2.2.8)
+ addressable (2.3.2)
api_smith (1.2.0)
hashie (~> 1.0)
httparty
net-http-persistent
arel (3.0.2)
bcrypt-ruby (3.0.1)
- bootstrap-sass (2.0.3)
+ bootstrap-sass (2.0.4.0)
builder (3.0.0)
- capistrano (2.12.0)
+ capistrano (2.13.3)
highline
net-scp (>= 1.0.0)
net-sftp (>= 2.0.0)
@@ -99,64 +88,66 @@ GEM
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
- childprocess (0.3.1)
- ffi (~> 1.0.6)
+ childprocess (0.3.5)
+ ffi (~> 1.0, >= 1.0.6)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
coffee-script (2.2.0)
coffee-script-source
execjs
- coffee-script-source (1.2.0)
+ coffee-script-source (1.3.3)
columnize (0.3.6)
curb (0.7.18)
- daemons (1.1.8)
- database_cleaner (0.7.2)
- debugger (1.1.3)
+ daemons (1.1.9)
+ database_cleaner (0.8.0)
+ debugger (1.2.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.1.1)
- debugger-ruby_core_source (~> 1.1.2)
- debugger-linecache (1.1.1)
+ debugger-ruby_core_source (~> 1.1.3)
+ debugger-linecache (1.1.2)
debugger-ruby_core_source (>= 1.1.1)
- debugger-ruby_core_source (1.1.2)
+ debugger-ruby_core_source (1.1.3)
diff-lcs (1.1.3)
erubis (2.7.0)
eventmachine (0.12.10)
- execjs (1.3.0)
+ excon (0.16.1)
+ execjs (1.4.0)
multi_json (~> 1.0)
- fabrication (1.3.2)
+ fabrication (2.2.2)
fakeweb (1.3.0)
- faye-websocket (0.4.5)
+ faye-websocket (0.4.6)
eventmachine (>= 0.12.0)
- ffi (1.0.11)
- friendly_id (4.0.4)
+ ffi (1.1.5)
+ friendly_id (4.0.8)
hashie (1.2.0)
- heroku (2.25.0)
+ heroku (2.30.4)
+ heroku-api (~> 0.3.4)
launchy (>= 0.3.2)
- netrc (~> 0.7.1)
+ netrc (~> 0.7.7)
rest-client (~> 1.6.1)
rubyzip
- highline (1.6.12)
+ heroku-api (0.3.4)
+ excon (~> 0.16.1)
+ highline (1.6.13)
hike (1.2.1)
http_parser.rb (0.5.3)
- httparty (0.8.2)
- multi_json
+ httparty (0.8.3)
+ multi_json (~> 1.0)
multi_xml
i18n (0.6.0)
- journey (1.0.3)
- jquery-rails (2.0.2)
- railties (>= 3.2.0, < 5.0)
+ journey (1.0.4)
+ jquery-rails (2.1.1)
+ railties (>= 3.1.0, < 5.0)
thor (~> 0.14)
- jruby-pageant (1.0.2)
- json (1.6.6)
- json_pure (1.6.6)
+ json (1.7.5)
kaminari (0.13.0)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
- launchy (2.1.0)
- addressable (~> 2.2.6)
- libwebsocket (0.1.3)
+ launchy (2.1.2)
+ addressable (~> 2.3)
+ libwebsocket (0.1.5)
addressable
loofah (1.2.1)
nokogiri (>= 1.4.4)
@@ -166,24 +157,23 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
- mime-types (1.18)
+ mime-types (1.19)
moneta (0.6.0)
multi_json (1.3.6)
multi_xml (0.5.1)
- net-http-persistent (1.4.1)
+ net-http-persistent (2.7)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
net-sftp (2.0.5)
net-ssh (>= 2.0.9)
- net-ssh (2.4.0)
- jruby-pageant (>= 1.0.2)
+ net-ssh (2.5.2)
net-ssh-gateway (1.1.0)
net-ssh (>= 1.99.1)
- netrc (0.7.1)
- nokogiri (1.5.2)
- pg (0.13.2)
- poltergeist (0.6.0)
- capybara (~> 1.0)
+ netrc (0.7.7)
+ nokogiri (1.5.5)
+ pg (0.14.0)
+ poltergeist (0.7.0)
+ capybara (~> 1.1)
childprocess (~> 0.3)
faye-websocket (~> 0.4, >= 0.4.4)
http_parser.rb (~> 0.5.3)
@@ -218,65 +208,64 @@ GEM
json (~> 1.4)
rest-client (1.6.7)
mime-types (>= 1.16)
- rocket_pants (1.1.0)
+ rocket_pants (1.5.4)
actionpack (~> 3.0)
api_smith
hashie (~> 1.0)
moneta
railties (~> 3.0)
will_paginate (~> 3.0)
- rspec (2.9.0)
- rspec-core (~> 2.9.0)
- rspec-expectations (~> 2.9.0)
- rspec-mocks (~> 2.9.0)
- rspec-core (2.9.0)
- rspec-expectations (2.9.1)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
- rspec-mocks (2.9.0)
- rspec-rails (2.9.0)
+ rspec-mocks (2.11.2)
+ rspec-rails (2.11.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
- rspec (~> 2.9.0)
- rubyzip (0.9.7)
- rvm-capistrano (1.2.1)
+ rspec (~> 2.11.0)
+ rubyzip (0.9.9)
+ rvm-capistrano (1.2.5)
capistrano (>= 2.0.0)
- sass (3.1.15)
+ sass (3.2.1)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
sax-machine (0.1.0)
nokogiri (> 0.0.0)
- selenium-webdriver (2.21.0)
+ selenium-webdriver (2.25.0)
childprocess (>= 0.2.5)
- ffi (~> 1.0)
libwebsocket (~> 0.1.3)
multi_json (~> 1.0)
rubyzip
- simple_form (2.0.1)
+ simple_form (2.0.2)
actionpack (~> 3.0)
activemodel (~> 3.0)
simplecov (0.6.4)
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
- slim (1.2.0)
+ slim (1.2.2)
temple (~> 0.4.0)
tilt (~> 1.3.3)
slim-rails (1.0.3)
actionpack (~> 3.0)
activesupport (~> 3.0)
railties (~> 3.0)
slim (~> 1.0)
- sprockets (2.1.2)
+ sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
- sqlite3 (1.3.5)
- systemu (2.5.1)
+ sqlite3 (1.3.6)
+ systemu (2.5.2)
temple (0.4.0)
- thin (1.3.1)
+ thin (1.4.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
@@ -287,12 +276,12 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)
- uglifier (1.2.4)
+ uglifier (1.2.7)
execjs (>= 0.3.0)
- multi_json (>= 1.0.2)
+ multi_json (~> 1.3)
uuid (2.3.5)
macaddr (~> 1.0)
- vcr (2.0.1)
+ vcr (2.2.4)
will_paginate (3.0.3)
xpath (0.1.4)
nokogiri (~> 1.3)
@@ -314,7 +303,7 @@ DEPENDENCIES
feedzirra!
friendly_id (~> 4.0.0)
heroku
- httparty (= 0.8.2)
+ httparty (~> 0.8.2)
jquery-rails
kaminari
launchy
@@ -332,7 +321,6 @@ DEPENDENCIES
simple_form
simplecov
slim-rails
- sparql-client!
sqlite3
thin
uglifier (>= 1.0.3)
View
48 app/models/audience.rb
@@ -1,4 +1,5 @@
require 'active_rdf'
+require 'rdf'
class Audience < ActiveRDF::Model
@@ -9,12 +10,12 @@ class Audience < ActiveRDF::Model
class << self
def prefixes
- <<-pref
-PREFIX dct: <#{RDF::DC}>
-PREFIX foaf: <http://xmlns.com/foaf/0.1/>
-PREFIX fabio: <http://purl.org/spar/fabio/>
-PREFIX rev: <http://purl.org/stuff/rev#>
- pref
+ RDF::Virtuoso::Prefixes.new ({
+ dct: "#{RDF::DC}",
+ foaf: "http://xmlns.com/foaf/0.1/",
+ fabio: "http://purl.org/spar/fabio/",
+ rev: "http://purl.org/stuff/rev#"
+ })
end
def type
@@ -26,14 +27,15 @@ def graph
if defined?(Rails)
url = url.join Rails.env if Rails.env.test?
end
- url / "reviews"
+ url / "audiences"
end
def all(*args)
options = args.dup
options = options.last.is_a?(Hash) ? options.last : {}
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?label
@@ -43,9 +45,11 @@ def all(*args)
rdfs:label ?label . }
}
q
-
+=end
+ query = QUERY.select(:subject, :label).where([:subject, RDF.type, self.type],[:subject, RDF::RDFS.label, :label]).graph(self.graph)
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ #query << "LIMIT %d" % limit
+ query.limit(limit)
end
Rails.logger.warn query
@@ -58,7 +62,7 @@ def all(*args)
end
def find_by_subject(subject, conditions = {})
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?label
@@ -68,13 +72,15 @@ def find_by_subject(subject, conditions = {})
rdfs:label ?label . }
}
q
-
+=end
+ query = QUERY.select(:label).where([RDF::URI(subject), RDF.type, self.type], [RDF::URI(subject), RDF::RDFS.label, :label]).graph(self.graph)
if Rails.env.development?
puts "From find: \n%s" % query
end
Rails.logger.warn query
-
+
+ #results = REPOSITORY.select(query)
results = execute(query)
begin
@@ -101,7 +107,7 @@ def update(attribute_names = @attributes.keys)
klass = self.class
subject = subject_for(self.id)
-
+=begin
query = <<-q
#{klass.prefixes}
MODIFY GRAPH <#{klass.graph}>
@@ -110,10 +116,15 @@ def update(attribute_names = @attributes.keys)
INSERT { <#{subject}> rdfs:label "#{self.label}" }
q
-
+=end
+ deletequery = QUERY.delete_data([RDF::URI(subject), RDF::RDFS.label, "#{self.label_was}"]).graph(klass.graph)
+ insertquery = QUERY.insert_data([RDF::URI(subject), RDF::RDFS.label, "#{self.label}"]).graph(klass.graph)
+
Rails.logger.warn query
- CLIENT.update(query)
+ #REPOSITORY.update(query)
+ REPOSITORY.delete_data(deletequery)
+ REPOSITORY.insert_data(insertquery)
@changed_attributes.clear
number_of_changed_attributes
@@ -123,18 +134,19 @@ def create
klass = self.class
id = guid
subject = subject_for(id)
-
+=begin
query = <<-q
#{klass.prefixes}
insert into <#{klass.graph}> {
<#{subject}> a <#{klass.type}>;
rdfs:label "#{self.label}" .
}
q
-
+=end
+ query = QUERY.insert_data([RDF::URI(subject), RDF.type, klass.type], [RDF::URI(subject), RDF::RDFS.label, "#{self.label}"]).graph(klass.graph)
Rails.logger.warn query
- CLIENT.insert(query)
+ REPOSITORY.insert_data(query)
self.subject = subject
self.id = id
@changed_attributes.clear
View
166 app/models/book.rb
@@ -1,4 +1,5 @@
require 'active_rdf'
+require 'rdf'
class Book < ActiveRDF::Model
@@ -14,23 +15,23 @@ class Book < ActiveRDF::Model
class << self
def prefixes
- <<-pref
-PREFIX bibo: <http://purl.org/ontology/bibo/>
-PREFIX dbonto: <http://dbpedia.org/ontology/>
-PREFIX dct: <#{RDF::DC}>
-PREFIX deich: <http://data.deichman.no/>
-PREFIX foaf: <#{RDF::FOAF}>
-PREFIX format: <http://data.deichman.no/format/>
-PREFIX person: <http://data.deichman.no/person/>
-PREFIX rdfs: <#{RDF::RDFS}>
-PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
-PREFIX bibsys: <http://def.bibsys.no/xmlns/radatana/1.0#>
-PREFIX fabio: <http://purl.org/spar/fabio/>
- pref
+ RDF::Virtuoso::Prefixes.new ({
+ bibo: "http://purl.org/ontology/bibo/",
+ dbonto: "http://dbpedia.org/ontology/",
+ dct: "#{RDF::DC}",
+ deich: "http://data.deichman.no/",
+ foaf: "#{RDF::FOAF}",
+ format: "http://data.deichman.no/format/",
+ person: "http://data.deichman.no/person/",
+ rdfs: "#{RDF::RDFS}",
+ skos: "http://www.w3.org/2004/02/skos/core#",
+ bibsys: "http://def.bibsys.no/xmlns/radatana/1.0#",
+ fabio: "http://purl.org/spar/fabio/"
+ })
end
def type
- RDF::URI('http://purl.org/ontology/bibo/Document')
+ RDF::BIBO.Document
end
def graph
@@ -45,7 +46,7 @@ def all(*args)
options = args.dup
options = options.last.is_a?(Hash) ? options.last : {}
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?title ?isbn
@@ -57,20 +58,25 @@ def all(*args)
dct:title ?title .
}}
q
-
+=end
+ query = QUERY.select(:subject, :title, :isbn).where(
+ [:subject, RDF.type, self.type],
+ [:subject, RDF::DC.format, RDF::FORMAT.Book],
+ [:subject, RDF::BIBO.isbn, :isbn],
+ [:subject, RDF::DC.title, :title]
+ ).from(self.graph)
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ query.limit(limit)
end
if Rails.env.development?
puts "From all: \n#{query}"
end
-
execute(query)
end
def find_by_subject(subject, conditions = {})
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?isbn ?title (sql:GROUP_DIGEST(?author, ', ', 1000, 1)) as ?author ?cover_url
@@ -85,12 +91,28 @@ def find_by_subject(subject, conditions = {})
OPTIONAL { <#{subject}> foaf:depiction ?cover_url . }
}}
q
-
+=end
+ subject = RDF::URI(subject)
+ query = QUERY.select(:isbn, :title, :cover_url).group_digest(:author, ', ', 1000, 1)
+ query.from(self.graph)
+ query.where(
+ [subject, RDF.type, self.type],
+ [subject, RDF::DC.format, RDF::FORMAT.Book],
+ [subject, RDF::BIBO.isbn, :isbn],
+ [subject, RDF::DC.title, :title]
+ ).optional(
+ [subject, RDF::DC.creator, :creator],
+ [:creator, RDF::FOAF.name, :author]
+ ).optional(
+ [subject, RDF::FOAF.depiction, :cover_url]
+ )
if Rails.env.development?
puts "From find: \n#{query}"
end
+ #results = REPOSITORY.select(query)
results = execute(query)
+
return nil unless results.any?
record = results.fetch(0)
@@ -104,7 +126,7 @@ def find_by_subject(subject, conditions = {})
def find_by_isbn(isbn, conditions = {})
isbn = sanitize_isbn(isbn)
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?title (sql:GROUP_DIGEST(?author, ', ', 1000, 1)) as ?author ?cover_url
@@ -119,17 +141,34 @@ def find_by_isbn(isbn, conditions = {})
OPTIONAL { ?subject foaf:depiction ?cover_url . }
}}
q
-
+=end
+ query = QUERY.select(:subject, :title, :cover_url).group_digest(:author, ', ', 1000, 1)
+ query.from(self.graph)
+ query.where(
+ [:subject, RDF.type, self.type],
+ [:subject, RDF::DC.format, RDF::FORMAT.Book],
+ [:subject, RDF::BIBO.isbn, "#{isbn}"],
+ [:subject, RDF::DC.title, :title]
+ ).optional(
+ [:subject, RDF::DC.creator, :creator],
+ [:creator, RDF::FOAF.name, :author]
+ ).optional(
+ [:subject, RDF::FOAF.depiction, :cover_url]
+ )
if Rails.env.development?
puts "From find_by_isbn: \n#{query}"
end
+ #results = REPOSITORY.select(query)
results = execute(query)
return nil unless results.any?
record = results.first
- if record.cover_url.blank?
+ if record[:cover_url].blank?
+
+ ## check this query!
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?title (sql:GROUP_DIGEST(?author, ', ', 1000, 1)) as ?author ?cover_url
@@ -149,20 +188,68 @@ def find_by_isbn(isbn, conditions = {})
foaf:depiction ?cover_url .}
}}
q
+=end
+ query = QUERY.select(:subject, :title, :cover_url).group_digest(:author, ', ', 1000, 1)
+ query.from(self.graph)
+ query.where(
+ [:subject, RDF.type, self.type],
+ [:subject, RDF::DC.format, :format],
+ [:subject, RDF::DC.language, :lang],
+ [:subject, RDF::BIBO.isbn, "#{isbn}"],
+ [:subject, RDF::DC.title, :title]
+ ).optional(
+ [:subject, RDF::DC.creator, :creator],
+ [:creator, RDF::FOAF.name, :author]
+ ).optional(
+ [:work, RDF::FABIO.hasManifestation, :subject],
+ [:work, RDF::FABIO.hasManifestation, :other],
+ [:other, RDF::DC.format, :format],
+ [:other, RDF::DC.language, :lang],
+ [:other, RDF::FOAF.depiction, :cover_url],
+ )
results = execute(query)
+ # results = REPOSITORY.select(query)
+
found = results.first if results.any?
- record = found unless found.cover_url.blank?
+ record = found unless found[:cover_url].blank?
end
-
- record.isbn = isbn
+ record[:isbn] = isbn
record.changed_attributes.clear # Should this be here?
record
end
def sanitize_isbn(isbn="")
isbn.strip.gsub(/\s+|-/, '')
end
+
+ # find cover from one of three optionals
+ def cover_url
+ query = QUERY.select(:cover_url, :same_language_image, :any_image)
+ query.from(self.graph)
+ query.where([subject = RDF::URI(subject), RDF::DC.language, :lang])
+ optional([:book, RDF::FOAF.depiction, :cover_url])
+ optional([:work, RDF::FABIO.hasManifestation, :book])
+ optional([:work, RDF::FABIO.hasManifestation, :another_book],
+ [:another_book, RDF::DC.language, :lang],
+ [:another_book, RDF::FOAF.depiction, :same_language_image])
+ optional([:work, RDF::FABIO.hasManifestation, :any_book],
+ [:any_book, RDF::FOAF.depiction, :any_image])
+
+
+ results = execute(query)
+ # results = REPOSITORY.select(query)
+ found = results.first if results.any?
+ if !found[:cover_url].blank?
+ cover_url = found[:cover_url]
+ elsif !found[:same_language_image].blank?
+ cover_url = found[:same_language_image]
+ elsif !found[:any_image].blank?
+ cover_url = found[:any_image]
+ else
+ cover_url = nil
+ end
+ end
end # End class methods
@@ -185,7 +272,7 @@ def update(attribute_names = @attributes.keys)
klass = self.class
subject = subject_for(self.id)
-
+=begin
query = <<-q
#{klass.prefixes}
MODIFY GRAPH <#{klass.graph}>
@@ -196,9 +283,16 @@ def update(attribute_names = @attributes.keys)
INSERT { <#{subject}> dct:title "#{self.title}" ;
bibo:isbn "#{self.isbn}" . }
q
-
- CLIENT.update(query)
-
+=end
+ deletequery = QUERY.delete_data([RDF::URI(subject), RDF::DC.title, "#{self.title_was}"],
+ [RDF::URI(subject), RDF::BIBO.isbn, "#{self.isbn_was}"]).graph(klass.graph)
+ insertquery = QUERY.insert_data([RDF::URI(subject), RDF::DC.title, "#{self.title}"],
+ [RDF::URI(subject), RDF::BIBO.isbn, "#{self.isbn}"]).graph(klass.graph)
+
+ #REPOSITORY.update(query)
+ REPOSITORY.delete_data(deletequery)
+ REPOSITORY.insert_data(insertquery)
+
@changed_attributes.clear
number_of_changed_attributes
end
@@ -208,7 +302,7 @@ def create
klass = self.class
id = guid
subject = subject_for(id)
-
+=begin
query = <<-q
#{klass.prefixes}
insert into <#{klass.graph}> {
@@ -218,12 +312,18 @@ def create
dct:title "#{self.title}"
}
q
-
+=end
+ subject = RDF::URI(subject)
+ query = QUERY.insert_data([subject, RDF.type, klass.type],
+ [subject, RDF::DC.format, RDF::FORMAT.Book],
+ [subject, RDF::BIBO.isbn, "#{self.isbn}"],
+ [subject, RDF::DC.title, "#{self.title}"]
+ ).graph(klass.graph)
if Rails.env.development?
puts "From create: \n#{query}"
end
- CLIENT.insert(query)
+ REPOSITORY.insert_data(query)
self.subject = subject
self.id = id
@changed_attributes.clear
View
43 app/models/genre.rb
@@ -17,7 +17,7 @@ def prefixes
end
def type
- RDF::URI('http://dbpedia.org/class/yago/LiteraryGenres')
+ RDF::YAGO.LiteraryGenres
end
def graph
@@ -32,7 +32,7 @@ def all(*args)
options = args.dup
options = options.last.is_a?(Hash) ? options.last : {}
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?label
@@ -42,16 +42,19 @@ def all(*args)
rdfs:label ?label
}}
q
-
+=end
+ query = QUERY.select(:subject, :label).where([:subject, RDF.type, self.type],[:subject, RDF::RDFS.label, :label).from(self.graph)
+
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ query.limit(limit)
end
if Rails.env.development?
puts "From all: \n#{query}"
end
-
- execute(query)
+
+ REPOSITORY.select(query)
+ #execute(query)
end
def find(object_or_id, conditions = {})
@@ -61,7 +64,7 @@ def find(object_or_id, conditions = {})
when self then object_or_id.subject
else raise ActiveModel::MissingAttributeError.new(object_or_id.inspect)
end
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?label
@@ -71,14 +74,16 @@ def find(object_or_id, conditions = {})
rdfs:label ?label . }
}
q
-
+=end
+ query = QUERY.select(:label).where([RDF::URI(subject), RDF.type, self.type],[:subject, RDF::RDFS.label, :label).from(self.graph)
+
if Rails.env.development?
puts "From find: \n%s" % query
end
Rails.logger.warn query
-
- results = execute(query)
+ REPOSITORY.select(query)
+ #results = execute(query)
begin
record = results.fetch(0)
@@ -104,7 +109,7 @@ def update(attribute_names = @attributes.keys)
klass = self.class
subject = subject_for(self.id)
-
+=begin
query = <<-q
#{klass.prefixes}
MODIFY GRAPH <#{klass.graph}>
@@ -113,10 +118,15 @@ def update(attribute_names = @attributes.keys)
INSERT { <#{subject}> rdfs:label "#{self.label}" }
q
-
+=end
+ deletequery = QUERY.delete_data([RDF::URI(subject), RDF::RDFS.label, "#{self.label_was}"]).graph(klass.graph)
+ insertquery = QUERY.insert_data([RDF::URI(subject), RDF::RDFS.label, "#{self.label}"]).graph(klass.graph)
+
Rails.logger.warn query
- CLIENT.update(query)
+ #REPOSITORY.update(query)
+ REPOSITORY.delete_data(deletequery)
+ REPOSITORY.insert_data(insertquery)
@changed_attributes.clear
number_of_changed_attributes
@@ -126,18 +136,19 @@ def create
klass = self.class
id = guid
subject = subject_for(id)
-
+=begin
query = <<-q
#{klass.prefixes}
insert into <#{self.graph}> {
<#{subject}> a <#{klass.type}>;
rdfs:label "#{self.label}" .
}
q
-
+=end
+ query = QUERY.insert_data([RDF::URI(subject), RDF.type, klass.type], [RDF::URI(subject), RDF::RDFS.label, "#{self.label}"]).graph(self.graph)
Rails.logger.warn query
- CLIENT.insert(query)
+ REPOSITORY.insert_data(query)
self.subject = subject
self.id = id
@changed_attributes.clear
View
37 app/models/organization.rb
@@ -30,7 +30,7 @@ def all(*args)
options = args.dup
options = options.last.is_a?(Hash) ? options.last : {}
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?name
@@ -40,17 +40,20 @@ def all(*args)
foaf:name ?name . }
}
q
+=end
+
+ query = QUERY.select(:subject, :name).where([:subject, RDF.type, self.type],[:subject, RDF::FOAF.name, :name]).from(self.graph)
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ query.limit(limit)
end
Rails.logger.warn query
if Rails.env.development?
puts "From all: \n%s" % query
end
-
+ #REPOSITORY.select(query)
execute(query)
end
@@ -59,7 +62,7 @@ def find_by_id(id, conditions = {})
end
def find_by_subject(subject, conditions = {})
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?name
@@ -69,13 +72,15 @@ def find_by_subject(subject, conditions = {})
foaf:name ?name . }
}
q
-
+=end
+ query = QUERY.select(:name).where([RDF::URI(subject), RDF.type, self.type],[RDF::URI(subject), RDF::FOAF.name, :name]).from(self.graph)
+
if Rails.env.development?
puts "From find: \n%s" % query
end
Rails.logger.warn query
-
+ #results = REPOSITORY.select(query)
results = execute(query)
begin
@@ -103,6 +108,7 @@ def update(attribute_names = @attributes.keys)
klass = self.class
subject = subject_for(self.id)
+=begin
query = <<-q
#{klass.prefixes}
MODIFY GRAPH <#{klass.graph}>
@@ -111,10 +117,16 @@ def update(attribute_names = @attributes.keys)
INSERT { <#{subject}> foaf:name "#{self.name}" }
q
+=end
+ deletequery = QUERY.delete_data([RDF::URI(subject), RDF::FOAF.name, "#{self.name_was}"]).graph(klass.graph)
+ insertquery = QUERY.insert_data([RDF::URI(subject), RDF::FOAF.name, "#{self.name}"]).graph(klass.graph)
- Rails.logger.warn query
-
- CLIENT.update(query)
+ Rails.logger.warn deletequery
+ Rails.logger.warn insertquery
+
+ #REPOSITORY.update(query)
+ REPOSITORY.delete_data(deletequery)
+ REPOSITORY.insert_data(insertquery)
@changed_attributes.clear
number_of_changed_attributes
@@ -124,16 +136,19 @@ def create
klass = self.class
id = guid
subject = subject_for(id)
-
+=begin
query = <<-q
#{klass.prefixes}
insert into <#{klass.graph}> {
<#{subject}> a <#{klass.type}>;
foaf:name "#{self.name}" .
}
q
+=end
+ query = QUERY.insert_data([RDF::URI(subject), RDF.type, klass.type], [RDF::URI(subject), RDF::FOAF.name, "#{self.name}"]).graph(self.graph)
+
Rails.logger.warn query
- CLIENT.insert(query)
+ REPOSITORY.insert_data(query)
self.subject = subject
self.id = id
@changed_attributes.clear
View
27 app/models/person.rb
@@ -30,7 +30,7 @@ def all(*args)
options = args.dup
options = options.last.is_a?(Hash) ? options.last : {}
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?name
@@ -40,16 +40,19 @@ def all(*args)
foaf:name ?name . }
}
q
+=end
+ query = QUERY.select(:subject, :name).where([:subject, RDF.type, self.type],[:subject, RDF::FOAF.name, :name]).from(self.graph)
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ query.limit(limit)
end
if Rails.env.development?
puts "From all: \n%s" % query
end
- execute(query)
+ REPOSITORY.select(query)
+ #execute(query)
end
def find(object_or_id, conditions = {})
@@ -60,6 +63,7 @@ def find(object_or_id, conditions = {})
else raise ActiveModel::MissingAttributeError.new(object_or_id.inspect)
end
+=begin
query = <<-q
#{self.prefixes}
SELECT ?name
@@ -69,15 +73,20 @@ def find(object_or_id, conditions = {})
foaf:name ?name . }
}
q
+=end
+
+ query = QUERY.select(:name).where([RDF::URI(subject), RDF.type, self.type],[RDF::URI(subject), RDF::FOAF.name, :name]).from(self.graph)
if Rails.env.development?
puts "From find: \n%s" % query
end
+ #results = REPOSITORY.select(query)
results = execute(query)
begin
record = results.fetch(0)
+ p record
record.subject = subject
record.id = id_for(subject)
record.changed_attributes.clear # Should this be here?
@@ -110,7 +119,12 @@ def update(attribute_names = @attributes.keys)
INSERT { <#{subject}> foaf:name "#{self.name}" }
q
- CLIENT.update(query)
+ deletequery = QUERY.delete_data([RDF::URI(subject), RDF::FOAF.name, "#{self.name_was}"]).graph(klass.graph)
+ insertquery = QUERY.insert_data([RDF::URI(subject), RDF::FOAF.name, "#{self.name}"]).graph(klass.graph)
+
+ #REPOSITORY.update(query)
+ REPOSITORY.delete_data(deletequery)
+ REPOSITORY.insert_data(insertquery)
@changed_attributes.clear
number_of_changed_attributes
@@ -121,15 +135,18 @@ def create
id = guid
subject = subject_for(id)
+=begin
query = <<-q
#{klass.prefixes}
insert into <#{klass.graph}> {
<#{subject}> a <#{klass.type}>;
foaf:name "#{self.name}" .
}
q
+=end
+ query = QUERY.insert_data([RDF::URI(subject), RDF.type, klass.type], [RDF::URI(subject), RDF::FOAF.name, "#{self.name}"]).graph(self.graph)
- CLIENT.insert(query)
+ REPOSITORY.insert_data(query)
self.subject = subject
self.id = id
@changed_attributes.clear
View
78 app/models/query_parser.rb
@@ -26,6 +26,7 @@ def parse(params = {})
subject = params[:subject]
person = params[:person]
+=begin
query = ""
query << prefixes
query << "SELECT DISTINCT "
@@ -69,12 +70,45 @@ def parse(params = {})
query << "} ORDER BY DESC(?issued)"
query << " LIMIT 10"
query
-
+=end
+ # book data
+ bookgraph = {:context => RDF::URI("http://data.deichman.no/books")}
+
+ query = QUERY.select(:review, :title, :text, :abstract).sample(:book_title)
+ query.group_digest(:book_author, ', ', 1000, 1)
+ query.sample(:img)
+ query.where([:book, RDF::DC.format, RDF::FORMAT.Book, bookgraph])
+ query.where([:book, RDF::DC.audience, RDF::URI(audience), bookgraph]) unless audience.blank?
+ query.where([:book, RDF::DEICHMAN.literaryFormat, RDF::URI(format), bookgraph]) unless format.blank?
+ query.where([:book, RDF::DBO.literaryGenre, RDF::URI(genre), bookgraph]) unless genre.blank?
+ query.where([:book, RDF::DC.subject, RDF::URI(subject), bookgraph]) unless subject.blank?
+ query.where([:book, RDF::DC.subject, RDF::URI(person), bookgraph]) unless person.blank?
+ query.where([:book, RDF::DC.title, :book_title, bookgraph])
+ # work data
+ query.optional([:book, RDF::FOAF.depiction, :img, bookgraph],
+ [:work, RDF::FABIO.hasManifestation, :book, bookgraph])
+ query.optional([:work, RDF::DC.creator, :creator, bookgraph],
+ [:creator, RDF::FOAF.name, :book_author, bookgraph])
+
+ # review data
+ reviewgraph = {:context => RDF::URI("http://data.deichman.no/reviews")}
+
+ query.where(
+ [:review, RDF::DC.subject, :work, reviewgraph],
+ [:review, RDF::REV.title, :title, reviewgraph],
+ [:review, RDF::DC.issued, :issued, reviewgraph]
+ )
+ query.optional([:review, RDF::REV.text, :text, reviewgraph])
+ query.optional([:review, RDF::DC.abstract, :abstract, reviewgraph])
+ query.filter("lang(?text) != \"nn\"")
+ query.order('DESC(?issued)')
+ query.limit(10)
+
if Rails.env.development? || Rails.env.staging?
puts "From QueryParser#parse: \n#{query}"
end
- result = CLIENT.select(query)
+ result = REPOSITORY.select(query)
collection = []
result.each_solution do |solution|
review = Review.new(
@@ -93,21 +127,33 @@ def parse(params = {})
end
def dropdown(attr)
+ select = QUERY.select.where
selector = case attr
+
when "audience"
- "dct:audience ?value .\n?value rdfs:label ?label .\n"
+ #"dct:audience ?value .\n?value rdfs:label ?label .\n"
+ p1 = [:book, RDF::DC.audience, :value]
+ p2 = [:value, RDF::RDFS.label, :label]
when "literary_format"
- "deich:literaryFormat ?value . ?value rdfs:label ?label .\n"
+ #"deich:literaryFormat ?value . ?value rdfs:label ?label .\n"
+ p1 = [:book, RDF::DEICHMAN.literaryFormat, :value]
+ p2 = [:value, RDF::RDFS.label, :label]
when "genre"
- "dbo:literaryGenre ?value .\n?value rdfs:label ?label .\n"
+ #"dbo:literaryGenre ?value .\n?value rdfs:label ?label .\n"
+ p1 = [:book, RDF::DBO.literaryGenre, :value]
+ p2 = [:value, RDF::RDFS.label, :label]
when "subject"
- "dct:subject ?value .\n?value skos:prefLabel ?label .\n"
+ #"dct:subject ?value .\n?value skos:prefLabel ?label .\n"
+ p1 = [:book, RDF::DC.subject, :value]
+ p2 = [:value, RDF::SKOS.prefLabel, :label]
when "person"
- "dct:subject ?value .\n?value bibsys:catalogueName ?label .\n"
+ #"dct:subject ?value .\n?value bibsys:catalogueName ?label .\n"
+ p1 = [:book, RDF::DC.subject, :value]
+ p2 = [:value, RDF::RADATANA.catalogueName, :label]
end
return [] unless selector.present?
-
+=begin
query = ""
query << prefixes
query << "SELECT DISTINCT ?value ?label\n"
@@ -118,12 +164,24 @@ def dropdown(attr)
query << "?work fabio:hasManifestation ?book ; "
query << "rev:hasReview ?review .\n"
query << "} ORDER BY str(?label)"
-
+=end
+
+ query = QUERY.select(:value, :label).distinct
+ query.where([:book, RDF::DC.format, RDF::FORMAT.Book],
+ [:book, RDF::DC.title, :title],
+ [:book, RDF::FOAF.depiction, :img])
+ query.where([:work, RDF::FABIO.hasManifestation, :book],
+ [:work, RDF::REV.hasReview, :review])
+ query.where(p1)
+ query.where(p2)
+
+ query.order('str(?label)')
+
if Rails.env.development? || Rails.env.staging?
puts "From QueryParser#dropdown: \n#{query}"
end
- parse_response(CLIENT.select(query), attr)
+ parse_response(REPOSITORY.select(query), attr)
end
def parse_response(response, attr)
View
234 app/models/review.rb
@@ -43,7 +43,7 @@ def prefixes
end
def type
- RDF::URI('http://purl.org/stuff/rev#') / 'Review'
+ RDF::REV.Review
end
#def graph
@@ -52,6 +52,7 @@ def type
#end
def count
+=begin
query = <<-q
#{self.prefixes}
SELECT COUNT(distinct ?subject)
@@ -66,15 +67,23 @@ def count
}
}
q
- result = CLIENT.select(query)
- result.first['callret-0'].to_i
+=end
+ query = QUERY.select.count(:subject).distinct.from(self.graph).where(
+ [:subject, RDF.type, self.type],
+ [:subject, RDF::REV.title, :title],
+ [:subject, RDF::REV.text, :text],
+ [:subject, RDF::DC.issued, :issued]
+ ).optional(
+ [:subject, RDF::DEICHMAN.basedOnManifestation, :book_id]).filter("lang(?text) != \"nn\"").filter("regex(?book_id, \"http://data.deichman.no\")")
+ solutions = REPOSITORY.select(query)
+ count = solutions.first[:count].to_i
end
def all(*args)
options = args.dup
options = options.last.is_a?(Hash) ? options.last : {}
-
+=begin
query = <<-q
#{self.prefixes}
SELECT
@@ -108,13 +117,25 @@ def all(*args)
}
} ORDER BY DESC(?issued)
q
-
+=end
+ query = QUERY.select(:subject, :title, :abstract, :text, :book_id).sample(:book_title)
+ query.group_digest(:book_author, ', ', 1000, 1).sample(:cover_url).where(
+ [:subject, RDF.type, self.type],
+ [:subject, RDF::REV.title, :title],
+ [:subject, RDF::REV.text, :text],
+ [:subject, RDF::DC.issued, :issued]
+ ).optional(
+ [:subject, RDF::DC.abstract, :abstract]).optional(
+ [:subject, RDF::DEICHMAN.basedOnManifestation, :book_id])
+ query.filter('lang(?text) != "nn"')
+ query.filter('regex(?book_id, "http://data.deichman.no")')
+ query.order('DESC(?issued)').from(self.graph)
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ query.limit(limit)
end
if offset = options[:offset]
- query << " OFFSET %d" % offset
+ query.offset(offset)
end
if Rails.env.development? || Rails.env.staging?
@@ -136,7 +157,7 @@ def find_by_user(user, options = {})
return [] unless user.person.present?
reviewer_id = user.person.subject
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?title ?abstract ?text ?book_id ?source_id ?publisher_id ?audience_id ?work_id ?issued
@@ -149,13 +170,21 @@ def find_by_user(user, options = {})
deich:basedOnManifestation ?book_id ;
rev:reviewer <#{reviewer_id}> .
q
-
+=end
+ query = QUERY.select(:subject, :title, :abstract, :text, :book_id, :source_id, :publisher_id, :audience_id, :work_id, :issued).where(
+ [:subject, RDF.type, self.type],
+ [:subject, RDF::REV.title, :title],
+ [:subject, RDF::DC.abstract, :abstract],
+ [:subject, RDF::REV.text, :text],
+ [:subject, RDF::DEICHMAN.basedOnManifestation, :book_id],
+ [:subject, RDF::REV.reviewer, RDF::URI(reviewer_id)]).from(self.graph)
+
if options[:published] == true
- query << "?subject dct:issued ?issued .\n"
+ query << [:subject, RDF::DC.issued, :issued]
elsif options[:published] == false
- query << "MINUS { ?subject dct:issued ?issued } .\n"
+ query.minus([:subject, RDF::DC.issued, :issued])
end
-
+=begin
query << <<-q
OPTIONAL { ?subject dct:source ?source_id }
OPTIONAL { ?subject dct:publisher ?publisher_id }
@@ -165,13 +194,18 @@ def find_by_user(user, options = {})
FILTER(lang(?text) != "nn")
} ORDER BY DESC(?issued)
q
-
+=end
+ query.optional([:subject, RDF::DC.source, :source_id])
+ query.optional([:subject, RDF::DC.publisher, :publisher_id])
+ query.optional([:subject, RDF::DC.audience, :audience_id])
+ query.optional([:subject, RDF::DC.subject, :work_id])
+
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ query.limit(limit)
end
if offset = options[:offset]
- query << " OFFSET %d" % offset
+ query.offset(offset)
end
if Rails.env.development? || Rails.env.staging?
@@ -182,7 +216,7 @@ def find_by_user(user, options = {})
end
def find_by_subject(subject, conditions = {})
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?title ?abstract ?text ?book_id ?source_id ?publisher_id ?audience_id ?work_id ?reviewer_id ?issued
@@ -201,7 +235,22 @@ def find_by_subject(subject, conditions = {})
OPTIONAL { <#{subject}> dct:issued ?issued }
}}
q
-
+=end
+ subject = RDF::URI(subject)
+
+ query = QUERY.select(:title, :abstract, :text, :book_id, :source_id, :publisher_id, :audience_id, :work_id, :reviewer_id, :issued).where(
+ [subject, RDF.type, self.type],
+ [subject, RDF::REV.title, :title],
+ [subject, RDF::REV.text, :text]).from(self.graph)
+ query.optional([subject, RDF::DC.abstract, :abstract])
+ query.optional([subject, RDF::DEICHMAN.basedOnManifestation, :book_id])
+ query.optional([subject, RDF::DC.source, :source_id])
+ query.optional([subject, RDF::DC.publisher, :publisher_id])
+ query.optional([subject, RDF::DC.audience, :audience_id])
+ query.optional([subject, RDF::DC.subject, :work_id])
+ query.optional([subject, RDF::REV.reviewer, :reviewer_id])
+ query.optional([subject, RDF::DC.issued, :issued])
+
if Rails.env.development?
puts "From find: \n#{query}"
end
@@ -219,7 +268,7 @@ def find_by_subject(subject, conditions = {})
end
def find_by_book_id(book_id)
-
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?title ?abstract ?text ?reviewer_id
@@ -237,14 +286,29 @@ def find_by_book_id(book_id)
} FILTER(lang(?text) != "nn")
}
q
+=end
+ book_id = RDF::URI(book_id)
+ query = QUERY.select(:subject, :title, :abstract, :text, :reviewer_id).where(
+ [:work, RDF::FABIO.hasManifestation, book_id, :context => Book.graph],
+ [:work, RDF::REV.hasReview, :subject, :context => Book.graph])
+ query.where(
+ [:subject, RDF::REV.title, :title, :context => self.graph],
+ [:subject, RDF::DC.abstract, :abstract, :context => self.graph],
+ [:subject, RDF::REV.text, :text, :context => self.graph],
+ [:subject, RDF::DC.issued, :issued, :context => self.graph])
+ query.optional(
+ [:subject, RDF::REV.reviewer, :reviewer_id, :context => self.graph]).filter("lang(?text) != \"nn\"")
+
+
if Rails.env.development?
puts "From find_by_book_id: \n#{query}"
end
-
+
execute(query)
end
def find_by_source(source)
+=begin
query = <<-q
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@@ -257,6 +321,10 @@ def find_by_source(source)
}
}
q
+=end
+ query = QUERY.select(:subject).distinct.where(
+ [:subject, RDF.type, RDF::URI(self.type)],
+ [:subject, RDF::DC.source, RDF::URI(source.subject)]).from(self.graph)
execute(query)
end
@@ -266,8 +334,8 @@ def find_by_source(source)
# TODO: Move this into a presenter
def cover_url
- #@cover_url ||= (book.cover_url if book.present?)
- @cover_url
+ @cover_url ||= (book.cover_url if book.present?)
+ #@cover_url
end
def cover_url=(url)
@@ -350,68 +418,90 @@ def update(attribute_names = @attributes.keys)
insert_statements = []
if self.title_changed?
- delete_statements << "<#{subject}> rev:title \"#{self.title_was}\""
- insert_statements << "<#{subject}> rev:title \"#{self.title}\""
+ #delete_statements << "<#{subject}> rev:title \"#{self.title_was}\""
+ #insert_statements << "<#{subject}> rev:title \"#{self.title}\""
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::REV.title, "#{self.title_was}")
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::REV.title, "#{self.title}")
end
if self.abstract_changed?
- delete_statements << "<#{subject}> dct:abstract \"\"\"#{self.abstract_was}\"\"\"@no"
- insert_statements << "<#{subject}> dct:abstract \"\"\"#{self.abstract}\"\"\"@no"
+ #delete_statements << "<#{subject}> dct:abstract \"\"\"#{self.abstract_was}\"\"\"@no"
+ #insert_statements << "<#{subject}> dct:abstract \"\"\"#{self.abstract}\"\"\"@no"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.abstract, RDF::Literal(self.abstract_was, :language => @no))
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.abstract, RDF::Literal(self.abstract, :language => @no))
end
if self.text_changed?
- delete_statements << "<#{subject}> rev:text \"\"\"#{self.text_was}\"\"\"@no"
- insert_statements << "<#{subject}> rev:text \"\"\"#{self.text}\"\"\"@no"
+ #delete_statements << "<#{subject}> rev:text \"\"\"#{self.text_was}\"\"\"@no"
+ #insert_statements << "<#{subject}> rev:text \"\"\"#{self.text}\"\"\"@no"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::REV.text, RDF::Literal(self.text_was, :language => @no))
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::REV.text, RDF::Literal(self.text, :language => @no))
end
if self.audience_id_changed?
- delete_statements << "<#{subject}> dct:audience <#{self.audience_id_was}>" if self.audience_id_was.present?
- insert_statements << "<#{subject}> dct:audience <#{self.audience_id}>"
+ #delete_statements << "<#{subject}> dct:audience <#{self.audience_id_was}>" if self.audience_id_was.present?
+ #insert_statements << "<#{subject}> dct:audience <#{self.audience_id}>"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.audience, "#{self.audience_was}")
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.audience, "#{self.audience}")
end
if self.source_id_changed?
- delete_statements << "<#{subject}> dct:source <#{self.source_id_was}>" if self.source_id_was.present?
- insert_statements << "<#{subject}> dct:source <#{self.source_id}>"
+ #delete_statements << "<#{subject}> dct:source <#{self.source_id_was}>" if self.source_id_was.present?
+ #insert_statements << "<#{subject}> dct:source <#{self.source_id}>"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.source, "#{self.source_was}")
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.source, "#{self.source}")
end
if self.book_id_changed?
- delete_statements << "<#{subject}> deich:basedOnManifestation <#{self.book_id_was}>" if self.book_id_was.present?
- insert_statements << "<#{subject}> deich:basedOnManifestation <#{self.book_id}>"
+ #delete_statements << "<#{subject}> deich:basedOnManifestation <#{self.book_id_was}>" if self.book_id_was.present?
+ #insert_statements << "<#{subject}> deich:basedOnManifestation <#{self.book_id}>"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DEICHMAN.basedOnManifestation, "#{self.book_id_was}") if self.book_id_was.present?
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::DEICHMAN.basedOnManifestation, "#{self.book_id}")
end
if self.work_id_changed?
- delete_statements << "<#{subject}> dct:subject <#{self.work_id_was}>" if self.work_id_was.present?
- insert_statements << "<#{subject}> dct:subject <#{self.work_id}>"
+ #delete_statements << "<#{subject}> dct:subject <#{self.work_id_was}>" if self.work_id_was.present?
+ #insert_statements << "<#{subject}> dct:subject <#{self.work_id}>"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.subject, "#{self.work_id_was}") if self.work_id_was.present?
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.subject, "#{self.work_id}")
end
if self.reviewer_id_changed?
- delete_statements << "<#{subject}> rev:reviewer <#{self.reviewer_id_was}>" if self.reviewer_id_was.present?
- insert_statements << "<#{subject}> rev:reviewer <#{self.reviewer_id}>"
+ #delete_statements << "<#{subject}> rev:reviewer <#{self.reviewer_id_was}>" if self.reviewer_id_was.present?
+ #insert_statements << "<#{subject}> rev:reviewer <#{self.reviewer_id}>"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::REV.reviewer, "#{self.reviewer_id_was}") if self.reviewer_id_was.present?
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::REV.reviewer, "#{self.reviewer_id}")
end
if self.publisher_id_changed?
- delete_statements << "<#{subject}> dct:publisher <#{self.publisher_id_was}>" if self.publisher_id_was.present?
- insert_statements << "<#{subject}> dct:publisher <#{self.publisher_id}>"
+ #delete_statements << "<#{subject}> dct:publisher <#{self.publisher_id_was}>" if self.publisher_id_was.present?
+ #insert_statements << "<#{subject}> dct:publisher <#{self.publisher_id}>"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.publisher, "#{self.publisher_id_was}") if self.publisher_id_was.present?
+ insert_statements << RDF::Statement.new(RDF::URI(subject), DC.publisher, "#{self.publisher_id}")
end
if self.published_changed?
if @attributes["published"] == "true"
- delete_statements << "<#{subject}> dct:issued \"#{self.issued}\"^^<http://www.w3.org/2001/XMLSchema#dateTime>"
- insert_statements << "<#{subject}> dct:issued \"#{Time.now.xmlschema}\"^^<http://www.w3.org/2001/XMLSchema#dateTime>"
+ #delete_statements << "<#{subject}> dct:issued \"#{self.issued}\"^^<http://www.w3.org/2001/XMLSchema#dateTime>"
+ #insert_statements << "<#{subject}> dct:issued \"#{Time.now.xmlschema}\"^^<http://www.w3.org/2001/XMLSchema#dateTime>"
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.issued, RDF::Literal(self.issued, :datatype => RDF::XSD.dateTime))
+ insert_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.issued, RDF::Literal(self.Time.now.xmlschema, :datatype => RDF::XSD.dateTime))
else
- delete_statements << "<#{subject}> dct:issued \"#{self.issued}\"^^<http://www.w3.org/2001/XMLSchema#dateTime>" if self.issued.present?
+ #delete_statements << "<#{subject}> dct:issued \"#{self.issued}\"^^<http://www.w3.org/2001/XMLSchema#dateTime>" if self.issued.present?
+ delete_statements << RDF::Statement.new(RDF::URI(subject), RDF::DC.issued, RDF::Literal(self.issued, :datatype => RDF::XSD.dateTime)) if self.issued.present?
end
end
- query << "\nDELETE { " << delete_statements.join(' . ') << " }"
-
- query << "\nINSERT { " << insert_statements.join(' . ') << " }"
+ deletequery = QUERY.delete_data(delete_statements).graph(klass.graph)
+ insertquery = QUERY.insert_data(insert_statements).graph(klass.graph)
if Rails.env.development?
puts "From update: \n#{query}"
end
- CLIENT.update(query)
+ #REPOSITORY.update(query)
+ REPOSITORY.delete_data(deletequery)
+ REPOSITORY.insert_data(insertquery)
@changed_attributes.clear
number_of_changed_attributes
@@ -422,7 +512,7 @@ def create
klass = self.class
id = guid
subject = subject_for(id)
-
+=begin
query = <<-q
#{klass.prefixes}
insert into <#{klass.graph}> {
@@ -433,49 +523,67 @@ def create
rev:text """#{self.text}"""@no ;
deich:basedOnManifestation <#{self.book_id}> .
q
-
+=end
+ subject = RDF::URI(subject)
+ insert_statements = []
+ insert_statements << RDF::Statement.new(subject, RDF.type, klass.type)
+ insert_statements << RDF::Statement.new(subject, RDF::REV.title, "#{self.title}")
+ insert_statements << RDF::Statement.new(subject, RDF::DC.abstract, RDF::Literal(self.abstract, :language => @no))
+ insert_statements << RDF::Statement.new(subject, RDF::DC.created, RDF::Literal(Time.now.xmlschema, :datatype => RDF::XSD.dateTime))
+ insert_statements << RDF::Statement.new(subject, RDF::REV.text, RDF::Literal(self.text, :language => @no))
+ insert_statements << RDF::Statement.new(subject, RDF::DEICHMAN.basedOnManifestation, RDF::URI(self.book_id))
+
if self.audience_id.present?
- query << "<#{subject}> dct:audience <#{self.audience_id}> . "
+ #query << "<#{subject}> dct:audience <#{self.audience_id}> . "
+ insert_statements << RDF::Statement.new(subject, RDF::DC.audience, RDF::URI(self.audience_id))
end
if self.publisher_id.present?
- query << "<#{subject}> dct:publisher <#{self.publisher_id}> . "
+ #query << "<#{subject}> dct:publisher <#{self.publisher_id}> . "
+ insert_statements << RDF::Statement.new(subject, RDF::DC.publisher, RDF::URI(self.publisher_id))
end
if self.source_id.present?
- query << "<#{subject}> dct:source <#{self.source_id}> . "
+ #query << "<#{subject}> dct:source <#{self.source_id}> . "
+ insert_statements << RDF::Statement.new(subject, RDF::DC.source, RDF::URI(self.source_id))
end
if self.work_id.present?
- query << "<#{subject}> dct:subject <#{self.work_id}> . "
+ #query << "<#{subject}> dct:subject <#{self.work_id}> . "
+ insert_statements << RDF::Statement.new(subject, RDF::DC.subject, RDF::URI(self.work_id))
end
if self.reviewer_id.present?
- query << "<#{subject}> rev:reviewer <#{self.reviewer_id}> . "
+ #query << "<#{subject}> rev:reviewer <#{self.reviewer_id}> . "
+ insert_statements << RDF::Statement.new(subject, RDF::REV.reviewer, RDF::URI(self.reviewer_id))
end
if self.issued.present?
- query << "<#{subject}> dct:issued \"#{self.issued}\"^^<http://www.w3.org/2001/XMLSchema#dateTime> . "
+ #query << "<#{subject}> dct:issued \"#{self.issued}\"^^<http://www.w3.org/2001/XMLSchema#dateTime> . "
+ insert_statements << RDF::Statement.new(subject, RDF::DC.issued, RDF::Literal(self.issued, :datatype => RDF::XSD.dateTime))
end
- query << " } "
+ query = QUERY.insert_data(insert_statements).graph(self.graph)
## TODO insert into books graph add triple to work
- if Rails.env.development?
- puts "From create: \n#{query}"
- end
-
- CLIENT.insert(query)
+# if Rails.env.development?
+# puts "From create: \n#{query}"
+# end
+ Rails.logger.warn query
+
+ REPOSITORY.insert_data(query)
self.subject = subject
self.id = id
-
+=begin
query = ""
query << "PREFIX rev: <http://purl.org/stuff/rev#>\n"
query << "INSERT INTO GRAPH <#{Book.graph}> { <#{self.work_id}> rev:hasReview <#{subject}> }"
-
- CLIENT.insert(query)
+=end
+ query = QUERY.insert_data([RDF::URI(self.work_id), RDF::REV.hasReview, RDF::URI(subject)]).graph(Book.graph)
+
+ REPOSITORY.insert_data(query)
@changed_attributes.clear
id
View
87 app/models/review_repository.rb
@@ -1,4 +1,5 @@
require 'rest_client'
+require 'rdf/virtuoso'
require 'nokogiri'
class ReviewRepository
@@ -8,7 +9,10 @@ def self.query(query_id=nil)
return results if query_id.nil?
id = query_id.to_sym
return results unless self.respond_to?(id)
- response = ENDPOINT_QUERY.query(self.send(id))
+
+ Rails.logger.warn self.send(id)
+
+ response = REPOSITORY.select(self.send(id))
return results if response.empty?
response.each do |solution|
@@ -23,18 +27,19 @@ def self.query(query_id=nil)
def self.prefixes
<<-eos
-PREFIX foaf: <http://xmlns.com/foaf/0.1/>
-PREFIX bibo: <http://purl.org/ontology/bibo/>
-PREFIX dc: <http://purl.org/dc/terms/>
-PREFIX fabio: <http://purl.org/spar/fabio/>
-PREFIX rev: <http://purl.org/stuff/rev#>
-PREFIX format: <http://data.deichman.no/format/>
-PREFIX dbo: <http://dbpedia.org/ontology/>
-PREFIX xfoaf: <http://www.foafrealm.org/xfoaf/0.1/>
+PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+PREFIX bibo: <http://purl.org/ontology/bibo/>
+PREFIX dc: <http://purl.org/dc/terms/>
+PREFIX fabio: <http://purl.org/spar/fabio/>
+PREFIX rev: <http://purl.org/stuff/rev#>
+PREFIX format: <http://data.deichman.no/format/>
+PREFIX dbo: <http://dbpedia.org/ontology/>
+PREFIX xfoaf: <http://www.foafrealm.org/xfoaf/0.1/>
eos
end
def self.selects
+=begin
<<-eos
SELECT DISTINCT
(sql:sample(?book_title)) as ?book_title
@@ -45,11 +50,13 @@ def self.selects
?text
?abstract
eos
+=end
+ query = QUERY.select(:review, :title, :text, :abstract).distinct.sample(:book_title).group_digest(:book_author, ',', 1000, 1).sample(:any_image).sample(:same_language_image).sample(:img)
end
# Books for adults
def self.query_1
-
+=begin
query = <<-eos
#{prefixes}
@@ -75,11 +82,37 @@ def self.query_1
}
} ORDER BY DESC(?date) LIMIT 10
eos
+=end
+
+ bookgraph = {:context => RDF::URI("http://data.deichman.no/books")}
+ reviewgraph = {:context => RDF::URI("http://data.deichman.no/reviews")}
+
+ query = self.selects
+ query.where(
+ [:book, RDF::DC.format, RDF::FORMAT.Book, bookgraph],
+ [:book, RDF::DC.audience, RDF::URI('http://data.deichman.no/audience/adult')],
+ [:book, RDF::DC.title, :book_title],
+ [:work, RDF::FABIO.hasManifestation, :book]
+ )
+ query.optional([:book, RDF::FOAF.depiction, :img, bookgraph])
+ query.optional([:work, RDF::DC.creator, :creator, bookgraph])
+ query.optional([:creator, RDF::FOAF.name, :book_author, bookgraph])
+ query.where(
+ [:review, RDF::DC.subject, :work, reviewgraph],
+ [:review, RDF::DC.issued, :date, reviewgraph],
+ [:review, RDF::REV.title, :title, reviewgraph])
+ query.optional([:review, RDF::REV.text, :text, reviewgraph])
+ query.optional([:review, RDF::DC.abstract, :abstract, reviewgraph])
+ query.filter('lang(?text) != "nn"')
+ query.order('DESC(?date)')
+ query.limit(10)
+ #query.optimize
+
end
# Books for juveniles
def self.query_2
-
+=begin
query = <<-eos
#{prefixes}
@@ -105,6 +138,38 @@ def self.query_2
}
} ORDER BY DESC(?date) LIMIT 10
eos
+=end
+
+ bookgraph = {:context => RDF::URI("http://data.deichman.no/books")}
+ reviewgraph = {:context => RDF::URI("http://data.deichman.no/reviews")}
+
+ query = self.selects
+ query.where(
+ [:book, RDF::DC.format, RDF::FORMAT.Book, bookgraph],
+ [:book, RDF::DC.audience, RDF::URI('http://data.deichman.no/audience/juvenile'), bookgraph],
+ [:book, RDF::DC.title, :book_title, bookgraph],
+ [:book, RDF::DC.language, :lang, bookgraph],
+ [:work, RDF::FABIO.hasManifestation, :book, bookgraph]
+ )
+ query.where(
+ [:review, RDF::DC.issued, :date, reviewgraph],
+ [:review, RDF::REV.title, :title, reviewgraph],
+ [:review, RDF::DEICHMAN.basedOnManifestation, :book, reviewgraph]
+ )
+ query.optional([:book, RDF::FOAF.depiction, :img, bookgraph])
+ query.optional([:work, RDF::FABIO.hasManifestation, :book, bookgraph],
+ [:book, RDF::DC.language, :lang, bookgraph],
+ [:book, RDF::FOAF.depiction, :same_language_image, bookgraph])
+ query.optional([:work, RDF::FABIO.hasManifestation, :book, bookgraph],
+ [:book, RDF::FOAF.depiction, :any_image, bookgraph])
+ query.optional([:work, RDF::DC.creator, :creator, bookgraph],
+ [:creator, RDF::FOAF.name, :book_author, bookgraph])
+ query.optional([:review, RDF::REV.text, :text, reviewgraph])
+ query.optional([:review, RDF::DC.abstract, :abstract, reviewgraph])
+ query.filter('lang(?text) != "nn"')
+ query.order('DESC(?date)')
+ query.limit(10)
+
end
# Fantasy books for juveniles
View
76 app/models/source.rb
@@ -37,7 +37,8 @@ def all(*args)
options = args.dup
options = options.last.is_a?(Hash) ? options.last : {}
-
+
+=begin
query = <<-q
#{self.prefixes}
SELECT ?subject ?label ?description ?url
@@ -49,9 +50,14 @@ def all(*args)
OPTIONAL { ?subject foaf:homepage ?url }
}}
q
+=end
+ query = QUERY.select(:subject, :label, :description, :url).where([:subject, RDF.type, self.type],[:subject, RDF::RDFS.label, :label]).optional(
+ [:subject, RDF::DC.description, :description]).optional(
+ [:subject, RDF::FOAF.homepage, :url]).from(self.graph)
if limit = options[:limit]
- query << "LIMIT %d" % limit
+ query.limit(limit)
+ #query << "LIMIT %d" % limit
end
if Rails.env.development?
@@ -62,6 +68,7 @@ def all(*args)
end
def find_by_subject(subject, conditions = {})
+=begin
query = <<-q
#{self.prefixes}
SELECT ?label ?description ?url
@@ -73,6 +80,10 @@ def find_by_subject(subject, conditions = {})
OPTIONAL { <#{subject}> foaf:homepage ?url }
}}
q
+=end
+ query = QUERY.select(:label, :description, :url).where([:subject, RDF.type, self.type],[:subject, RDF::RDFS.label, :label]).optional(
+ [RDF::URI("#{subject}"), RDF::DC.description, :description]).optional(
+ [RDF::URI("#{subject}"), RDF::FOAF.homepage, :url]).from(self.graph)
if Rails.env.development?
puts "From find: \n#{query}"
@@ -129,39 +140,51 @@ def update(attribute_names = @attributes.keys)
klass = self.class
subject = subject_for(self.id)
-
+=begin
query = <<-q
#{klass.prefixes}
MODIFY GRAPH <#{klass.graph}>
DELETE {
q
-
+
query << "<#{subject}> rdfs:label \"#{self.label_was}\" . " if self.label_changed?
query << "<#{subject}> dct:description \"#{self.description_was}\" . " if self.description_changed?
-
+=end
+
+ deletes = []
+ deletes << RDF::Statement.new(RDF::URI(subject), RDF::RDFS.label, self.label_was) if self.label_changed?
+ deletes << RDF::Statement.new(RDF::URI(subject), RDF::DC.description, self.description_was) if self.description_changed?
+
unless self.url_was.blank?
- query << "<#{subject}> foaf:homepage <#{self.url_was}> . "
+ deletes << RDF::Statement.new(RDF::URI(subject), RDF::FOAF.homepage, self.url_was)
end
- query << "}\n"
-
+
+=begin
query << <<-q
INSERT {
q
+=end
- query << "<#{subject}> rdfs:label \"#{self.label}\" . " if self.label_changed?
- query << "<#{subject}> dct:description \"#{self.description}\" . " if self.description_changed?
-
+ inserts = []
+ inserts << RDF::Statement.new(RDF::URI(subject), RDF::RDFS.label, self.label) if self.label_changed?
+ inserts << RDF::Statement.new(RDF::URI(subject), RDF::DC.description, self.description) if self.description_changed?
+
unless self.url.blank?
- query << "<#{subject}> foaf:homepage <#{self.url}> . "
+ inserts << RDF::Statement.new(RDF::URI(subject), RDF::FOAF.homepage, self.url)
end
- query << "}"
- if Rails.env.development?
- puts "From update: \n#{query}"
- end
-
- CLIENT.update(query)
+
+ deletequery = QUERY.delete_data(deletes).graph(klass.graph)
+ insertquery = QUERY.insert_data(inserts).graph(klass.graph)
+
+ Rails.logger.warn deletequery
+ Rails.logger.warn insertquery
+
+ #REPOSITORY.update(query)
+ REPOSITORY.delete_data(deletequery)
+ REPOSITORY.insert_data(insertquery)
+
@changed_attributes.clear
number_of_changed_attributes
end
@@ -172,20 +195,27 @@ def create
klass = self.class
id = guid
subject = subject_for(id)
-
+=begin
query = <<-q
#{klass.prefixes}
insert into <#{klass.graph}> {
<#{subject}> a <#{klass.type}>;
rdfs:label "#{self.label}" ;
dct:description "#{self.description}"
q
+=end
+ inserts = []
+ inserts << RDF::Statement.new(RDF::URI(subject), RDF.type, klass.type)
+ inserts << RDF::Statement.new(RDF::URI(subject), RDF::RDFS.label, self.label)
+ inserts << RDF::Statement.new(RDF::URI(subject), RDF::DC.description, self.description)
+
unless self.url.blank?
- query << " . <#{subject}> foaf:homepage <#{self.url}>"
+ inserts << RDF::Statement.new(RDF::URI(subject), RDF::FOAF.homepage, self.url)
end
- query << "}"
-
- CLIENT.insert(query)
+
+ insertquery = QUERY.insert_data(inserts).graph(klass.graph)
+
+ REPOSITORY.insert_data(insertquery)
self.subject = subject
self.id = id
@changed_attributes.clear
View
2 app/models/user.rb
@@ -66,7 +66,7 @@ def ensure_person
person_id = Person.create!(name: self.fullname).subject
self.person_id = person_id
else
- self.person.name = self.fullname
+ self.person.name = self.fullname # NB! this crashes initial user modification
self.person.save
end
end