Skip to content

Commit

Permalink
support for finding an offer by number and better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jambun committed Jun 17, 2024
1 parent 4c70d22 commit 2242f7c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 56 deletions.
27 changes: 16 additions & 11 deletions backend/controllers/reftracker.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
class ArchivesSpaceService < Sinatra::Base

Endpoint.get('/plugins/reftracker/offers')
.description("Get a list of offers that are ready to import from RefTracker")
.params(['page', Integer, "Page number", :default => 1])
.description("Get a list of offers that are ready to import from RefTracker, or a specific offer")
.params(['page', Integer, "Page number", :default => 1],
['ono', String, 'The offer number to get', :optional => true])
.permissions([])
.returns([200, "[offers]"]) \
do
json_response(RefTrackerClient.manuscript_offers(params[:page]))
begin
json_response(RefTrackerClient.manuscript_offers(params[:page], params[:ono]))
rescue ReftrackerAPIException => e
json_response({:error => e.message})
end
end

Endpoint.get('/plugins/reftracker/offer/:ono')
.description("Get an offer from RefTracker")
.params(['ono', String, 'The offer number to get'])
.permissions([])
.returns([200, "offer"], [404, 'not found']) \
.returns([200, "offer"], [400, 'API error']) \
do
begin
json_response(RefTrackerClient.get_question(params[:ono]))
rescue RecordNotFound => e
json_response({:error => e.message}, 404)
rescue ReftrackerAPIException => e
json_response({:error => e.message}, 400)
end
end

Endpoint.get('/plugins/reftracker/codetable/:table')
.description("Get a RefTracker code table")
.params(['table', String, 'The code table to get'])
.permissions([])
.returns([200, "codetable"], [404, 'not found']) \
.returns([200, "codetable"], [400, 'API error']) \
do
begin
RefTrackerClient.get_codetable(params[:table])
rescue RecordNotFound => e
json_response({:error => e.message}, 404)
rescue ReftrackerAPIException => e
json_response({:error => e.message}, 400)
end
end

Expand All @@ -40,7 +45,7 @@ class ArchivesSpaceService < Sinatra::Base
.params(["repo_id", :repo_id],
['offer', String, 'The offer number to import'])
.permissions([:update_accession_record])
.returns([200, "success"], [404, "not found"]) \
.returns([200, "success"], [400, "API error"]) \
do
json_response(RefTrackerHandler.import(params[:offer]))
end
Expand All @@ -50,7 +55,7 @@ class ArchivesSpaceService < Sinatra::Base
.params(["repo_id", :repo_id],
['offers', String, 'The Offer numbers to import'])
.permissions([:update_accession_record])
.returns([200, "success"], [404, "not found"]) \
.returns([200, "success"], [400, "API error"]) \
do
json_response(RefTrackerHandler.import(params[:offers]))
end
Expand Down
97 changes: 58 additions & 39 deletions backend/model/reftracker_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,68 @@ def self.get_question(question_no)
# [{"result":"No Question for these parameters format:json key:question_no value:blah","status":"200"}]
# so just going to assume if it is an array then it wasn't found - otherwise it would be a hash
if resp.is_a? Array
raise RecordNotFound.new("No Question for number #{question_no}")
raise ReftrackerAPIException.new("No offer for number #{question_no}")
end

# decode and strip markup - phewee - reftracker is not being friendly here
Hash[resp.map {|k, v| [k, strip_markup(v)]}]
end


def self.manuscript_offers(page = 1)
columns = [
'question_no',
'question_text',
'bib_udf_tb03',
'bib_title',
'client_name',
'question_format',
'question_update_datetime',
]
def self.manuscript_offers(page = 1, offer_number)
offers = []
if offer_number
resp = ASUtils.json_parse(self.get('getQuestion', {:parameters => {:key => 'question_no', :value => offer_number, :format => 'json'}.to_json}))

# status of 700 is 'Closed successful' found this using /codetable?table=status
# :status => '700' - not doing this any more

# qtype of 100 is 'Offerer service' - new requirement
# db = 5 is a magic number from the original plugin.
# without it the api complains about missing a param called 'source'
# sortby = 3 is ClosedDate -- no longer using this

# last update: qnudt - can't sort by this so qno instead
# :sortby => '50' is question_no

# question_format = Manuscripts
# :qnfmid => '10'
if resp.is_a? Array
raise ReftrackerAPIException.new("No offer for number #{offer_number}")
end

search_params = {
:qtype => '100',
:qnfmid => '10',
:db => '5',
:sortby => '50',
:sortorder => 'DESC',
:columnList => columns.join('|'),
:pagenumber => page,
:pagesize => 20,
}
self.get('search', {:parameters => search_params.to_json})
offers << resp
else
columns = [
'question_no',
'question_text',
'bib_udf_tb03',
'bib_title',
'client_name',
'question_format',
'question_update_datetime',
]

# status of 700 is 'Closed successful' found this using /codetable?table=status
# :status => '700' - not doing this any more

# qtype of 100 is 'Offerer service' - new requirement
# db = 5 is a magic number from the original plugin.
# without it the api complains about missing a param called 'source'
# sortby = 3 is ClosedDate -- no longer using this

# last update: qnudt - can't sort by this so qno instead
# :sortby => '50' is question_no

# question_format = Manuscripts
# :qnfmid => '10'

search_params = {
:qtype => '100',
:qnfmid => '10',
:db => '5',
:sortby => '50',
:sortorder => 'DESC',
:columnList => columns.join('|'),
:pagenumber => page,
:pagesize => 20,
}

