Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use find_by_source_identifier instead of find_by_bulkrax_identifier #907

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 3 additions & 2 deletions app/factories/bulkrax/object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class ObjectFactory # rubocop:disable Metrics/ClassLength
class_attribute :transformation_removes_blank_hash_values, default: false

define_model_callbacks :save, :create
attr_reader :attributes, :object, :source_identifier_value, :klass, :replace_files, :update_files, :work_identifier, :work_identifier_search_field, :related_parents_parsed_mapping, :importer_run_id
attr_reader :attributes, :object, :source_identifier_value, :klass, :replace_files, :update_files, :work_identifier, :work_identifier_search_field, :related_parents_parsed_mapping, :importer_run_id, :entry

# rubocop:disable Metrics/ParameterLists
def initialize(attributes:, source_identifier_value:, work_identifier:, work_identifier_search_field:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, importer_run_id: nil, update_files: false)
def initialize(attributes:, source_identifier_value:, work_identifier:, work_identifier_search_field:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, importer_run_id: nil, update_files: false, entry:)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worries this is a breaking change for upgraded non valkyrie users.

Should I make it optional?

Copy link
Contributor Author

@ShanaLMoore ShanaLMoore Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disregard, I don't need it. Object factory is already passing work_identifier: parser.work_identifier.

@attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes)
@replace_files = replace_files
@update_files = update_files
Expand All @@ -42,6 +42,7 @@ def initialize(attributes:, source_identifier_value:, work_identifier:, work_ide
@source_identifier_value = source_identifier_value
@klass = klass || Bulkrax.default_work_type.constantize
@importer_run_id = importer_run_id
@entry = entry
end
# rubocop:enable Metrics/ParameterLists

Expand Down
5 changes: 4 additions & 1 deletion app/factories/bulkrax/valkyrie_object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def search_by_identifier
# Query can return partial matches (something6 matches both something6 and something68)
# so we need to weed out any that are not the correct full match. But other items might be
# in the multivalued field, so we have to go through them one at a time.
match = Hyrax.query_service.custom_queries.find_by_bulkrax_identifier(identifier: source_identifier_value)
match = Hyrax.query_service.custom_queries.find_by_source_identifier(
work_identifier: entry.work_identifier,
source_identifier_value: source_identifier_value
)

return match if match
rescue => err
Expand Down
3 changes: 2 additions & 1 deletion app/models/concerns/bulkrax/import_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def factory
user: user,
klass: factory_class,
importer_run_id: importerexporter.last_run.id,
update_files: update_files)
update_files: update_files,
entry: self)
end

def factory_class
Expand Down
4 changes: 2 additions & 2 deletions app/parsers/bulkrax/application_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def records(_opts = {})
# @return [Symbol] the name of the identifying property in the source system from which we're
# importing (e.g. is *not* this application that mounts *this* Bulkrax engine).
#
# @see #work_identifier
# @see #source_identifier
# @see https://github.com/samvera-labs/bulkrax/wiki/CSV-Importer#source-identifier Bulkrax Wiki regarding source identifier
def source_identifier
@source_identifier ||= get_field_mapping_hash_for('source_identifier')&.values&.first&.[]('from')&.first&.to_sym || :source_identifier
end

# @return [Symbol] the name of the identifying property for the system which we're importing
# into (e.g. the application that mounts *this* Bulkrax engine)
# @see #source_identifier
# @see #work_identifier
def work_identifier
@work_identifier ||= get_field_mapping_hash_for('source_identifier')&.keys&.first&.to_sym || :source
end
Expand Down
35 changes: 35 additions & 0 deletions app/services/hyrax/custom_queries/find_by_source_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Hyrax
module CustomQueries
##
# @see https://github.com/samvera/valkyrie/wiki/Queries#custom-queries
class FindBySourceIdentifier
def self.queries
[:find_by_source_identifier]
end

def initialize(query_service:)
@query_service = query_service
end

attr_reader :query_service
delegate :resource_factory, to: :query_service
delegate :orm_class, to: :resource_factory

##
# @param identifier String
def find_by_source_identifier(work_identifier:, source_identifier_value:)
sql_query = sql_by_source_identifier
query_service.run_query(sql_query, work_identifier, source_identifier_value).first
end

def sql_by_source_identifier
<<-SQL
SELECT * FROM orm_resources
WHERE metadata -> ? ->> 0 = ?;
SQL
end
end
end
end
28 changes: 28 additions & 0 deletions app/services/wings/custom_queries/find_by_source_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Wings
module CustomQueries
class FindBySourceIdentifier
# Custom query override specific to Wings

def self.queries
[:find_by_source_identifier]
end

attr_reader :query_service
delegate :resource_factory, to: :query_service

def initialize(query_service:)
@query_service = query_service
end

def find_by_source_identifier(identifier:, use_valkyrie: true)
af_object = ActiveFedora::Base.where("bulkrax_identifier_sim:#{identifier}").first

return af_object unless use_valkyrie

resource_factory.to_resource(object: af_object)
end
end
end
end
3 changes: 2 additions & 1 deletion spec/factories/bulkrax_object_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
attributes: {},
source_identifier_value: :source_identifier,
work_identifier: :source,
work_identifier_search_field: 'source_sim'
work_identifier_search_field: 'source_sim',
entry: FactoryBot.build(:bulkrax_entry)
)
end
end
Expand Down
6 changes: 4 additions & 2 deletions spec/models/bulkrax/object_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module Bulkrax
factory = described_class.new(attributes: attributes,
source_identifier_value: 123,
work_identifier: "filled_string",
work_identifier_search_field: 'filled_string_sim')
work_identifier_search_field: 'filled_string_sim',
entry: FactoryBot.build(:bulkrax_entry))
factory.base_permitted_attributes = %i[empty_array empty_string filled_array filled_string]
expect(factory.send(:transform_attributes)).to eq(attributes.stringify_keys)
end
Expand All @@ -34,7 +35,8 @@ module Bulkrax
factory = described_class.new(attributes: attributes,
source_identifier_value: 123,
work_identifier: "filled_string",
work_identifier_search_field: 'filled_string_sim')
work_identifier_search_field: 'filled_string_sim',
entry: FactoryBot.build(:bulkrax_entry))
factory.base_permitted_attributes = %i[empty_array empty_string filled_array filled_string]
factory.transformation_removes_blank_hash_values = true
expect(factory.send(:transform_attributes))
Expand Down
Loading