Module: CsvTemplateGenerator

Defined in:
backend/app/lib/csv_template_generator.rb

Defined Under Namespace

Classes: CsvTemplateError, Template, TemplateSpec

Class Method Summary collapse

Class Method Details

.csv_for_digital_object_generation(resource_id) ⇒ Object

Module-level methods here are primarily what’s called by consumers at the controller level. Generally they will consist of 1. instantiate a template definition 2. fetch a a dataset 3. return an enumerator over CSV lines for streaming to frontend/direct download



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'backend/app/lib/csv_template_generator.rb', line 240

def csv_for_digital_object_generation(resource_id)
  csv_template_path = File.join(ASUtils.find_base_directory, 'templates', 'bulk_import_DO_template.csv')
  csv = CSV.read(csv_template_path)

  # Remove first entry because it's automatically prepended with `field_name_text` option in TemplateSpec below.
  column_field_names = csv[0].drop(1) # CSV headers
  column_explanations = csv[1].drop(1) # CSV header explanations

  columns = {}
  for x in 0..(column_field_names.length - 1)
    columns[column_field_names[x].to_sym] = {
      title: column_explanations[x]
    }
  end

  template = Template.new(
    TemplateSpec.new(
      sheet_description: "Archival Object Digital Object Generation Template Prefilled",
      field_name_text: "ArchivesSpace digital object import field codes (please don't edit this row)",
      title_text: "Field Name",
      columns: columns
    )
  )

  resource = ::Resource.find(id: resource_id)

  dataset = DB.open do |ds|
    ds[:resource].
      inner_join(:archival_object, :root_record_id => :id).
      where(q(:resource, :id) => resource_id).
      select(
        q(:archival_object, :id).as(:archival_object_id)
      ).
      order(q(:archival_object, :id))
  end

  data_hash = []
  dataset.paged_each do |row|
    archival_object = ::ArchivalObject.find(id: row[:archival_object_id])

    data_hash.push({
      res_uri: resource.uri,
      ao_uri: archival_object.uri,
    })
  end

  template.each_hash_row(data_hash)
end

.csv_for_top_container_generation(resource_id) ⇒ Object

Module-level methods here are primarily what’s called by consumers at the controller level. Generally they will consist of 1. instantiate a template definition 2. fetch a a dataset 3. return an enumerator over CSV lines for streaming to frontend/direct download



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'backend/app/lib/csv_template_generator.rb', line 185

def csv_for_top_container_generation(resource_id)
  template = Template.new(
    TemplateSpec.new(
      sheet_description: "Archival Object Top Container Generation Template",
      field_name_text: "ArchivesSpace field code (please don't edit this row)",
      title_text: "Field Name",
      group_text: "Maps to ASpace Type",
      columns: {
        # For reference
        archival_object_id: {title: "Archival Object ID", group: "Archival Object"},
        ref_id: {title: "Ref ID", group: "Archival Object"},
        component_id: {title: "Component ID", group: "Archival Object"},
        archival_object_title: {title: "Title", group: "Archival Object"},
        resource_title: {title: "Resource Title", group: "Resource"},
        ead_id: {title: "EAD ID", group: "Resource"},
        identifier: {title: "Identifier", group: "Resource", formatter: ->(value) { JSON.parse(value).compact.join(" ") }},
        # Editable
        instance_type: {title: "Instance Type", group: "Instance", blank: true},
        top_container_id: {title: "Top Container ID (existing top container, leave blank if creating new container)", group: "Container", blank: true},
        top_container_type: {title: "Top Container Type", blank: true, group: "Top Container"},
        top_container_indicator: {title: "Top Container Indicator", group: "Top Container", blank: true},
        top_container_barcode: {title: "Top container barcode", group: "Top Container", blank: true},
        container_profile_id: {title: "Container Profile ID", group: "Top Container", blank: true},
        child_type: {title: "Child Type", group: "Sub Container", blank: true},
        child_indicator: {title: "Child Indicator", group: "Sub Container", blank: true},
        child_barcode: {title: "Child Barcode", group: "Sub Container", blank: true},
        location_id: {title: "Location ID", group: "Location", blank: true}
      }
    )
  )

  dataset = DB.open do |ds|
    ds[:resource].
      inner_join(:archival_object, :root_record_id => :id).
      where(q(:resource, :id) => resource_id).
      select(
        q(:archival_object, :id).as(:archival_object_id),
        q(:archival_object, :ref_id),
        q(:archival_object, :component_id),
        q(:archival_object, :title).as(:archival_object_title),
        q(:resource, :title).as(:resource_title),
        q(:resource, :ead_id),
        q(:resource, :identifier)
      ).
      order(q(:archival_object, :id))
  end

  template.each(dataset)
end

.q(*args) ⇒ Object



175
176
177
178
# File 'backend/app/lib/csv_template_generator.rb', line 175

def q(*args)
  # Convenience method to get qualified names without having to type 14 chars
  Sequel.qualify(*args)
end