Commit d0fa2ba717085edd10219e2baa5b03e773ab26f2

Authored by Francisco Júnior
2 parents 9508e848 8ae986e8

Merge branch 'virtuoso_integration' of gitlab.com:participa/noosfero into virtuoso_integration

plugins/virtuoso/Gemfile
1 gem 'rdf' 1 gem 'rdf'
2 gem 'rdf-virtuoso' 2 gem 'rdf-virtuoso'
3 gem 'oai' 3 gem 'oai'
  4 +gem 'liquid'
plugins/virtuoso/controllers/virtuoso_plugin_admin_controller.rb
@@ -8,7 +8,7 @@ class VirtuosoPluginAdminController < AdminController @@ -8,7 +8,7 @@ class VirtuosoPluginAdminController < AdminController
8 8
9 if request.post? 9 if request.post?
10 @settings.save! 10 @settings.save!
11 - session[:notice] = 'Settings succefully saved.' 11 + session[:notice] = 'Settings successfully saved.'
12 redirect_to :action => 'index' 12 redirect_to :action => 'index'
13 end 13 end
14 end 14 end
plugins/virtuoso/lib/ext/literal.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +class RDF::Literal
  2 +
  3 + def to_liquid
  4 + value
  5 + end
  6 +
  7 +end
plugins/virtuoso/lib/ext/solution.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +class RDF::Query::Solution
  2 +
  3 + def to_liquid
  4 + HashWithIndifferentAccess.new(to_hash)
  5 + end
  6 +
  7 +end
plugins/virtuoso/lib/virtuoso_plugin.rb
@@ -20,4 +20,8 @@ class VirtuosoPlugin < Noosfero::Plugin @@ -20,4 +20,8 @@ class VirtuosoPlugin < Noosfero::Plugin
20 @virtuoso_client ||= RDF::Virtuoso::Repository.new("#{settings.virtuoso_uri}/sparql", :update_uri => "#{settings.virtuoso_uri}/sparql-auth", :username => settings.virtuoso_username, :password => settings.virtuoso_password, :auth_method => 'digest', :timeout => 30) 20 @virtuoso_client ||= RDF::Virtuoso::Repository.new("#{settings.virtuoso_uri}/sparql", :update_uri => "#{settings.virtuoso_uri}/sparql-auth", :username => settings.virtuoso_username, :password => settings.virtuoso_password, :auth_method => 'digest', :timeout => 30)
21 end 21 end
22 22
  23 + def stylesheet?
  24 + true
  25 + end
  26 +
23 end 27 end
plugins/virtuoso/lib/virtuoso_plugin/dspace_harvest.rb
@@ -27,7 +27,7 @@ class VirtuosoPlugin::DspaceHarvest @@ -27,7 +27,7 @@ class VirtuosoPlugin::DspaceHarvest
27 values = [metadata.send(c)].flatten.compact 27 values = [metadata.send(c)].flatten.compact
28 values.each do |value| 28 values.each do |value|
29 query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(metadata.identifier), RDF::URI.new("http://purl.org/dc/elements/1.1/#{c}"), value]).graph(RDF::URI.new(settings.dspace_uri)) 29 query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(metadata.identifier), RDF::URI.new("http://purl.org/dc/elements/1.1/#{c}"), value]).graph(RDF::URI.new(settings.dspace_uri))
30 - virtuoso_client.insert(query) 30 + plugin.virtuoso_client.insert(query)
31 end 31 end
32 end 32 end
33 end 33 end
plugins/virtuoso/lib/virtuoso_plugin/dublin_core_metadata.rb
@@ -9,7 +9,7 @@ class VirtuosoPlugin::DublinCoreMetadata @@ -9,7 +9,7 @@ class VirtuosoPlugin::DublinCoreMetadata
9 @creator = xpath(element, './/dc:creator') 9 @creator = xpath(element, './/dc:creator')
10 @subject = xpath_all(element, './/dc:subject').map(&:text) 10 @subject = xpath_all(element, './/dc:subject').map(&:text)
11 @description = xpath(element, './/dc:description') 11 @description = xpath(element, './/dc:description')
12 - @date = xpath_all(element, './/dc:date').map(&:text) 12 + @date = xpath(element, './/dc:date')
13 @type = xpath(element, './/dc:type') 13 @type = xpath(element, './/dc:type')
14 @identifier = xpath(element, './/dc:identifier') 14 @identifier = xpath(element, './/dc:identifier')
15 @language = xpath(element, './/dc:language') 15 @language = xpath(element, './/dc:language')
plugins/virtuoso/lib/virtuoso_plugin/triples_template.rb
@@ -25,9 +25,14 @@ class VirtuosoPlugin::TriplesTemplate < Article @@ -25,9 +25,14 @@ class VirtuosoPlugin::TriplesTemplate < Article
25 end 25 end
26 26
27 def template_content 27 def template_content
28 - plugin.virtuoso_client.query(query).map do |r|  
29 - template % r  
30 - end.join 28 + begin
  29 + results = plugin.virtuoso_client.query(query)
  30 + liquid_template = Liquid::Template.parse("{% for row in results %}#{template}{% endfor %}")
  31 + liquid_template.render('results' => results)
  32 + rescue => ex
  33 + logger.info ex.to_s
  34 + "Failed to process the template"
  35 + end
