Class: ContainerInstanceHandler

Inherits:
Handler
  • Object
show all
Defined in:
backend/app/lib/bulk_import/container_instance_handler.rb

Constant Summary

Constants inherited from Handler

Handler::DISAMB_STR

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Instance Method Summary collapse

Methods inherited from Handler

#clear, #save, #search, #stored

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 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(current_user, validate_only = false) ⇒ ContainerInstanceHandler

Returns a new instance of ContainerInstanceHandler.



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

def initialize(current_user, validate_only = false)
  super
  @top_containers = {}
  @container_types ||= CvList.new("container_type", @current_user)
  @instance_types ||= CvList.new("instance_instance_type", @current_user) # for when we move instances over here
end

Instance Method Details

#build(type, indicator, barcode) ⇒ Object



20
21
22
23
24
25
26
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 20

def build(type, indicator, barcode)
  {
    :type => @container_types.value(type || "Box"),
    :indicator => indicator || "Unknown",
    :barcode => barcode,
  }
end

#create_container_instance(instance_type, type, indicator, barcode, resource_uri, report, subcont = {}) ⇒ Object



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

def create_container_instance(instance_type, type, indicator, barcode, resource_uri, report, subcont = {})
  errs = []
  instance = JSONModel(:instance).new._always_valid!
  sc = validate_container_instance(instance_type, type, instance, errs, subcont)
  tc = get_or_create(type, indicator, barcode, resource_uri, report)
  unless @validate_only || tc.nil? || sc.nil?
    begin
      sc["top_container"] = { "ref" => tc.uri }
      instance.sub_container = JSONModel(:sub_container).from_hash(sc)
    rescue BulkImportException => ee
      errs << ee.message
    rescue Exception => e
      errs << ee.message
    end
  end
  if !errs.empty?
    raise BulkImportException.new(errs.join("; "))
  end
  %w(2 3).each do |num|
    if subcont["type_#{num}"]
      sc["type_#{num}"] = value_check(@container_types, subcont["type_#{num}"], errs)
      sc["indicator_#{num}"] = subcont["indicator_#{num}"] || "Unknown"
      sc["barcode_#{num}"] = subcont["barcode_#{num}"] || nil
    end
  end
  instance
end

#format_container_instance(instance_type, tc, subcont = {}) ⇒ Object

Formats the container instance without a db retrieval or creation



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

def format_container_instance(instance_type, tc, subcont = {})
  instance = nil
  sc = {'top_container' => {'ref' => tc.uri}, 'jsonmodel_type' => 'sub_container'}
  %w(2 3).each do |num|
    if subcont["type_#{num}"]
      sc["type_#{num}"] = @container_types.value(subcont["type_#{num}"])
      sc["indicator_#{num}"] = subcont["indicator_#{num}"] || 'Unknown'
      sc["barcode_#{num}"] = subcont["barcode_#{num}"] || nil
    end
  end
  instance = JSONModel(:instance).new._always_valid!
  instance.instance_type = @instance_types.value(instance_type)
  instance.sub_container = JSONModel(:sub_container).from_hash(sc)
  instance
end

#get_existing_tc(top_container, resource_uri) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 59

def get_existing_tc(top_container, resource_uri)
  existing = if !top_container[:barcode].nil?
               get_tc_by_barcode(top_container[:barcode], resource_uri)
             else
               get_tc_by_type_indicator(top_container, resource_uri)
             end

  existing
end

#get_or_create(type, indicator, barcode, resource, report) ⇒ Object

returns a top container JSONModel



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 36

def get_or_create(type, indicator, barcode, resource, report)
  begin
    top_container = build(type, indicator, barcode)
    tc_key = key_for(top_container, resource)
    # check to see if we already have fetched one from the db, or created one.
    existing_tc = @top_containers.fetch(tc_key, false) || get_existing_tc(top_container, resource)
    if !existing_tc
      tc = JSONModel(:top_container).new._always_valid!
      tc.type = top_container[:type]
      tc.indicator = top_container[:indicator]
      tc.barcode = top_container[:barcode] if top_container[:barcode]
      tc.repository = { "ref" => resource.split("/")[0..2].join("/") }
      tc = save(tc, TopContainer)
      created(tc, "#{I18n.t("bulk_import.tc")}", "#{I18n.t("bulk_import.tc")} [#{tc.type} #{tc.indicator}]", report)
      existing_tc = tc
    end
  rescue Exception => e
    raise BulkImportException.new(e.message)
  end
  @top_containers[tc_key] = existing_tc if existing_tc
  existing_tc
end

#get_tc_by_barcode(barcode, resource_uri) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 78

def get_tc_by_barcode(barcode, resource_uri)
  repo_id = resource_uri.split("/")[2]
  ret_tc = nil
  if barcode
    begin
      tc_params = {}
      tc_params[:q] = "barcode_u_sstr:\"#{barcode}\""
      ret_tc = search(repo_id, tc_params, :top_container, "top_container")
    rescue Exception => e
      # we don't care why
    end
  end

  ret_tc
end

#get_tc_by_type_indicator(top_container, resource_uri) ⇒ Object



69
70
71
72
73
74
75
76
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 69

def get_tc_by_type_indicator(top_container, resource_uri)
  tc_str = "#{top_container[:type]} #{top_container[:indicator]}"
  tc_params = {}
  tc_params[:q] = "display_string:\"#{tc_str}\" AND collection_uri_u_sstr:\"#{resource_uri}\""
  ret_tc = search(nil, tc_params, :top_container, "top_container", "display_string:#{tc_str}")

  ret_tc
end

#get_top_container_json_from_hash(type, indicator, barcode, resource) ⇒ Object



28
29
30
31
32
33
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 28

def get_top_container_json_from_hash(type, indicator, barcode, resource)
  top_container_json = build(type, indicator, barcode)
  tc_key = key_for(top_container_json, resource)
  tc = @top_containers.fetch(tc_key, nil)
  tc
end

#key_for(top_container, resource) ⇒ Object



14
15
16
17
18
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 14

def key_for(top_container, resource)
  key = "'#{resource}' #{top_container[:type]}: #{top_container[:indicator]}"
  key += " #{top_container[:barcode]}" if top_container[:barcode]
  key
end

#renewObject



9
10
11
12
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 9

def renew
  clear(@container_types)
  clear(@instance_types)
end

#validate_container_instance(instance_type, type, instance, errs, subcont = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 94

def validate_container_instance(instance_type, type, instance, errs, subcont = {})
  sc = { "jsonmodeltype" => "sub_container" }
  if instance_type.nil?
    errs << I18n.t("bulk_import.error.missing_instance_type")
  else
    instance.instance_type = value_check(@instance_types, instance_type, errs)
  end
  %w(2 3).each do |num|
    if subcont["type_#{num}"]
      sc["type_#{num}"] = value_check(@container_types, subcont["type_#{num}"], errs)
      sc["indicator_#{num}"] = subcont["indicator_#{num}"] || "Unknown"
      sc["barcode_#{num}"] = subcont["barcode_#{num}"] || nil
    end
  end
  sc
end