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

Instance Method Summary collapse

Methods inherited from BulkImportParser

#initialize_info, #record_uris, #run

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
13
14
15
16
17
18
# 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
  @date_types = CvList.new("date_type", @current_user)
  @date_labels = CvList.new("date_label", @current_user)
  @date_certainty = CvList.new("date_certainty", @current_user)
  @extent_types = CvList.new("extent_extent_type", @current_user)
  @extent_portions = CvList.new("extent_portion", @current_user)
end

Instance Method Details

#check_rowObject

required fields for a digital object row: ead match, (ao_ref_id or ao_uri)



119
120
121
122
123
124
125
126
127
128
129
130
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 119

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
  end
  normalize_boolean_column(@row_hash, 'publish')
  normalize_boolean_column(@row_hash, 'digital_object_link_publish')
  normalize_boolean_column(@row_hash, 'thumbnail_publish')
  err_arr.join("; ")
end

#create_instance(ao) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 20

def create_instance(ao)
  dig_instance = nil

  @notes_handler = NotesHandler.new
  @agent_handler = AgentHandler.new(@current_user, @validate_only)
  @subject_handler = SubjectHandler.new(@current_user, @validate_only)

  begin
    normalize_boolean_column(@row_hash, 'digital_object_publish')
    normalize_boolean_column(@row_hash, 'restrictions')
    normalize_boolean_column(@row_hash, 'nonrep_publish')
    dates = create_dates
    notes = create_notes
    extents = process_extents
    subjects = process_subjects
    linked_agents = process_agents

    dig_instance = @digital_object_handler.create(
      @row_hash["digital_object_title"],
      @row_hash["digital_object_id"],
      @row_hash["digital_object_publish"],
      @row_hash["level"],
      @row_hash["digital_object_type"],
      @row_hash["restrictions"],
      dates,
      notes,
      extents,
      subjects,
      linked_agents,
      ao,
      @report,
      representative_file_version,
      non_representative_file_version)
  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



132
133
134
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 132

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

#log_row(row) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 101

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 69

def process_row
  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



137
138
139
140
141
142
143
144
145
146
# File 'backend/app/lib/bulk_import/import_digital_objects.rb', line 137

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)
  end
  ao
end