Commit 864b0f2dff863d095d71a8924c3a0b4276762055

Authored by Victor Costa
2 parents 85002c49 5d6d1157

Merge branch 'virtuoso_integration' into stable

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/noosfero_harvest.rb 0 → 100644
... ... @@ -0,0 +1,118 @@
  1 +class VirtuosoPlugin::NoosferoHarvest
  2 +
  3 + COMMON_MAPPING = {
  4 + :type => {:predicate => "http://purl.org/dc/terms/type", :value => lambda {|s, t| t.class.name}},
  5 + :created_at => {:predicate => "http://purl.org/dc/terms/created"},
  6 + :updated_at => {:predicate => "http://purl.org/dc/terms/modified"},
  7 + }
  8 +
  9 + ARTICLE_MAPPING = {
  10 + :title => {:predicate => "http://purl.org/dc/terms/title"},
  11 + :abstract => {:predicate => "http://purl.org/dc/terms/abstract"},
  12 + :body => {:predicate => "http://purl.org/dc/terms/description"},
  13 + :part_of => {:predicate => "http://purl.org/dc/terms/isPartOf", :value => lambda {|s, t| url_for(s.url)} },
  14 + :published_at => {:predicate => "http://purl.org/dc/terms/issued"},
  15 + :author => {:predicate => "http://purl.org/dc/terms/creator", :value => lambda {|s, t| url_for(t.author_url) if t.author_url} },
  16 + }
  17 + PROFILE_MAPPING = {
  18 + :name => {:predicate => "http://purl.org/dc/terms/title"},
  19 + :public? => {:predicate => "http://purl.org/socialparticipation/opa#publicProfile"},
  20 + }
  21 + COMMENT_MAPPING = {
  22 + :title => {:predicate => "http://purl.org/dc/terms/title"},
  23 + :body => {:predicate => "http://purl.org/dc/terms/description"},
  24 + :part_of => {:predicate => "http://purl.org/dc/terms/isPartOf", :value => lambda {|s, t| url_for(s.url)} },
  25 + :author => {:predicate => "http://purl.org/dc/terms/creator", :value => lambda {|s, t| url_for(t.author_url) if t.author_url} },
  26 + }
  27 + FRIENDSHIP_MAPPING = {
  28 + :knows => {:predicate => "http://xmlns.com/foaf/0.1/knows", :value => lambda {|s, t| url_for(t.url)} },
  29 + :type => nil, :created_at => nil, :updated_at => nil,
  30 + }
  31 +
  32 + def initialize(environment)
  33 + @environment = environment
  34 + @graph = environment.top_url
  35 + end
  36 +
  37 + attr_reader :environment
  38 +
  39 + def plugin
  40 + @plugin ||= VirtuosoPlugin.new(self)
  41 + end
  42 +
  43 + delegate :settings, :to => :plugin
  44 +
  45 + include Rails.application.routes.url_helpers
  46 +
  47 + def triplify_comments(article)
  48 + total = article.comments.count
  49 + count = 0
  50 + article.comments.each do |comment|
  51 + subject_identifier = url_for(comment.url)
  52 + puts "triplify #{subject_identifier} comment (#{count+=1}/#{total})"
  53 + triplify_mappings(COMMENT_MAPPING, subject_identifier, article, comment)
  54 + end
  55 + end
  56 +
  57 + def triplify_articles(profile)
  58 + total = profile.articles.count
  59 + count = 0
  60 + profile.articles.public.each do |article|
  61 + subject_identifier = url_for(article.url)
  62 + puts "triplify #{subject_identifier} article (#{count+=1}/#{total})"
  63 + triplify_mappings(ARTICLE_MAPPING, subject_identifier, profile, article)
  64 + triplify_comments(article)
  65 + end
  66 + end
  67 +
  68 + def triplify_friendship(person)
  69 + total = person.friends.count
  70 + count = 0
  71 + person.friends.each do |friend|
  72 + subject_identifier = url_for(person.url)
  73 + puts "triplify #{subject_identifier} friendship (#{count+=1}/#{total})"
  74 + triplify_mappings(FRIENDSHIP_MAPPING, subject_identifier, person, friend)
  75 + end
  76 + end
  77 +
  78 + def triplify_profiles
  79 + total = environment.profiles.count
  80 + count = 0
  81 + environment.profiles.each do |profile|
  82 + subject_identifier = url_for(profile.url)
  83 + puts "triplify #{subject_identifier} profile (#{count+=1}/#{total})"
  84 + triplify_mappings(PROFILE_MAPPING, subject_identifier, environment, profile)
  85 + triplify_articles(profile) if profile.public?
  86 + triplify_friendship(profile) if profile.person?
  87 + end
  88 + end
  89 +
  90 + def run
  91 + triplify_profiles
  92 + end
  93 +
  94 + protected
  95 +
  96 + def triplify_mappings(mapping, subject_identifier, source, target)
  97 + COMMON_MAPPING.merge(mapping).each do |k, v|
  98 + next unless v
  99 + value = nil
  100 + if v[:value]
  101 + value = v[:value].kind_of?(Proc) ? v[:value].call(source, target) : v[:value]
  102 + elsif target.respond_to?(k)
  103 + value = target.send(k)
  104 + end
  105 + insert_triple(RDF::URI.new(subject_identifier), RDF::URI.new(v[:predicate]), value) if value.present?
  106 + end
  107 + end
  108 +
  109 + def insert_triple(subject, predicate, value)
  110 + value = RDF::URI.new(value) if value.kind_of?(String) && /https?:\/\//.match(value)
  111 + value = RDF::Literal::DateTime.new(value) if value.kind_of?(ActiveSupport::TimeWithZone)
  112 + value = RDF::Literal::Boolean.new(value) if !!value == value
  113 +
  114 + query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(subject), RDF::URI.new(predicate), value]).graph(RDF::URI.new(@graph))
  115 + plugin.virtuoso_client.insert(query)
  116 + end
  117 +
  118 +end
... ...
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
... ...