app/models/ddr/batch/batch_object.rb in ddr-batch-1.7.2 vs app/models/ddr/batch/batch_object.rb in ddr-batch-2.0.0.alpha.1
- old
+ new
@@ -5,13 +5,11 @@
class BatchObject < ActiveRecord::Base
belongs_to :batch, inverse_of: :batch_objects
has_many :batch_object_attributes, -> { order "id ASC" }, inverse_of: :batch_object, dependent: :destroy
has_many :batch_object_datastreams, inverse_of: :batch_object, dependent: :destroy
- has_many :batch_object_messages, inverse_of: :batch_object, dependent: :destroy
has_many :batch_object_relationships, inverse_of: :batch_object, dependent: :destroy
- has_many :batch_object_roles, inverse_of: :batch_object, dependent: :destroy
VERIFICATION_PASS = "PASS"
VERIFICATION_FAIL = "FAIL"
EVENT_SUMMARY = <<-EOS
@@ -19,12 +17,10 @@
Batch object database id: %{batch_id}
Batch object identifier: %{identifier}
Model: %{model}
EOS
- ProcessingResultsMessage = Struct.new(:level, :message)
-
def self.pid_from_identifier(identifier, batch_id)
query = "identifier = :identifier"
query << " and batch_id = :batch_id" if batch_id
params = { :identifier => identifier }
params[:batch_id] = batch_id if batch_id
@@ -77,11 +73,11 @@
def validate_datastreams
errs = []
batch_object_datastreams.each do |d|
if model_datastream_keys.present?
- unless model_datastream_keys.include?(d.name)
+ unless model_datastream_keys.include?(d.name.to_sym)
errs << "#{@error_prefix} Invalid datastream name for #{model}: #{d.name}"
end
end
unless BatchObjectDatastream::PAYLOAD_TYPES.include?(d.payload_type)
errs << "#{@error_prefix} Invalid payload type for #{d.name} datastream: #{d.payload_type}"
@@ -113,15 +109,16 @@
if r[:object_type].eql?(BatchObjectRelationship::OBJECT_TYPE_PID)
if batch.present? && batch.found_pids.keys.include?(r[:object])
obj_model = batch.found_pids[r[:object]]
else
begin
- obj_model = SolrDocument.find(r[:object]).active_fedora_model
+ obj = ActiveFedora::Base.find(r[:object], :cast => true)
+ obj_model = obj.class.name
if batch.present?
- batch.add_found_pid(r[:object], obj_model)
+ batch.add_found_pid(obj.pid, obj_model)
end
- rescue SolrDocument::NotFound
+ rescue ActiveFedora::ObjectNotFoundError
pid_in_batch = false
if batch.present?
if batch.pre_assigned_pids.include?(r[:object])
pid_in_batch = true
end
@@ -154,11 +151,11 @@
rescue ActiveFedora::ObjectNotFound
verifications["Object exists in repository"] = VERIFICATION_FAIL
else
verifications["Object exists in repository"] = VERIFICATION_PASS
verifications["Object is correct model"] = verify_model(repo_object) if model
- verifications["Object has correct label"] = verify_label(repo_object) if label
+ # verifications["Object has correct label"] = verify_label(repo_object) if label
unless batch_object_attributes.empty?
batch_object_attributes.each do |a|
if a.operation == BatchObjectAttribute::OPERATION_ADD
verifications["#{a.name} attribute set correctly"] = verify_attribute(repo_object, a)
end
@@ -173,15 +170,10 @@
unless batch_object_relationships.empty?
batch_object_relationships.each do |r|
verifications["#{r.name} relationship is correct"] = verify_relationship(repo_object, r)
end
end
- unless batch_object_roles.empty?
- batch_object_roles.each do |r|
- verifications["#{r.role_scope} #{r.role_type} #{r.agent} role is correct"] = verify_role(repo_object, r)
- end
- end
result = Ddr::Actions::FixityCheck.execute repo_object
verifications["Fixity check"] = result.success ? VERIFICATION_PASS : VERIFICATION_FAIL
end
verifications
end
@@ -196,17 +188,22 @@
rescue NameError
return VERIFICATION_FAIL
end
end
- def verify_label(repo_object)
- repo_object.label.eql?(label) ? VERIFICATION_PASS : VERIFICATION_FAIL
- end
-
+ # def verify_label(repo_object)
+ # repo_object.label.eql?(label) ? VERIFICATION_PASS : VERIFICATION_FAIL
+ # end
+ #
def verify_attribute(repo_object, attribute)
- repo_object.datastreams[attribute.datastream].values(attribute.name).include?(attribute.value.strip) ?
- VERIFICATION_PASS : VERIFICATION_FAIL
+ verified = case attribute.datastream
+ when 'descMetadata'
+ repo_object.descMetadata.values(attribute.name).include?(attribute.value)
+ when 'adminMetadata'
+ repo_object.adminMetadata.values(attribute.name).include?(attribute.value)
+ end
+ verified ? VERIFICATION_PASS : VERIFICATION_FAIL
end
def verify_datastream(repo_object, datastream)
if repo_object.datastreams.include?(datastream.name) &&
repo_object.datastreams[datastream.name].has_content?
@@ -234,37 +231,27 @@
else
VERIFICATION_FAIL
end
end
- def verify_role(repo_object, role)
- role_hash = { "role_type"=>[ role.role_type ], "agent"=>[ role.agent ], "scope"=>[ role.role_scope ] }
- repo_object.roles.role_set.map(&:to_h).include?(role_hash) ? VERIFICATION_PASS : VERIFICATION_FAIL
- end
-
def add_attribute(repo_object, attribute)
- repo_object.datastreams[attribute.datastream].add_value(attribute.name, attribute.value)
+ repo_object.send(attribute.datastream).add_value(attribute.name, attribute.value)
return repo_object
end
def clear_attribute(repo_object, attribute)
- repo_object.datastreams[attribute.datastream].set_values(attribute.name, nil)
+ repo_object.send(attribute.datastream).set_values(attribute.name, nil)
return repo_object
end
def clear_attributes(repo_object, attribute)
- repo_object.datastreams[attribute.datastream].class.term_names.each do |term|
- repo_object.datastreams[attribute.datastream].set_values(term, nil) if repo_object.datastreams[attribute.datastream].values(term)
+ Ddr::Models::DescriptiveMetadata.unqualified_names.each do |term|
+ repo_object.descMetadata.set_values(term, nil) if repo_object.descMetadata.values(term)
end
return repo_object
end
- def add_role(repo_object, role)
- repo_object.roles.grant(scope: role.role_scope, type: role.role_type, agent: role.agent)
- return repo_object
- end
-
def populate_datastream(repo_object, datastream)
case datastream[:payload_type]
when BatchObjectDatastream::PAYLOAD_TYPE_BYTES
ds_content = datastream[:payload]
if repo_object.datastreams[datastream[:name]].is_a? ActiveFedora::RDFDatastream
@@ -276,10 +263,14 @@
ds_content = set_rdf_subject(repo_object, File.read(datastream[:payload]))
mime_type = "application/n-triples"
else
ds_content = File.new(datastream[:payload])
end
- repo_object.add_file(ds_content, datastream[:name], mime_type: mime_type)
+ file_name = File.basename(datastream[:payload])
+ dsid = datastream[:name]
+ opts = { filename: file_name }
+ opts.merge({ mime_type: mime_type }) if mime_type
+ repo_object.add_file(ds_content, path: dsid)
end
return repo_object
end
def add_relationship(repo_object, relationship)