Class: Dor::RegistrationService

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/registration_service.rb

Class Method Summary collapse

Class Method Details

.check_source_id(source_id_string) ⇒ String

Returns the same qualified source:id for immediate use.

Parameters:

  • source_id_string (String)

    a fully qualified source:val or empty string

Returns:

  • (String)

    the same qualified source:id for immediate use

Raises:



26
27
28
29
30
31
32
33
# File 'lib/dor/services/registration_service.rb', line 26

def check_source_id(source_id_string)
  return '' if source_id_string == ''
  unless SearchService.query_by_id(source_id_string.to_s).first.nil?
    raise Dor::DuplicateIdError.new(source_id_string), "An object with the source ID '#{source_id_string}' has already been registered."
  end

  source_id_string
end

.create_from_request(params) ⇒ Object

Parameters:

  • params (Hash)

See Also:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dor/services/registration_service.rb', line 127

def create_from_request(params)
  other_ids = Array(params[:other_id]).map do |id|
    if id =~ /^symphony:(.+)$/
      "#{$1.length < 14 ? 'catkey' : 'barcode'}:#{$1}"
    else
      id
    end
  end

  if params[:label] == ':auto'
    params.delete(:label)
    params.delete('label')
     = Dor::MetadataService.resolvable(other_ids).first
    params[:label] = Dor::MetadataService.label_for()
  end

  dor_params = {
    pid: params[:pid],
    admin_policy: params[:admin_policy],
    content_model: params[:model],
    label: params[:label],
    object_type: params[:object_type],
    other_ids: ids_to_hash(other_ids),
    parent: params[:parent],
    source_id: ids_to_hash(params[:source_id]),
    tags: params[:tag] || [],
    seed_datastream: params[:seed_datastream],
    initiate_workflow: Array(params[:initiate_workflow]) + Array(params[:workflow_id]),
    rights: params[:rights],
    metadata_source: params[:metadata_source],
    collection: params[:collection],
    workflow_priority: params[:workflow_priority]
  }
  dor_params.delete_if { |_k, v| v.nil? }

  dor_obj = register_object(dor_params)
  pid = dor_obj.pid
  location = URI.parse(Dor::Config.fedora.safeurl.sub(/\/*$/, '/')).merge("objects/#{pid}").to_s
  dor_params.dup.merge(location: location, pid: pid)
end

.register_object(params = {}) ⇒ Object

Parameters:

  • params (Hash{Symbol => various}) (defaults to: {})

Options Hash (params):

  • :object_type (String)

    required

  • :label (String)

    required

  • :admin_policy (String)

    required

  • :metadata_source (String)
  • :rights (String)
  • :collection (String)
  • :source_id (Hash{String => String})

    Primary ID from another system, max one key/value pair!

  • :other_ids (Hash)

    including :uuid if known

  • :pid (String)

    Fully qualified PID if you don’t want one generated for you

  • :workflow_priority] (Integer)
  • :seed_datastream (Array<String>)

    datastream_names

  • :initiate_workflow (Array<String>)

    workflow_ids

  • :tags (Array)

Raises:



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
# File 'lib/dor/services/registration_service.rb', line 49

def register_object(params = {})
  %i[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
   = params[:metadata_source]
  raise Dor::ParameterError, "label cannot be empty to call #{name}.register_object" if params[:label].length < 1 && %w[label none].include?()

  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]
  # parent        = params[:parent]
  label         = params[:label]
  source_id     = params[:source_id] || {}
  other_ids     = params[:other_ids] || {}
  tags          = params[:tags] || []
  collection    = params[:collection]

  # Check for sourceId conflict *before* potentially minting PID
  source_id_string = check_source_id [source_id.keys.first, source_id[source_id.keys.first]].compact.join(':')
  pid = unduplicated_pid(params[:pid])

  raise ArgumentError, ":source_id Hash can contain at most 1 pair: recieved #{source_id.size}" if source_id.size > 1

  rights = nil
  if params[:rights]
    rights = params[:rights]
    raise Dor::ParameterError, "Unknown rights setting '#{rights}' when calling #{name}.register_object" unless rights == 'default' || RightsMetadataDS.valid_rights_type?(rights)
  end

  other_ids[:uuid] = UUIDTools::UUID.timestamp_create.to_s if (other_ids.key?(:uuid) || other_ids.key?('uuid')) == false
  apo_object = Dor.find(params[:admin_policy])
  new_item = item_class.new(pid: pid)
  new_item.label = label.length > 254 ? label[0, 254] : label
  idmd = new_item.
  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

  apo_object..ng_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].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
  new_item.add_collection(collection) if collection
  if rights && %w(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) unless rights == 'default' # already defaulted to default!
  end
  # create basic mods from the label
  (new_item, label) if  == 'label'

  workflow_priority = params[:workflow_priority] ? params[:workflow_priority].to_i : 0

  seed_datastreams(Array(params[:seed_datastream]), new_item)
  initiate_workflow(workflows: Array(params[:initiate_workflow]), item: new_item, priority: workflow_priority)

  new_item.class.ancestors.select { |x| x.respond_to?(:to_class_uri) && x != ActiveFedora::Base }.each do |parent_class|
    new_item.add_relationship(:has_model, parent_class.to_class_uri)
  end

  new_item.save
  new_item
end

.unduplicated_pid(pid = nil) ⇒ String

Returns a pid you can use immidately, either freshly minted or your checked value.

Parameters:

  • pid (String) (defaults to: nil)

    an ID to check, if desired. If not passed (or nil), a new ID is minted

Returns:

  • (String)

    a pid you can use immidately, either freshly minted or your checked value

Raises:



14
15
16
17
18
19
20
21
# File 'lib/dor/services/registration_service.rb', line 14

def unduplicated_pid(pid = nil)
  return Dor::SuriService.mint_id unless pid

  existing_pid = SearchService.query_by_id(pid).first
  raise Dor::DuplicateIdError.new(existing_pid), "An object with the PID #{pid} has already been registered." unless existing_pid.nil?

  pid
end