Commit f84ac88bc5965d91fee342757a094e504d5db626

Authored by Evandro Jr
2 parents 54249390 cdca25dc

Merge branch 'virtuoso_integration' into stable

plugins/virtuoso/controllers/virtuoso_plugin_admin_controller.rb
@@ -4,7 +4,12 @@ class VirtuosoPluginAdminController < AdminController @@ -4,7 +4,12 @@ class VirtuosoPluginAdminController < AdminController
4 settings = params[:settings] 4 settings = params[:settings]
5 settings ||= {} 5 settings ||= {}
6 @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, settings) 6 @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, settings)
7 - @harvest_running = VirtuosoPlugin::DspaceHarvest.new(environment).find_job.present? 7 + @harvest_running = {}
  8 + if @settings.dspace_servers.present?
  9 + @settings.dspace_servers.each do |i|
  10 + @harvest_running[i['dspace_uri']] = VirtuosoPlugin::DspaceHarvest.new(environment, i).find_job(i['dspace_uri']).present?
  11 + end
  12 + end
8 if request.post? 13 if request.post?
9 settings[:dspace_servers].delete_if do | server | 14 settings[:dspace_servers].delete_if do | server |
10 server[:dspace_uri].empty? 15 server[:dspace_uri].empty?
plugins/virtuoso/lib/virtuoso_plugin/dspace_harvest.rb
1 #inspired by https://github.com/code4lib/ruby-oai/blob/master/lib/oai/harvester/harvest.rb 1 #inspired by https://github.com/code4lib/ruby-oai/blob/master/lib/oai/harvester/harvest.rb
2 class VirtuosoPlugin::DspaceHarvest 2 class VirtuosoPlugin::DspaceHarvest
3 3
4 - def initialize(environment, dspace_uri = nil) 4 + attr_reader :environment, :dspace_settings
  5 +
  6 + def initialize(environment, dspace_settings = nil)
5 @environment = environment 7 @environment = environment
6 - @dspace_uri = dspace_uri 8 + @dspace_settings = dspace_settings
  9 + end
  10 +
  11 + def dspace_uri
  12 + unless dspace_settings.nil?
  13 + dspace_settings["dspace_uri"] if dspace_settings.has_key?("dspace_uri")
  14 + end
7 end 15 end
8 16
9 - attr_reader :environment 17 + def last_harvest
  18 + unless dspace_settings.nil?
  19 + dspace_settings["last_harvest"] if dspace_settings.has_key?("last_harvest")
  20 + end
  21 + end
10 22
11 def plugin 23 def plugin
12 @plugin ||= VirtuosoPlugin.new(self) 24 @plugin ||= VirtuosoPlugin.new(self)
@@ -15,7 +27,7 @@ class VirtuosoPlugin::DspaceHarvest @@ -15,7 +27,7 @@ class VirtuosoPlugin::DspaceHarvest
15 delegate :settings, :to => :plugin 27 delegate :settings, :to => :plugin
16 28
17 def dspace_client 29 def dspace_client
18 - @dspace_client ||= OAI::Client.new("#{@dspace_uri}/oai/request") 30 + @dspace_client ||= OAI::Client.new("#{dspace_uri}/oai/request")
19 end 31 end
20 32
21 def triplify(record) 33 def triplify(record)
@@ -26,7 +38,7 @@ class VirtuosoPlugin::DspaceHarvest @@ -26,7 +38,7 @@ class VirtuosoPlugin::DspaceHarvest
26 settings.ontology_mapping.each do |mapping| 38 settings.ontology_mapping.each do |mapping|
27 values = [metadata.extract_field(mapping[:source])].flatten.compact 39 values = [metadata.extract_field(mapping[:source])].flatten.compact
28 values.each do |value| 40 values.each do |value|
29 - query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(subject_identifier), RDF::URI.new(mapping[:target]), value]).graph(RDF::URI.new(@dspace_uri)) 41 + query = RDF::Virtuoso::Query.insert_data([RDF::URI.new(subject_identifier), RDF::URI.new(mapping[:target]), value]).graph(RDF::URI.new(dspace_uri))
30 plugin.virtuoso_client.insert(query) 42 plugin.virtuoso_client.insert(query)
31 end 43 end
32 end 44 end
@@ -34,8 +46,8 @@ class VirtuosoPlugin::DspaceHarvest @@ -34,8 +46,8 @@ class VirtuosoPlugin::DspaceHarvest
34 46
35 def run 47 def run
36 harvest_time = Time.now.utc 48 harvest_time = Time.now.utc
37 - params = settings.last_harvest ? {:from => settings.last_harvest.utc} : {}  
38 - puts "starting harvest #{params} #{@dspace_uri} #{settings.virtuoso_uri}" 49 + params = last_harvest ? {:from => last_harvest.utc} : {}
  50 + puts "starting harvest #{params} #{dspace_uri} #{settings.virtuoso_uri}"
