diff --git a/app/factories/bulkrax/valkyrie_object_factory.rb b/app/factories/bulkrax/valkyrie_object_factory.rb index 5bb9c43c..b6a504a3 100644 --- a/app/factories/bulkrax/valkyrie_object_factory.rb +++ b/app/factories/bulkrax/valkyrie_object_factory.rb @@ -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: work_identifier, + source_identifier_value: source_identifier_value + ) return match if match rescue => err diff --git a/app/parsers/bulkrax/application_parser.rb b/app/parsers/bulkrax/application_parser.rb index 2a46d124..5fe41fe6 100644 --- a/app/parsers/bulkrax/application_parser.rb +++ b/app/parsers/bulkrax/application_parser.rb @@ -68,7 +68,7 @@ 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 @@ -76,7 +76,7 @@ def source_identifier # @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 diff --git a/app/services/hyrax/custom_queries/find_by_source_identifier.rb b/app/services/hyrax/custom_queries/find_by_source_identifier.rb new file mode 100644 index 00000000..6a7f77ea --- /dev/null +++ b/app/services/hyrax/custom_queries/find_by_source_identifier.rb @@ -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 diff --git a/app/services/wings/custom_queries/find_by_source_identifier.rb b/app/services/wings/custom_queries/find_by_source_identifier.rb new file mode 100644 index 00000000..c51da662 --- /dev/null +++ b/app/services/wings/custom_queries/find_by_source_identifier.rb @@ -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