Module: AgentManager::Mixin::ClassMethods

Defined in:
backend/app/model/mixins/agent_manager.rb

Instance Method Summary collapse

Instance Method Details

#assemble_hash_fields(json) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'backend/app/model/mixins/agent_manager.rb', line 316

def assemble_hash_fields(json)
  fields = []

  json.dates_of_existence.each do |date|
    fields << hash_chunk(JSONModel(:structured_date_label).from_hash(date),
                         %w(date_type_structured date_label))
  end

  json.agent_contacts.each do |contact|
    fields << hash_chunk(JSONModel(:agent_contact).from_hash(contact),
                         %w(name salutation telephone address_1 address_2 address_3 city region country post_code email email_signature note))
  end

  json.external_documents.each do |doc|
    fields << hash_chunk(JSONModel(:external_document).from_hash(doc),
                         %w(title location))
  end

  json.notes.each do |note|
    note_json = note.clone
    note_json.delete("publish")
    note_json.delete("persistent_id")
    fields << note_json.to_json.to_s
  end

  name_model = my_agent_type[:name_model]

  name_fields = []
  json.names.each do |name|
    next if !name["authorized"]

    name_fields += name_model.assemble_hash_fields(name)
  end

  fields += name_fields.sort

  fields
end

#calculate_hash(json) ⇒ Object



356
357
358
359
360
361
# File 'backend/app/model/mixins/agent_manager.rb', line 356

def calculate_hash(json)
  fields = assemble_hash_fields(json)
  digest = Digest::SHA1.hexdigest(fields.sort.join('-'))

  digest
end

#combine_unauthorized_names(json) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'backend/app/model/mixins/agent_manager.rb', line 160

def combine_unauthorized_names(json)
  return if Array(json['names']).empty?

  name_model = my_agent_type[:name_model]

  # We'll use this to reorder our deduplicated list of names to match the
  # original input.
  original_ordering = json.names.map {|name|
    Digest::SHA1.hexdigest(name_model.assemble_hash_fields(name).sort.join('-'))
  }

  # Names in descending order of importance: authorized name first, then
  # the display name (if different to the authorized name), then the
  # others.
  ranked_names = json.names
                   .sort_by {|name| [
                               name['authorized'] ? 1 : 0,
                               name['is_display_name'] ? 1 : 0
                             ]}
                   .reverse

  # Keep the first occurrence of each name, prioritising the authorized
  # and display names.
  seen_names = []
  json.names = ranked_names.select {|name|
    name_hash = Digest::SHA1.hexdigest(name_model.assemble_hash_fields(name).sort.join('-'))

    if seen_names.include?(name_hash)
      false
    else
      seen_names << name_hash
      true
    end
  }

  # If the name marked for display was a duplicate of the authorized name,
  # it will have been dropped.  Make sure *something* is marked as the
  # display name.
  ensure_display_name(json)

  # Finally, reorder to match our original input.
  json.names.sort_by! {|name|
    name_hash = Digest::SHA1.hexdigest(name_model.assemble_hash_fields(name).sort.join('-'))
    original_ordering.index(name_hash)
  }
end

#create_from_json(json, opts = {}) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'backend/app/model/mixins/agent_manager.rb', line 282

def create_from_json(json, opts = {})
  self.ensure_authorized_name(json)
  self.ensure_display_name(json)
  self.combine_unauthorized_names(json)
  self.set_publish_for_subrecords_with_subjects(json)

  # Force validation to make sure we're left with a valid record after our
  # changes
  json.to_hash

  # Called for the sake of updating the JSON blob sent to the realtime indexer
  self.populate_display_name(json)

  if opts[:skip_sha]
    super(json, opts)
  else
    super(json, opts.merge(:agent_sha1 => calculate_hash(json)))
  end
end

#ensure_authorized_name(json) ⇒ Object



143
144
145
146
147
# File 'backend/app/model/mixins/agent_manager.rb', line 143

def ensure_authorized_name(json)
  if !Array(json['names']).empty? && json['names'].none? {|name| name['authorized']}
    json['names'][0]['authorized'] = true
  end
end

#ensure_display_name(json) ⇒ Object



150
151
152
153
154
155
156
157
# File 'backend/app/model/mixins/agent_manager.rb', line 150

def ensure_display_name(json)
  if !Array(json['names']).empty? && json['names'].none? {|name| name['is_display_name']}
    # If no display name was specified, take the authorized one as display
    # name.
    authorized_name = json['names'].find {|name| name['authorized']}
    authorized_name['is_display_name'] = true
  end
end

#ensure_exists(json, referrer) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'backend/app/model/mixins/agent_manager.rb', line 218

