Class: Bulkrax::Exporter

Inherits:
ApplicationRecord show all
Includes:
ImporterExporterBehavior, StatusInfo
Defined in:
app/models/bulkrax/exporter.rb

Instance Method Summary collapse

Methods included from StatusInfo

#current_status, #failed?, #last_error, #set_status_info, #skipped?, #status, #status_at, #succeeded?

Methods included from ImporterExporterBehavior

#file?, #increment_counters, #key_without_numbers, #keys_without_numbers, #last_imported_at, #next_import_at, #parser, #parser_class, #zip?

Instance Method Details

#current_run(skip_counts: false) ⇒ Object



114
115
116
117
118
119
120
# File 'app/models/bulkrax/exporter.rb', line 114

def current_run(skip_counts: false)
  @current_run ||= self.exporter_runs.create! if skip_counts
  return @current_run if @current_run

  total = self.limit || parser.total
  @current_run = self.exporter_runs.create!(total_work_entries: total, enqueued_records: total)
end

#date_filterObject



50
51
52
# File 'app/models/bulkrax/exporter.rb', line 50

def date_filter
  self.start_date.present? || self.finish_date.present?
end

#exportObject



24
25
26
27
28
29
# File 'app/models/bulkrax/exporter.rb', line 24

def export
  current_run && setup_export_path
  send("create_from_#{self.export_from}")
rescue StandardError => e
  set_status_info(e)
end

#export_from_listObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/bulkrax/exporter.rb', line 86

def export_from_list
  if defined?(::Hyrax)
    [
      [I18n.t('bulkrax.exporter.labels.importer'), 'importer'],
      [I18n.t('bulkrax.exporter.labels.collection'), 'collection'],
      [I18n.t('bulkrax.exporter.labels.worktype'), 'worktype'],
      [I18n.t('bulkrax.exporter.labels.all'), 'all']
    ]
  else
    [
      [I18n.t('bulkrax.exporter.labels.importer'), 'importer'],
      [I18n.t('bulkrax.exporter.labels.collection'), 'collection'],
      [I18n.t('bulkrax.exporter.labels.all'), 'all']
    ]
  end
end

#export_propertiesObject



144
145
146
147
# File 'app/models/bulkrax/exporter.rb', line 144

def export_properties
  # TODO: Does this work for Valkyrie?
  Bulkrax.object_factory.export_properties
end

#export_source_collectionObject



42
43
44
# File 'app/models/bulkrax/exporter.rb', line 42

def export_source_collection
  self.export_source if self.export_from == 'collection'
end

#export_source_importerObject

#export_source accessors Used in form to prevent it from getting confused as to which value to populate #export_source with. Also, used to display the correct selected value when rendering edit form.



38
39
40
# File 'app/models/bulkrax/exporter.rb', line 38

def export_source_importer
  self.export_source if self.export_from == 'importer'
end

#export_source_worktypeObject



46
47
48
# File 'app/models/bulkrax/exporter.rb', line 46

def export_source_worktype
  self.export_source if self.export_from == 'worktype'
end

#export_type_listObject



103
104
105
106
107
108
# File 'app/models/bulkrax/exporter.rb', line 103

def export_type_list
  [
    [I18n.t('bulkrax.exporter.labels.metadata'), 'metadata'],
    [I18n.t('bulkrax.exporter.labels.full'), 'full']
  ]
end

#exporter_export_pathObject



130
131
132
# File 'app/models/bulkrax/exporter.rb', line 130

def exporter_export_path
  @exporter_export_path ||= File.join(parser.base_path('export'), self.id.to_s, self.exporter_runs.last.id.to_s)
end

#exporter_export_zip_filesObject



140
141
142
# File 'app/models/bulkrax/exporter.rb', line 140

def exporter_export_zip_files
  @exporter_export_zip_files ||= Dir["#{exporter_export_zip_path}/**"].map { |zip| Array(zip.split('/').last) }
end

#exporter_export_zip_pathObject



134
135
136
137
138
# File 'app/models/bulkrax/exporter.rb', line 134

def exporter_export_zip_path
  @exporter_export_zip_path ||= File.join(parser.base_path('export'), "export_#{self.id}_#{self.exporter_runs.last.id}")
rescue
  @exporter_export_zip_path ||= File.join(parser.base_path('export'), "export_#{self.id}_0")
end

#generated_metadata?Boolean



58
59
60
# File 'app/models/bulkrax/exporter.rb', line 58

def generated_metadata?
  self.
end

#importers_listObject



110
111
112
# File 'app/models/bulkrax/exporter.rb', line 110

def importers_list
  Importer.all.map { |i| [i.name, i.id] }
end

#include_thumbnails?Boolean



54
55
56
# File 'app/models/bulkrax/exporter.rb', line 54

def include_thumbnails?
  self.include_thumbnails
end

#last_runObject



122
123
124
# File 'app/models/bulkrax/exporter.rb', line 122

def last_run
  @last_run ||= self.exporter_runs.last
end

#mappingObject

If field_mapping is empty, setup a default based on the export_properties



76
77
78
79
80
81
82
83
84
# File 'app/models/bulkrax/exporter.rb', line 76

def mapping
  @mapping ||= self.field_mapping ||
               ActiveSupport::HashWithIndifferentAccess.new(
                 export_properties.map do |m|
                   Bulkrax.default_field_mapping.call(m)
                 end.inject(:merge)
               ) ||
               [{}]
end

#metadata_only?Boolean



149
150
151
# File 'app/models/bulkrax/exporter.rb', line 149

def 
  export_type == 'metadata'
end

#remove_and_rerunObject



31
32
33
# File 'app/models/bulkrax/exporter.rb', line 31

def remove_and_rerun
  self.parser_fields['remove_and_rerun']
end

#setup_export_pathObject



126
127
128
# File 'app/models/bulkrax/exporter.rb', line 126

def setup_export_path
  FileUtils.mkdir_p(exporter_export_path) unless File.exist?(exporter_export_path)
end

#sort_zip_files(zip_files) ⇒ Object



153
154
155
156
157
158
159
160
# File 'app/models/bulkrax/exporter.rb', line 153

def sort_zip_files(zip_files)
  zip_files.sort_by do |item|
    number = item.split('_').last.match(/\d+/)&.[](0) || 0.to_s
    sort_number = number.rjust(4, "0")

    sort_number
  end
end

#work_visibility_listObject



62
63
64
65
66
67
68
69
# File 'app/models/bulkrax/exporter.rb', line 62

def work_visibility_list
  [
    ['Any', ''],
    [I18n.t('hyrax.visibility.open.text'), Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC],
    [I18n.t('hyrax.visibility.restricted.text'), Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE],
    [I18n.t('hyrax.visibility.authenticated.text', institution: I18n.t('hyrax.institution_name')), Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_AUTHENTICATED]
  ]
end

#workflow_status_listObject



71
72
73
# File 'app/models/bulkrax/exporter.rb', line 71

def workflow_status_list
  Sipity::WorkflowState.all.map { |s| [s.name&.titleize, s.name] }.uniq if defined?(::Hyrax)
end