Module: MergeHelpers

Defined in:
backend/app/lib/merge_helpers.rb

Instance Method Summary collapse

Instance Method Details

#check_repository(target, victims, repo_id) ⇒ Object

Raises:



9
10
11
12
13
14
15
# File 'backend/app/lib/merge_helpers.rb', line 9

def check_repository(target, victims, repo_id)
  repo_uri = JSONModel(:repository).uri_for(repo_id)

  if ([target] + victims).any? {|r| r[:repository] != repo_uri}
    raise BadParamsException.new(:merge_request => ["All records to merge must be in the repository specified"])
  end
end

#ensure_type(target, victims, type) ⇒ Object

Raises:



18
19
20
21
22
# File 'backend/app/lib/merge_helpers.rb', line 18

def ensure_type(target, victims, type)
  if (victims.map {|r| r[:type]} + [target[:type]]).any? {|t| t != type}
    raise BadParamsException.new(:merge_request => ["This merge request can only merge #{type} records"])
  end
end

#find_subrec_index_in_victim(victim, subrec_name, position) ⇒ Object

we don’t know how the user reordered the subrecords on the merge form, so find the index with the right data given the position of the right thing to replace/add by searching for it.



183
184
185
186
187
188
189
190
191
192
193
# File 'backend/app/lib/merge_helpers.rb', line 183

def find_subrec_index_in_victim(victim, subrec_name, position)
  ind = nil
  victim[subrec_name].each_with_index do |subrec, i|
    if i == position
      ind = i
      break
    end
  end

  return ind ? ind : -1
end

#merge_details(target, victim, selections, params) ⇒ Object



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
126
127
128
129
130
131
132
133
134
135
# File 'backend/app/lib/merge_helpers.rb', line 82

def merge_details(target, victim, selections, params)
  target[:linked_events] = []
  victim[:linked_events] = []

  subrec_add_replacements = []
  field_replacements = []
  victim_values = {}
  values_from_params = params[:merge_request_detail].selections

  # this code breaks selections into arrays like this:
  # ["agent_record_identifiers", 1, "append", 2] // add entire subrec, record is in position 2
  # ["agent_record_controls", 0, "replace", 1] // replace entire subrec, record is in position 1
  # ["agent_record_controls", 0, "maintenance_status", 1] // replace field, record is in position 1
  # ["agent_record_controls", 0, "publication_status", 0] // replace field, record is in position 0
  # ["agent_record_controls", 0, "maintenance_agency", 3]
  # and then creates data structures for the subrecords to append, replace entirely, and replace by field. record in in position 3
  selections.each_key do |key|
    path = key.split(".")
    path_fix = []
    path.each do |part|
      if part.length === 1
        part = part.to_i
      elsif (part.length === 2) and (part.start_with?('1'))
        part = part.to_i
      end
      path_fix.push(part)
    end

    subrec_name = path_fix[0]
    victim_values[subrec_name] = values_from_params[subrec_name]

    # subrec level add/replace
    if path_fix[2] == "append" || path_fix[2] == "replace"
      subrec_add_replacements.push(path_fix)

    # field level replace
    else
      field_replacements.push(path_fix)
    end
  end

  merge_details_subrec(target, victim, subrec_add_replacements, victim_values)
  merge_details_replace_field(target, victim, field_replacements, victim_values)

  target['title'] = target['names'][0]['sort_name']
  target

# This code can be hard to debug when things go wrong, especially since details of problems aren't bubbled up to the frontend where the user is.
# So we'll make sure to catch problems and dump out any info we know.
rescue => e
  STDERR.puts "EXCEPTION!"
  STDERR.puts e.inspect
  STDERR.puts e.backtrace
end

#merge_details_replace_field(target, victim, selections, values) ⇒ Object

do field replace operations



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'backend/app/lib/merge_helpers.rb', line 138

def merge_details_replace_field(target, victim, selections, values)
  selections.each do |path_fix|
    subrec_name = path_fix[0]
    # this is the index of the order the user arranged the subrecs in the form, not the order of the subrecords in the DB.
    ind         = path_fix[1]
    field       = path_fix[2]
    position    = path_fix[3]

    subrec_index = find_subrec_index_in_victim(victim, subrec_name, position)

    target[subrec_name][ind][field] = victim[subrec_name][subrec_index][field]
  end
end

#merge_details_subrec(target, victim, selections, values) ⇒ Object

do subrec replace operations



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
# File 'backend/app/lib/merge_helpers.rb', line 154

def merge_details_subrec(target, victim, selections, values)
  selections.each do |path_fix|
    subrec_name = path_fix[0]
    # this is the index of the order the user arranged the subrecs in the form, not the order of the subrecords in the DB.
    ind         = path_fix[1]
    mode        = path_fix[2]
    position    = path_fix[3]

    subrec_index = find_subrec_index_in_victim(victim, subrec_name, position)

    replacer = victim[subrec_name][subrec_index]

    # notes are a special case because of the way they store JSON in a db field. So Reordering is not supported, and we can assume the position in the merge request is the position in the victims notes subrecord JSON.
    if subrec_name == "notes"
      replacer = victim["notes"][ind]
      to_append = process_subrecord_for_merge(target, replacer, subrec_name, mode, ind)

      target[subrec_name].push(process_subrecord_for_merge(target, replacer, subrec_name, mode, ind))
    elsif mode == "replace"
      target[subrec_name][ind] = process_subrecord_for_merge(target, replacer, subrec_name, mode, ind)
    elsif mode == "append"
      target[subrec_name].push(process_subrecord_for_merge(target, replacer, subrec_name, mode, ind))
    end

  end
