20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'backend/app/lib/bulk_import/digital_object_handler.rb', line 20
def create(
title,
id,
publish,
level,
digital_object_type,
restrictions,
dates,
notes,
extents,
subjects,
linked_agents,
archival_object,
report,
representative_file_version = nil,
non_representative_file_version = nil
)
digital_object = nil
digital_object_instance = nil
if @validate_only
if archival_object.nil?
archival_object = JSONModel(:archival_object).new._always_valid!
archival_object.title = "random title"
archival_object.ref_id = "VAL#{rand(1000000)}"
end
end
osn = id || SecureRandom.hex
if !check_digital_id(osn)
raise BulkImportException.new(I18n.t("bulk_import.error.dig_obj_unique", :id => osn))
end
errors = []
if representative_file_version
errors << validate_enum(representative_file_version[:file_format_name], @file_format_names, report)
errors << validate_enum(representative_file_version[:use_statement], @file_use_statement, report)
end
if non_representative_file_version
errors << validate_enum(non_representative_file_version[:file_format_name], @file_format_names, report)
errors << validate_enum(non_representative_file_version[:use_statement], @file_use_statement, report)
end
return nil unless errors.compact.empty?
files = []
files.push JSONModel(:file_version).from_hash(representative_file_version) if representative_file_version
files.push JSONModel(:file_version).from_hash(non_representative_file_version) if non_representative_file_version
digital_object = JSONModel(:digital_object).new._always_valid!
digital_object.title = title.nil? ? archival_object.display_string : title
digital_object.digital_object_id = osn
digital_object.file_versions = files if files.any?
digital_object.publish = publish
digital_object.level = level
digital_object.digital_object_type = digital_object_type
digital_object.restrictions = restrictions
digital_object.dates = dates
digital_object.notes = notes
digital_object.extents = extents
subjects.each { |subject| digital_object.subjects.push({ "ref" => subject.uri }) }
digital_object.linked_agents = linked_agents
begin
digital_object = save(digital_object, DigitalObject)
rescue JSONModel::ValidationException => ve
report.add_errors(I18n.t("bulk_import.error.dig_validation", :err => ve.errors))
return nil
rescue Exception => e
raise e
end
report.add_info(I18n.t(@create_key, :what => I18n.t("bulk_import.dig"), :id => "'#{digital_object.title}' #{digital_object.uri} [#{digital_object.digital_object_id}]"))
digital_object_instance = JSONModel(:instance).new._always_valid!
digital_object_instance.instance_type = "digital_object"
digital_object_instance.digital_object = { "ref" => @validate_only ? "http://x" : digital_object.uri }
digital_object_instance
end
|