Skip to content

Commit

Permalink
add some additionally functionality that was discovered while fixing …
Browse files Browse the repository at this point in the history
…leases in #6127
  • Loading branch information
alishaevn committed Aug 4, 2023
1 parent 255437e commit 8e77bca
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
13 changes: 13 additions & 0 deletions app/indexers/hyrax/valkyrie_file_set_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def to_solr # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Met
solr_doc['hasRelatedMediaFragment_ssim'] = resource.representative_id.to_s
solr_doc['hasRelatedImage_ssim'] = resource.thumbnail_id.to_s

index_embargo(solr_doc)
# Add in metadata from the original file.
file_metadata = Hyrax::FileSetFileService.new(file_set: resource).original_file
return solr_doc unless file_metadata
Expand Down Expand Up @@ -117,4 +118,16 @@ def file_format(file)
end
end
end

def index_embargo(doc)
if resource.embargo&.active?
doc['embargo_release_date_dtsi'] = resource.embargo.embargo_release_date&.to_datetime
doc['visibility_after_embargo_ssim'] = resource.embargo.visibility_after_embargo
doc['visibility_during_embargo_ssim'] = resource.embargo.visibility_during_embargo
else
doc['embargo_history_ssim'] = resource&.embargo&.embargo_history
end

doc
end
end
38 changes: 35 additions & 3 deletions app/services/hyrax/embargo_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ def release_embargo_for!(resource:, query_service: Hyrax.query_service)
new(resource: resource, query_service: query_service)
.release!
end

# Creates or updates an existing embargo on a member to match the embargo on the parent work
# @param [Array<Valkyrie::Resource>] members
# @param [Hyrax::Work] work
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def create_or_update_embargo_on_members(members, work)
# TODO: account for all members and levels, not just file sets. ref: #6131

members.each do |member|
member_embargo_needs_updating = work.embargo.updated_at > member.embargo&.updated_at if member.embargo

if member.embargo && member_embargo_needs_updating
member.embargo.embargo_release_date = work.embargo['embargo_release_date']
member.embargo.visibility_during_embargo = work.embargo['visibility_during_embargo']
member.embargo.visibility_after_embargo = work.embargo['visibility_after_embargo']
member.embargo = Hyrax.persister.save(resource: member.embargo)
else
work_embargo_manager = Hyrax::EmbargoManager.new(resource: work)
work_embargo_manager.copy_embargo_to(target: member)
member = Hyrax.persister.save(resource: member)
end

user ||= ::User.find_by_user_key(member.depositor)
# the line below works in that it indexes the file set with the necessary lease properties
# I do not know however if this is the best event_id to pass
Hyrax.publisher.publish('object.metadata.updated', object: member, user: user)
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
end

# Deactivates the embargo and logs a message to the embargo_history property
Expand All @@ -126,13 +155,13 @@ def deactivate!
embargo_record = embargo_history_message(
embargo_state,
Time.zone.today,
DateTime.parse(embargo.embargo_release_date),
embargo.embargo_release_date,
embargo.visibility_during_embargo,
embargo.visibility_after_embargo
)

nullify(force: true)
release(force: true)
nullify(force: true)
embargo.embargo_history += [embargo_record]
end

Expand Down Expand Up @@ -180,14 +209,17 @@ def embargo
end

##
# Drop the embargo by setting its release date to `nil`.
# Drop the embargo by setting its release date and visibility settings to `nil`.
#
# @param force [boolean] force the nullify even when the embargo period is current
#
# @return [void]
def nullify(force: false)
return false if !force && under_embargo?

embargo.embargo_release_date = nil
embargo.visibility_during_embargo = nil
embargo.visibility_after_embargo = nil
end

##
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/base/_form_permission_embargo.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="form-inline">
<%= visibility_badge(Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_EMBARGO) %>
<%= f.input :visibility_during_embargo, wrapper: :inline, collection: visibility_options(:restrict), include_blank: false %>
<%= f.input :embargo_release_date, wrapper: :inline, input_html: { value: f.object.embargo_release_date || Date.tomorrow, class: 'datepicker' } %>
<%= f.input :embargo_release_date, wrapper: :inline, input_html: { value: f.object.embargo_release_date&.to_date || Date.tomorrow, class: 'datepicker' } %>
<%= f.input :visibility_after_embargo, wrapper: :inline, collection: visibility_options(:loosen), include_blank: false %>
</div>
6 changes: 6 additions & 0 deletions lib/hyrax/transactions/steps/add_file_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def initialize(handler: Hyrax::WorkUploadsHandler)
# @return [Dry::Monads::Result]
def call(obj, uploaded_files: [], file_set_params: [])
if @handler.new(work: obj).add(files: uploaded_files, file_set_params: file_set_params).attach
file_sets = obj.member_ids.map do |member|
Hyrax.query_service.find_by(id: member) if Hyrax.query_service.find_by(id: member).is_a? Hyrax::FileSet
end

Hyrax::LeaseManager.create_or_update_lease_on_members(file_sets, obj)if obj.lease
Hyrax::EmbargoManager.create_or_update_embargo_on_members(file_sets, obj)if obj.embargo
Success(obj)
else
Failure[:failed_to_attach_file_sets, uploaded_files]
Expand Down
6 changes: 5 additions & 1 deletion lib/wings/model_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def build
attrs = attributes.tap { |hash| hash[:new_record] = pcdm_object.new_record? }
attrs[:alternate_ids] = [::Valkyrie::ID.new(pcdm_object.id)] if pcdm_object.id

klass.new(**attrs).tap { |resource| ensure_current_permissions(resource) }
klass.new(**attrs).tap do |resource|
resource.lease = pcdm_object.lease&.valkyrie_resource if pcdm_object.respond_to?(:lease) && pcdm_object.lease
resource.embargo = pcdm_object.embargo&.valkyrie_resource if pcdm_object.respond_to?(:embargo) && pcdm_object.embargo
ensure_current_permissions(resource)
end
end

def ensure_current_permissions(resource)
Expand Down

0 comments on commit 8e77bca

Please sign in to comment.