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



109
110
111
112
113
114
115
# File 'app/models/bulkrax/exporter.rb', line 109

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



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

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

#exportObject



19
20
21
22
23
24
# File 'app/models/bulkrax/exporter.rb', line 19

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

#export_from_listObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/bulkrax/exporter.rb', line 81

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



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

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

#export_source_collectionObject



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

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.



33
34
35
# File 'app/models/bulkrax/exporter.rb', line 33

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

#export_source_worktypeObject



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

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

#export_type_listObject



98
99
100
101
102
103
# File 'app/models/bulkrax/exporter.rb', line 98

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

#exporter_export_pathObject



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

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



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

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



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

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

Returns:

  • (Boolean)


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

def generated_metadata?
  self.
end

#importers_listObject



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

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

#include_thumbnails?Boolean

Returns:

  • (Boolean)


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

def include_thumbnails?
  self.include_thumbnails
end

#last_runObject



117
118
119
# File 'app/models/bulkrax/exporter.rb', line 117

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

#mappingObject

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



71
72
73
74
75
76
77
78
79
# File 'app/models/bulkrax/exporter.rb', line 71

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

Returns:

  • (Boolean)


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

def 
  export_type == 'metadata'
end

#remove_and_rerunObject



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

def remove_and_rerun
  self.parser_fields['remove_and_rerun']
end

#setup_export_pathObject



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

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

#sort_zip_files(zip_files) ⇒ Object



148
149
150
151
152
153
154
155
# File 'app/models/bulkrax/exporter.rb', line 148

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



57
58
59
60
61
62
63
64
# File 'app/models/bulkrax/exporter.rb', line 57

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



66
67
68
# File 'app/models/bulkrax/exporter.rb', line 66

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