From 8eb9fa1b1683b3494135307f0c3e585ad86629be Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Tue, 6 Feb 2018 11:59:46 -0800 Subject: [PATCH] Add support for a @role attribute on a contentMetadata's file element --- lib/dor/datastreams/content_metadata_ds.rb | 13 ++++++++++-- spec/datastreams/content_metadata_ds_spec.rb | 21 +++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/dor/datastreams/content_metadata_ds.rb b/lib/dor/datastreams/content_metadata_ds.rb index be9d6f64..e3a040bb 100644 --- a/lib/dor/datastreams/content_metadata_ds.rb +++ b/lib/dor/datastreams/content_metadata_ds.rb @@ -19,6 +19,7 @@ class ContentMetadataDS < ActiveFedora::OmDatastream t.mimeType :path => { :attribute => 'mimeType' }, :index_as => [:displayable] t.dataType :path => { :attribute => 'dataType' }, :index_as => [:displayable] t.size :path => { :attribute => 'size' }, :index_as => [:displayable] # , :data_type => :long + t.role :path => { :attribute => 'role' }, :index_as => [:not_searchable] t.shelve :path => { :attribute => 'shelve' }, :index_as => [:not_searchable] # , :data_type => :boolean t.publish :path => { :attribute => 'publish' }, :index_as => [:not_searchable] # , :data_type => :boolean t.preserve :path => { :attribute => 'preserve' }, :index_as => [:not_searchable] # , :data_type => :boolean @@ -85,6 +86,7 @@ def to_solr(solr_doc = {}, *args) shelved_size = 0 counts = Hash.new(0) # default count is zero resource_type_counts = Hash.new(0) # default count is zero + file_roles = ::Set.new mime_types = ::Set.new first_shelved_image = nil @@ -100,6 +102,7 @@ def to_solr(solr_doc = {}, *args) first_shelved_image ||= file['id'] if file['id'] =~ /jp2$/ end mime_types << file['mimetype'] + file_roles << file['role'] if file['role'] end end solr_doc['content_type_ssim' ] = doc.root['type'] @@ -110,6 +113,7 @@ def to_solr(solr_doc = {}, *args) solr_doc['preserved_size_dbtsi' ] = preserved_size # double (trie) to support very large sizes solr_doc['shelved_size_dbtsi' ] = shelved_size # double (trie) to support very large sizes solr_doc['resource_types_ssim' ] = resource_type_counts.keys if resource_type_counts.size > 0 + solr_doc['content_file_roles_ssim' ] = file_roles.to_a if file_roles.size > 0 resource_type_counts.each do |key, count| solr_doc["#{key}_resource_count_itsi"] = count end @@ -146,6 +150,7 @@ def add_file(file, resource_name) end file_node['size' ] = file[:size ] if file[:size ] file_node['mimetype'] = file[:mime_type] if file[:mime_type] + file_node['role'] = file[:role] if file[:role] file_node end @@ -207,6 +212,7 @@ def add_resource(files, resource_name, position, type = 'file') file_node.add_child(checksum_node) } file_node['size'] = file[:size] if file[:size] + file_node['role'] = file[:role] if file[:role] end ng_xml.search('//contentMetadata').first.add_child(node) node @@ -236,12 +242,15 @@ def remove_file(file_name) # @param [String] publish # @param [String] shelve # @param [String] preserve - def update_attributes(file_name, publish, shelve, preserve) + def update_attributes(file_name, publish, shelve, preserve, attributes = {}) self.ng_xml_will_change! file_node = ng_xml.search('//file[@id=\'' + file_name + '\']').first file_node['publish' ] = publish file_node['shelve' ] = shelve file_node['preserve'] = preserve + attributes.each do |key, value| + file_node[key] = value + end end # @param file [Object] some hash-like file @@ -261,7 +270,7 @@ def update_file(file, old_file_id) checksum_node.content = file[algo] } - [:size, :shelve, :preserve, :publish].each { |x| + [:size, :shelve, :preserve, :publish, :role].each { |x| file_node[x.to_s] = file[x] if file[x] } end diff --git a/spec/datastreams/content_metadata_ds_spec.rb b/spec/datastreams/content_metadata_ds_spec.rb index e14498ed..04dc211d 100644 --- a/spec/datastreams/content_metadata_ds_spec.rb +++ b/spec/datastreams/content_metadata_ds_spec.rb @@ -14,7 +14,7 @@ 3d3ff46d98f3d517d0bf086571e05c18 ca1eb0edd09a21f9dd9e3a89abc790daf4d04916 - + 406d5d80fdd9ecc0352d339badb4a8fb 61940d4fad097cba98a3e9dd9f12a90dde0be1ac @@ -105,6 +105,18 @@ expect(checksum.content).to eq(checksum['type'] == 'md5' ? '123456' : '56789') end end + + it 'should add a file with a role="transcription"' do + files = [ + @file.merge(:name => 'transcription.txt', :role => 'transcription', :size => '23456', :preserve => 'yes') + ] + @cm.add_resource(files, 'resource', 1, 'page') + nodes = @cm.ng_xml.search('//resource[@id=\'resource\']/file') + expect(nodes.length).to eq(1) + node = nodes.first + expect(node['id' ]).to eq('transcription.txt') + expect(node['role' ]).to eq('transcription') + end end describe 'remove_resource' do @@ -128,7 +140,7 @@ end describe 'add_file' do it 'should add a file to the resource' do - @cm.add_file(@file, '0001') + @cm.add_file(@file.merge(role: 'some-role'), '0001') xml = @cm.ng_xml hits = xml.search('//resource[@id=\'0001\']/file') expect(hits.length).to eq(4) @@ -138,13 +150,14 @@ expect(new_file['publish' ]).to eq('no') expect(new_file['preserve']).to eq('no') expect(new_file['size' ]).to eq('12345') + expect(new_file['role' ]).to eq('some-role') expect(@cm).to be_changed end end describe 'update_file' do it 'should modify an existing file record' do - @cm.update_file(@file, 'gw177fc7976_05_0001.jp2') + @cm.update_file(@file.merge(role: 'some-role'), 'gw177fc7976_05_0001.jp2') file = @cm.ng_xml.search('//file[@id=\'new_file.jp2\']') expect(file.length).to eq(1) file = file.first @@ -152,6 +165,7 @@ expect(file['publish' ]).to eq('no') expect(file['preserve']).to eq('no') expect(file['size' ]).to eq('12345') + expect(file['role' ]).to eq('some-role') end it 'should error out if there isnt an existing record to modify' do expect { @cm.update_file(@file, 'gw177fc7976_05_0001_different.jp2')}.to raise_error(StandardError) @@ -201,6 +215,7 @@ expected = { 'content_type_ssim' => 'map', 'content_file_mimetypes_ssim' => ['image/jp2', 'image/gif', 'image/tiff'], + 'content_file_roles_ssim' => ['derivative'], 'shelved_content_file_count_itsi' => 1, 'resource_count_itsi' => 1, 'content_file_count_itsi' => 3,