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