-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
4,476 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module Importer; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Importer | ||
class AsynchronousRecordImporter < Darlingtonia::RecordImporter | ||
## | ||
# @param error_stream [#<<] | ||
def initialize(error_stream: Importer::SerializableStream.new(ActiveJob::Base.logger), | ||
info_stream: Importer::SerializableStream.new(ActiveJob::Base.logger)) | ||
self.error_stream = error_stream | ||
self.info_stream = info_stream | ||
end | ||
|
||
## | ||
# @param record [InputRecord] | ||
# | ||
# @return [void] | ||
def import(record:) | ||
MigrationImportJob.perform_later(record.pid, | ||
record.mapper.source_host, | ||
error_stream, | ||
info_stream) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Importer | ||
class LaevigataImporter < Darlingtonia::Parser | ||
## | ||
# @!attribute [rw] source_host | ||
# @return [String] | ||
attr_accessor :source_host | ||
|
||
class << self | ||
## | ||
# Matches all "file" types; we assume anything passed in will be a list | ||
# of Fedora 3 PIDs. | ||
def match?(**_opts) | ||
true | ||
end | ||
end | ||
|
||
## | ||
# Gives a {Darlingtonia::InputRecord} for each pid in the {#file}. | ||
def records | ||
return enum_for(:records) unless block_given? | ||
|
||
file.each_line do |pid| | ||
yield Darlingtonia::InputRecord | ||
.from(metadata: pid.strip, | ||
mapper: MigrationMapper.new(source_host: source_host)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
module Importer | ||
class MigrationImportJob < ActiveJob::Base | ||
queue_as :migration | ||
|
||
def perform(record, source_host, error_stream, info_stream) | ||
pid = record | ||
record = Darlingtonia::InputRecord | ||
.from(metadata: record, | ||
mapper: Importer::MigrationMapper.new(source_host: source_host)) | ||
|
||
attributes = record.attributes.dup | ||
attributes.delete(:pid) | ||
|
||
if attributes[:email] | ||
user_key = "migration_#{attributes[:email].first.split('@').first}" | ||
|
||
# Create a user and update email/display_name; we can tolerate churn in | ||
# these attributes in the case of multiple submissions by the same user. | ||
depositor = User.find_or_create_by(ppid: user_key) | ||
depositor.update_attributes(email: attributes[:email].first, | ||
display_name: attributes[:creator]&.first) | ||
else | ||
depositor = User.find_or_create_system_user('migration_user') | ||
end | ||
|
||
binary_file = record.mapper.primary_file | ||
primary_file = File.open(tmp_path(binary_file.name), 'w', encoding: 'ascii-8bit') | ||
primary_file.write(binary_file.content) | ||
|
||
attributes[:uploaded_files] = | ||
[Hyrax::UploadedFile.create(user: depositor, file: primary_file, pcdm_use: FileSet::PRIMARY).id] | ||
|
||
primary_file.close | ||
File.unlink(primary_file) | ||
|
||
record.mapper | ||
.supplementary_files | ||
.each_with_object(attributes[:uploaded_files]) do |supplementary_file, files| | ||
file = File.open(tmp_path(supplementary_file.name), 'w', encoding: 'ascii-8bit') | ||
file.write(supplementary_file.content) | ||
|
||
files << Hyrax::UploadedFile.create(user: depositor, | ||
file: file, | ||
pcdm_use: FileSet::SUPPLEMENTARY, | ||
title: supplementary_file.name).id | ||
|
||
file.close | ||
File.unlink(file) | ||
end | ||
|
||
actor = Hyrax::Actors::ActorStack.new(Etd.new, ::Ability.new(depositor), stack_actors) | ||
actor.create(attributes) | ||
|
||
info_stream << "INFO: [#{pid}] created." | ||
rescue Ldp::Error, Faraday::ConnectionFailed => e | ||
info_stream << "INFO: [#{pid}] soft failed, requeueing.\n#{e.class}:\n\t#{e.message}" | ||
raise e | ||
rescue MigrationMapper::MappingError => e | ||
error_stream << "ERROR: [#{pid}] failed on mapping.\n#{e.class}:\n\t#{e.message}" | ||
rescue => e | ||
error_stream << "ERROR: [#{pid}] soft failed, requeueing.\n#{e.class}:\n\t#{e.message}" | ||
raise e | ||
end | ||
|
||
def stack_actors | ||
[Hyrax::Actors::TransactionalRequest, | ||
Hyrax::Actors::OptimisticLockValidator, | ||
Hyrax::CreateWithFilesActor, | ||
Hyrax::Actors::AddAsMemberOfCollectionsActor, | ||
Hyrax::Actors::AddToWorkActor, | ||
Hyrax::Actors::AssignRepresentativeActor, | ||
Hyrax::Actors::AttachFilesActor, | ||
Hyrax::Actors::AttachMembersActor, | ||
Hyrax::Actors::ApplyOrderActor, | ||
Hyrax::Actors::InterpretVisibilityActor, | ||
SetEmbargoActor, | ||
ImportAdminSetActor, | ||
Hyrax::ApplyPermissionTemplateActor, | ||
Hyrax::Actors::EtdActor, | ||
Hyrax::Actors::InitializeWorkflowActor] | ||
end | ||
|
||
def tmp_path(name) | ||
Pathname.new(Dir.tmpdir).join(name) | ||
end | ||
|
||
class ImportAdminSetActor < Hyrax::Actors::AbstractActor | ||
## | ||
# @see Hyrax::AbstractActor#create | ||
def create(attributes) | ||
attributes[:admin_set_id] = AdminSet.find_or_create_default_admin_set_id | ||
next_actor.create(attributes) | ||
end | ||
end | ||
|
||
class SetEmbargoActor < Hyrax::Actors::AbstractActor | ||
## | ||
# @see Hyrax::AbstractActor#create | ||
def create(attributes) | ||
apply_embargo(date: attributes.delete(:embargo_lift_date)) | ||
next_actor.create(attributes) | ||
end | ||
|
||
private | ||
|
||
def apply_embargo(date:) | ||
return unless date && date >= Time.zone.today | ||
|
||
curation_concern.apply_embargo( | ||
date, | ||
Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC, | ||
Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC | ||
) | ||
|
||
return unless curation_concern.embargo | ||
curation_concern.embargo.save | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.