From 9e1325bff540b664a7fa17ce023f2d88275f5f64 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Sun, 21 Jan 2024 17:03:04 +1100 Subject: [PATCH] Add BWF XML export --- app/graphql/types/item_bwf_xml_type.rb | 8 ++++ app/graphql/types/query_type.rb | 28 +++++++++++++ app/models/essence.rb | 3 ++ app/views/items/show_bwf.xml.haml | 31 ++++++++++++++ config/environments/test.rb | 2 + spec/graphql/item_bwf_xml_query_spec.rb | 54 +++++++++++++++++++++++++ spec/models/essence_spec.rb | 3 ++ 7 files changed, 129 insertions(+) create mode 100644 app/graphql/types/item_bwf_xml_type.rb create mode 100644 app/views/items/show_bwf.xml.haml create mode 100644 spec/graphql/item_bwf_xml_query_spec.rb diff --git a/app/graphql/types/item_bwf_xml_type.rb b/app/graphql/types/item_bwf_xml_type.rb new file mode 100644 index 00000000..c7539840 --- /dev/null +++ b/app/graphql/types/item_bwf_xml_type.rb @@ -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 diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 5dc0214e..7710e8f4 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -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 diff --git a/app/models/essence.rb b/app/models/essence.rb index 150ac2f1..09d2ff5e 100644 --- a/app/models/essence.rb +++ b/app/models/essence.rb @@ -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 diff --git a/app/views/items/show_bwf.xml.haml b/app/views/items/show_bwf.xml.haml new file mode 100644 index 00000000..bc5e1ed2 --- /dev/null +++ b/app/views/items/show_bwf.xml.haml @@ -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 diff --git a/config/environments/test.rb b/config/environments/test.rb index 77e3cd1d..de3e1ed9 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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 diff --git a/spec/graphql/item_bwf_xml_query_spec.rb b/spec/graphql/item_bwf_xml_query_spec.rb new file mode 100644 index 00000000..078aac27 --- /dev/null +++ b/spec/graphql/item_bwf_xml_query_spec.rb @@ -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 diff --git a/spec/models/essence_spec.rb b/spec/models/essence_spec.rb index 34bc0288..5eeba196 100644 --- a/spec/models/essence_spec.rb +++ b/spec/models/essence_spec.rb @@ -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'