@@ -6,23 +6,28 @@ class HyacinthSynchronizer
66 attr_reader :downloading_errors , :syncing_errors
77
88 def initialize
9- # @logger = Logger.new($stdout)
9+ @logger = Logger . new ( $stdout)
1010 end
1111
1212 # Performs MARC downloads and syncs resources to Hyacinth
1313 # @param [Integer] last_x_hours Records newer than this are synced.
1414 def download_and_sync_folio_to_hyacinth_records ( last_x_hours )
15- download_marc_from_folio ( last_x_hours )
15+ # download_marc_from_folio(last_x_hours)
1616 prepare_hyacinth_records
1717 end
1818
19+ def clear_downloads!
20+ @logger . info ( 'Clearing downloaded MARC files...' )
21+ FileUtils . rm_rf ( downloaded_marc_files_path )
22+ end
23+
1924 def download_marc_from_folio ( last_x_hours )
2025 downloader = FolioSync ::FolioToHyacinth ::MarcDownloader . new
2126 downloader . download_965hyacinth_marc_records ( last_x_hours )
2227
2328 return if downloader . downloading_errors . blank?
2429
25- puts "Error downloading MARC records from FOLIO: #{ downloader . downloading_errors } "
30+ @logger . error ( "Error downloading MARC records from FOLIO: #{ downloader . downloading_errors } " )
2631 @downloading_errors = downloader . downloading_errors
2732 end
2833
@@ -42,68 +47,8 @@ def downloaded_marc_files_path
4247 end
4348
4449 def process_marc_file ( marc_file_path )
45- folio_hrid = extract_hrid_from_filename ( marc_file_path )
46- hyacinth_results = fetch_hyacinth_results ( marc_file_path )
47- puts "Found #{ hyacinth_results . length } Hyacinth records for FOLIO HRID #{ folio_hrid } "
48-
49- case hyacinth_results . length
50- when 0
51- create_new_hyacinth_record ( marc_file_path , folio_hrid )
52- when 1
53- update_existing_hyacinth_record ( marc_file_path , hyacinth_results . first , folio_hrid )
54- else
55- handle_multiple_records_error ( folio_hrid )
56- end
57- rescue StandardError => e
58- puts "Failed to process #{ folio_hrid } : #{ e . message } "
59- @syncing_errors << "Error processing #{ folio_hrid } : #{ e . message } "
60- end
61-
62- def extract_hrid_from_filename ( marc_file_path )
63- File . basename ( marc_file_path , '.mrc' )
64- end
65-
66- def create_new_hyacinth_record ( marc_file_path , folio_hrid )
67- puts "Creating new Hyacinth record for #{ folio_hrid } "
68-
69- new_record = FolioToHyacinthRecord . new ( marc_file_path )
70- response = FolioSync ::Hyacinth ::Client . instance . create_new_record (
71- new_record . digital_object_data ,
72- publish : true
73- )
74-
75- puts "Created record for #{ folio_hrid } : #{ response . inspect } "
76- response
77- end
78-
79- def update_existing_hyacinth_record ( marc_file_path , existing_record , folio_hrid )
80- puts "Updating existing Hyacinth record for #{ folio_hrid } "
81-
82- preserved_data = { 'identifiers' => existing_record [ 'identifiers' ] }
83- updated_record = FolioToHyacinthRecord . new ( marc_file_path , preserved_data )
84-
85- response = FolioSync ::Hyacinth ::Client . instance . update_existing_record (
86- existing_record [ 'pid' ] ,
87- updated_record . digital_object_data ,
88- publish : true
89- )
90-
91- puts "Updated record #{ existing_record [ 'pid' ] } : #{ response . inspect } "
92- response
93- end
94-
95- def handle_multiple_records_error ( folio_hrid )
96- error_message = "Multiple Hyacinth records found for FOLIO HRID #{ folio_hrid } "
97- puts error_message
98- @syncing_errors << error_message
99- end
100-
101- def fetch_hyacinth_results ( marc_file_path )
102- folio_hrid = File . basename ( marc_file_path , '.mrc' )
103- potential_clio_identifier = "clio#{ folio_hrid } "
104- client = FolioSync ::Hyacinth ::Client . instance
105- client . find_by_identifier ( potential_clio_identifier ,
106- { f : { digital_object_type_display_label_sim : [ 'Item' ] } } )
50+ processor = FolioSync ::FolioToHyacinth ::MarcProcessor . new ( marc_file_path )
51+ processor . create_and_sync_hyacinth_record!
10752 end
10853 end
10954 end
0 commit comments