Skip to content

Commit

Permalink
Add BWF XML export
Browse files Browse the repository at this point in the history
  • Loading branch information
johnf committed Jan 21, 2024
1 parent 00022db commit 9e1325b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/graphql/types/item_bwf_xml_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Types::ItemBwfXmlType < Types::BaseObject
field :full_identifier, String, null: false
field :collection_identifier, String, null: false
field :item_identifier, String, null: false
field :xml, String, null: false
field :created_at, GraphQL::Types::ISO8601DateTime
field :updated_at, GraphQL::Types::ISO8601DateTime
end
28 changes: 28 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ def item(full_identifier:)
collection.items.find_by(identifier: item_identifier)
end

field :item_bwf_xml, ItemBwfXmlType, 'Get the BWF XML for an item' do
argument :full_identifier, ID
end
def item_bwf_xml(full_identifier:)
raise(GraphQL::ExecutionError, 'Not authorised') unless context[:current_user]&.admin?

collection_identifier, item_identifier = full_identifier.split('-')
collection = Collection.find_by(identifier: collection_identifier)
raise(GraphQL::ExecutionError, 'Not found') unless collection

item = collection.items.find_by(identifier: item_identifier)
raise(GraphQL::ExecutionError, 'Not found') unless item

warden = Warden::Proxy.new({}, Warden::Manager.new({})).tap do |i|
i.set_user(context[:current_user], scope: :user)
end
item_renderer = ItemsController.renderer.new('warden' => warden)

{
full_identifier: item.full_identifier,
collection_identifier: collection.identifier,
item_identifier: item.identifier,
xml: item_renderer.render('items/show_bwf', formats: [:xml], assigns: { item: }),
created_at: item.created_at,
updated_at: item.updated_at
}
end

field :items, Types::ItemResultType, null: true do
argument :limit, Integer, default_value: 10, required: false
argument :page, Integer, default_value: 1, required: false
Expand Down
3 changes: 3 additions & 0 deletions app/models/essence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#
# * `index_essences_on_item_id`:
# * **`item_id`**
# * `index_essences_on_item_id_and_filename` (_unique_):
# * **`item_id`**
# * **`filename`**
#

class Essence < ApplicationRecord
Expand Down
31 changes: 31 additions & 0 deletions app/views/items/show_bwf.xml.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
%REPORT
%BEXTDATA
%QUALITYREPORT
%BASICDATA
%ARCHIVENUMBER= @item.full_identifier
%TITLE= @item.title
%OPERATOR= @item.operator_name
%QUALITYEVENTS
%EVENT
%TYPE IngestNotes0
%COMMENT= @item.ingest_notes
%SAMPLECOUNT 0

%Core
%Description
= "# Notes
= "Reference: https://catalog.paradisec.org.au/repository/#{@item.collection.identifier}/#{@item.identifier}"
= ""
= "Description: #{@item.description}."
= ""

- unless @item.subject_languages.empty?
= "Language: \"#{@item.subject_languages.first.name}\" #{@item.subject_languages.first.code};"

- unless @item.countries.empty?
= "Country: #{@item.countries.first.code};"

%Originator= @item.collector_name
%OriginationDate= @item.originated_on
%BextVersion 1
%CodingHistory A=PCM,F=96000,W=24,M=stereo,T=Paragest Pipeline
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@
config.active_job.queue_adapter = :inline

config.action_mailer.default_url_options = { host: 'example.com' }

config.hosts << 'www.example.com'
end
54 changes: 54 additions & 0 deletions spec/graphql/item_bwf_xml_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'rails_helper'

describe 'graphql' do
describe 'item_bwf_xml' do
let(:user) { create(:user) }
let(:admin_user) { create(:admin_user) }
let(:item) { create(:item) }

let(:query) do
<<-GRAPHQL
query GetItemBwfXmlQuery($fullIdentifier: ID!) {
itemBwfXml(fullIdentifier: $fullIdentifier) {
fullIdentifier
itemIdentifier
collectionIdentifier
xml
createdAt
updatedAt
}
}
GRAPHQL
end

context 'when standard user' do
it 'loads bwf xml' do
result = NabuSchema.execute(query, variables: { fullIdentifier: item.full_identifier })
expect(result['errors'].first['message']).to eq('Not authorised')
end
end

context 'when admin' do
it 'loads bwf xml' do
result = NabuSchema.execute(query, variables: { fullIdentifier: item.full_identifier },
context: { current_user: admin_user })

item_result = result['data']['itemBwfXml']
expect(item_result['itemIdentifier']).to eq(item.identifier)
end

it 'Missing item' do
result = NabuSchema.execute(query, variables: { fullIdentifier: 'FOO-001' },
context: { current_user: admin_user })
expect(result['errors'].first['message']).to eq('Not found')
end
end

context 'when not logged in' do
it 'throws an error' do
result = NabuSchema.execute(query, variables: { fullIdentifier: item.full_identifier })
expect(result['errors'].first['message']).to eq('Not authorised')
end
end
end
end
3 changes: 3 additions & 0 deletions spec/models/essence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#
# * `index_essences_on_item_id`:
# * **`item_id`**
# * `index_essences_on_item_id_and_filename` (_unique_):
# * **`item_id`**
# * **`filename`**
#

require 'rails_helper'
Expand Down

0 comments on commit 9e1325b

Please sign in to comment.