Class: Bulkrax::ApplicationParser

Inherits:
Object
  • Object
show all
Defined in:
app/parsers/bulkrax/application_parser.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

CsvParser, OaiDcParser, XmlParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(importerexporter) ⇒ ApplicationParser

Returns a new instance of ApplicationParser.



27
28
29
30
# File 'app/parsers/bulkrax/application_parser.rb', line 27

def initialize(importerexporter)
  @importerexporter = importerexporter
  @headers = []
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'app/parsers/bulkrax/application_parser.rb', line 6

def headers
  @headers
end

#importerexporterObject Also known as: importer, exporter

Returns the value of attribute importerexporter.



6
7
8
# File 'app/parsers/bulkrax/application_parser.rb', line 6

def importerexporter
  @importerexporter
end

Class Method Details

.export_supported?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/parsers/bulkrax/application_parser.rb', line 19

def self.export_supported?
  false
end

.import_supported?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/parsers/bulkrax/application_parser.rb', line 23

def self.import_supported?
  true
end

.parser_fieldsObject



15
16
17
# File 'app/parsers/bulkrax/application_parser.rb', line 15

def self.parser_fields
  {}
end

Instance Method Details

#base_path(type = 'import') ⇒ Object

Base path for imported and exported files



136
137
138
139
140
# File 'app/parsers/bulkrax/application_parser.rb', line 136

def base_path(type = 'import')
  # account for multiple versions of hyku
  is_multitenant = ENV['HYKU_MULTITENANT'] == 'true' || ENV['SETTINGS__MULTITENANCY__ENABLED'] == 'true'
  is_multitenant ? File.join(Bulkrax.send("#{type}_path"), ::Site.instance..name) : Bulkrax.send("#{type}_path")
end

#collection_entry_classObject

Raises:

  • (StandardError)


38
39
40
# File 'app/parsers/bulkrax/application_parser.rb', line 38

def collection_entry_class
  raise StandardError, 'must be defined'
end

#collections_totalObject



241
242
243
# File 'app/parsers/bulkrax/application_parser.rb', line 241

def collections_total
  0
end

#create_collectionsObject

Raises:

  • (StandardError)


107
108
109
# File 'app/parsers/bulkrax/application_parser.rb', line 107

def create_collections
  raise StandardError, 'must be defined' if importer?
end

#create_file_setsObject

Raises:

  • (StandardError)


115
116
117
# File 'app/parsers/bulkrax/application_parser.rb', line 115

def create_file_sets
  raise StandardError, 'must be defined' if importer?
end

#create_relationshipsObject

Raises:

  • (StandardError)


119
120
121
# File 'app/parsers/bulkrax/application_parser.rb', line 119

def create_relationships
  raise StandardError, 'must be defined' if importer?
end

#create_worksObject

Raises:

  • (StandardError)


111
112
113
# File 'app/parsers/bulkrax/application_parser.rb', line 111

def create_works
  raise StandardError, 'must be defined' if importer?
end

#entry_classObject

Raises:

  • (StandardError)


33
34
35
# File 'app/parsers/bulkrax/application_parser.rb', line 33

def entry_class
  raise StandardError, 'must be defined'
end

#exporter?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'app/parsers/bulkrax/application_parser.rb', line 162

def exporter?
  importerexporter.is_a?(Bulkrax::Exporter)
end

#file?Boolean

Is this a file?

Returns:

  • (Boolean)


280
281
282
# File 'app/parsers/bulkrax/application_parser.rb', line 280

def file?
  parser_fields&.[]('import_file_path') && File.file?(parser_fields['import_file_path'])
end

#file_sets_totalObject



245
246
247
# File 'app/parsers/bulkrax/application_parser.rb', line 245

def file_sets_total
  0
end

#find_or_create_entry(entryclass, identifier, type, raw_metadata = nil) ⇒ Object



217
218
219
220
221
222
223
224
225
226
# File 'app/parsers/bulkrax/application_parser.rb', line 217

def find_or_create_entry(entryclass, identifier, type,  = nil)
  entry = entryclass.where(
    importerexporter_id: importerexporter.id,
    importerexporter_type: type,
    identifier: identifier
  ).first_or_create!
  entry. = 
  entry.save!
  entry
end

#generated_metadata_mappingObject



55
56
57
# File 'app/parsers/bulkrax/application_parser.rb', line 55

def 
   ||= 'generated'
end

#get_field_mapping_hash_for(key) ⇒ Object

Raises:

  • (StandardError)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/parsers/bulkrax/application_parser.rb', line 75

