Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-10-30 19:13:32 UTC using RuboCop version 1.78.0.
# on 2026-02-18 17:54:03 UTC using RuboCop version 1.78.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 20
# Offense count: 19
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 34
Expand All @@ -21,7 +21,7 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 8

# Offense count: 22
# Offense count: 24
# Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Exclude:
Expand All @@ -35,6 +35,7 @@ Metrics/MethodLength:
- 'lib/folio_sync/archives_space_to_folio/job_result_processor.rb'
- 'lib/folio_sync/archives_space_to_folio/marc_record_enhancer.rb'
- 'lib/folio_sync/archives_space_to_folio/record_processor.rb'
- 'lib/folio_sync/folio_to_hyacinth/hyacinth_record_writer.rb'
- 'lib/folio_sync/folio_to_hyacinth/marc_downloader.rb'
- 'lib/folio_sync/folio_to_hyacinth/marc_parsing_methods/title.rb'
- 'lib/folio_sync/rake/error_logger.rb'
Expand Down Expand Up @@ -66,14 +67,13 @@ Rails/FindEach:
Exclude:
- 'lib/tasks/test_create_or_update.rake'

# Offense count: 5
# Offense count: 3
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Include.
# Include: **/app/**/*.rb, **/config/**/*.rb, db/**/*.rb, **/lib/**/*.rb
Rails/Output:
Exclude:
- 'app/models/folio_to_hyacinth_record.rb'
- 'lib/folio_sync/folio_to_hyacinth/marc_parsing_methods/project.rb'
- 'lib/folio_sync/rake/env_validator.rb'

# Offense count: 1
Expand Down
68 changes: 68 additions & 0 deletions lib/folio_sync/folio_to_hyacinth/hyacinth_record_writer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

module FolioSync
module FolioToHyacinth
class HyacinthRecordWriter
attr_reader :syncing_errors

def initialize
@logger = Logger.new($stdout)
@client = FolioSync::Hyacinth::Client.instance
@syncing_errors = []
end

# @param [String] marc_file_path
# @param [String] folio_hrid
# @param [Array] existing_records
def sync(marc_file_path, folio_hrid, existing_records)
case existing_records.length
when 0
create_new_record(marc_file_path, folio_hrid)
when 1
update_existing_record(marc_file_path, folio_hrid, existing_records.first)
else
handle_multiple_records(folio_hrid)
end
end

private

def create_new_record(marc_file_path, folio_hrid)
@logger.info("Creating new Hyacinth record for #{folio_hrid}")

new_record = FolioToHyacinthRecord.new(marc_file_path)
response = @client.create_new_record(new_record.digital_object_data, publish: true)

@logger.info("Created record for #{folio_hrid}: #{response.inspect}")
rescue StandardError => e
error_message = "Failed to create record for #{folio_hrid}: #{e.message}"
@logger.error(error_message)
@syncing_errors << error_message
end

def update_existing_record(marc_file_path, folio_hrid, existing_record)
@logger.info("Updating existing Hyacinth record for #{folio_hrid}")
preserved_data = { 'identifiers' => existing_record['identifiers'] }
updated_record = FolioToHyacinthRecord.new(marc_file_path, preserved_data)

response = @client.update_existing_record(
existing_record['pid'],
updated_record.digital_object_data,
publish: true
)

@logger.info("Updated record #{existing_record['pid']}: #{response.inspect}")
rescue StandardError => e
error_message = "Failed to update record #{existing_record['pid']} for #{folio_hrid}: #{e.message}"
@logger.error(error_message)
@syncing_errors << error_message
end

def handle_multiple_records(folio_hrid)
error_message = "Multiple Hyacinth records found for FOLIO HRID #{folio_hrid}"
@logger.error(error_message)
@syncing_errors << error_message
end
end
end
end
59 changes: 59 additions & 0 deletions lib/folio_sync/folio_to_hyacinth/hyacinth_synchronizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module FolioSync
module FolioToHyacinth
class HyacinthSynchronizer
attr_reader :downloading_errors, :syncing_errors

def initialize
@logger = Logger.new($stdout)
@downloading_errors = []
@syncing_errors = []
end

# Performs MARC downloads and syncs resources to Hyacinth
# @param [Integer] last_x_hours Records newer than this are synced.
def download_and_sync_folio_to_hyacinth_records(last_x_hours)
download_marc_from_folio(last_x_hours)
prepare_and_sync_folio_to_hyacinth_records
end

