Class: TopContainerLinkerValidator

Inherits:
BulkImportParser show all
Includes:
BulkImportMixins
Defined in:
backend/app/lib/bulk_import/top_container_linker_validator.rb

Constant Summary collapse

START_MARKER =

ASpace field headers row indicator

/ArchivesSpace field code/.freeze

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Instance Method Summary collapse

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 inherited from BulkImportParser

#initialize_handler_enums, #initialize_info, #record_uris, #run

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) ⇒ TopContainerLinkerValidator

Returns a new instance of TopContainerLinkerValidator.



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

def initialize(input_file, content_type, current_user, opts)
  super(input_file, content_type, current_user, opts, nil)
  @resource_ref = "/repositories/#{@opts[:repo_id]}/resources/#{@opts[:rid]}"
  @start_marker = START_MARKER
  @barcode_tc_existing_in_spreadsheet = {}
  @instance_types ||= CvList.new("instance_instance_type", @current_user) # for when we move instances over here
end

Instance Method Details

#get_hash_tableObject



161
162
163
# File 'backend/app/lib/bulk_import/top_container_linker_validator.rb', line 161

def get_hash_table()
  @barcode_tc_existing_in_spreadsheet
end

#process_row(row_hash = nil) ⇒ Object

look for all the required fields to make sure they are legit strip all the strings and turn publish and restrictions_flag into true/false



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'backend/app/lib/bulk_import/top_container_linker_validator.rb', line 19

