Commit 9ab7908b0f7632e0f874ea9cff11069822afac03

Authored by Victor Costa
2 parents 35593276 94eba99c

Merge branch 'virtuoso_integration' into stable

plugins/virtuoso/controllers/virtuoso_plugin_admin_controller.rb
... ... @@ -32,18 +32,25 @@ class VirtuosoPluginAdminController < AdminController
32 32 render :action => 'triple_management'
33 33 end
34 34  
35   - def triple_update
36   - graph_uri = params[:graph_uri]
37   - triples = params[:triples]
  35 + def update_triple
  36 + if request.post?
  37 + from_triple = VirtuosoPlugin::Triple.new
  38 + from_triple.graph = params[:from_triple][:graph]
  39 + from_triple.subject = params[:from_triple][:subject]
  40 + from_triple.predicate = params[:from_triple][:predicate]
  41 + from_triple.object = params[:from_triple][:object]
38 42  
39   - triples_management = VirtuosoPlugin::TriplesManagement.new(environment)
  43 + to_triple = VirtuosoPlugin::Triple.new
  44 + to_triple.graph = params[:to_triple][:graph]
  45 + to_triple.subject = params[:to_triple][:subject]
  46 + to_triple.predicate = params[:to_triple][:predicate]
  47 + to_triple.object = params[:to_triple][:object]
40 48  
41   - triples.each { |triple_key, triple_content|
42   - triples_management.update_triple(graph_uri, triple_content[:from], triple_content[:to])
43   - }
  49 + triples_management = VirtuosoPlugin::TriplesManagement.new(environment)
  50 + triples_management.update_triple(from_triple, to_triple)
44 51  
45   - session[:notice] = _('Triple(s) succesfully updated.')
46   - redirect_to :action => :triple_management
  52 + render json: { :ok => true, :message => _('Triple succesfully updated.') }
  53 + end
47 54 end
48 55  
49 56 def add_triple
... ... @@ -63,16 +70,18 @@ class VirtuosoPluginAdminController < AdminController
63 70 end
64 71  
65 72 def remove_triple
66   - triple = VirtuosoPlugin::Triple.new
67   - triple.graph = params[:triple][:graph]
68   - triple.subject = params[:triple][:subject]
69   - triple.predicate = params[:triple][:predicate]
70   - triple.object = params[:triple][:object]
  73 + if request.post?
  74 + triple = VirtuosoPlugin::Triple.new
  75 + triple.graph = params[:triple][:graph]
  76 + triple.subject = params[:triple][:subject]
  77 + triple.predicate = params[:triple][:predicate]
  78 + triple.object = params[:triple][:object]
71 79  
72   - triples_management = VirtuosoPlugin::TriplesManagement.new(environment)
73   - triples_management.remove_triple(triple)
  80 + triples_management = VirtuosoPlugin::TriplesManagement.new(environment)
  81 + triples_management.remove_triple(triple)
74 82  
75   - render json: { :ok => true, :message => _('Triple succesfully removed.') }
  83 + render json: { :ok => true, :message => _('Triple succesfully removed.') }
  84 + end
76 85 end
77 86  
78 87 end
... ...
plugins/virtuoso/lib/ext/uri.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +class RDF::URI
  2 +
  3 + def to_liquid
  4 + value
  5 + end
  6 +
  7 +end
... ...
plugins/virtuoso/lib/virtuoso_plugin/dspace_harvest.rb
... ... @@ -55,10 +55,12 @@ class VirtuosoPlugin::DspaceHarvest
55 55  
56 56 def self.harvest_all(environment, from_start)
57 57 settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin)
58   - settings.dspace_servers.each do |k, v|
59   - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, k[:dspace_uri])
60   - harvest.start(from_start)
61   - end
  58 + if defined?(settings.dspace_servers)
  59 + settings.dspace_servers.each do |k, v|
  60 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, k[:dspace_uri])
  61 + harvest.start(from_start)
  62 + end
  63 + end
62 64 end
63 65  
64 66 def start(from_start = false)
... ...
plugins/virtuoso/lib/virtuoso_plugin/triples_management.rb
... ... @@ -15,16 +15,8 @@ class VirtuosoPlugin::TriplesManagement
15 15 plugin.virtuoso_client.query(query)
16 16 end
17 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 = format_triple_term(from_triple[:object])
22   -
23   - to_subject = to_triple[:subject]
24   - to_predicate = to_triple[:predicate]
25   - to_object = format_triple_term(to_triple[:object])
26   -
27   - query = "WITH <#{graph_uri}> DELETE { <#{from_subject}> <#{from_predicate}> #{from_object} } INSERT { <#{to_subject}> <#{to_predicate}> #{to_object} }"
  18 + def update_triple(from_triple, to_triple)
  19 + query = "WITH <#{from_triple.graph}> DELETE { <#{from_triple.subject}> <#{from_triple.predicate}> #{from_triple.object} } INSERT { <#{to_triple.subject}> <#{to_triple.predicate}> #{to_triple.object} }"