39 begin 51 begin
40 records = dspace_client.list_records(params) 52 records = dspace_client.list_records(params)
41 records.each do |record| 53 records.each do |record|
@@ -48,16 +60,26 @@ class VirtuosoPlugin::DspaceHarvest @@ -48,16 +60,26 @@ class VirtuosoPlugin::DspaceHarvest
48 raise ex 60 raise ex
49 end 61 end
50 end 62 end
51 - settings.last_harvest = harvest_time  
52 - settings.save!  
53 puts "ending harvest #{harvest_time}" 63 puts "ending harvest #{harvest_time}"
54 end 64 end
55 65
  66 + def save_harvest_time_settings(harvest_time)
  67 + dspace_settings = {"dspace_uri" => dspace_uri, "last_harvest" => last_harvest}
  68 + settings.dspace_servers.each do |s|
  69 + if s["dspace_uri"] == dspace_uri
  70 + settings.dspace_servers.delete(dspace_settings)
  71 + end
  72 + end
  73 + @dspace_settings = {"dspace_uri" => dspace_uri, "last_harvest" => harvest_time}
  74 + settings.dspace_servers << @dspace_settings
  75 + settings.save!
  76 + end
  77 +
56 def self.harvest_all(environment, from_start) 78 def self.harvest_all(environment, from_start)
57 settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin) 79 settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin)
58 if settings.dspace_servers.present? 80 if settings.dspace_servers.present?
59 - settings.dspace_servers.each do |k, v|  
60 - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, k[:dspace_uri]) 81 + settings.dspace_servers.each do |i|
  82 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, i)
61 harvest.start(from_start) 83 harvest.start(from_start)
62 end 84 end
63 end 85 end
@@ -66,22 +88,22 @@ class VirtuosoPlugin::DspaceHarvest @@ -66,22 +88,22 @@ class VirtuosoPlugin::DspaceHarvest
66 def start(from_start = false) 88 def start(from_start = false)
67 if find_job.empty? 89 if find_job.empty?
68 if from_start 90 if from_start
69 - settings.last_harvest = nil  
70 - settings.save! 91 + save_harvest_time_settings(nil)
71 end 92 end
72 - job = VirtuosoPlugin::DspaceHarvest::Job.new(@environment.id, @dspace_uri) 93 + job = VirtuosoPlugin::DspaceHarvest::Job.new(@environment.id, dspace_uri)
73 Delayed::Job.enqueue(job) 94 Delayed::Job.enqueue(job)
74 end 95 end
75 end 96 end
76 97
77 - def find_job  
78 - Delayed::Job.where(:handler => "--- !ruby/struct:VirtuosoPlugin::DspaceHarvest::Job\nenvironment_id: #{@environment.id}\ndspace_uri: #{@dspace_uri}\n") 98 + def find_job(_dspace_uri=nil)
  99 + _dspace_uri ||= dspace_uri
  100 + Delayed::Job.where(:handler => "--- !ruby/struct:VirtuosoPlugin::DspaceHarvest::Job\nenvironment_id: #{@environment.id}\ndspace_uri: #{_dspace_uri}\n")
79 end 101 end
80 102
81 class Job < Struct.new(:environment_id, :dspace_uri) 103 class Job < Struct.new(:environment_id, :dspace_uri)
82 def perform 104 def perform
83 - environment = Environment.find(environment_id)  
84 - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, dspace_uri) 105 + environment = Environment.find(environment_id, dspace_uri)
  106 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri" => dspace_uri, "last_harvest" => last_harvest})
