Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce statefullness of DigitalObject #569

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 16 additions & 39 deletions app/lib/pre_assembly/digital_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ class DigitalObject
to: :bundle

attr_accessor :container,
:content_md_xml,
:label,
:manifest_row,
:object_files,
:pre_assem_finished,
:source_id,
:technical_md_xml
:source_id

attr_writer :dor_object

Expand All @@ -36,9 +34,7 @@ def initialize(bundle, params = {})
end

def setup
self.label = 'Unknown' # used for registration when no label is provided in the manifest
self.content_md_xml = ''
self.technical_md_xml = ''
self.label = 'Unknown' # used for registration when no label is provided in the manifest
end

def stager(source, destination)
Expand Down Expand Up @@ -147,12 +143,6 @@ def stage_files
end
end

# Technical metadata combined file for Media.
def generate_technical_metadata
create_technical_metadata
write_technical_metadata
end

# create technical metadata for media projects only
def create_technical_metadata
return unless content_md_creation == 'media_cm_style'
Expand All @@ -171,46 +161,33 @@ def create_technical_metadata
tm.root << tech_md_xml.root
end
FileUtils.cd(current_directory)
self.technical_md_xml = tm.to_xml
tm.to_xml
end

# write technical metadata out to a file only if it exists
def write_technical_metadata
# write technical metadata out for media projects
def generate_technical_metadata
technical_md_xml = create_technical_metadata
return if technical_md_xml.blank?
file_name = assembly_directory.technical_metadata_file
log " - write_technical_metadata_xml(#{file_name})"
File.open(file_name, 'w') { |fh| fh.puts technical_md_xml }
end

# Content metadata.
# Write contentMetadata.xml file
def generate_content_metadata
create_content_metadata
write_content_metadata
File.open(assembly_directory.content_metadata_file, 'w') { |fh| fh.puts create_content_metadata }
end

# Invoke the contentMetadata creation method used by the project
def create_content_metadata
if content_md_creation == 'media_cm_style'
self.content_md_xml = media_manifest.generate_cm(druid.id)
else
# otherwise use the content metadata generation gem
params = { druid: druid.id, objects: content_object_files, add_exif: false, bundle: content_md_creation.to_sym, style: content_md_creation_style }
self.content_md_xml = Assembly::ContentMetadata.create_content_metadata(params)
end
end

# write content metadata out to a file
def write_content_metadata
file_name = assembly_directory.content_metadata_file

File.open(file_name, 'w') { |fh| fh.puts content_md_xml }

# NOTE: This is being skipped because it now removes empty nodes, and we need an a node like this: <file id="filename" /> when first starting with contentMetadat
# If this node gets removed, then nothing works. - Peter Mangiafico, October 3, 2015
# mods_xml_doc = Nokogiri::XML(@content_md_xml) # create a nokogiri doc
# normalizer = Normalizer.new
# normalizer.normalize_document(mods_xml_doc.root) # normalize it
# File.open(file_name, 'w') { |fh| fh.puts mods_xml_doc.to_xml } # write out normalized result
return media_manifest.generate_cm(druid.id) if content_md_creation == 'media_cm_style'

# otherwise use the content metadata generation gem
Assembly::ContentMetadata.create_content_metadata(druid: druid.id,
objects: content_object_files,
add_exif: false,
bundle: content_md_creation.to_sym,
style: content_md_creation_style)
end

# Object files that should be included in content metadata.
Expand Down
11 changes: 4 additions & 7 deletions spec/lib/pre_assembly/digital_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def add_object_files(extension = 'tif')
allow(bc).to receive(:content_structure).and_return('simple_image')
add_object_files('tif')
add_object_files('jp2')
object.create_content_metadata
end

it 'content_object_files() should filter @object_files correctly' do
Expand All @@ -174,7 +173,7 @@ def add_object_files(extension = 'tif')
end

it 'generates the expected xml text' do
expect(noko_doc(object.content_md_xml)).to be_equivalent_to exp_xml
expect(noko_doc(object.create_content_metadata)).to be_equivalent_to exp_xml
end

