From 08fd122687ad6fab371e7f537c9627537c4681dc Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Thu, 1 Feb 2024 16:14:49 -0800 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=A7=B9=20Make=20the=20relationships?= =?UTF-8?q?=20job=20work=20for=20Valkyrie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will add a relationships path for Valkyrie objects. It also will add a transactions call so set child flag will fire off in IIIF Print. ref: - https://github.com/scientist-softserv/hykuup_knapsack/issues/141 --- app/factories/bulkrax/object_factory.rb | 2 +- app/jobs/bulkrax/create_relationships_job.rb | 25 ++++++++++++++++--- .../active_fedora_adapter.rb | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/factories/bulkrax/object_factory.rb b/app/factories/bulkrax/object_factory.rb index 02af478e..65366175 100644 --- a/app/factories/bulkrax/object_factory.rb +++ b/app/factories/bulkrax/object_factory.rb @@ -92,7 +92,7 @@ def find return found if found.present? rescue Valkyrie::Persistence::ObjectNotFoundError false - ensure + else search_by_identifier if attributes[work_identifier].present? end diff --git a/app/jobs/bulkrax/create_relationships_job.rb b/app/jobs/bulkrax/create_relationships_job.rb index a0dfc44c..2940958e 100644 --- a/app/jobs/bulkrax/create_relationships_job.rb +++ b/app/jobs/bulkrax/create_relationships_job.rb @@ -81,7 +81,13 @@ def perform(parent_identifier:, importer_run_id:) # rubocop:disable Metrics/AbcS if @parent_record_members_added parent_record.save! # Ensure that the new relationship gets indexed onto the children - @child_members_added.each(&:update_index) + if parent_record.kind_of?(Valkyrie::Resource) + @child_members_added.each do |child| + Hyrax.index_adapter.save(resource: child) + end + else + @child_members_added.each(&:update_index) + end end end else @@ -165,13 +171,26 @@ def add_to_collection(child_record, parent_record) end def add_to_work(child_record, parent_record) - return true if parent_record.ordered_members.to_a.include?(child_record) + parent_record.kind_of?(Valkyrie::Resource) ? add_to_valkyrie_work(child_record, parent_record) : add_to_af_work(child_record, parent_record) - parent_record.ordered_members << child_record @parent_record_members_added = true @child_members_added << child_record end + def add_to_valkyrie_work(child_record, parent_record) + return true if parent_record.member_ids.include?(child_record.id) + + parent_record.member_ids << child_record.id + change_set = Hyrax::ChangeSet.for(parent_record) + Hyrax::Transactions::Container['change_set.update_work'].call(change_set) + end + + def add_to_af_work(child_record, parent_record) + return true if parent_record.ordered_members.to_a.include?(child_record) + + parent_record.ordered_members << child_record + end + def reschedule(parent_identifier:, importer_run_id:) CreateRelationshipsJob.set(wait: 10.minutes).perform_later( parent_identifier: parent_identifier, diff --git a/lib/bulkrax/persistence_layer/active_fedora_adapter.rb b/lib/bulkrax/persistence_layer/active_fedora_adapter.rb index 5ea70c5c..884dc372 100644 --- a/lib/bulkrax/persistence_layer/active_fedora_adapter.rb +++ b/lib/bulkrax/persistence_layer/active_fedora_adapter.rb @@ -6,7 +6,7 @@ class ActiveFedoraAdapter < AbstractAdapter def self.find(id) ActiveFedora::Base.find(id) rescue ActiveFedora::ObjectNotFoundError => e - raise PersistenceLayer::RecordNotFound, e.message + raise PersistenceLayer::ObjectNotFoundError, e.message end def self.query(q, **kwargs) From 30021c9b092db1bbace5f983bf76be6ea90bc54d Mon Sep 17 00:00:00 2001 From: Shana Moore Date: Thu, 1 Feb 2024 16:26:56 -0800 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=92=84=20rubocop=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Kirk Wang --- app/jobs/bulkrax/create_relationships_job.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/jobs/bulkrax/create_relationships_job.rb b/app/jobs/bulkrax/create_relationships_job.rb index 2940958e..e27a0274 100644 --- a/app/jobs/bulkrax/create_relationships_job.rb +++ b/app/jobs/bulkrax/create_relationships_job.rb @@ -81,7 +81,7 @@ def perform(parent_identifier:, importer_run_id:) # rubocop:disable Metrics/AbcS if @parent_record_members_added parent_record.save! # Ensure that the new relationship gets indexed onto the children - if parent_record.kind_of?(Valkyrie::Resource) + if parent_record.is_a?(Valkyrie::Resource) @child_members_added.each do |child| Hyrax.index_adapter.save(resource: child) end @@ -171,7 +171,7 @@ def add_to_collection(child_record, parent_record) end def add_to_work(child_record, parent_record) - parent_record.kind_of?(Valkyrie::Resource) ? add_to_valkyrie_work(child_record, parent_record) : add_to_af_work(child_record, parent_record) + parent_record.is_a?(Valkyrie::Resource) ? add_to_valkyrie_work(child_record, parent_record) : add_to_af_work(child_record, parent_record) @parent_record_members_added = true @child_members_added << child_record From 60aff984ef0ff1e7283de0ba29004a99c3815433 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Fri, 2 Feb 2024 11:54:06 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Adjust=20rescue=20logi?= =?UTF-8?q?c=20to=20move=20closer=20to=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also adds some consideration for refactoring the queries to instead use the persistence layer. --- app/factories/bulkrax/object_factory.rb | 9 ++++++--- app/jobs/bulkrax/create_relationships_job.rb | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/factories/bulkrax/object_factory.rb b/app/factories/bulkrax/object_factory.rb index 65366175..8aa26133 100644 --- a/app/factories/bulkrax/object_factory.rb +++ b/app/factories/bulkrax/object_factory.rb @@ -90,17 +90,18 @@ def update def find found = find_by_id if attributes[:id].present? return found if found.present? - rescue Valkyrie::Persistence::ObjectNotFoundError + return search_by_identifier if attributes[work_identifier].present? false - else - search_by_identifier if attributes[work_identifier].present? end def find_by_id + # TODO: Push logic into Bulkrax.persistence_adapter; maybe # Rails / Ruby upgrade, we moved from :exists? to :exist? However we want to continue (for a # bit) to support older versions. method_name = klass.respond_to?(:exist?) ? :exist? : :exists? klass.find(attributes[:id]) if klass.send(method_name, attributes[:id]) + rescue Valkyrie::Persistence::ObjectNotFoundError + false end def find_or_create @@ -110,11 +111,13 @@ def find_or_create end def search_by_identifier + # TODO: Push logic into Bulkrax.persistence_adapter; maybe query = { work_identifier_search_field => source_identifier_value } # 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 = klass.where(query).detect { |m| m.send(work_identifier).include?(source_identifier_value) } return match if match end diff --git a/app/jobs/bulkrax/create_relationships_job.rb b/app/jobs/bulkrax/create_relationships_job.rb index e27a0274..5ec0d81e 100644 --- a/app/jobs/bulkrax/create_relationships_job.rb +++ b/app/jobs/bulkrax/create_relationships_job.rb @@ -80,6 +80,7 @@ def perform(parent_identifier:, importer_run_id:) # rubocop:disable Metrics/AbcS # save record if members were added if @parent_record_members_added parent_record.save! + # TODO: Push logic into Bulkrax.persistence_adapter # Ensure that the new relationship gets indexed onto the children if parent_record.is_a?(Valkyrie::Resource) @child_members_added.each do |child| From f889c31c1ed6d1dbcf094ac8475d75aeefa1abcc Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Fri, 2 Feb 2024 11:59:54 -0500 Subject: [PATCH 4/4] Adding notes about transactions --- app/jobs/bulkrax/create_relationships_job.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/jobs/bulkrax/create_relationships_job.rb b/app/jobs/bulkrax/create_relationships_job.rb index 5ec0d81e..d52a2349 100644 --- a/app/jobs/bulkrax/create_relationships_job.rb +++ b/app/jobs/bulkrax/create_relationships_job.rb @@ -182,6 +182,14 @@ def add_to_valkyrie_work(child_record, parent_record) return true if parent_record.member_ids.include?(child_record.id) parent_record.member_ids << child_record.id + + # TODO: Hyrax is in the process of extracting an "Action" object that we could call. It does + # provide validation that we may want to consider. + # + # NOTE: We may need to look at the step args we're passing, see + # `Hyrax::WorksControllerBehavior#update_valkyrie_work` + # Hyrax's `./app/controllers/concerns/hyrax/works_controller_behavior.rb` + # change_set = Hyrax::ChangeSet.for(parent_record) Hyrax::Transactions::Container['change_set.update_work'].call(change_set) end