85 harvest.run 107 harvest.run
86 end 108 end
87 end 109 end
@@ -90,7 +112,7 @@ class VirtuosoPlugin::DspaceHarvest @@ -90,7 +112,7 @@ class VirtuosoPlugin::DspaceHarvest
90 112
91 def extract_identifier(record) 113 def extract_identifier(record)
92 parsed_identifier = /oai:(.+):(\d+\/\d+)/.match(record.header.identifier) 114 parsed_identifier = /oai:(.+):(\d+\/\d+)/.match(record.header.identifier)
93 - "#{@dspace_uri}/handle/#{parsed_identifier[2]}" 115 + "#{dspace_uri}/handle/#{parsed_identifier[2]}"
94 end 116 end
95 117
96 end 118 end
plugins/virtuoso/test/functional/virtuoso_plugin_admin_controller_test.rb
@@ -17,9 +17,11 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase @@ -17,9 +17,11 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
17 :virtuoso_readonly_username=>"readonly_username", 17 :virtuoso_readonly_username=>"readonly_username",
18 :virtuoso_readonly_password=>"readonly_password", 18 :virtuoso_readonly_password=>"readonly_password",
19 :dspace_servers=>[ 19 :dspace_servers=>[
20 - {"dspace_uri"=>"http://dspace1.noosfero.com"}, 20 + {"dspace_uri"=>"http://dspace1.noosfero.com","last_harvest" => 5 },
21 {"dspace_uri"=>"http://dspace2.noosfero.com"}, 21 {"dspace_uri"=>"http://dspace2.noosfero.com"},
22 - {"dspace_uri"=>"http://dspace3.noosfero.com"} 22 + {"dspace_uri"=>"http://dspace3.noosfero.com", "last_harvest" => 0},
  23 + {"dspace_uri"=>"http://dspace4.noosfero.com", "last_harvest" => nil},
  24 + {"dspace_uri"=>"http://dspace5.noosfero.com", "last_harvest" => 9},
23 ] 25 ]
24 } 26 }
25 end 27 end
@@ -35,6 +37,7 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase @@ -35,6 +37,7 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
35 assert_equal 'http://dspace1.noosfero.com', @settings.settings[:dspace_servers][0][:dspace_uri] 37 assert_equal 'http://dspace1.noosfero.com', @settings.settings[:dspace_servers][0][:dspace_uri]
36 assert_equal 'http://dspace2.noosfero.com', @settings.settings[:dspace_servers][1][:dspace_uri] 38 assert_equal 'http://dspace2.noosfero.com', @settings.settings[:dspace_servers][1][:dspace_uri]
37 assert_equal 'http://dspace3.noosfero.com', @settings.settings[:dspace_servers][2][:dspace_uri] 39 assert_equal 'http://dspace3.noosfero.com', @settings.settings[:dspace_servers][2][:dspace_uri]
  40 + assert_equal "9", @settings.settings[:dspace_servers][4][:last_harvest]