it 'is able to write the content_metadata XML to a file' do
Expand All @@ -186,7 +185,7 @@ def add_object_files(extension = 'tif')
assembly_directory.create_object_directories
file_name = object.assembly_directory.content_metadata_file
expect(File.exist?(file_name)).to eq(false)
object.write_content_metadata
object.generate_content_metadata
expect(noko_doc(File.read(file_name))).to be_equivalent_to exp_xml
end
end
Expand Down Expand Up @@ -225,11 +224,10 @@ def add_object_files(extension = 'tif')
allow(bc).to receive(:content_md_creation).and_return('filename')
add_object_files('tif')
add_object_files('jp2')
object.create_content_metadata
end

it 'generates the expected xml text' do
expect(noko_doc(object.content_md_xml)).to be_equivalent_to(exp_xml)
expect(noko_doc(object.create_content_metadata)).to be_equivalent_to(exp_xml)
end
end

Expand Down Expand Up @@ -271,7 +269,6 @@ def add_object_files(extension = 'tif')
allow(object).to receive(:content_type_tag).and_return('File') # this is what the object tag says, so we should get the file type out
add_object_files('tif')
add_object_files('jp2')
object.create_content_metadata
end

it 'content_object_files() should filter @object_files correctly' do
Expand All @@ -294,7 +291,7 @@ def add_object_files(extension = 'tif')

it 'generates the expected xml text' do
expect(object.content_md_creation_style).to eq(:file)
expect(noko_doc(object.content_md_xml)).to be_equivalent_to(exp_xml)
expect(noko_doc(object.create_content_metadata)).to be_equivalent_to(exp_xml)
end
end

Expand Down
30 changes: 12 additions & 18 deletions spec/lib/pre_assembly/media_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
end

it 'generates technicalMetadata for Media by combining all existing _techmd.xml files' do
dobj1.create_technical_metadata
exp_xml = noko_doc(dobj1.technical_md_xml)
exp_xml = noko_doc(dobj1.create_technical_metadata)
expect(exp_xml.css('technicalMetadata').size).to eq(1) # one top level node
expect(exp_xml.css('Mediainfo').size).to eq(2) # two Mediainfo nodes
counts = exp_xml.css('Count')
Expand All @@ -29,10 +28,8 @@
end

it 'generates content metadata from a Media manifest with no thumb columns' do
dobj1.create_content_metadata
dobj2.create_content_metadata
expect(noko_doc(dobj1.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111)
expect(noko_doc(dobj2.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)
expect(noko_doc(dobj1.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111)
expect(noko_doc(dobj2.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)
end
end

Expand All @@ -41,30 +38,27 @@
media_manifest = described_class.new(csv_filename: 'media_manifest_with_thumb.csv', bundle_dir: bundle_dir)
dobj1 = setup_dobj('aa111aa1111', media_manifest)
dobj2 = setup_dobj('bb222bb2222', media_manifest)
dobj1.create_content_metadata
dobj2.create_content_metadata
expect(noko_doc(dobj1.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111_with_thumb)
expect(noko_doc(dobj2.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)

expect(noko_doc(dobj1.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111_with_thumb)
expect(noko_doc(dobj2.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)
end

it 'generates content metadata from a Media manifest with a thumb column set to true' do
media_manifest = described_class.new(csv_filename: 'media_manifest_with_thumb_true.csv', bundle_dir: bundle_dir)
dobj1 = setup_dobj('aa111aa1111', media_manifest)
dobj2 = setup_dobj('bb222bb2222', media_manifest)
dobj1.create_content_metadata
dobj2.create_content_metadata
expect(noko_doc(dobj1.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111_with_thumb)
expect(noko_doc(dobj2.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)

expect(noko_doc(dobj1.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111_with_thumb)
expect(noko_doc(dobj2.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)
end

it 'generates content metadata from a Media manifest with no thumbs when the thumb column is set to no' do
media_manifest = described_class.new(csv_filename: 'media_manifest_thumb_no.csv', bundle_dir: bundle_dir)
dobj1 = setup_dobj('aa111aa1111', media_manifest)
dobj2 = setup_dobj('bb222bb2222', media_manifest)
dobj1.create_content_metadata
dobj2.create_content_metadata
expect(noko_doc(dobj1.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111)
expect(noko_doc(dobj2.content_md_xml)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)

expect(noko_doc(dobj1.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_aa111aa1111)
expect(noko_doc(dobj2.create_content_metadata)).to be_equivalent_to noko_doc(exp_xml_object_bb222bb2222)
end
end

Expand Down