def get_field_mapping_hash_for(key)
  return instance_variable_get("@#{key}_hash") if instance_variable_get("@#{key}_hash").present?

  mapping = importerexporter.field_mapping.is_a?(Hash) ? importerexporter.field_mapping : {}
  instance_variable_set(
    "@#{key}_hash",
    mapping&.with_indifferent_access&.select { |_, h| h.key?(key) }
  )
  raise StandardError, "more than one #{key} declared: #{instance_variable_get("@#{key}_hash").keys.join(', ')}" if instance_variable_get("@#{key}_hash").length > 1

  instance_variable_get("@#{key}_hash")
end

#import_file_pathObject

Path for the import



290
291
292
# File 'app/parsers/bulkrax/application_parser.rb', line 290

def import_file_path
  @import_file_path ||= real_import_file_path
end

#importer?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'app/parsers/bulkrax/application_parser.rb', line 158

def importer?
  importerexporter.is_a?(Bulkrax::Importer)
end

#invalid_record(message) ⇒ Object

rubocop:disable Rails/SkipsModelValidations



193
194
195
196
197
198
199
# File 'app/parsers/bulkrax/application_parser.rb', line 193

def invalid_record(message)
  current_run.invalid_records ||= ""
  current_run.invalid_records += message
  current_run.save
  ImporterRun.find(current_run.id).increment!(:failed_records)
  ImporterRun.find(current_run.id).decrement!(:enqueued_records) unless ImporterRun.find(current_run.id).enqueued_records <= 0 # rubocop:disable Style/IdenticalConditionalBranches
end

#limit_reached?(limit, index) ⇒ boolean

Parameters:

  • limit (Integer)

    limit set on the importerexporter

  • index (Integer)

    index of current iteration

Returns:

  • (boolean)


169
170
171
172
# File 'app/parsers/bulkrax/application_parser.rb', line 169

def limit_reached?(limit, index)
  return false if limit.nil? || limit.zero? # no limit
  index >= limit
end

#model_field_mappingsObject



88
89
90
91
92
93
# File 'app/parsers/bulkrax/application_parser.rb', line 88

def model_field_mappings
  model_mappings = Bulkrax.field_mappings[self.class.to_s]&.dig('model', :from) || []
  model_mappings |= ['model']

  model_mappings
end

#new_entry(entryclass, type) ⇒ Object



210
211
212
213
214
215
# File 'app/parsers/bulkrax/application_parser.rb', line 210

def new_entry(entryclass, type)
  entryclass.new(
    importerexporter_id: importerexporter.id,
    importerexporter_type: type
  )
end

#path_for_importObject

Path where we’ll store the import metadata and files

this is used for uploaded and cloud files


144
145
146
147
148
# File 'app/parsers/bulkrax/application_parser.rb', line 144

def path_for_import
  @path_for_import = File.join(base_path, importerexporter.path_string)
  FileUtils.mkdir_p(@path_for_import) unless File.exist?(@path_for_import)
  @path_for_import
end

#perform_methodObject



95
96
97
98
99
100
101
# File 'app/parsers/bulkrax/application_parser.rb', line 95

def perform_method
  if self.validate_only
    'perform_now'
  else
    'perform_later'
  end
end

#record(identifier, _opts = {}) ⇒ Object



229
230
231
232
233
234
235
# File 'app/parsers/bulkrax/application_parser.rb', line 229

def record(identifier, _opts = {})
  return @record if @record

  @record = entry_class.new(self, identifier)
  @record.build
  return @record
end

#record_has_source_identifier(record, index) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/parsers/bulkrax/application_parser.rb', line 179

def record_has_source_identifier(record, index)
  if record[source_identifier].blank?
    if Bulkrax.fill_in_blank_source_identifiers.present?
      record[source_identifier] = Bulkrax.fill_in_blank_source_identifiers.call(self, index)
    else
      invalid_record("Missing #{source_identifier} for #{record.to_h}\n")
      false
    end
  else
    true
  end
end

#records(_opts = {}) ⇒ Object

Raises:

  • (StandardError)


43
44
45
# File 'app/parsers/bulkrax/application_parser.rb', line 43

def records(_opts = {})
  raise StandardError, 'must be defined'
end


71
72
73
# File 'app/parsers/bulkrax/application_parser.rb', line 71

def related_children_parsed_mapping
  @related_children_parsed_mapping ||= (get_field_mapping_hash_for('related_children_field_mapping')&.keys&.first || 'children')
end


67
68
69
# File 'app/parsers/bulkrax/application_parser.rb', line 67

