Module: AgentManager::Mixin::ClassMethods

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) assemble_hash_fields(json)



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
266
267
268
# File 'backend/app/model/mixins/agent_manager.rb', line 231

def assemble_hash_fields(json)
  fields = []

  json.dates_of_existence.each do |date|
    fields << hash_chunk(JSONModel(:date).from_hash(date),
                         %w(date_type label certainty expression begin end era calendar))
  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

- (Object) calculate_hash(json)



271
272
273
274
275
276
# File 'backend/app/model/mixins/agent_manager.rb', line 271

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

- (Object) combine_unauthorized_names(json)



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'backend/app/model/mixins/agent_manager.rb', line 135

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

  name_model = my_agent_type[:name_model]
  seen_names = []

  json.names = json.names.select {|name|
    name_hash = Digest::SHA1.hexdigest(name_model.assemble_hash_fields(name).sort.join('-'))

    result = name['authorized'] || !seen_names.include?(name_hash)
    seen_names << name_hash
    result
  }
end

- (Object) create_from_json(json, opts = {})



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'backend/app/model/mixins/agent_manager.rb', line 202

def create_from_json(json, opts = {})
  self.ensure_authorized_name(json)
  self.ensure_display_name(json)
  self.combine_unauthorized_names(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)

  super(json, opts.merge(:agent_sha1 => calculate_hash(json)))
end

- (Object) ensure_authorized_name(json)



118
119
120
121
122
# File 'backend/app/model/mixins/agent_manager.rb', line 118

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

- (Object) ensure_display_name(json)



125
126
127
128
129
130
131
132
# File 'backend/app/model/mixins/agent_manager.rb', line 125

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

- (Object) ensure_exists(json, referrer)



151
152
153
154
155
156
157
158
159
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
# File 'backend/app/model/mixins/agent_manager.rb', line 151

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"] )
                .and( Sequel.qualify( name_type.intern, :authorized)  => 1 ).select_all(agent_type.intern).first
                

    else
      agent = find_matching(json)
    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

- (Object) find_matching(json)



197
198
199
# File 'backend/app/model/mixins/agent_manager.rb', line 197

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

- (Object) hash_chunk(rec, field_array)



218
219
220
221
222
223
224
225
226
227
228
229
# File 'backend/app/model/mixins/agent_manager.rb', line 218

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

- (Object) my_agent_type



309
310
311
# File 'backend/app/model/mixins/agent_manager.rb', line 309

def my_agent_type
  AgentManager.agent_type_of(self)
end

- (Object) populate_display_name(json)



113
114
115
# File 'backend/app/model/mixins/agent_manager.rb', line 113

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

- (Object) register_agent_type(opts)



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'backend/app/model/mixins/agent_manager.rb', line 279

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 :date, :class => "ASDate"


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

end

- (Object) sequel_to_jsonmodel(objs, opts = {})



314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'backend/app/model/mixins/agent_manager.rb', line 314

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

  jsons.zip(objs).each do |json, obj|
    json.agent_type = my_agent_type[:jsonmodel].to_s
    json.linked_agent_roles = obj.linked_agent_roles

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

  jsons
end