def clear_downloads!
@logger.info('Clearing downloaded MARC files...')
FileUtils.rm_rf(downloaded_marc_files_path)
end

def download_marc_from_folio(last_x_hours)
downloader = FolioSync::FolioToHyacinth::MarcDownloader.new
downloader.download_965hyacinth_marc_records(last_x_hours)

return if downloader.downloading_errors.blank?

@logger.error("Error downloading MARC records from FOLIO: #{downloader.downloading_errors}")
@downloading_errors = downloader.downloading_errors
end

def prepare_and_sync_folio_to_hyacinth_records
marc_files = Dir.glob(downloaded_marc_files_path)
@logger.info("Processing #{marc_files.count} MARC files")

marc_files.each do |marc_file_path|
process_marc_file(marc_file_path)
end
end

private

def downloaded_marc_files_path
"#{Rails.configuration.folio_to_hyacinth[:download_directory]}/*.mrc"
end

def process_marc_file(marc_file_path)
processor = FolioSync::FolioToHyacinth::MarcProcessor.new(marc_file_path)
processor.prepare_and_sync_folio_to_hyacinth_record!

@syncing_errors.concat(processor.syncing_errors) if processor.syncing_errors.any?
end
end
end
end
40 changes: 40 additions & 0 deletions lib/folio_sync/folio_to_hyacinth/marc_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

class FolioSync::FolioToHyacinth::MarcProcessor
attr_reader :syncing_errors

def initialize(marc_file_path)
@marc_file_path = marc_file_path
@logger = Logger.new($stdout)
@record_syncer = FolioSync::FolioToHyacinth::HyacinthRecordWriter.new
@syncing_errors = []
end

def prepare_and_sync_folio_to_hyacinth_record!
folio_hrid = extract_hrid_from_filename(@marc_file_path)
existing_records = fetch_existing_hyacinth_records(folio_hrid)

@logger.info("Found #{existing_records.length} Hyacinth records for FOLIO HRID: #{folio_hrid}")

@record_syncer.sync(@marc_file_path, folio_hrid, existing_records)
@syncing_errors.concat(@record_syncer.syncing_errors) if @record_syncer.syncing_errors.any?
rescue StandardError => e
@logger.error("Failed to process #{folio_hrid}: #{e.message}")
@syncing_errors << "Error processing #{folio_hrid}: #{e.message}"
end

private

def extract_hrid_from_filename(marc_file_path)
File.basename(marc_file_path, '.mrc')
end

def fetch_existing_hyacinth_records(folio_hrid)
potential_clio_identifier = "clio#{folio_hrid}"
client = FolioSync::Hyacinth::Client.instance
client.find_by_identifier(
potential_clio_identifier,
{ f: { digital_object_type_display_label_sim: ['Item'] } }
)
end
end
93 changes: 54 additions & 39 deletions lib/tasks/hyacinth_sync.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,42 @@
namespace :folio_sync do
namespace :folio_to_hyacinth do
task run: :environment do
puts 'Starting Folio to Hyacinth sync task...'

modified_since = ENV['modified_since']

modified_since_sanitized =
if modified_since && !modified_since.strip.empty?
begin
Integer(modified_since)
rescue ArgumentError
puts 'Error: modified_since must be an integer (number of hours).'
exit 1
end
end

clear_downloads = ENV['clear_downloads'].nil? || ENV['clear_downloads'] == 'true'
puts "Will downloads be cleared? #{clear_downloads}"

synchronizer = FolioSync::FolioToHyacinth::HyacinthSynchronizer.new
synchronizer.clear_downloads! if clear_downloads
synchronizer.download_and_sync_folio_to_hyacinth_records(modified_since_sanitized)

if synchronizer.downloading_errors.any? || synchronizer.syncing_errors.any?
puts 'Errors encountered during Folio to Hyacinth sync:'
puts "Downloading Errors: #{synchronizer.downloading_errors}" if synchronizer.downloading_errors.any?
puts "Syncing Errors: #{synchronizer.syncing_errors}" if synchronizer.syncing_errors.any?

exit 1
else
puts 'Folio to Hyacinth sync completed successfully.'
end
end

