8
9
10
11
12
13
14
15
16
17
18
19
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/dor/services/registration_service.rb', line 8
def register_object(params = {})
Dor.ensure_models_loaded!
[:object_type, :label].each do |required_param|
raise Dor::ParameterError, "#{required_param.inspect} must be specified in call to #{name}.register_object" unless params[required_param]
end
metadata_source=params[:metadata_source]
if params[:label].length<1 && (metadata_source=='label' || metadata_source=='none')
raise Dor::ParameterError, "label cannot be empty to call #{name}.register_object"
end
object_type = params[:object_type]
item_class = Dor.registered_classes[object_type]
raise Dor::ParameterError, "Unknown item type: '#{object_type}'" if item_class.nil?
content_model = params[:content_model]
admin_policy = params[:admin_policy]
label = params[:label]
source_id = params[:source_id] || {}
other_ids = params[:other_ids] || {}
tags = params[:tags] || []
parent = params[:parent]
collection = params[:collection]
pid = nil
metadata_source=params[:metadata_source]
if params[:pid]
pid = params[:pid]
existing_pid = SearchService.query_by_id(pid).first
unless existing_pid.nil?
raise Dor::DuplicateIdError.new(existing_pid), "An object with the PID #{pid} has already been registered."
end
else
pid = Dor::SuriService.mint_id
end
rights=nil
if params[:rights]
rights=params[:rights]
unless ['world','stanford','dark','default','none'].include? rights
raise Dor::ParameterError,"Unknown rights setting" + rights + "when calling #{name}.register_object"
end
end
source_id_string = [source_id.keys.first,source_id[source_id.keys.first]].compact.join(':')
unless source_id.empty?
existing_pid = SearchService.query_by_id("#{source_id_string}").first
unless existing_pid.nil?
raise Dor::DuplicateIdError.new(existing_pid), "An object with the source ID '#{source_id_string}' has already been registered."
end
end
if (other_ids.has_key?(:uuid) || other_ids.has_key?('uuid')) == false
other_ids[:uuid] = UUIDTools::UUID.timestamp_create.to_s
end
short_label=label
if label.length>254
short_label=label[0,254]
end
apo_object = Dor.find(admin_policy, :lightweight => true)
adm_xml = apo_object.administrativeMetadata.ng_xml
new_item = item_class.new(:pid => pid)
new_item.label = short_label
idmd = new_item.identityMetadata
idmd.sourceId = source_id_string
idmd.add_value(:objectId, pid)
idmd.add_value(:objectCreator, 'DOR')
idmd.add_value(:objectLabel, label)
idmd.add_value(:objectType, object_type)
other_ids.each_pair { |name,value| idmd.add_otherId("#{name}:#{value}") }
tags.each { |tag| idmd.add_value(:tag, tag) }
new_item.admin_policy_object = apo_object
adm_xml.xpath('/administrativeMetadata/relationships/*').each do |rel|
short_predicate = ActiveFedora::RelsExtDatastream.short_predicate rel.namespace.href+rel.name
if short_predicate.nil?
ix = 0
ix += 1 while ActiveFedora::Predicates.predicate_mappings[rel.namespace.href].has_key?(short_predicate = :"extra_predicate_#{ix}")
ActiveFedora::Predicates.predicate_mappings[rel.namespace.href][short_predicate] = rel.name
end
new_item.add_relationship short_predicate, rel['rdf:resource']
end
if collection
new_item.add_collection(collection)
end
if(rights && ['item','collection'].include?(object_type) )
rights_xml=apo_object.defaultObjectRights.ng_xml
new_item.datastreams['rightsMetadata'].content=rights_xml.to_s
new_item.set_read_rights(rights)
end
if(metadata_source=='label')
ds=new_item.build_datastream('descMetadata');
builder = Nokogiri::XML::Builder.new { |xml|
xml.mods( 'xmlns' => 'http://www.loc.gov/mods/v3', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',:version => '3.3', "xsi:schemaLocation" => 'http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-3.xsd'){
xml.titleInfo{
xml.title label
}
}
}
ds.content=builder.to_xml
end
workflow_priority = params[:workflow_priority] ? params[:workflow_priority].to_i : 0
Array(params[:seed_datastream]).each { |datastream_name| new_item.build_datastream(datastream_name) }
Array(params[:initiate_workflow]).each { |workflow_id| new_item.initialize_workflow(workflow_id, !new_item.new_object?, workflow_priority)}
new_item.assert_content_model
new_item.save
begin
new_item.update_index if ::ENABLE_SOLR_UPDATES
rescue StandardError => e
Dor.logger.warn "Dor::RegistrationService.register_object failed to update solr index for #{new_item.pid}: #<#{e.class.name}: #{e.message}>"
end
(new_item)
end
|