def process_row(row_hash = nil)
  #This allows the processing of a single row
  if (!row_hash.nil?)
    @row_hash = row_hash
  end
  err_arr = []
  begin
    # Check that the archival object ref id exists
    ref_id = @row_hash["ref_id"]
    if ref_id.nil?
      err_arr.push I18n.t("top_container_linker.error.ref_id_miss", :row_num => @counter.to_s)
      raise BulkImportException.new(err_arr.join("; "))
    else
      #Check that the AO can be found in the db
      ao = archival_object_from_ref(ref_id.strip)
      if (ao.nil?)
        err_arr.push I18n.t("top_container_linker.error.ao_not_in_db", :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      end
    end

    ead_id = @row_hash["ead_id"]
    if ead_id.nil?
      err_arr.push I18n.t("top_container_linker.error.ead_id_miss", :ref_id => ref_id.to_s, :row_num => @counter.to_s)
    else
      #Check that the AO can be found in the db
      resource = resource_from_ref(ead_id.strip)
      if (resource.nil?)
        err_arr.push I18n.t("top_container_linker.error.resource_not_in_db", :ead_id => ead_id.to_s, :row_num => @counter.to_s)
      elsif (resource.uri != @resource_ref)
        err_arr.push I18n.t("top_container_linker.error.resources_do_not_match", :spreadsheet_resource => resource.uri, :ead_id => ead_id.to_s, :current_resource => @resource_ref, :row_num => @counter.to_s)
      end
    end

    #Check that the instance type exists
    instance_type = @row_hash["instance_type"]
    if instance_type.nil?
      err_arr.push I18n.t("top_container_linker.error.instance_type_miss", :ref_id => ref_id.to_s, :row_num => @counter.to_s)
    end
    retval = value_check(@instance_types, instance_type, [])
    if (retval == nil)
      err_arr.push I18n.t("top_container_linker.error.instance_type_does_not_exist", :instance_type => instance_type, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
    end

    #Check that either the Top Container Indicator or Top Container Record No. is present
    tc_indicator = @row_hash["top_container_indicator"]
    tc_record_no = @row_hash["top_container_id"]
    #Both missing
    if (tc_indicator.nil? && tc_record_no.nil?)
      err_arr.push I18n.t("top_container_linker.error.tc_indicator_and_record_no_miss", :ref_id => ref_id.to_s, :row_num => @counter.to_s)
    end
    #Both exist
    if (!tc_indicator.nil? && !tc_record_no.nil?)
      err_arr.push I18n.t("top_container_linker.error.tc_indicator_and_record_no_exist", :ref_id => ref_id.to_s, :row_num => @counter.to_s)
    end

    if (!tc_record_no.nil?)
      begin
        tc_obj = TopContainer.get_or_die(tc_record_no.strip.to_i)
      rescue NotFoundException
        tc_obj = nil
      end
      if tc_obj.nil?
        #Cannot find TC record with ID
        err_arr.push I18n.t("top_container_linker.error.tc_record_no_missing", :tc_id=> tc_record_no, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      end
    end

    #Container type/Container indicator combo already exists
    tc_type = @row_hash["top_container_type"]
    if (!tc_indicator.nil? && !tc_type.nil?)
      type_id = BackendEnumSource.id_for_value("container_type", tc_type.strip)
      tc_exists = indicator_and_type_exist_for_resource?(ead_id, tc_indicator, type_id)
      if (tc_exists)
        err_arr.push I18n.t("top_container_linker.error.tc_ind_ct_exists", :indicator=> tc_indicator, :tc_type=> tc_type, :ead_id => ead_id, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      end
    end

    #Check if the barcode already exists in the db (fail if so)
    barcode = @row_hash["top_container_barcode"]
    if (!barcode.nil?)
      tc_obj = find_top_container({:barcode => barcode.strip})
      if (!tc_obj.nil?)
        err_arr.push I18n.t("top_container_linker.error.tc_barcode_exists", :barcode=> barcode, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      elsif barcode_differs_from_prev_tc?(tc_indicator, tc_type, barcode)
        err_arr.push I18n.t("top_container_linker.error.tc_barcode_differs", :barcode=> barcode, :type=>tc_type, :indicator=> tc_indicator, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      else
        add_tc_bc(tc_indicator, tc_type, barcode)
      end
    end


    #Check if the barcode_2 already exists in the db (fail if so).
    #This will be put in place when Harvard's code is merged
    barcode_2 = @row_hash["child_barcode"]
    child_type = @row_hash["child_type"]
    child_indicator = @row_hash["child_indicator"]
    if (!barcode_2.nil?)
      sc_obj = sub_container_from_barcode(barcode_2.strip)
      if (!sc_obj.nil?)
        err_arr.push I18n.t("top_container_linker.error.sc_barcode_exists", :barcode=> barcode_2, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      elsif (!child_type.nil? && !child_indicator.nil?)
        if barcode_differs_from_prev_tc?(child_indicator, child_type, barcode_2, true)
          err_arr.push I18n.t("top_container_linker.error.sc_barcode_differs", :barcode=> barcode_2, :type=>child_type, :indicator=>child_indicator, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
        end
        add_tc_bc(child_indicator, child_type, barcode_2, true)
      end
    end

    #Check if the location ID can be found in the db
    loc_id = @row_hash["location_id"]
    if (!loc_id.nil?)
      begin
        loc = Location.get_or_die(loc_id.strip.to_i)
      rescue NotFoundException
        loc = nil
      end
      if (loc.nil?)
        err_arr.push I18n.t("top_container_linker.error.loc_not_in_db", :loc_id=> loc_id.to_s, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      end
    end

    #Check if Container Profile Record No. can be found in the db
    cp_id = @row_hash["container_profile_id"]
    if (!cp_id.nil?)
      begin
        cp = ContainerProfile.get_or_die(cp_id.strip.to_i)
      rescue NotFoundException
        cp = nil
      end
      if (cp.nil?)
        err_arr.push I18n.t("top_container_linker.error.cp_not_in_db", :cp_id=> cp_id.to_s, :ref_id => ref_id.to_s, :row_num => @counter.to_s)
      end
    end
  rescue Exception => e
    Log.error(["UNEXPLAINED EXCEPTION on check row", e.message, e.backtrace, @row_hash].pretty_inspect)
    raise
  end
  if !err_arr.empty?
    raise BulkImportException.new(err_arr.join("; "))
  end
end