Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Add support for a @ROLE attribute on a contentMetadata's file element
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 6, 2018
1 parent 3a50dd8 commit 8eb9fa1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/dor/datastreams/content_metadata_ds.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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']
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 18 additions & 3 deletions spec/datastreams/content_metadata_ds_spec.rb
Expand Up @@ -14,7 +14,7 @@
<checksum type="md5">3d3ff46d98f3d517d0bf086571e05c18</checksum>
<checksum type="sha1">ca1eb0edd09a21f9dd9e3a89abc790daf4d04916</checksum>
</file>
<file format="GIF" id="gw177fc7976_05_0001.gif" mimetype="image/gif" preserve="no" publish="no" shelve="no" size="4128877">
<file format="GIF" id="gw177fc7976_05_0001.gif" mimetype="image/gif" preserve="no" publish="no" shelve="no" size="4128877" role="derivative">
<imageData height="4580" width="5939"/>
<checksum type="md5">406d5d80fdd9ecc0352d339badb4a8fb</checksum>
<checksum type="sha1">61940d4fad097cba98a3e9dd9f12a90dde0be1ac</checksum>
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -138,20 +150,22 @@
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
expect(file['shelve' ]).to eq('no')
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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8eb9fa1

Please sign in to comment.