38 assert_redirected_to :action => 'index' 41 assert_redirected_to :action => 'index'
39 end 42 end
40 43
@@ -45,7 +48,7 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase @@ -45,7 +48,7 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
45 48
46 should 'create delayed job to start harvest on force action' do 49 should 'create delayed job to start harvest on force action' do
47 post :index, :settings => mock_settings 50 post :index, :settings => mock_settings
48 - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace1.noosfero.com") 51 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace1.noosfero.com", "last_harvest" => 5 })
49 assert !harvest.find_job.present? 52 assert !harvest.find_job.present?
50 get :force_harvest 53 get :force_harvest
51 assert harvest.find_job.present? 54 assert harvest.find_job.present?
@@ -54,17 +57,17 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase @@ -54,17 +57,17 @@ class VirtuosoPluginAdminControllerTest &lt; ActionController::TestCase
54 should 'force harvest from start' do 57 should 'force harvest from start' do
55 post :index, :settings => mock_settings 58 post :index, :settings => mock_settings
56 get :force_harvest, :from_start => true 59 get :force_harvest, :from_start => true
57 - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace2.noosfero.com") 60 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace4.noosfero.com", "last_harvest" => nil})
58 assert harvest.find_job.present? 61 assert harvest.find_job.present?
59 assert_equal nil, harvest.settings.last_harvest 62 assert_equal nil, harvest.settings.last_harvest
60 end 63 end
61 - 64 +
62 should 'not create delayed job to start harvest on force action without settings' do 65 should 'not create delayed job to start harvest on force action without settings' do
63 post :index, :settings => mock_settings 66 post :index, :settings => mock_settings
64 - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace8.noosfero.com") 67 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment)
65 assert !harvest.find_job.present?, "testing if no job is running" 68 assert !harvest.find_job.present?, "testing if no job is running"
66 get :force_harvest 69 get :force_harvest
67 - assert !harvest.find_job.present?, "testing if no job is running again" 70 + assert !harvest.find_job.present?, "testing if no job is running again"
68 end 71 end
69 - 72 +
70 end 73 end
plugins/virtuoso/test/unit/dspace_harvest_test.rb
@@ -15,17 +15,39 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase @@ -15,17 +15,39 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase
15 :virtuoso_readonly_username=>"readonly_username", 15 :virtuoso_readonly_username=>"readonly_username",
16 :virtuoso_readonly_password=>"readonly_password", 16 :virtuoso_readonly_password=>"readonly_password",
17 :dspace_servers=>[ 17 :dspace_servers=>[
18 - {"dspace_uri"=>"http://dspace1.noosfero.com"}, 18 + {"dspace_uri"=>"http://dspace1.noosfero.com","last_harvest" => 5 },
19 {"dspace_uri"=>"http://dspace2.noosfero.com"}, 19 {"dspace_uri"=>"http://dspace2.noosfero.com"},
20 - {"dspace_uri"=>"http://dspace3.noosfero.com"} 20 + {"dspace_uri"=>"http://dspace3.noosfero.com", "last_harvest" => 0},
  21 + {"dspace_uri"=>"http://dspace4.noosfero.com", "last_harvest" => nil},
  22 + {"dspace_uri"=>"http://dspace5.noosfero.com", "last_harvest" => 9},
21 ] 23 ]
22 } 24 }
23 end 25 end
24 26
  27 + should 'initialize with dspace_uri' do
  28 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace1.noosfero.com"})
  29 + assert harvest.dspace_uri, "http://dspace1.noosfero.com"
  30 + end
  31 +
  32 + should 'initialize with dspace_uri and last_harvest' do
  33 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace9.noosfero.com", "last_harvest" => 5})
  34 + assert harvest.dspace_uri, "http://dspace9.noosfero.com"
  35 + assert harvest.last_harvest, 5
  36 + end
  37 +
  38 + should 'save_harvest_time_settings' do
  39 + @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
  40 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace5.noosfero.com", "last_harvest" => 9})
  41 + assert harvest.last_harvest, 9
  42 + harvest.save_harvest_time_settings(10)
  43 + @settings = Noosfero::Plugin::Settings.new(environment.reload, VirtuosoPlugin)
  44 + assert harvest.last_harvest, 10
  45 + end
  46 +
25 should 'create delayed job when start' do 47 should 'create delayed job when start' do
26 @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings) 48 @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
27 @settings.save! 49 @settings.save!
28 - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace1.noosfero.com") 50 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace1.noosfero.com", "last_harvest" => 5})
29 assert !harvest.find_job.present? 51 assert !harvest.find_job.present?
30 harvest.start 52 harvest.start
31 assert harvest.find_job.present? 53 assert harvest.find_job.present?
@@ -34,7 +56,7 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase @@ -34,7 +56,7 @@ class DspaceHarvestTest &lt; ActiveSupport::TestCase
34 should 'not duplicate harvest job' do 56 should 'not duplicate harvest job' do
35 @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings) 57 @settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
36 @settings.save! 58 @settings.save!
37 - harvest = VirtuosoPlugin::DspaceHarvest.new(environment, "http://dspace1.noosfero.com") 59 + harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace1.noosfero.com", "last_harvest" => 5})
38 assert_difference "harvest.find_job.count", 1 do 60 assert_difference "harvest.find_job.count", 1 do
39 5.times { harvest.start } 61 5.times { harvest.start }
40 end 62 end
plugins/virtuoso/views/virtuoso_plugin_admin/_server_list_item.html.erb
@@ -2,6 +2,13 @@ @@ -2,6 +2,13 @@
2 <% value = server_list_item[:dspace_uri] if server_list_item %> 2 <% value = server_list_item[:dspace_uri] if server_list_item %>
3 <%= text_field_tag 'settings[dspace_servers][][dspace_uri]', value, { :class => 'link-name', :maxlength => 60, :size=> 58 } %> 3 <%= text_field_tag 'settings[dspace_servers][][dspace_uri]', value, { :class => 'link-name', :maxlength => 60, :size=> 58 } %>
4 <%= button_without_text(:delete, _('Delete'), "#" , :class=>"delete-server-list-row") %> 4 <%= button_without_text(:delete, _('Delete'), "#" , :class=>"delete-server-list-row") %>
5 - <BR><BR> 5 + <% last_execution = server_list_item[:last_harvest] if server_list_item
  6 + if last_execution %>
  7 + <div class="date" style="display: inline-block;">
  8 + <span class="label"><strong><%= _('Last execution:') %></strong></span>
  9 + <span class="value"><%= time_ago_as_sentence last_execution %></span>
  10 + </div>
  11 + <% end %>
  12 + <BR><BR>