end

#parse_references(request) ⇒ Object



2
3
4
5
6
7
# File 'backend/app/lib/merge_helpers.rb', line 2

def parse_references(request)
  target = JSONModel.parse_reference(request.target['ref'])
  victims = request.victims.map {|victim| JSONModel.parse_reference(victim['ref'])}

  [target, victims]
end

#parse_selections(selections, path = [], all_values = {}) ⇒ Object



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
# File 'backend/app/lib/merge_helpers.rb', line 24

def parse_selections(selections, path=[], all_values={})
  selections.each_pair do |k, v|
    path << k

    position = selections['position']

    case v
    when String
      if v === "REPLACE"
        all_values.merge!({"#{path.join(".")}.#{position}" => "#{v}"})
        path.pop
      else
        path.pop
        next
      end
    when Hash then parse_selections(v, path, all_values)
    when Array then v.each_with_index do |v2, index|
        next if v2.is_a? String
        path << index
        parse_selections(v2, path, all_values)
      end
                    path.pop
    else
      path.pop
      next
    end
  end
  path.pop

  return all_values
end

#preview_sort_name(target) ⇒ Object

NOTE: this code is a duplicate of the auto_generate code for creating sort name in the name_person, name_family, name_software, name_corporate_entity models Consider refactoring when continued work done on the agents model enhancements



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
269
270
271
272
273
274
275
276
277
278
279
# File 'backend/app/lib/merge_helpers.rb', line 232

def preview_sort_name(target)
  result = ""

  case target['jsonmodel_type']
  when 'name_person'
    if target["name_order"] === "inverted"
      result << target["primary_name"] if target["primary_name"]
      result << ", #{target["rest_of_name"]}" if target["rest_of_name"]
    elsif target["name_order"] === "direct"
      result << target["rest_of_name"] if target["rest_of_name"]
      result << " #{target["primary_name"]}" if target["primary_name"]
    else
      result << target["primary_name"] if target["primary_name"]
    end

    result << ", #{target["prefix"]}" if target["prefix"]
    result << ", #{target["suffix"]}" if target["suffix"]
    result << ", #{target["title"]}" if target["title"]
    result << ", #{target["number"]}" if target["number"]
    result << " (#{target["fuller_form"]})" if target["fuller_form"]
    result << ", #{target["dates"]}" if target["dates"]
  when 'name_corporate_entity'
    result << "#{target["primary_name"]}" if target["primary_name"]
    result << ". #{target["subordinate_name_1"]}" if target["subordinate_name_1"]
    result << ". #{target["subordinate_name_2"]}" if target["subordinate_name_2"]

    grouped = [target["number"], target["dates"]].reject {|v| v.nil?}
    result << " (#{grouped.join(" : ")})" if not grouped.empty?
  when 'name_family'
    result << target["family_name"] if target["family_name"]
    result << ", #{target["prefix"]}" if target["prefix"]
    result << ", #{target["dates"]}" if target["dates"]
  when 'name_software'
    result << "#{target["manufacturer"]} " if target["manufacturer"]
    result << "#{target["software_name"]}" if target["software_name"]
    result << " #{target["version"]}" if target["version"]
  end

  result << " (#{target["qualifier"]})" if target["qualifier"]

  result.lstrip!

  if result.length > 255
    return result[0..254]
  else
    return result
  end
end

#process_subrecord_for_merge(target, subrecord, jsonmodel_type, mode, ind) ⇒ Object

before we can merge a subrecord, we need to update the IDs, tweak things to prevent validation issues, etc



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'backend/app/lib/merge_helpers.rb', line 196

def process_subrecord_for_merge(target, subrecord, jsonmodel_type, mode, ind)
  target_id = target['id']

  if jsonmodel_type == 'names'
    # an agent name can only have one authorized or display name.
    # make sure the name being merged in doesn't conflict with this

    # if appending, always disable fields that validate across a set. If replacing, always keep values from target
    if mode == "append"
      subrecord['authorized']      = false
      subrecord['is_display_name'] = false
    elsif mode == "replace"
      subrecord['authorized']      = target['names'][ind]['authorized']
      subrecord['is_display_name'] = target['names'][ind]['is_display_name']
    end

  elsif jsonmodel_type == 'agent_record_identifiers'
    # same with agent_record_identifiers being marked as primary, we can only have one

    if mode == "append"
      subrecord['primary_identifier'] = false

    elsif mode == "replace"
      subrecord['primary_identifier'] = target['agent_record_identifiers'][ind]['primary_identifier']
    end
  end

  set_agent_id(target_id, subrecord)

  return subrecord
end

#set_agent_id(target_id, subrecord) ⇒ Object

when merging, set the agent id foreign key (e.g, agent_person_id, agent_family_id…) from the victim to the target



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'backend/app/lib/merge_helpers.rb', line 57

def set_agent_id(target_id, subrecord)
  if subrecord['agent_person_id']
    subrecord['agent_person_id'] = target_id

  elsif subrecord['agent_family_id']
    subrecord['agent_family_id'] = target_id

  elsif subrecord['agent_corporate_entity_id']
    subrecord['agent_corporate_entity_id'] = target_id

  elsif subrecord['agent_software_id']
    subrecord['agent_software_id'] = target_id

  # this section updates related_agents ids
  elsif subrecord['agent_person_id_0']
    subrecord['agent_person_id_0'] = target_id

  elsif subrecord['agent_family_id_0']
    subrecord['agent_family_id_0'] = target_id

  elsif subrecord['agent_corporate_entity_id_0']
    subrecord['agent_corporate_entity_id_0'] = target_id
  end
end