def related_children_raw_mapping
  @related_children_raw_mapping ||= get_field_mapping_hash_for('related_children_field_mapping')&.values&.first&.[]('from')&.first
end


63
64
65
# File 'app/parsers/bulkrax/application_parser.rb', line 63

def related_parents_parsed_mapping
  @related_parents_parsed_mapping ||= (get_field_mapping_hash_for('related_parents_field_mapping')&.keys&.first || 'parents')
end


59
60
61
# File 'app/parsers/bulkrax/application_parser.rb', line 59

def related_parents_raw_mapping
  @related_parents_raw_mapping ||= get_field_mapping_hash_for('related_parents_field_mapping')&.values&.first&.[]('from')&.first
end

#required_elementsObject

rubocop:enable Rails/SkipsModelValidations



202
203
204
205
206
207
208
# File 'app/parsers/bulkrax/application_parser.rb', line 202

def required_elements
  if Bulkrax.fill_in_blank_source_identifiers
    ['title']
  else
    ['title', source_identifier]
  end
end

#retrieve_cloud_files(files) ⇒ Object

Optional, define if using browse everything for file upload



124
# File 'app/parsers/bulkrax/application_parser.rb', line 124

def retrieve_cloud_files(files); end

#setup_export_fileObject

Raises:

  • (StandardError)


150
151
152
# File 'app/parsers/bulkrax/application_parser.rb', line 150

def setup_export_file
  raise StandardError, 'must be defined' if exporter?
end

#source_identifierObject



47
48
49
# File 'app/parsers/bulkrax/application_parser.rb', line 47

def source_identifier
  @source_identifier ||= get_field_mapping_hash_for('source_identifier')&.values&.first&.[]('from')&.first&.to_sym || :source_identifier
end

#totalObject



237
238
239
# File 'app/parsers/bulkrax/application_parser.rb', line 237

def total
  0
end

#unzip(file_to_unzip) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'app/parsers/bulkrax/application_parser.rb', line 254

def unzip(file_to_unzip)
  Zip::File.open(file_to_unzip) do |zip_file|
    zip_file.each do |entry|
      entry_path = File.join(importer_unzip_path, entry.name)
      FileUtils.mkdir_p(File.dirname(entry_path))
      zip_file.extract(entry, entry_path) unless File.exist?(entry_path)
    end
  end
end

#valid_import?Boolean

Override to add specific validations

Returns:

  • (Boolean)


175
176
177
# File 'app/parsers/bulkrax/application_parser.rb', line 175

def valid_import?
  true
end

#visibilityObject



103
104
105
# File 'app/parsers/bulkrax/application_parser.rb', line 103

def visibility
  @visibility ||= self.parser_fields['visibility'] || 'open'
end

#work_identifierObject



51
52
53
# File 'app/parsers/bulkrax/application_parser.rb', line 51

def work_identifier
  @work_identifier ||= get_field_mapping_hash_for('source_identifier')&.keys&.first&.to_sym || :source
end

#writeObject



249
250
251
252
# File 'app/parsers/bulkrax/application_parser.rb', line 249

def write
  write_files
  zip
end

#write_filesObject

Raises:

  • (StandardError)


154
155
156
# File 'app/parsers/bulkrax/application_parser.rb', line 154

def write_files
  raise StandardError, 'must be defined' if exporter?
end

#write_import_file(file) ⇒ Object



126
127
128
129
130
131
132
133
# File 'app/parsers/bulkrax/application_parser.rb', line 126

def write_import_file(file)
  path = File.join(path_for_import, file.original_filename)
  FileUtils.mv(
    file.path,
    path
  )
  path
end

#zipObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/parsers/bulkrax/application_parser.rb', line 264

def zip
  FileUtils.mkdir_p(exporter_export_zip_path)

  Dir["#{exporter_export_path}/**"].each do |folder|
    zip_path = "#{exporter_export_zip_path.split('/').last}_#{folder.split('/').last}.zip"
    FileUtils.rm_rf("#{exporter_export_zip_path}/#{zip_path}")

    Zip::File.open(File.join("#{exporter_export_zip_path}/#{zip_path}"), create: true) do |zip_file|
      Dir["#{folder}/**/**"].each do |file|
        zip_file.add(file.sub("#{folder}/", ''), file)
      end
    end
  end
end

#zip?Boolean

Is this a zip file?

Returns:

  • (Boolean)


285
286
287
# File 'app/parsers/bulkrax/application_parser.rb', line 285

def zip?
  parser_fields&.[]('import_file_path') && MIME::Types.type_for(parser_fields['import_file_path']).include?('application/zip')
end