28 20  
29 21 plugin.virtuoso_client.query(query)
30 22 end
... ... @@ -39,10 +31,4 @@ class VirtuosoPlugin::TriplesManagement
39 31 plugin.virtuoso_client.query(query)
40 32 end
41 33  
42   - protected
43   -
44   - def format_triple_term(term)
45   - term =~ /^(http|https):\/\// ? "<#{term}>" : "'#{term}'"
46   - end
47   -
48 34 end
... ...
plugins/virtuoso/public/triples_management.js
  1 +function update_triple(triple_id) {
  2 + graph = jQuery("input#graph_uri").val();
  3 +
  4 + from_subject = jQuery("input#triples_triple" + triple_id + "_from_subject").val();
  5 + from_predicate = jQuery("input#triples_triple" + triple_id + "_from_predicate").val();
  6 + from_object = jQuery("input#triples_triple" + triple_id + "_from_object").val();
  7 +
  8 + to_subject = jQuery("input#triples_triple" + triple_id + "_to_subject").val();
  9 + to_predicate = jQuery("input#triples_triple" + triple_id + "_to_predicate").val();
  10 + to_object = jQuery("input#triples_triple" + triple_id + "_to_object").val();
  11 +
  12 + var formData = {
  13 + from_triple: { graph: graph, subject: from_subject, predicate: from_predicate, object: from_object },
  14 + to_triple: { graph: graph, subject: to_subject, predicate: to_predicate, object: to_object }
  15 + }
  16 +
  17 + jQuery.ajax({
  18 + cache: false,
  19 + type: 'POST',
  20 + url: '/admin/plugin/virtuoso/update_triple',
  21 + data: formData,
  22 + dataType: 'json',
  23 + success: function(data, status, ajax){
  24 + if ( !data.ok ) {
  25 + display_notice(data.error.message);
  26 + }
  27 + else {
  28 + display_notice(data.message);
  29 + jQuery("input#triples_triple" + triple_id + "_from_object").val(jQuery("input#triples_triple" + triple_id + "_to_object").val());
  30 + }
  31 + },
  32 + error: function(ajax, status, errorThrown) {
  33 + alert('Send request - HTTP '+status+': '+errorThrown);
  34 + }
  35 + });
  36 +
  37 +}
  38 +