31 end 36 end
32 37
33 end 38 end
plugins/virtuoso/public/style.css 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +.virtuoso-triples-template .template {
  2 + margin: 10px 0;
  3 +}
plugins/virtuoso/test/unit/triples_template_test.rb 0 → 100644
@@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class TriplesTemplateTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @article = VirtuosoPlugin::TriplesTemplate.new
  7 + end
  8 +
  9 + attr_reader :article
  10 +
  11 + should 'evaluate template using query results' do
  12 + article.stubs(:plugin).returns(mock)
  13 + article.plugin.expects(:virtuoso_client).at_least_once.returns(mock)
  14 + article.plugin.virtuoso_client.expects(:query).returns([{'var' => 'Hello '}, {'var' => 'World'}])
  15 + article.template = "{{row.var}}"
  16 +
  17 + assert_equal 'Hello World', article.template_content
  18 + end
  19 +
  20 + should 'display error message when failed to execute the query' do
  21 + article.stubs(:plugin).returns(mock)
  22 + article.plugin.expects(:virtuoso_client).at_least_once.returns(mock)
  23 + article.plugin.virtuoso_client.expects(:query).raises(RuntimeError.new)
  24 + article.template = "{{row.var}}"
  25 +
  26 + assert_equal "Failed to process the template", article.template_content
  27 + end
  28 +
  29 +end
plugins/virtuoso/views/cms/virtuoso_plugin/_triples_template.html.erb
1 -<%= required_fields_message %> 1 +<div class="virtuoso-triples-template">
  2 + <%= required_fields_message %>
2 3
3 -<%= render :file => 'shared/tiny_mce' %> 4 + <%= render :file => 'shared/tiny_mce' %>
4 5
5 -<%= required labelled_form_field(_('Title'), text_field(:article, 'name', :size => '64', :maxlength => 150)) %>  
6 -<%= labelled_form_field(_('SPARQL Query'), text_area(:article, :query, :style => 'width: 98%; height: 120px;')) %>  
7 -<%= labelled_form_field(_('Template'), text_area(:article, :template, :style => 'width: 98%; height: 200px;', :class => 'mceEditor')) %> 6 + <%= required labelled_form_field(_('Title'), text_field(:article, 'name', :size => '64', :maxlength => 150)) %>
  7 + <%= labelled_form_field(_('SPARQL Query'), text_area(:article, :query, :style => 'width: 98%; height: 120px;')) %>
8 8
9 -<%= render :partial => 'general_fields' %>  
10 -<%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true} %> 9 + <div class="template">
  10 + <span class="label"><%= _('Template') %></span>
  11 + <span class="reference" style="float: right;"><a href="https://github.com/Shopify/liquid/wiki/Liquid-for-Designers"><%= _('Template reference') %></a></span>
  12 + <span class="input">
  13 + <%= text_area(:article, :template, :style => 'width: 98%; height: 200px;', :class => 'mceEditor') %>
  14 + </span>
  15 + </div>
  16 +
  17 + <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true} %>
  18 + <%= render :partial => 'general_fields' %>
  19 +</div>