def ensure_exists(json, referrer)
  DB.attempt {
    self.ensure_authorized_name(json)

    authorized_name = json['names'].find {|name| name['authorized']}
    if authorized_name["authority_id"]
      authorized_name_id = NameAuthorityId.find(:authority_id => authorized_name["authority_id"])
      raise AgentManager::AuthorizedNameError, "Agent Authorized Name: Agent cannot have a authorized name with an existing authorized id" if authorized_name_id
    end

    self.create_from_json(json)
  }.and_if_constraint_fails {|exception|


    if exception.is_a? AgentManager::AuthorizedNameError
      authorized_name = json['names'].find {|name| name['authorized']}

      agent_type = json["jsonmodel_type"]
      name_type = authorized_name["jsonmodel_type"]

      agent = join(name_type.intern, "#{agent_type}_id".intern => "#{agent_type}__id".intern)
                .join(:name_authority_id, "#{name_type}_id".intern => "#{name_type}__id".intern )
                .where( Sequel.qualify(:name_authority_id, :authority_id) => authorized_name["authority_id"] )
                .where( Sequel.qualify( name_type.intern, :authorized) => 1 ).select_all(agent_type.intern).first


    elsif exception.message.end_with?(AGENT_MUST_BE_UNIQUE) || exception.message =~ AGENT_MUST_BE_UNIQUE_MYSQL_CONSTRAINT
      # If the agent already exists, find and reuse them
      agent = find_matching(json)

    else
      # If anything else went wrong, report it
      raise $!
    end

    if !agent
      # The agent exists but we can't find it.  This could mean it was
      # created in a currently running transaction.  Abort this one to trigger
      # a retry.
      Log.info("Agent '#{json.names}' seems to have been created by a currently running transaction.  Restarting this one.")
      sleep 5
      raise RetryTransaction.new
    end


    agent
  }
end

#find_matching(json) ⇒ Object



277
278
279
# File 'backend/app/model/mixins/agent_manager.rb', line 277

def find_matching(json)
  find(:agent_sha1 => calculate_hash(json))
end

#find_matching_id(json) ⇒ Object



268
269
270
271
272
273
274
# File 'backend/app/model/mixins/agent_manager.rb', line 268

def find_matching_id(json)
  authorized_id = json['names'].find {|name| name['authority_id']}
  existing_link = NameAuthorityId.find(:authority_id => authorized_id['authority_id'])
  existing_name_record = my_agent_type[:name_model].find(:id => existing_link[:"#{authorized_id['jsonmodel_type']}_id"])

  find(:id => existing_name_record[:"#{json['jsonmodel_type']}_id"])
end

#hash_chunk(rec, field_array) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
# File 'backend/app/model/mixins/agent_manager.rb', line 303

def hash_chunk(rec, field_array)
  field_array.map {|property|
      if !rec[property]
        ' '
      elsif rec.class.schema["properties"][property]["dynamic_enum"]
        enum = rec.class.schema["properties"][property]["dynamic_enum"]
        BackendEnumSource.id_for_value(enum, rec[property])
      else
        rec[property.to_s]
      end
    }.join('_')
end

#my_agent_typeObject



471
472
473
# File 'backend/app/model/mixins/agent_manager.rb', line 471

def my_agent_type
  AgentManager.agent_type_of(self)
end

#populate_display_name(json) ⇒ Object



138
139
140
# File 'backend/app/model/mixins/agent_manager.rb', line 138

def populate_display_name(json)
  json.display_name = json['names'].find {|name| name['is_display_name']}
end

#register_agent_type(opts) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'backend/app/model/mixins/agent_manager.rb', line 364

