Module: BulkImportMixins

Includes:
CrudHelpers
Included in:
BulkImportParser, Handler, TopContainerLinker, TopContainerLinkerValidator
Defined in:
backend/app/lib/bulk_import/bulk_import_mixins.rb

Overview

contains methods that might be needed for more than one bulk import converter

Instance Method Summary collapse

Methods included from CrudHelpers

#handle_create, #handle_delete, #handle_listing, #handle_raw_listing, #handle_unlimited_listing, #handle_update, scoped_dataset, with_record_conflict_reporting, #with_record_conflict_reporting

Instance Method Details

#ao_save(ao) ⇒ Object



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
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 19

def ao_save(ao)
  revived = nil
  if @validate_only
    valid(ao, I18n.t("ao"))
    ao.uri = ao.uri || "valid"
    revived = ao
  else
    begin
      archObj = nil
      if ao.id.nil?
        archObj = ArchivalObject.create_from_json(ao)
      else
        obj = ArchivalObject.get_or_die(ao.id)
        archObj = obj.update_from_json(ao)
      end
      objs = ArchivalObject.sequel_to_jsonmodel([archObj])
      revived = objs[0] if !objs.empty?
    rescue JSONModel::ValidationException => ve
      raise BulkImportException.new(I18n.t("bulk_import.error.ao_validation", :err => ve.errors))
    rescue Exception => e
      Log.error("UNEXPECTED ao save error: #{e.message}\n#{e.backtrace}")
      Log.error(ASUtils.jsonmodels_to_hashes(ao).pretty_inspect) if ao
      raise e
    end
  end
  revived
end

#archival_object_from_ref(ref_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 62

def archival_object_from_ref(ref_id)
  dataset = CrudHelpers.scoped_dataset(ArchivalObject, { :ref_id => ref_id })
  ao = nil
  if !dataset.empty?
    objs = dataset.respond_to?(:all) ? dataset.all : dataset
    jsonms = ArchivalObject.sequel_to_jsonmodel(objs)
    if jsonms.length == 1
      ao = jsonms[0]
    else
      raise BulkImportException.new(I18n.t("bulk_import.error.bad_ao_ref_id", :ref_id => ref_id))
    end
  end
  ao
end

#archival_object_from_ref_or_uri(ref_id, uri) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 77

def archival_object_from_ref_or_uri(ref_id, uri)
  ao = nil
  errs = ""
  if uri.nil? && ref_id.nil?
    errs = I18n.t("bulk_import.error.no_uri_or_ref")
  elsif !uri.nil?
    begin
      ao = archival_object_from_uri(uri)
    rescue BulkImportException => e
      errs = e.message
    end
  elsif ao.nil? && !ref_id.nil?
    begin
      ao = archival_object_from_ref(ref_id)
    rescue BulkImportException => e
      errs = "#{errs} #{e.message}"
    end
  end
  { :ao => ao, :errs => errs }
end

#archival_object_from_uri(uri) ⇒ Object

accepts either the full URI or just the ID



99
100
101
102
103
104
105
106
107
108
109
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 99

def archival_object_from_uri(uri)
  ao = nil
  begin
    uris = uri.split("/")
    aoid = uris.length == 1 ? uri : uris[4]
    ao = ArchivalObject.to_jsonmodel(Integer(aoid))
  rescue
    raise BulkImportException.new(I18n.t("bulk_import.error.bad_ao_uri", :uri => uri))
  end
  ao
end

#create_date(dates_label, date_begin, date_end, date_type, expression, date_certainty) ⇒ Object

The following methods assume @report is defined, and is a BulkImportReport object



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
266
267
268
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 221

def create_date(dates_label, date_begin, date_end, date_type, expression, date_certainty)
  date_str = "(Date: type:#{date_type}, label: #{dates_label}, begin: #{date_begin}, end: #{date_end}, expression: #{expression})"
  date = {}

  begin
    date['date_type'] = @date_types.value(date_type || "inclusive")
  rescue Exception => e
    @report.add_errors(I18n.t("bulk_import.error.date_type",
                              :what => date_type,
                              :date_str => date_str))

    return nil
  end
  begin
    date['label'] = @date_labels.value(dates_label || "creation")
  rescue Exception => e
    @report.add_errors(I18n.t("bulk_import.error.date_label",
                              :what => dates_label,
                              :date_str => date_str))

    return nil
  end

  if date_certainty
    begin
      date["certainty"] = @date_certainty.value(date_certainty)
    rescue Exception => e
      @report.add_errors(I18n.t("bulk_import.error.certainty", :what => e.message, :date_str => date_str))
    end
  end

  date["begin"] = date_begin if date_begin
  date["end"] = date_end if date_end
  date["expression"] = expression if expression
  invalids = JSONModel::Validations.check_date(date)
  unless (invalids.nil? || invalids.empty?)
    err_msg = ""
    invalids.each do |inv|
      err_msg << " #{inv[0]}: #{inv[1]}"
    end
    @report.add_errors(I18n.t("bulk_import.error.invalid_date", :what => err_msg, :date_str => date_str))
    return nil
  end
  if date_type == "single" && !date_end.nil?
    @report.add_errors(I18n.t("bulk_import.warn.single_date_end", :date_str => date_str))
  end
  d = JSONModel(:date).new(date)
end

#created(obj, type, message, report) ⇒ Object



168
169
170
171
172
173
174
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 168

def created(obj, type, message, report)
  if @validate_only
    report.add_info(I18n.t("bulk_import.could_be", :what => message))
  else
    report.add_info(I18n.t("bulk_import.created", :what => message, :id => obj.uri))
  end
end

#find_top_container(where_params) ⇒ Object

Finds the top container using the hash values (AND clause only)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 112