offers = ASUtils.json_parse(self.get('search', {:parameters => search_params.to_json}))
end

# here's how accession.identifier looks in the db :(
# ["moo",null,null,null]
# NLA only uses id_0 so this works
offer_ids = offers.map{|offer| offer['bib_udf_tb03']}.select{|id| !id.empty?}.map{|id| '["' + id + '",null,null,null]'}.compact


# find out which of the offer_ids already exist in AS
found_ids = nil
DB.open{ |db| found_ids = db[:accession].filter(:identifier => offer_ids).select(:identifier).map{|i| ASUtils.json_parse(i[:identifier]).first}}
Expand Down Expand Up @@ -102,8 +113,16 @@ def self.get_codetable(table)


def self.get(uri, params = {})
url = URI(File.join(AppConfig[:reftracker_base_url], uri))
url.query = URI.encode_www_form(params) unless params.empty?
Net::HTTP.get(url)
begin
url = URI(File.join(AppConfig[:reftracker_base_url], uri))
url.query = URI.encode_www_form(params) unless params.empty?
Net::HTTP.get(url)
rescue => e
if e.class == SocketError
raise ReftrackerAPIException.new('Failed to connect to Reftracker')
else
raise ReftrackerAPIException.new(e.message)
end
end
end
end
10 changes: 9 additions & 1 deletion frontend/controllers/reftracker_offers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ class ReftrackerOffersController < ApplicationController
set_access_control "update_accession_record" => [:index, :import]

def index
render :locals => { :offers => JSONModel::HTTP::get_json('/plugins/reftracker/offers', :page => params['page']) }
offers = JSONModel::HTTP::get_json('/plugins/reftracker/offers', :page => (params['page'] || 1), :ono => params['offer_number'])
error = nil
if offers.is_a?(Hash) && offers['error']
error = offers['error']
offers = []
end
render :locals => { :offers => offers,
:offer_number => params['offer_number'],
:error => error }
end

def import
Expand Down
7 changes: 6 additions & 1 deletion frontend/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ en:
reftracker_offers:
label: RefTracker Offers
import_button_label: Import
find_button_label: Find Offer
cancel_find_button_label: Cancel
results:
offers_description: Showing Manuscripts offers, in descending offer number order, excluding those that lack an identifier and those with identifiers that are already in ArchivesSpace.
offers_description: Showing <b>Manuscripts</b> offers <b>with an identifier</b> that is <b>not already in ArchivesSpace</b>, in descending offer number order.
offers_error_description: Problem retrieving offers
found_offer_description: Showing offer <b>%{offer_number}</b>. Click Cancel to see all offers.
offer_error_description: Problem finding offer <b>%{offer_number}</b>
offers_instruction: Select offers using the checkboxes and click Import to import them as Accessions.
question_no: "Offer #"
bib_udf_tb03: Identifier
Expand Down
33 changes: 29 additions & 4 deletions frontend/views/reftracker_offers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@

<%= render_aspace_partial :partial => "shared/form_messages" %>

<p><%= I18n.t('plugins.reftracker_offers.results.offers_description') %></p>
<% if offer_number %>
<% if error %>
<p class="alert alert-danger"><%= I18n.t('plugins.reftracker_offers.results.offer_error_description', :offer_number => offer_number) %>: <%= error %></p>
<% else %>
<p class="alert alert-info"><%= I18n.t('plugins.reftracker_offers.results.found_offer_description', :offer_number => offer_number) %></p>
<% end %>
<% else %>
<% if error %>
<p class="alert alert-danger"><%= I18n.t('plugins.reftracker_offers.results.offers_error_description') %>: <%= error %></p>
<% else %>
<p class="alert alert-info"><%= I18n.t('plugins.reftracker_offers.results.offers_description') %></p>
<% end %>
<% end %>

<%= form_tag({:controller => :reftracker_offers, :action => :index}, :method => :get) do %>

<input type="text" name="offer_number" placeholder="Enter an offer number" value="<%= offer_number %>">
<button type="submit" id="rto-offer-submit" class="btn btn-sm btn-primary"><%= I18n.t("plugins.reftracker_offers.find_button_label") %></button>

<% if offer_number %>
<a type="cancel" id="rto-offer-cancel" href="<%= url_for(:controller => :reftracker_offers, :action => :index) %>" class="btn btn-sm btn-default"><%= I18n.t("plugins.reftracker_offers.cancel_find_button_label") %></a>
<% end %>
<% end %>

<hr/>

<p><%= I18n.t('plugins.reftracker_offers.results.offers_instruction') %></p>

<%= form_tag({:controller => :reftracker_offers, :action => :import}) do %>
Expand All @@ -12,7 +37,7 @@

<br/><br/>

<% if page = offers.first %>
<% if (page = offers.first) && page['search_page'] %>
<% current_page = page['search_page'].to_i %>
<% last_page = (page['search_total'].to_f / page['search_page_size'].to_f).ceil %>
<p>
Expand Down Expand Up @@ -45,7 +70,7 @@
<td class="rto-td rto-nowrap">
<%= offer['bib_udf_tb03'] %>
</td>
<td class="rto-td">
<td class="rto-td rto-title">
<%= offer['bib_title'].html_safe %>
</td>
<td class="rto-td">
Expand All @@ -67,7 +92,7 @@

<% end %>

<% if page = offers.first %>
<% if (page = offers.first) && page['search_page'] %>
<% current_page = page['search_page'].to_i %>
<% last_page = (page['search_total'].to_f / page['search_page_size'].to_f).ceil %>
<% if last_page > 1 %>
Expand Down

0 comments on commit 2242f7c

Please sign in to comment.