Class: CdmMigrator::CreateWorkJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/cdm_migrator/create_work_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(ingest_work, user, admin_set_id, collection_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/jobs/cdm_migrator/create_work_job.rb', line 5

def perform(ingest_work, user, admin_set_id, collection_id)
  admin_set = ::AdminSet.find(admin_set_id) rescue nil
  collection = ::Collection.find(collection_id) rescue nil
  work = Object.const_get(ingest_work.work_type).new
  #status_after, embargo_date, lease_date = nil, nil, nil
  work.(user)
  work.attributes = ingest_work.data
  if ingest_work.data.has_key? 'downloadable'
    # Convert string to boolean
    work.downloadable = ActiveModel::Type::Boolean.new.cast(ingest_work.data['downloadable'])
  elsif work.attributes.include? 'downloadable' # Set work to downloadable by default
    work.downloadable = true
  end
  work.member_of_collections = [collection] if collection
  work.admin_set = admin_set if admin_set
  work.date_uploaded = DateTime.now
  begin
    work.save!
  # Weird error where descriptions with whitespace chars \n or \r don't save the 1st time
  # but do on the second
  rescue Ldp::BadRequest
    old_descr = work.description.clone.to_a
    work.description = work.description.map { |w| w.gsub("\n","").gsub("\r","") }
    work.save!
    work.description = old_descr
    work.save!
  end
  # To use the BatchCreateFilesWithOrderedMembersJob instead, replace the following line
  # with BatchCreateFilesWithOrderedMembersJob.perform_later(work, ingest_work, user)
  BatchCreateFilesJob.perform_later(work, ingest_work, user)
end