Commit 45d5481fefafb4c6560749c157b551fcbda505e3
Exists in
staging
and in
4 other branches
Merge branch 'virtuoso_integration' of gitlab.com:participa/noosfero into virtuoso_integration
Showing
9 changed files
with
190 additions
and
11 deletions
Show diff stats
plugins/virtuoso/controllers/virtuoso_plugin_admin_controller.rb
@@ -20,4 +20,31 @@ class VirtuosoPluginAdminController < AdminController | @@ -20,4 +20,31 @@ class VirtuosoPluginAdminController < AdminController | ||
20 | redirect_to :action => :index | 20 | redirect_to :action => :index |
21 | end | 21 | end |
22 | 22 | ||
23 | + def triple_management | ||
24 | + triples_management = VirtuosoPlugin::TriplesManagement.new(environment) | ||
25 | + @triples = [] | ||
26 | + if request.post? | ||
27 | + @query = params[:query] | ||
28 | + @graph_uri = params[:graph_uri] | ||
29 | + @triples = triples_management.search_triples(@graph_uri, @query) | ||
30 | + end | ||
31 | + render :action => 'triple_management' | ||
32 | + end | ||
33 | + | ||
34 | + def triple_update | ||
35 | + graph_uri = params[:graph_uri] | ||
36 | + triples = params[:triples] | ||
37 | + | ||
38 | + triples_management = VirtuosoPlugin::TriplesManagement.new(environment) | ||
39 | + | ||
40 | + triples.each { |triple| | ||
41 | + from_triple = triple[:from] | ||
42 | + to_triple = triple[:to] | ||
43 | + triples_management.update_triple(graph_uri, from_triple, to_triple) | ||
44 | + } | ||
45 | + | ||
46 | + session[:notice] = _('Triple(s) succesfully updated.') | ||
47 | + redirect_to :action => :triple_management | ||
48 | + end | ||
49 | + | ||
23 | end | 50 | end |
plugins/virtuoso/lib/virtuoso_plugin.rb
plugins/virtuoso/lib/virtuoso_plugin/triples_management.rb
0 → 100644
@@ -0,0 +1,32 @@ | @@ -0,0 +1,32 @@ | ||
1 | +class VirtuosoPlugin::TriplesManagement | ||
2 | + | ||
3 | + def initialize(environment) | ||
4 | + @environment = environment | ||
5 | + end | ||
6 | + | ||
7 | + attr_reader :environment | ||
8 | + | ||
9 | + def plugin | ||
10 | + @plugin ||= VirtuosoPlugin.new(self) | ||
11 | + end | ||
12 | + | ||
13 | + def search_triples(graph_uri, query_sparql) | ||
14 | + query = "WITH <#{graph_uri}> #{query_sparql}" | ||
15 | + plugin.virtuoso_client.query(query) | ||
16 | + end | ||
17 | + | ||
18 | + def update_triple(graph_uri, from_triple, to_triple) | ||
19 | + from_subject = from_triple[:subject] | ||
20 | + from_predicate = from_triple[:predicate] | ||
21 | + from_object = from_triple[:object] | ||
22 | + | ||
23 | + to_subject = to_triple[:subject] | ||
24 | + to_predicate = to_triple[:predicate] | ||
25 | + to_object = to_triple[:object] | ||
26 | + | ||
27 | + query = "WITH <#{graph_uri}> DELETE { <#{from_subject}> <#{from_predicate}> '#{from_object}' } INSERT { <#{to_subject}> <#{to_predicate}> '#{to_object}' }" | ||
28 | + | ||
29 | + plugin.virtuoso_client.query(query) | ||
30 | + end | ||
31 | + | ||
32 | +end |
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 | - results = plugin.virtuoso_client.query(query) | ||
29 | - liquid_template = Liquid::Template.parse("{% for row in results %}#{template}{% endfor %}") | ||
30 | - liquid_template.render('results' => results) | 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 |
@@ -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 => 'shared/lead_and_body', :locals => {:tiny_mce => true} %> | ||
10 | -<%= render :partial => 'general_fields' %> | 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> |
plugins/virtuoso/views/virtuoso_plugin_admin/index.html.erb
@@ -25,7 +25,6 @@ | @@ -25,7 +25,6 @@ | ||
25 | <span class="value"><%= time_ago_as_sentence @settings.last_harvest %></span> | 25 | <span class="value"><%= time_ago_as_sentence @settings.last_harvest %></span> |
26 | </div> | 26 | </div> |
27 | <% end %> | 27 | <% end %> |
28 | - <br/> | ||
29 | <div class="actions"> | 28 | <div class="actions"> |
30 | <% if @harvest_running %> | 29 | <% if @harvest_running %> |
31 | <%= _('Running...') %> | 30 | <%= _('Running...') %> |
@@ -35,3 +34,11 @@ | @@ -35,3 +34,11 @@ | ||
35 | <% end %> | 34 | <% end %> |
36 | </div> | 35 | </div> |
37 | </div> | 36 | </div> |
37 | + | ||
38 | +<hr /> | ||
39 | + | ||
40 | +<div class="triple-management"> | ||
41 | + <div class="actions"> | ||
42 | + <%= button :edit, _('Triple management'), :action => :triple_management %> | ||
43 | + </div> | ||
44 | +</div> |
plugins/virtuoso/views/virtuoso_plugin_admin/triple_management.html.erb
0 → 100644
@@ -0,0 +1,63 @@ | @@ -0,0 +1,63 @@ | ||
1 | +<h1><%= _('Virtuoso settings : Triples Management')%></h1> | ||
2 | + | ||
3 | +<%= form_tag('/admin/plugin/virtuoso/triple_management', :method => 'post') do %> | ||
4 | + | ||
5 | + <%= labelled_form_field(_('Default Graph IRI:'), text_field_tag(:graph_uri, @graph_uri, :style => 'width: 100%' ) ) %> | ||
6 | + | ||
7 | + <%= labelled_form_field(_('Query SPARQL:'), text_area_tag(:query, @query, { :rows => 7, :style => 'width: 99%' } )) %> | ||
8 | + | ||
9 | + <% button_bar do %> | ||
10 | + <%= submit_button(:search, _('Search')) %> | ||
11 | + <% end %> | ||
12 | + | ||
13 | +<% end %> | ||
14 | + | ||
15 | +<style> | ||
16 | +.triple + .triple { | ||
17 | + border-top: 1px solid #CCC; | ||
18 | + margin-top: 20px; | ||
19 | + padding-top: 15px; | ||
20 | +} | ||
21 | +</style> | ||
22 | + | ||
23 | +<% unless @triples.empty? %> | ||
24 | + | ||
25 | + <hr /> | ||
26 | + | ||
27 | + <%= form_tag('/admin/plugin/virtuoso/triple_update', :method => 'post') do %> | ||
28 | + | ||
29 | + <%= hidden_field_tag(:graph_uri, @graph_uri) %> | ||
30 | + | ||
31 | + <% @triples.each { |triple| %> | ||
32 | + | ||
33 | + <div class="triple"> | ||
34 | + | ||
35 | + <div class="triple-subject" style="margin: 3px 0 3px 0;"> | ||
36 | + <label class="formlabel"><%= _('Subject:') %></label> | ||
37 | + <%= hidden_field_tag('triples[][from[subject]]', triple[:s].to_s) %> | ||
38 | + <%= text_field_tag('triples[][to[subject]]', triple[:s].to_s, :style => 'width: 99%;') %> | ||
39 | + </div> | ||
40 | + | ||
41 | + <div class="triple-predicate" style="margin: 3px 0 3px 0;"> | ||
42 | + <label class="formlabel"><%= _('Predicate:') %></label> | ||
43 | + <%= hidden_field_tag('triples[][from[predicate]]', triple[:p].to_s) %> | ||
44 | + <%= text_field_tag('triples[][to[predicate]]', triple[:p].to_s, :style => 'width: 99%;') %> | ||
45 | + </div> | ||
46 | + | ||
47 | + <div class="triple-object" style="margin: 3px 0 3px 0;"> | ||
48 | + <label class="formlabel"><%= _('Object:') %></label> | ||
49 | + <%= hidden_field_tag('triples[][from[object]]', triple[:o].to_s) %> | ||
50 | + <%= text_field_tag('triples[][to[object]]', triple[:o].to_s, :style => 'width: 99%;') %> | ||
51 | + </div> | ||
52 | + | ||
53 | + </div> | ||
54 | + | ||
55 | + <% } %> | ||
56 | + | ||
57 | + <% button_bar do %> | ||
58 | + <%= submit_button(:save, _('Save')) %> | ||
59 | + <% end %> | ||
60 | + | ||
61 | + <% end %> | ||
62 | + | ||
63 | +<% end %> |