def find_top_container(where_params)
  dataset = CrudHelpers.scoped_dataset(TopContainer, where_params)
  tc = nil
  if !dataset.empty?
    objs = dataset.respond_to?(:all) ? dataset.all : dataset
    jsonms = TopContainer.sequel_to_jsonmodel(objs)
    if jsonms.length > 0
      tc = jsonms[0]
    else
      raise BulkImportException.new(I18n.t('bulk_import.error.find_tc', :where => where_params.pretty_inspect))
    end
  end
  tc
end

#handle_notes(ao, hash, dig_obj = false) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 270

def handle_notes(ao, hash, dig_obj = false)
  @nh = NotesHandler.new
  publish = ao.publish
  errs = []
  notes_keys = hash.keys.grep(/^n_/)
  if notes_keys
    notes_keys.each do |key|
      unless hash[key].nil?
        content = hash[key]
        type = key.match(/n_(.+)$/)[1]
        if type == 'accessrestrict'
          b_date = hash['b_accessrestrict']
          e_date = hash['e_accessrestrict']
          local_restriction = hash['t_accessrestrict']
        end
        pubnote = hash["p_#{type}"]
        if pubnote.nil?
          pubnote = publish
        else
          pubnote = (pubnote == "1")
        end
        note_label = hash["l_#{type}"]
        begin
          note = @nh.create_note(type, note_label, content, pubnote, dig_obj, b_date, e_date, local_restriction)
          ao.notes.push(note) if !note.nil?
        rescue BulkImportException => bei
          errs.push([bei.message])
        end
      end
    end
  end
  errs
end

#indicator_and_type_exist_for_resource?(ead_id, indicator, type_id) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 127

def indicator_and_type_exist_for_resource?(ead_id, indicator, type_id)
  return TopContainer
    .join(:top_container_link_rlshp, :top_container_link_rlshp__top_container_id => :top_container__id)
    .join(:sub_container, :sub_container__id => :top_container_link_rlshp__sub_container_id)
    .join(:instance, :instance__id => :sub_container__instance_id)
    .join(:archival_object, :archival_object__id => :instance__archival_object_id)
    .join(:resource, :resource__id => :archival_object__root_record_id)
    .filter(:resource__ead_id => ead_id, :indicator => indicator, :type_id => type_id).count > 0
end

#resolvesObject

METHOD(s)



7
8
9
10
11
12
13
14
15
16
17
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 7

def resolves
  ["subjects", "related_resources", "linked_agents",
   "revision_statements",
   "container_locations", "digital_object", "classifications",
   "related_agents", "resource", "parent", "creator",
   "linked_instances", "linked_records", "related_accessions",
   "linked_events", "linked_events::linked_records",
   "linked_events::linked_agents",
   "top_container", "container_profile", "location_profile",
   "owner_repo"]
end

#resource_from_ref(ead_id) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 47

def resource_from_ref(ead_id)
  dataset = CrudHelpers.scoped_dataset(Resource, {:ead_id => ead_id})
  resource = nil
  if !dataset.empty?
    objs = dataset.respond_to?(:all) ? dataset.all : dataset
    jsonms = Resource.sequel_to_jsonmodel(objs)
    if jsonms.length == 1
      resource = jsonms[0]
    else
      raise BulkImportException.new(I18n.t('bulk_import.error.resource_ref_id', :ref_id => ead_id))
    end
  end
  resource
end

#resource_match(resource, ead_id, uri) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 176

def resource_match(resource, ead_id, uri)
  if uri.nil? && ead_id.nil?
    raise BulkImportException.new(I18n.t("bulk_import.error.row_missing_ead_uri"))
  end
  match = false
  # try uri first
  if !uri.nil?
    if uri == resource["uri"]
      match = true
    else
      raise BulkImportException.new(I18n.t("bulk_import.error.uri_mismatch", :res_uri => resource[:record_uri], :row_uri => uri))
    end
  elsif !ead_id.nil?
    if ead_id == resource["ead_id"]
      match = true
    else
      raise BulkImportException.new(I18n.t("bulk_import.error.res_ead")) if resource["ead_id"].nil?
      raise BulkImportException.new(I18n.t("bulk_import.error.ead_mismatch", :res_ead => resource["ead_id"], :row_ead => ead_id))
    end
  end
  match
end

#sub_container_from_barcode(barcode) ⇒ Object



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

def sub_container_from_barcode(barcode)
  dataset = CrudHelpers.scoped_dataset(SubContainer, {:barcode_2 => barcode})
  sc = nil
  if !dataset.empty?
    objs = dataset.respond_to?(:all) ? dataset.all : dataset
    jsonms = SubContainer.sequel_to_jsonmodel(objs)
    if jsonms.length > 0
      sc = jsonms[0]
    else
      raise BulkImportException.new(I18n.t('bulk_import.error.sc_barcode', :barcode => barcode))
    end
  end
  sc
end

#test_exceptions(obj, what = "") ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 304

def test_exceptions(obj, what = "")
  ret_val = false
  begin
    obj._exceptions
    ret_val = true
  rescue Exception => e
    raise BulkImportException.new("editable?") if e.message.include?("editable?")
    raise e
  end
  ret_val
end

#valid(obj, what) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 199

def valid(obj, what)
  ret_val = false
  begin
    test_exceptions(obj)
    ret_val = true
  rescue Exception => ex
    raise BulkImportException.new(I18n.t("bulk_import.error.validation_error", :what => what, :err => ex.message))
  end
  ret_val
end

#value_check(cvlist, value, errs) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'backend/app/lib/bulk_import/bulk_import_mixins.rb', line 210

def value_check(cvlist, value, errs)
  ret_val = nil
  begin
    ret_val = cvlist.value(value)
  rescue Exception => ex
    errs << ex.message
  end
  ret_val
end