Commit 6b09f6b6348a88eddf1efef1d9ded38d7f9a53e6

Authored by Victor Costa
2 parents c69c6c7c e07ff8d5

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

plugins/virtuoso/controllers/virtuoso_plugin_admin_controller.rb
... ... @@ -47,15 +47,16 @@ class VirtuosoPluginAdminController < AdminController
47 47 to_triple.object = params[:to_triple][:object]
48 48  
49 49 triples_management = VirtuosoPlugin::TriplesManagement.new(environment)
50   - triples_management.update_triple(from_triple, to_triple)
51 50  
52   - render json: { :ok => true, :message => _('Triple succesfully updated.') }
  51 + triple_updated = triples_management.update_triple(from_triple, to_triple)
  52 + message = triple_updated ? _('Triple succesfully updated.') : _('Triple not updated.')
  53 +
  54 + render json: { :ok => triple_updated, :message => message }
53 55 end
54 56 end
55 57  
56 58 def add_triple
57 59 if request.post?
58   -
59 60 triple = VirtuosoPlugin::Triple.new
60 61 triple.graph = params[:triple][:graph]
61 62 triple.subject = params[:triple][:subject]
... ... @@ -63,9 +64,11 @@ class VirtuosoPluginAdminController < AdminController
63 64 triple.object = params[:triple][:object]
64 65  
65 66 triples_management = VirtuosoPlugin::TriplesManagement.new(environment)
66   - triples_management.add_triple(triple)
67 67  
68   - render json: { :ok => true, :message => _('Triple succesfully added.') }
  68 + triple_added = triples_management.add_triple(triple)
  69 + message = triple_added ? _('Triple succesfully added.') : _('Triple not added.')
  70 +
  71 + render json: { :ok => triple_added, :message => message }
69 72 end
70 73 end
71 74  
... ... @@ -78,9 +81,11 @@ class VirtuosoPluginAdminController < AdminController
78 81 triple.object = params[:triple][:object]
79 82  
80 83 triples_management = VirtuosoPlugin::TriplesManagement.new(environment)
81   - triples_management.remove_triple(triple)
82 84  
83   - render json: { :ok => true, :message => _('Triple succesfully removed.') }
  85 + triple_deleted = triples_management.remove_triple(triple)
  86 + message = triple_deleted ? _('Triple succesfully removed.') : _('Triple not removed.')
  87 +
  88 + render json: { :ok => triple_deleted, :message => message }
84 89 end
85 90 end
86 91  
... ...
plugins/virtuoso/lib/virtuoso_plugin/triples_management.rb
... ... @@ -18,17 +18,37 @@ class VirtuosoPlugin::TriplesManagement
18 18 def update_triple(from_triple, to_triple)
19 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} }"
20 20  
21   - plugin.virtuoso_client.query(query)
  21 + begin
  22 + query_result = plugin.virtuoso_client.query(query)[0][:"callret-0"].to_s
  23 + rescue RDF::Virtuoso::Repository::MalformedQuery => e
  24 + query_result = e.to_s
  25 + end
  26 +
  27 + return query_result =~ /^Modify.*done$/ ? true : false
22 28 end
23 29  
24 30 def add_triple(triple)
25 31 query = "WITH <#{triple.graph}> INSERT { <#{triple.subject}> <#{triple.predicate}> #{triple.object} }"
26   - plugin.virtuoso_client.query(query)
  32 +
  33 + begin
  34 + query_result = plugin.virtuoso_client.query(query)[0][:"callret-0"].to_s
  35 + rescue RDF::Virtuoso::Repository::MalformedQuery => e
  36 + query_result = e.to_s
  37 + end
  38 +
  39 + return query_result =~ /^Insert.*done$/ ? true : false
27 40 end
28 41  
29 42 def remove_triple(triple)
30 43 query = "WITH <#{triple.graph}> DELETE { <#{triple.subject}> <#{triple.predicate}> #{triple.object} }"
31   - plugin.virtuoso_client.query(query)
  44 +
  45 + begin
  46 + query_result = plugin.virtuoso_client.query(query)[0][:"callret-0"].to_s
  47 + rescue RDF::Virtuoso::Repository::MalformedQuery => e
  48 + query_result = e.to_s
  49 + end
  50 +
  51 + return query_result =~ /^Delete.*done$/ ? true : false