6 </div> 13 </div>
7 - 14 +
plugins/virtuoso/views/virtuoso_plugin_admin/index.html.erb
@@ -11,35 +11,29 @@ @@ -11,35 +11,29 @@
11 <%= labelled_form_field _('Virtuoso Read-Only Username:'), f.text_field(:virtuoso_readonly_username, :size=> 60) %> 11 <%= labelled_form_field _('Virtuoso Read-Only Username:'), f.text_field(:virtuoso_readonly_username, :size=> 60) %>
12 <%= labelled_form_field _('Virtuoso Read-Only Password:'), f.password_field(:virtuoso_readonly_password, :size=> 60) %> 12 <%= labelled_form_field _('Virtuoso Read-Only Password:'), f.password_field(:virtuoso_readonly_password, :size=> 60) %>
13 </strong> 13 </strong>
14 - <BR> 14 + <BR>
15 <div class="dspace-servers-config-box" > 15 <div class="dspace-servers-config-box" >
16 - 16 +
17 <strong><%= _('Dspace Servers\' URL:') %></strong> 17 <strong><%= _('Dspace Servers\' URL:') %></strong>
18 <div id='edit-server-list-block'> 18 <div id='edit-server-list-block'>
19 <div id="dropable-server-list"> 19 <div id="dropable-server-list">
20 <%= render :partial => 'server_list_item', :collection=>@settings.dspace_servers %> 20 <%= render :partial => 'server_list_item', :collection=>@settings.dspace_servers %>
21 </div> 21 </div>
22 <div id="dspace-new-template"> 22 <div id="dspace-new-template">
23 - <div > 23 + <div >
24 <%= render :partial => 'server_list_item', :locals => {:dspace_uri => "http://"} %> 24 <%= render :partial => 'server_list_item', :locals => {:dspace_uri => "http://"} %>
25 - </div> 25 + </div>
26 </div> 26 </div>
27 <%= link_to_function(_('New Dspace Server'), 'add_new_server();', :class => 'button icon-add with-text') %> 27 <%= link_to_function(_('New Dspace Server'), 'add_new_server();', :class => 'button icon-add with-text') %>
28 </div> 28 </div>
29 -</div> 29 +</div>
30 <% button_bar do %> 30 <% button_bar do %>
31 <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> 31 <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
32 <% end %> 32 <% end %>
33 <% end %> 33 <% end %>
34 <div class="harvest"> 34 <div class="harvest">
35 - <% if @settings.last_harvest %>  
36 - <div class="date">  
37 - <span class="label"><strong><%= _('Last execution:') %></strong></span>  
38 - <span class="value"><%= time_ago_as_sentence @settings.last_harvest %></span>  
39 - </div>  
40 -<% end %>  
41 <div class="actions"> 35 <div class="actions">
42 - <% if @harvest_running %> 36 + <% if @harvest_running.has_value?(true) %>
43 <%= _('Running...') %> 37 <%= _('Running...') %>
44 <% else %> 38 <% else %>
45 <%= button :next, _('Force harvest'), :action => :force_harvest %> 39 <%= button :next, _('Force harvest'), :action => :force_harvest %>