Skip to content

Commit

Permalink
Merge 739d8f6 into 799fbb5
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Johnson authored Mar 22, 2018
2 parents 799fbb5 + 739d8f6 commit 31ca638
Show file tree
Hide file tree
Showing 22 changed files with 4,476 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ inherit_gem:
bixby: bixby_default.yml

AllCops:
TargetRubyVersion: 2.3
Exclude:
- 'db/**/*'
- 'script/**/*'
- 'vendor/**/*'
- 'tmp/**/*'
- 'config/initializers/explain_partials.rb'

# BEGIN; Disabled in move to 2.3 sytax linter
Style/FrozenStringLiteralComment:
Enabled: false

Style/NumericPredicate:
Enabled: false

Style/SafeNavigation:
Enabled: false
# END; Disabled in move to 2.3 sytax linter

Layout/SpaceAroundOperators:
Exclude:
- 'spec/fixtures/form_submission_params/*'
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end
gem 'clamby', '>= 1.2.5'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails'
gem 'darlingtonia', github: 'curationexperts/darlingtonia', branch: 'master'
gem 'devise'
gem 'devise-guests', '~> 0.5'
gem 'dotenv-rails'
Expand Down Expand Up @@ -77,7 +78,7 @@ group :development, :test do
gem 'shoulda-matchers'
gem 'solr_wrapper', '>= 0.3'
gem 'vcr'
gem 'webmock'
gem 'webmock', '~> 3.3'
end

group :development do
Expand Down
15 changes: 12 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: https://github.com/curationexperts/darlingtonia.git
revision: 264b2399850aee436a1816918184c50508ee6020
branch: master
specs:
darlingtonia (1.0.0)
active-fedora (>= 11.0, <= 12.99)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -303,7 +311,7 @@ GEM
tilt
hamster (3.0.0)
concurrent-ruby (~> 1.0)
hashdiff (0.3.2)
hashdiff (0.3.7)
hashie (3.5.7)
hiredis (0.6.1)
honeybadger (3.3.0)
Expand Down Expand Up @@ -866,7 +874,7 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
webmock (2.3.2)
webmock (3.3.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
Expand Down Expand Up @@ -899,6 +907,7 @@ DEPENDENCIES
clamby (>= 1.2.5)
coffee-rails
coveralls
darlingtonia!
database_cleaner
devise
devise-guests (~> 0.5)
Expand Down Expand Up @@ -944,7 +953,7 @@ DEPENDENCIES
uglifier (>= 1.3.0)
vcr
web-console (>= 3.3.0)
webmock
webmock (~> 3.3)
whenever
xray-rails
yard
Expand Down
1 change: 1 addition & 0 deletions app/lib/importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Importer; end
22 changes: 22 additions & 0 deletions app/lib/importer/asynchronous_record_importer.rb
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
29 changes: 29 additions & 0 deletions app/lib/importer/laevigata_importer.rb
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
120 changes: 120 additions & 0 deletions app/lib/importer/migration_import_job.rb
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
Loading

0 comments on commit 31ca638

Please sign in to comment.