def register_agent_type(opts)
  AgentManager.register_agent_type(self, opts)

  self.one_to_many my_agent_type[:name_type]

  self.def_nested_record(:the_property => :names,
                         :contains_records_of_type => my_agent_type[:name_type],
                         :corresponding_to_association => my_agent_type[:name_type])

  self.one_to_many :agent_contact

  self.def_nested_record(:the_property => :agent_contacts,
                         :contains_records_of_type => :agent_contact,
                         :corresponding_to_association => :agent_contact)

  self.one_to_many :agent_record_control, :class => "AgentRecordControl"

  self.def_nested_record(:the_property => :agent_record_controls,
                         :contains_records_of_type => :agent_record_control,
                         :corresponding_to_association => :agent_record_control)

  self.one_to_many :agent_alternate_set, :class => "AgentAlternateSet"

  self.def_nested_record(:the_property => :agent_alternate_sets,
                         :contains_records_of_type => :agent_alternate_set,
                         :corresponding_to_association => :agent_alternate_set)

  self.one_to_many :agent_conventions_declaration, :class => "AgentConventionsDeclaration"

  self.def_nested_record(:the_property => :agent_conventions_declarations,
                         :contains_records_of_type => :agent_conventions_declaration,
                         :corresponding_to_association => :agent_conventions_declaration)

  self.one_to_many :agent_other_agency_codes, :class => "AgentOtherAgencyCodes"

  self.def_nested_record(:the_property => :agent_other_agency_codes,
                         :contains_records_of_type => :agent_other_agency_codes,
                         :corresponding_to_association => :agent_other_agency_codes)

  self.one_to_many :agent_maintenance_history, :class => "AgentMaintenanceHistory"

  self.def_nested_record(:the_property => :agent_maintenance_histories,
                         :contains_records_of_type => :agent_maintenance_history,
                         :corresponding_to_association => :agent_maintenance_history)

  self.one_to_many :agent_record_identifier, :class => "AgentRecordIdentifier"

  self.def_nested_record(:the_property => :agent_record_identifiers,
                         :contains_records_of_type => :agent_record_identifier,
                         :corresponding_to_association => :agent_record_identifier)

  self.one_to_many :agent_sources, :class => "AgentSources"

  self.def_nested_record(:the_property => :agent_sources,
                         :contains_records_of_type => :agent_sources,
                         :corresponding_to_association => :agent_sources)

  self.one_to_many :structured_date_label, :class => "StructuredDateLabel"

  self.def_nested_record(:the_property => :dates_of_existence,
                         :contains_records_of_type => :structured_date_label,
                         :corresponding_to_association => :structured_date_label)

  self.one_to_many :agent_place, :class => "AgentPlace"

  self.def_nested_record(:the_property => :agent_places,
                         :contains_records_of_type => :agent_place,
                         :corresponding_to_association => :agent_place)

  self.one_to_many :agent_occupation, :class => "AgentOccupation"

  self.def_nested_record(:the_property => :agent_occupations,
                         :contains_records_of_type => :agent_occupation,
                         :corresponding_to_association => :agent_occupation)

  self.one_to_many :agent_function, :class => "AgentFunction"

  self.def_nested_record(:the_property => :agent_functions,
                         :contains_records_of_type => :agent_function,
                         :corresponding_to_association => :agent_function)

  self.one_to_many :agent_topic, :class => "AgentTopic"

  self.def_nested_record(:the_property => :agent_topics,
                         :contains_records_of_type => :agent_topic,
                         :corresponding_to_association => :agent_topic)

  self.one_to_many :agent_identifier, :class => "AgentIdentifier"

  self.def_nested_record(:the_property => :agent_identifiers,
                         :contains_records_of_type => :agent_identifier,
                         :corresponding_to_association => :agent_identifier)

  self.one_to_many :used_language, :class => "UsedLanguage"

  self.def_nested_record(:the_property => :used_languages,
                         :contains_records_of_type => :used_language,
                         :corresponding_to_association => :used_language)

  self.one_to_many :agent_resource, :class => "AgentResource"

  self.def_nested_record(:the_property => :agent_resources,
                         :contains_records_of_type => :agent_resource,
                         :corresponding_to_association => :agent_resource)
end

#sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'backend/app/model/mixins/agent_manager.rb', line 476

def sequel_to_jsonmodel(objs, opts = {})
  jsons = super

  if opts[:calculate_linked_repositories]
    agents_to_repositories = GlobalRecordRepositoryLinkages.new(self, :linked_agents).call(objs)

    jsons.zip(objs).each do |json, obj|
      json.used_within_repositories = agents_to_repositories.fetch(obj, []).map {|repo| repo.uri}
      json.used_within_published_repositories = agents_to_repositories.fetch(obj, []).select {|repo| repo.publish == 1}.map {|repo| repo.uri}
    end
  end

  publication_status = ImpliedPublicationCalculator.new.for_agents(objs)

  jsonmodel_type = my_agent_type[:jsonmodel].to_s
  matching_users = Hash[User
                          .filter(:agent_record_id => objs.map(&:id),
                                  :agent_record_type => jsonmodel_type)
                          .map {|row| [row[:agent_record_id], row[:username]]}]
  repo_users = Hash[Repository
                          .filter(:agent_representation_id => objs.map(&:id))
                          .map {|row| [row[:agent_representation_id], row[:name]]}]

  jsons.zip(objs).each do |json, obj|
    json.agent_type = jsonmodel_type
    json.linked_agent_roles = obj.linked_agent_roles
    json.is_linked_to_published_record = publication_status.fetch(obj)

    populate_display_name(json)
    json.title = json['display_name']['sort_name']

    json.is_user = matching_users.fetch(obj.id, nil)
    if jsonmodel_type == 'agent_corporate_entity'
      json.is_repo_agent = repo_users.fetch(obj.id, nil)
    end
  end

  jsons
end

#set_publish_for_subrecords_with_subjects(json) ⇒ Object

Set the publish value of subrecords to match the publish value of the agent to work with implied publication for subjects



210
211
212
213
214
215
# File 'backend/app/model/mixins/agent_manager.rb', line 210

def set_publish_for_subrecords_with_subjects(json)
  publish = json['publish']
  AGENT_SUBRECORDS_WITH_SUBJECTS.each do |subrecord_type|
    json[subrecord_type].each { |subrecord| subrecord['publish'] = publish }
  end
end