Class: ContainerInstanceHandler
- Defined in:
- backend/app/lib/bulk_import/container_instance_handler.rb
Constant Summary
Constants inherited from Handler
Instance Method Summary collapse
-
#build(type, indicator, barcode) ⇒ Object
-
#create_container_instance(instance_type, type, indicator, barcode, resource_uri, report, subcont = {}) ⇒ Object
-
#format_container_instance(instance_type, tc, subcont = {}) ⇒ Object
Formats the container instance without a db retrieval or creation.
-
#get_existing_tc(top_container, resource_uri) ⇒ Object
-
#get_or_create(type, indicator, barcode, resource, report) ⇒ Object
returns a top container JSONModel.
-
#get_tc_by_barcode(barcode, resource_uri) ⇒ Object
-
#get_tc_by_type_indicator(top_container, resource_uri) ⇒ Object
-
#get_top_container_json_from_hash(type, indicator, barcode, resource) ⇒ Object
-
#initialize(current_user, validate_only = false) ⇒ ContainerInstanceHandler
constructor
A new instance of ContainerInstanceHandler.
-
#key_for(top_container, resource) ⇒ Object
-
#renew ⇒ Object
-
#validate_container_instance(instance_type, type, instance, errs, subcont = {}) ⇒ Object
Methods inherited from Handler
#clear, #save, #search, #stored
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, ) { :type => @container_types.value(type || "Box"), :indicator => indicator || "Unknown", :barcode => , } end |
#create_container_instance(instance_type, type, indicator, barcode, resource_uri, report, subcont = {}) ⇒ Object
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 |
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 131 def create_container_instance(instance_type, type, indicator, , 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, , 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. rescue Exception => e errs << ee. 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 115 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
62 63 64 65 66 67 68 69 70 |
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 62 def get_existing_tc(top_container, resource_uri) existing = if !top_container[:barcode].nil? (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 58 59 60 |
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 36 def get_or_create(type, indicator, , resource, report) begin if !@container_types.value(type) @container_types.add_value_to_enum(type) end top_container = build(type, indicator, ) 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. = 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.) end @top_containers[tc_key] = existing_tc if existing_tc existing_tc end |
#get_tc_by_barcode(barcode, resource_uri) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 81 def (, resource_uri) repo_id = resource_uri.split("/")[2] ret_tc = nil if 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
72 73 74 75 76 77 78 79 |
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 72 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, , resource) top_container_json = build(type, indicator, ) 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 |
#renew ⇒ Object
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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'backend/app/lib/bulk_import/container_instance_handler.rb', line 97 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 |