Class: Hybridge::Batch::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/hybridge/batch/entry.rb

Instance Method Summary collapse

Constructor Details

#initialize(work, files, collection_id, current_user, package_location) ⇒ Entry

Returns a new instance of Entry.



5
6
7
8
9
10
11
12
13
# File 'lib/hybridge/batch/entry.rb', line 5

def initialize(work, files, collection_id, current_user, package_location)
  @work = work
  @files = files
  @collection_id = collection_id
  @current_user = current_user
  @package_location = package_location

  process!
end

Instance Method Details

#based_near_attributes(values) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/hybridge/batch/entry.rb', line 63

def based_near_attributes(values)
  based_near_values = {}
  values.each_with_index do |value, index|
    based_near_values[index.to_s] = { "id" => value, "_destroy" => "" }
  end
  based_near_values
end

#field_attributes(work_form, work_type) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hybridge/batch/entry.rb', line 33

def field_attributes(work_form, work_type)
  data = {}
  @work.each do |key, attribute|
    field_sym = field_to_sym(key)
    if (field_sym.nil? || attribute.nil? || attribute.empty? || key.to_s.include?("Object Type") || !work_form.terms.include?(field_sym))
      next
    elsif(field_sym.id2name == "based_near")
      data[:based_near_attributes] = based_near_attributes(attribute.split("; "))
    elsif(field_sym.id2name == "rights_statement")
      active = Hyrax.config.rights_statement_service_class.new.active?(attribute) rescue false
      if active
        data[field_sym] = attribute
      else
        message = "Invalid/Inactive Rights Statement URL '#{attribute}' for '#{data[:title].first.to_s}'"
        Hyrax::MessengerService.deliver(User.batch_user, @current_user, message, "HyBridge Warning: Invalid/Inactive Rights Statement URL")
      end
    elsif(work_type.send(field_sym).nil?)
      data[field_sym] = attribute
    else
      data[field_sym] = attribute.split "; "
    end

    if !@collection_id.nil? && !@collection_id.empty?
      data[:member_of_collections_attributes] = {"0" => { "id" => @collection_id, "_destroy" => "false" }}
    end
    
  end
  data
end

#field_to_sym(field) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hybridge/batch/entry.rb', line 93

def field_to_sym(field)
  if field.downcase == "abstract or summary"
    field = "description"
  elsif field.downcase == "abstract / summary"
    field = "description"
  elsif field.downcase == "location"
    field = "based_near"
  end

  field.downcase.strip.gsub(/\s/,'_').to_sym
end

#file_location(filename) ⇒ Object



71
72
73
# File 'lib/hybridge/batch/entry.rb', line 71

def file_location(filename)
  File.join(@package_location, filename)
end

#process!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hybridge/batch/entry.rb', line 15

def process!
  work_type = @work["Object Type"].constantize.new
  work_form = ("Hyrax::" + @work["Object Type"] + "Form").constantize

  work_attributes = field_attributes(work_form, work_type)

  missing = work_form.required_fields - work_attributes.keys
  if !missing.empty?
    message = "Missing required fields: #{missing.map{|m| m.to_s.inspect}.join(', ')} for '#{work_attributes[:title].first}'"
    Hyrax::MessengerService.deliver(User.batch_user, @current_user, message, "HyBridge Warning: Missing required fields")
  end

  env = Hyrax::Actors::Environment.new(work_type, Ability.new(@current_user), work_attributes)
  work_actor = Hyrax::CurationConcern.actor.create(env)

  process_files! work_type unless @files.nil?
end

#process_files!(work_type) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hybridge/batch/entry.rb', line 75

def process_files!(work_type)
  work_permissions = work_type.permissions.map(&:to_hash)
  @files.each do |file_object|
    file_path = file_location(file_object["Filename"])
    if !File.file?(file_path)
      message = "Unable to find file '#{file_object["Filename"]}' for '#{work_type[:title].first.to_s}'"
      Hyrax::MessengerService.deliver(User.batch_user, @current_user, message, "HyBridge Warning: Missing file")
      next
    end

    file_actor = Hyrax::Actors::FileSetActor.new(FileSet.create, @current_user)
    file_actor.
    file_actor.create_content(File.new(file_path))
    file_actor.attach_to_work(work_type)
    file_actor.file_set.permissions_attributes = work_permissions
  end
end