1 39 function add_triple() {
2 40 graph = jQuery("input#triple_graph").val();
3 41 subject = jQuery("input#triple_subject").val();
... ...
plugins/virtuoso/test/functional/virtuoso_plugin_admin_controller_test.rb
... ... @@ -2,38 +2,45 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39;
2 2  
3 3 class VirtuosoPluginAdminControllerTest < ActionController::TestCase
4 4  
  5 + attr_reader :environment
  6 +
5 7 def setup
6 8 @environment = Environment.default
7 9 @profile = create_user('profile').person
8 10 login_as(@profile.identifier)
  11 + post :index, :settings => mock_settings
  12 + end
  13 +
  14 + def mock_settings
  15 + { :virtuoso_uri=>"http://virtuoso.noosfero.com",
  16 + :virtuoso_username=>"username",
  17 + :virtuoso_password=>"password",
  18 + :virtuoso_readonly_username=>"readonly_username",
  19 + :virtuoso_readonly_password=>"readonly_password",
  20 + :dspace_servers=>[
  21 + {"dspace_uri"=>"http://dspace1.noosfero.com"},
  22 + {"dspace_uri"=>"http://dspace2.noosfero.com"},
  23 + {"dspace_uri"=>"http://dspace3.noosfero.com"}
  24 + ]
  25 + }
9 26 end
10 27  
11   - attr_reader :environment
12   -
13 28 should 'save virtuoso plugin settings' do
14   - post :index, :settings =>
15   - {:virtuoso_uri=>"http://virtuoso.noosfero.com",
16   - :virtuoso_username=>"username", :virtuoso_password=>"password",
17   - :virtuoso_readonly_username=>"password",
18   - :virtuoso_readonly_password=>"password",
19   - :dspace_servers=>[
20   - {"dspace_uri"=>"http://dspace1.noosfero.com"},
21   - {"dspace_uri"=>"http://dspace2.noosfero.com"},
22   - {"dspace_uri"=>"http://dspace3.noosfero.com"}
23   - ]
24   - }
25 29 @settings = Noosfero::Plugin::Settings.new(environment.reload, VirtuosoPlugin)
26 30 assert_equal 'http://virtuoso.noosfero.com', @settings.settings[:virtuoso_uri]
27 31 assert_equal 'username', @settings.settings[:virtuoso_username]
28 32 assert_equal 'password', @settings.settings[:virtuoso_password]
  33 + assert_equal 'readonly_username', @settings.settings[:virtuoso_readonly_username]
  34 + assert_equal 'readonly_password', @settings.settings[:virtuoso_readonly_password]
29 35 assert_equal 'http://dspace1.noosfero.com', @settings.settings[:dspace_servers][0][:dspace_uri]
30 36 assert_equal 'http://dspace2.noosfero.com', @settings.settings[:dspace_servers][1][:dspace_uri]
31   - assert_equal 'http://dspace3.noosfero.com', @settings.settings[:dspace_servers][2][:dspace_uri]
  37 + assert_equal 'http://dspace3.noosfero.com', @settings.settings[:dspace_servers][2][:dspace_uri]
32 38 assert_redirected_to :action => 'index'
33 39 end
34 40  
  41 +
35 42 should 'redirect to index after save' do
36   - post :index, :settings => {"virtuoso_uri" => 'http://virtuoso.noosfero.com'}
  43 + post :index, :settings => mock_settings
37 44 assert_redirected_to :action => 'index'
38 45 end
39 46  
... ... @@ -51,4 +58,8 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
51 58 assert_equal nil, harvest.settings.last_harvest
52 59 end
53 60  
54   -end
  61 + should 'force harvest_all from start' do
  62 + get :force_harvest, :from_start => true
  63 + end
  64 +
  65 +end
55 66 \ No newline at end of file
... ...
plugins/virtuoso/test/unit/dspace_harvest_test.rb
... ... @@ -7,6 +7,20 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase
7 7 end
8 8  
9 9 attr_reader :environment
  10 +
  11 + def mock_settings
  12 + { :virtuoso_uri=>"http://virtuoso.noosfero.com",
  13 + :virtuoso_username=>"username",
  14 + :virtuoso_password=>"password",
  15 + :virtuoso_readonly_username=>"readonly_username",
  16 + :virtuoso_readonly_password=>"readonly_password",
  17 + :dspace_servers=>[
  18 + {"dspace_uri"=>"http://dspace1.noosfero.com"},
  19 + {"dspace_uri"=>"http://dspace2.noosfero.com"},
  20 + {"dspace_uri"=>"http://dspace3.noosfero.com"}
  21 + ]
  22 + }
  23 + end
10 24  
11 25 should 'create delayed job when start' do
12 26 harvest = VirtuosoPlugin::DspaceHarvest.new(environment)
... ... @@ -21,5 +35,29 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase
21 35 5.times { harvest.start }
22 36 end
23 37 end
  38 +
  39 + should 'harvest all dspaces from start' do
  40 + VirtuosoPlugin::DspaceHarvest.harvest_all(environment, true)
  41 + end
  42 +
  43 + should 'try to harvest all dspaces from start without any setting' do
  44 + VirtuosoPlugin::DspaceHarvest.harvest_all(environment, true)
  45 + end
  46 +
  47 + should 'try to harvest all dspaces from start with mock configuration' do
  48 + @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
  49 + @settings.save!
  50 + VirtuosoPlugin::DspaceHarvest.harvest_all(environment, true)
  51 + end
  52 +
  53 + should 'try to harvest all dspaces without any setting' do
  54 + VirtuosoPlugin::DspaceHarvest.harvest_all(environment, false)
  55 + end
  56 +
  57 + should 'try to harvest all dspaces with mock configuration' do
  58 + @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
  59 + @settings.save!
  60 + VirtuosoPlugin::DspaceHarvest.harvest_all(environment, false)
  61 + end
24 62  
25 63 end
... ...
plugins/virtuoso/views/virtuoso_plugin_admin/triple_management.html.erb
... ... @@ -36,6 +36,7 @@
36 36 <%= labelled_form_field(_('Object:'), text_field_tag("triples[triple#{triple_counter}[to][object]]", triple[:o].to_s) ) %>
37 37 </li>
38 38 <li class="triple-actions">
  39 + <%= button :save, _('Update triple'), {}, :href => '#', :onclick => "update_triple(#{triple_counter}); return false;" %>
39 40 <%= button :remove, _('Remove triple'), {}, :href => '#', :onclick => "remove_triple(#{triple_counter}); return false;" %>
40 41 </li>
41 42 </ul>
... ... @@ -47,10 +48,6 @@
47 48  
48 49 </ul>
49 50  
50   - <% button_bar do %>
51   - <%= submit_button(:save, _('Save')) %>
52   - <% end %>
53   -
54 51 <% end %>
55 52  
56 53 <% end %>
... ...