Class: ImportDigitalObjects

Inherits:
BulkImportParser show all
Defined in:
backend/app/lib/bulk_import/import_digital_objects.rb

Constant Summary collapse

START_MARKER =
/ArchivesSpace digital object import field codes/.freeze

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Instance Method Summary collapse

Methods inherited from BulkImportParser

#initialize_info, #record_uris, #run

Methods included from BulkImportMixins

#ao_save, #archival_object_from_ref, #archival_object_from_ref_or_uri, #archival_object_from_uri, #create_date, #created, #find_top_container, #handle_notes, #indicator_and_type_exist_for_resource?, #resolves, #resource_from_ref, #resource_match, #sub_container_from_barcode, #test_exceptions, #valid, #value_check

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

Methods included from URIResolver

add_resolve_wrapper, ensure_reference_is_valid, register_resolver, resolve_references, resolve_wrappers

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Constructor Details

#initialize(input_file, content_type, current_user, opts, log_method = nil) ⇒ ImportDigitalObjects

Returns a new instance of ImportDigitalObjects.



6
7
8
9
10
11
12
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 6

def initialize(input_file, content_type, current_user, opts, log_method = nil)
  super(input_file, content_type, current_user, opts, log_method)
  @find_uri = "/repositories/#{@opts[:repo_id]}/find_by_id/archival_objects"
  @resource_ref = "/repositories/#{@opts[:repo_id]}/resources/#{@opts[:id]}"
  @repo_id = @opts[:repo_id]
  @start_marker = START_MARKER  # replace down stream
end

Instance Method Details

#check_rowObject

required fields for a digital object row: ead match, (ao_ref_id or ao_uri) and at least one of digital_object_link, thumbnail



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 86

def check_row
  err_arr = []
  begin
    if @row_hash["ao_ref_id"].nil? && @row_hash["ao_uri"].nil?
      err_arr.push I18n.t("bulk_import.error.no_uri_or_ref")
    end
    obj_link = @row_hash["digital_object_link"]
    thumb = @row_hash["thumbnail"] || @row_hash["Thumbnail"]
    err_arr.push I18n.t("bulk_import.error.dig_info_miss") if @row_hash["digital_object_link"].nil? && thumb.nil?
  end
  v = @row_hash["publish"]
  @row_hash["publish"] = (v == "1")
  err_arr.join("; ")
end

#create_instance(ao) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 14

def create_instance(ao)
  dig_instance = nil
  begin
    dig_instance = @doh.create(@row_hash["digital_object_title"],
                               @row_hash["thumbnail"], @row_hash["digital_object_link"], @row_hash["digital_object_id"], @row_hash["publish"], ao, @report)
  rescue Exception => e
    @report.add_errors(e.message)
  end
  if dig_instance && !@validate_only # only try to save if not validate only
    ao.instances ||= []
    ao.instances << dig_instance
    begin
      ao = ao_save(ao)
      @report.add_info(I18n.t("bulk_import.dig_assoc"))
    rescue BulkImportException => ee
      @report.add_errors(I18n.t("bulk_import.error.dig_unassoc", :msg => ee.message))
    end
  end
  dig_instance
end

#initialize_handler_enumsObject



101
102
103
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 101

def initialize_handler_enums
  @doh = DigitalObjectHandler.new(@current_user, @validate_only)
end

#log_row(row) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 68

def log_row(row)
  unless row.archival_object_id.nil?
    log_obj = I18n.t("bulk_import.log_obj", :what => I18n.t("bulk_import.ao"), :nm => row.archival_object_display, :id => row.archival_object_id, :ref_id => row.ref_id)
    @log_method.call(I18n.t("bulk_import.log_info", :row => row.row, :what => log_obj))
  end
  unless row.info.empty?
    row.info.each do |info|
      @log_method.call(I18n.t("bulk_import.log_info", :row => row.row, :what => info))
    end
  end
  unless row.errors.empty?
    row.errors.each do |err|
      @log_method.call(I18n.t("bulk_import.log_error", :row => row.row, :what => err))
    end
  end
end

#process_rowObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 35

def process_row
  ret_str = ""
  errs = []
  begin
    resource_match(@resource, @row_hash["ead"], @row_hash["res_uri"])
  rescue Exception => e
    errs << e.message
  end
  errs << check_row
  errs.reject!(&:empty?)
  if !@validate_only && !errs.empty?
    err = errs.join("; ")
    raise BulkImportException.new(I18n.t("bulk_import.row_error", :row => @counter, :errs => err))
  end
  ao = verify_ao(@row_hash["ao_ref_id"], @row_hash["ao_uri"], errs)
  if ao.nil? && !@validate_only
    err = errs.join("; ")
    raise BulkImportException.new(I18n.t("bulk_import.error.bad_ao", :errs => err))
  end
  digital_instance = create_instance(ao)
  if !digital_instance & @validate_only
    @report.add_errors(errs.join("; ")) if !errs.empty?
    @report.add_errors(I18n.t("bulk_import.object_not_created_be", :what => I18n.t("bulk_import.dig")))
  elsif !errs.empty?
    err = errs.join("; ")
    @report.add_errors(I18n.t("bulk_import.error.dig_unassoc", :msg => err))
  end
  @created_refs.concat(
    [ao.uri, digital_instance.digital_object['ref']]
  ) if ao && digital_instance && !@validate_only
  digital_instance
end

#verify_ao(ref_id, uri, errs) ⇒ Object

any problem here would result in the digital object not being created



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 106

def verify_ao(ref_id, uri, errs)
  result = archival_object_from_ref_or_uri(ref_id, uri)
  ao = result[:ao]
  if ao.nil?
    errs << I18n.t("bulk_import.error.bad_ao", :errs => result[:errs])
  else
    @report.add_archival_object(ao)
    if ao.instances
      digs = []
      ao.instances.each { |instance| digs.append(1) if instance["instance_type"] == "digital_object" }
      if !digs.empty?
        err = I18n.t("bulk_import.error.has_dig_obj")
        if @validate_only
          errs << err
        else
          errs << I18n.t("bulk_import.row_error", :row => @counter, :errs => err)
        end
      end
    end
  end
  ao
end