32 52 end
33 53  
34 54 end
... ...
plugins/virtuoso/public/triples_management.js
... ... @@ -20,17 +20,14 @@ function update_triple(triple_id) {
20 20 url: '/admin/plugin/virtuoso/update_triple',
21 21 data: formData,
22 22 dataType: 'json',
23   - success: function(data, status, ajax){
  23 + success: function(data, status, ajax) {
24 24 if ( !data.ok ) {
25   - display_notice(data.error.message);
  25 + display_notice(data.message);
26 26 }
27 27 else {
28 28 display_notice(data.message);
29 29 jQuery("input#triples_triple" + triple_id + "_from_object").val(jQuery("input#triples_triple" + triple_id + "_to_object").val());
30 30 }
31   - },
32   - error: function(ajax, status, errorThrown) {
33   - alert('Send request - HTTP '+status+': '+errorThrown);
34 31 }
35 32 });
36 33  
... ... @@ -50,18 +47,9 @@ function add_triple() {
50 47 url: '/admin/plugin/virtuoso/add_triple',
51 48 data: formData,
52 49 dataType: 'json',
53   - success: function(data, status, ajax){
54   - if ( !data.ok ) {
55   - display_notice(data.error.message);
56   - jQuery.colorbox.close();
57   - }
58   - else {
59   - display_notice(data.message);
60   - jQuery.colorbox.close();
61   - }
62   - },
63   - error: function(ajax, status, errorThrown) {
64   - alert('Send request - HTTP '+status+': '+errorThrown);
  50 + success: function(data, status, ajax) {
  51 + display_notice(data.message);
  52 + jQuery.colorbox.close();
65 53 }
66 54 });
67 55  
... ... @@ -84,7 +72,7 @@ function remove_triple(triple_id) {
84 72 dataType: 'json',
85 73 success: function(data, status, ajax){
86 74 if ( !data.ok ) {
87   - display_notice(data.error.message);
  75 + display_notice(data.message);
88 76 }
89 77 else {
90 78 display_notice(data.message);
... ... @@ -97,9 +85,6 @@ function remove_triple(triple_id) {
97 85 }
98 86 });
99 87 }
100   - },
101   - error: function(ajax, status, errorThrown) {
102   - alert('Send request - HTTP '+status+': '+errorThrown);
103 88 }
104 89 });
105 90  
... ...
plugins/virtuoso/test/functional/virtuoso_plugin_admin_controller_test.rb
... ... @@ -8,7 +8,6 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
8 8 @environment = Environment.default
9 9 @profile = create_user('profile').person
10 10 login_as(@profile.identifier)
11   - post :index, :settings => mock_settings
12 11 end
13 12  
14 13 def mock_settings
... ... @@ -26,6 +25,7 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
26 25 end
27 26  
28 27 should 'save virtuoso plugin settings' do
  28 + post :index, :settings => mock_settings
29 29 @settings = Noosfero::Plugin::Settings.new(environment.reload, VirtuosoPlugin)
30 30 assert_equal 'http://virtuoso.noosfero.com', @settings.settings[:virtuoso_uri]
31 31 assert_equal 'username', @settings.settings[:virtuoso_username]
... ... @@ -38,28 +38,33 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
38 38 assert_redirected_to :action => 'index'
39 39 end
40 40  
41   -
42 41 should 'redirect to index after save' do
43 42 post :index, :settings => mock_settings
44 43 assert_redirected_to :action => 'index'
45 44 end
46 45  
47 46 should 'create delayed job to start harvest on force action' do
48   - harvest = VirtuosoPlugin::DspaceHarvest.new(environment)
  47 + post :index, :settings => mock_settings
  48 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace1.noosfero.com")
49 49 assert !harvest.find_job.present?
50 50 get :force_harvest
51 51 assert harvest.find_job.present?
52 52 end
53 53  
54 54 should 'force harvest from start' do
  55 + post :index, :settings => mock_settings
55 56 get :force_harvest, :from_start => true
56   - harvest = VirtuosoPlugin::DspaceHarvest.new(environment)
  57 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace2.noosfero.com")
57 58 assert harvest.find_job.present?
58 59 assert_equal nil, harvest.settings.last_harvest
59 60 end
60   -
61   - should 'force harvest_all from start' do
62   - get :force_harvest, :from_start => true
  61 +
  62 + should 'not create delayed job to start harvest on force action without settings' do
  63 + post :index, :settings => mock_settings
  64 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace8.noosfero.com")
  65 + assert !harvest.find_job.present?, "testing if no job is running"
  66 + get :force_harvest
  67 + assert !harvest.find_job.present?, "testing if no job is running again"
63 68 end
64   -
65   -end
66 69 \ No newline at end of file
  70 +
  71 +end
... ...
plugins/virtuoso/test/unit/dspace_harvest_test.rb
... ... @@ -23,6 +23,8 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase
23 23 end
24 24  
25 25 should 'create delayed job when start' do
  26 + @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
  27 + @settings.save!
26 28 harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace1.noosfero.com")
27 29 assert !harvest.find_job.present?
28 30 harvest.start
... ... @@ -30,16 +32,14 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase
30 32 end
31 33  
32 34 should 'not duplicate harvest job' do
  35 + @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
  36 + @settings.save!
33 37 harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace1.noosfero.com")
34 38 assert_difference "harvest.find_job.count", 1 do
35 39 5.times { harvest.start }
36 40 end
37 41 end
38 42  
39   - should 'harvest all dspaces from start' do
40   - VirtuosoPlugin::DspaceHarvest.harvest_all(environment, true)
41   - end
42   -
43 43 should 'try to harvest all dspaces from start without any setting' do
44 44 VirtuosoPlugin::DspaceHarvest.harvest_all(environment, true)
45 45 end
... ...