# Downloads FOLIO MARC records, skipping the syncing step
task download_folio_marc_files: :environment do
modified_since = ENV['modified_since']
modified_since_num =
modified_since_sanitized =
if modified_since && !modified_since.strip.empty?
begin
Integer(modified_since)
Expand All @@ -15,7 +49,7 @@ namespace :folio_sync do
end

downloader = FolioSync::FolioToHyacinth::MarcDownloader.new
downloader.download_965hyacinth_marc_records(modified_since_num)
downloader.download_965hyacinth_marc_records(modified_since_sanitized)

if downloader.downloading_errors.present?
puts "Errors encountered during MARC download: #{downloader.downloading_errors}"
Expand All @@ -34,43 +68,19 @@ namespace :folio_sync do
downloader.download_single_965hyacinth_marc_record(folio_hrid)
end

# WIP: This task syncs all downloaded FOLIO MARC records to Hyacinth
# Syncs all previously downloaded FOLIO MARC records to Hyacinth
task sync_to_hyacinth: :environment do
puts 'Starting Folio to Hyacinth sync task...'
file_dir = Rails.root.join('tmp/working_data/development/folio_to_hyacinth/downloaded_files')

# For each MARC file in the download directory, create or update the corresponding Hyacinth record
Dir.glob(File.join(file_dir, '*.mrc')).each do |marc_file_path|
puts "Processing MARC file: #{marc_file_path}"

# Check if the record already exists in Hyacinth
folio_hrid = File.basename(marc_file_path, '.mrc')
potential_clio_identifier = "clio#{folio_hrid}"
client = FolioSync::Hyacinth::Client.instance
results = client.find_by_identifier(potential_clio_identifier,
{ f: { digital_object_type_display_label_sim: ['Item'] } })
puts "Found #{results.length} records with identifier #{potential_clio_identifier}."

# TODO: Eventually this logic will be placed under FolioToHyacinth namespace
if results.empty?
puts 'No records found. Creating a new record in Hyacinth.'
new_record = FolioToHyacinthRecord.new(marc_file_path)
puts "New record digital object data: #{new_record.digital_object_data}"
response = client.create_new_record(new_record.digital_object_data, publish: true)
puts "Response from Hyacinth when creating record with hrid #{folio_hrid}: #{response.inspect}"
elsif results.length == 1
pid = results.first['pid']
puts "Found 1 record with pid: #{pid}."

# Get only the data needed for update
preserved_data = { 'identifiers' => results.first['identifiers'] }
updated_record = FolioToHyacinthRecord.new(marc_file_path, preserved_data)
puts "Updated record digital object data: #{updated_record.digital_object_data}"
response = client.update_existing_record(pid, updated_record.digital_object_data, publish: true)
puts "Response from Hyacinth when updating record #{pid}: #{response.inspect}"
else
puts "Error: Found multiple records with identifier #{potential_clio_identifier}."
end

synchronizer = FolioSync::FolioToHyacinth::HyacinthSynchronizer.new
synchronizer.prepare_and_sync_folio_to_hyacinth_records

if synchronizer.syncing_errors.any?
puts 'Errors encountered during Folio to Hyacinth sync:'
puts "Syncing Errors: #{synchronizer.syncing_errors}" if synchronizer.syncing_errors.any?
exit 1
else
puts 'Folio to Hyacinth sync completed successfully.'
end
end

Expand All @@ -91,7 +101,7 @@ namespace :folio_sync do
end

# Add 965p field with value academic_commons, ensure 965$a is set to 965hyacinth
marc_record.append(MARC::DataField.new('965', ' ', ' ', ['a', '965hyacinth'], ['p', 'academic_commons'], ['p', 'test']))
marc_record.append(MARC::DataField.new('965', ' ', ' ', ['a', '965hyacinth'], ['p', 'Test']))
puts "Modified MARC record with new 965 field: #{marc_record.inspect}"

new_filepath = Rails.root.join(Rails.configuration.folio_to_hyacinth[:download_directory], 'modified_marc.mrc')
Expand All @@ -100,7 +110,12 @@ namespace :folio_sync do
writer.write(marc_record)
writer.close
end
puts "Final MARC record: #{marc_record.inspect}"
reader = MARC::Reader.new(new_filepath.to_s)
reader.each do |record|
record.fields.each_by_tag(['965']) do |field|
puts field
end
end
end

task create_new_hyacinth_record: :environment do
Expand Down
Loading