From 8bcb6dc790f325812ab5a900d997d310c2b78ef3 Mon Sep 17 00:00:00 2001 From: James Bullen Date: Thu, 7 May 2020 10:53:27 +1000 Subject: [PATCH] pass top container and location handlers in with the context in arrearage --- backend/converters/arrearage_converter.rb | 36 +++++++++++------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/backend/converters/arrearage_converter.rb b/backend/converters/arrearage_converter.rb index b80d324..b10c637 100644 --- a/backend/converters/arrearage_converter.rb +++ b/backend/converters/arrearage_converter.rb @@ -35,8 +35,6 @@ def initialize(input_file) super @batch = ASpaceImport::RecordBatch.new @input_file = input_file - TopContainerHandler.init - LocationHandler.init end @@ -58,6 +56,8 @@ def run context = { :collection_handler => CollectionHandler.new, :parent_handler => ParentHandler.new, + :top_container_handler => TopContainerHandler.new, + :location_handler => LocationHandler.new, :position => 0, } @@ -142,11 +142,11 @@ def jsonmodel(context) class LocationHandler - def self.init + def initialize @locations = {} end - def self.get_or_create(location_hash) + def get_or_create(location_hash) loc_key = location_hash return @locations[loc_key] if @locations.has_key?(loc_key) @@ -169,19 +169,19 @@ def self.get_or_create(location_hash) class TopContainerHandler - def self.init + def initialize @top_containers = {} end - def self.key_for(top_container) + def key_for(top_container) key = "#{top_container[:type]}: #{top_container[:indicator]}" key += " #{top_container[:barcode]}" if top_container.has_key?(:barcode) key end - def self.build(top_container) + def build(top_container) { :type => top_container.fetch(:type, 'Box'), :indicator => top_container.fetch(:indicator, 'Unknown'), @@ -190,14 +190,14 @@ def self.build(top_container) end - def self.uri_or_false(top_container) + def uri_or_false(top_container) tc = @top_containers.fetch(key_for(build(top_container)), false) return false unless tc tc[:uri] end - def self.get_or_create(top_container) + def get_or_create(top_container) tc = build(top_container) unless top_container[:indicator] @@ -366,10 +366,10 @@ def load_instances(row) if row['location_room'] locations << { 'start_date' => Date.today.strftime('%Y-%m-%d'), - 'ref' => ::ArrearageConverter::LocationHandler.get_or_create(:room => row['location_room'], - :row => row['location_row'], - :unit => row['location_unit'], - :shelf => row['location_shelf']), + 'ref' => @context.fetch(:location_handler).get_or_create(:room => row['location_room'], + :row => row['location_row'], + :unit => row['location_unit'], + :shelf => row['location_shelf']), 'status' => 'current' } else @@ -377,8 +377,8 @@ def load_instances(row) if row['indicator_1'] # no location info, but we have an indicator # so let's see if we've seen this container before ... - unless (tc_uri = ::ArrearageConverter::TopContainerHandler.uri_or_false(:indicator => row['indicator_1'], - :type => row['type_1'])) + unless (tc_uri = @context.fetch(:top_container_handler).uri_or_false(:indicator => row['indicator_1'], + :type => row['type_1'])) # no location info and we haven't seen this indicator before, so blow up ... raise "Row (#{row['title']}) has no location for container indicator: #{row['indicator_1']}. " + "Please fix this and rerun. Aborting import ..." @@ -392,9 +392,9 @@ def load_instances(row) end top_container = tc_uri || - ::ArrearageConverter::TopContainerHandler.get_or_create(:indicator => row['indicator_1'], - :type => row['type_1'], - :container_locations => locations) + @context.fetch(:top_container_handler).get_or_create(:indicator => row['indicator_1'], + :type => row['type_1'], + :container_locations => locations) sub_container = { 'top_container' => {'ref' => top_container}