Class: MARCModel

Inherits:
ASpaceExport::ExportModel show all
Includes:
JSONModel
Defined in:
backend/app/exporters/models/marc21.rb

Constant Summary collapse

@@datafield =
Class.new do
@@subfield =
Class.new do

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Methods inherited from ASpaceExport::ExportModel

#apply_map, inherited, model_for, model_for?

Methods included from ASpaceExport::ExportModelHelpers

#extract_date_string, #extract_note_content, #get_subnotes_by_type

Constructor Details

#initialize(include_unpublished = false) ⇒ MARCModel

Returns a new instance of MARCModel.



75
76
77
78
# File 'backend/app/exporters/models/marc21.rb', line 75

def initialize(include_unpublished = false)
  @datafields = {}
  @include_unpublished = include_unpublished
end

Instance Attribute Details

#controlfield_stringObject

Returns the value of attribute controlfield_string



31
32
33
# File 'backend/app/exporters/models/marc21.rb', line 31

def controlfield_string
  @controlfield_string
end

#leader_stringObject

Returns the value of attribute leader_string



30
31
32
# File 'backend/app/exporters/models/marc21.rb', line 30

def leader_string
  @leader_string
end

Class Method Details

.assemble_controlfield_string(obj) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'backend/app/exporters/models/marc21.rb', line 115

def self.assemble_controlfield_string(obj)
  date = obj.dates[0] || {}
  string = obj['system_mtime'].scan(/\d{2}/)[1..3].join('')
  string += date['date_type'] == 'single' ? 's' : 'i'
  string += date['begin'] ? date['begin'][0..3] : "    "
  string += date['end'] ? date['end'][0..3] : "    "

  repo = obj['repository']['_resolved']

  if repo.has_key?('country') && !repo['country'].empty?
    # US is a special case, because ASpace has no knowledge of states, the
    # correct value is 'xxu'
    if repo['country'] == "US"
      string += "xxu"
    else
      string += repo['country'].downcase
    end
  else
    string += "xx"
  end

  # If only one Language and Script subrecord its code value should be exported in the MARC 008 field position 35-37; If more than one Language and Script subrecord is recorded, a value of "mul" should be exported in the MARC 008 field position 35-37.
  lang_materials = obj.lang_materials
  languages = lang_materials.map {|l| l['language_and_script']}.compact
  langcode = languages.count == 1 ? languages[0]['language'] : 'mul'

  # variable number of spaces needed since country code could have 2 or 3 chars
  (35-(string.length)).times { string += ' ' }
  string += (langcode || '|||')
  string += ' d'

  string
end

.df_handler(name, tag, ind1, ind2, code) ⇒ Object



6
7
8
9
10
11
# File 'backend/app/exporters/models/marc21.rb', line 6

def self.df_handler(name, tag, ind1, ind2, code)
  define_method(name) do |val|
    df(tag, ind1, ind2).with_sfs([code, val])
  end
  name.to_sym
end

.from_archival_object(obj, opts = {}) ⇒ Object

‘archival object’s in the abstract



94
95
96
97
98
99
# File 'backend/app/exporters/models/marc21.rb', line 94

def self.from_archival_object(obj, opts = {})
  marc = self.from_aspace_object(obj, opts)
  marc.apply_map(obj, @archival_object_map)

  marc
end

.from_aspace_object(obj, opts = {}) ⇒ Object



89
90
91
# File 'backend/app/exporters/models/marc21.rb', line 89

def self.from_aspace_object(obj, opts = {})
  self.new(opts[:include_unpublished])
end

.from_resource(obj, opts = {}) ⇒ Object

subtypes of ‘archival object’:



103
104
105
106
107
108
109
110
111
112
# File 'backend/app/exporters/models/marc21.rb', line 103

def self.from_resource(obj, opts = {})
  marc = self.from_archival_object(obj, opts)
  marc.apply_map(obj, @resource_map)
  marc.leader_string = "00000np$aa2200000 u 4500"
  marc.leader_string[7] = obj.level == 'item' ? 'm' : 'c'

  marc.controlfield_string = assemble_controlfield_string(obj)

  marc
end

Instance Method Details

#datafieldsObject



80
81
82
# File 'backend/app/exporters/models/marc21.rb', line 80

def datafields
  @datafields.map {|k, v| v}
end

#df(*args) ⇒ Object



158
159
160
161
162
163
164
165
# File 'backend/app/exporters/models/marc21.rb', line 158

def df(*args)
  if @datafields.has_key?(args.to_s)
    @datafields[args.to_s]
  else
    @datafields[args.to_s] = @@datafield.new(*args)
    @datafields[args.to_s]
  end
end

#df!(*args) ⇒ Object



150
151
152
153
154
155
# File 'backend/app/exporters/models/marc21.rb', line 150

def df!(*args)
  @sequence ||= 0
  @sequence += 1
  @datafields[@sequence] = @@datafield.new(*args)
  @datafields[@sequence]
end

#handle_agent_terms(terms) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'backend/app/exporters/models/marc21.rb', line 486

def handle_agent_terms(terms)
  sfs = []
  terms.each do |t|
    tag = case t['term_type']
          when 'uniform_title'; 't'
          when 'genre_form', 'style_period'; 'v'
          when 'topical', 'cultural_context'; 'x'
          when 'temporal'; 'y'
          when 'geographic'; 'z'
          end
    next if tag.nil?
    sfs << [(tag), t['term']]
  end

  sfs
end

#handle_agents(linked_agents) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'backend/app/exporters/models/marc21.rb', line 420

def handle_agents(linked_agents)
  handle_primary_creator(linked_agents)
  handle_other_creators(linked_agents)

  subjects = linked_agents.select {|a| a['role'] == 'subject'}

  subjects.each_with_index do |link, i|
    next unless link["_resolved"]["publish"] || @include_unpublished

    subject = link['_resolved']
    name = subject['display_name']
    terms = link['terms']
    ind2 = source_to_code(name['source'])

    relator_sfs = []
    if link['relator']
      handle_relators(relator_sfs, link['relator'])
    end

    case subject['agent_type']

    when 'agent_corporate_entity'
      code = '610'
      ind1 = '2'
      sfs = gather_agent_corporate_subfield_mappings(name, relator_sfs, subject, terms)

    when 'agent_person'
      ind1  = name['name_order'] == 'direct' ? '0' : '1'
      code = '600'
      sfs = gather_agent_person_subfield_mappings(name, relator_sfs, subject, terms)

    when 'agent_family'
      code = '600'
      ind1 = '3'
      sfs = gather_agent_family_subfield_mappings(name, relator_sfs, subject, terms)

    when 'agent_software'
      code = '653'
      ind1 = ' '
      sfs = [['a', name['software_name']]]

    end

    # ANW-825: Don't export $0 if $v, $x, $y, or $z are present
    sfs.reject! {|k| k[0] == 0 } if (['v', 'x', 'y', 'z'] - sfs.map { |k| k[0] }).length < 4

    if ind2 == '7'
      sfs << ['2', subject['names'].first['source']]
    end

    df(code, ind1, ind2, i).with_sfs(*sfs)
  end
end

#handle_ark(id, type = 'resource') ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'backend/app/exporters/models/marc21.rb', line 605

def handle_ark(id, type='resource')
  # If ARKs are enabled, add an 856
  #<datafield tag="856" ind1="4" ind2="2">
  #  <subfield code="z">Archival Resource Key:</subfield>
  #  <subfield code="u">ARK URL</subfield>
  #</datafield>
  if AppConfig[:arks_enabled]
    ark_url = ArkName::get_ark_url(id, type.to_sym)
    df('856', '4', '2').with_sfs(
                                 ['z', "Archival Resource Key:"],
                                 ['u', ark_url]
                               ) unless ark_url.nil? || ark_url.empty?

  end
end

#handle_dates(dates) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'backend/app/exporters/models/marc21.rb', line 232

def handle_dates(dates)
  return false if dates.empty?

  dates = [["single", "inclusive", "range"], ["bulk"]].map {|types|
    dates.find {|date| types.include? date['date_type'] }
  }.compact

  dates.each do |date|
    code = date['date_type'] == 'bulk' ? 'g' : 'f'
    val = nil
    if date['expression'] && date['date_type'] != 'bulk'
      val = date['expression']
    elsif date['date_type'] == 'single'
      val = date['begin']
    else
      val = "#{date['begin']} - #{date['end']}"
    end

    df('245', '1', '0').with_sfs([code, val])
  end
end

#handle_ead_loc(ead_loc) ⇒ Object

3/28/18: Updated: ANW-318



591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'backend/app/exporters/models/marc21.rb', line 591

def handle_ead_loc(ead_loc)
  # If there is EADlocation
  #<datafield tag="856" ind1="4" ind2="2">
  #  <subfield code="z">Finding aid online:</subfield>
  #  <subfield code="u">EADlocation</subfield>
  #</datafield>
  if ead_loc && !ead_loc.empty?
    df('856', '4', '2').with_sfs(
                                  ['z', "Finding aid online:"],
                                  ['u', ead_loc]
                                )
  end
end

#handle_extents(extents) ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
587
# File 'backend/app/exporters/models/marc21.rb', line 576

def handle_extents(extents)
  extents.each do |ext|
    e = ext['number']
    t = "#{I18n.t('enumerations.extent_extent_type.'+ext['extent_type'], :default => ext['extent_type'])}"

    if ext['container_summary']
      t << " (#{ext['container_summary']})"
    end

    df!('300').with_sfs(['a', e], ['f', t])
  end
end

#handle_id(*ids) ⇒ Object



168
169
170
171
# File 'backend/app/exporters/models/marc21.rb', line 168

def handle_id(*ids)
  ids.reject! {|i| i.nil? || i.empty?}
  df('099', ' ', ' ').with_sfs(['a', ids.join('.')])
end

#handle_languages(lang_materials) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'backend/app/exporters/models/marc21.rb', line 209

def handle_languages(lang_materials)
  # ANW-697: The Language subrecord code values should be exported in repeating subfield $a entries in the MARC 041 field.

  languages = lang_materials.map {|l| l['language_and_script']}.compact

  languages.each do |language|

    df('041', ' ', ' ').with_sfs(['a', language['language']])

  end

  # ANW-697: Language Text subrecords should be exported in the MARC 546 subfield $a

  language_notes = lang_materials.map {|l| l['notes']}.compact.reject {|e| e == [] }

  if language_notes
    language_notes.each do |note|
      handle_notes(note)
    end
  end
end

#handle_notes(notes) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'backend/app/exporters/models/marc21.rb', line 504

def handle_notes(notes)
  notes.each do |note|

    prefix =  case note['type']
              when 'dimensions'; "Dimensions"
              when 'physdesc'; "Physical Description note"
              when 'materialspec'; "Material Specific Details"
              when 'physloc'; "Location of resource"
              when 'phystech'; "Physical Characteristics / Technical Requirements"
              when 'physfacet'; "Physical Facet"
              when 'processinfo'; "Processing Information"
              when 'separatedmaterial'; "Materials Separated from the Resource"
              else; nil
              end

    marc_args = case note['type']

                when 'arrangement', 'fileplan'
                  ['351', 'a']
                when 'odd', 'dimensions', 'physdesc', 'materialspec', 'physloc', 'phystech', 'physfacet', 'processinfo', 'separatedmaterial'
                  ['500', 'a']
                when 'accessrestrict'
                  ['506', 'a']
                when 'scopecontent'
                  ['520', '2', ' ', 'a']
                when 'abstract'
                  ['520', '3', ' ', 'a']
                when 'prefercite'
                  ['524', ' ', ' ', 'a']
                when 'acqinfo'
                  ind1 = note['publish'] ? '1' : '0'
                  ['541', ind1, ' ', 'a']
                when 'relatedmaterial'
                  ['544', 'd']
                when 'bioghist'
                  ['545', 'a']
                when 'custodhist'
                  ind1 = note['publish'] ? '1' : '0'
                  ['561', ind1, ' ', 'a']
                when 'appraisal'
                  ind1 = note['publish'] ? '1' : '0'
                  ['583', ind1, ' ', 'a']
                when 'accruals'
                  ['584', 'a']
                when 'altformavail'
                  ['535', '2', ' ', 'a']
                when 'originalsloc'
                  ['535', '1', ' ', 'a']
                when 'userestrict', 'legalstatus'
                  ['540', 'a']
                when 'langmaterial'
                  ['546', 'a']
                when 'otherfindaid'
                  ['555', '0', ' ', 'a']
                else
                  nil
                end

    unless marc_args.nil?
      text = prefix ? "#{prefix}: " : ""
      text += ASpaceExport::Utils.extract_note_text(note, @include_unpublished, true)

      # only create a tag if there is text to show (e.g., marked published or exporting unpublished)
      if text.length > 0
        df!(*marc_args[0...-1]).with_sfs([marc_args.last, *Array(text)])
      end
    end

  end
end

#handle_other_creators(linked_agents) ⇒ Object

TODO: DRY this up this method is very similair to handle_primary_creator and handle_agents



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'backend/app/exporters/models/marc21.rb', line 374

def handle_other_creators(linked_agents)
  creators = linked_agents.select {|a| a['role'] == 'creator'}[1..-1] || []
  creators = creators + linked_agents.select {|a| a['role'] == 'source'}

  creators.each_with_index do |link, i|
    next unless link["_resolved"]["publish"] || @include_unpublished

    creator = link['_resolved']
    name = creator['display_name']
    role = link['role']

    relator_sfs = []
    if link['relator']
      handle_relators(relator_sfs, link['relator'])
    elsif role == 'source'
      relator_sfs << ['e', 'former owner']
    else
      relator_sfs << ['e', 'creator']
    end

    ind2 = ' '

    case creator['agent_type']

    when 'agent_corporate_entity'
      code = '710'
      ind1 = '2'
      sfs = gather_agent_corporate_subfield_mappings(name, relator_sfs, creator)

    when 'agent_person'
      ind1  = name['name_order'] == 'direct' ? '0' : '1'
      code = '700'
      sfs = gather_agent_person_subfield_mappings(name, relator_sfs, creator)

    when 'agent_family'
      ind1 = '3'
      code = '700'
      sfs = gather_agent_family_subfield_mappings(name, relator_sfs, creator)

    end

    df(code, ind1, ind2, i).with_sfs(*sfs)
  end
end

#handle_primary_creator(linked_agents) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'backend/app/exporters/models/marc21.rb', line 333

def handle_primary_creator(linked_agents)
  link = linked_agents.find {|a| a['role'] == 'creator'}
  return nil unless link
  return nil unless link["_resolved"]["publish"] || @include_unpublished

  creator = link['_resolved']
  name = creator['display_name']

  ind2 = ' '

  relator_sfs = []
  if link['relator']
    handle_relators(relator_sfs, link['relator'])
  else
    relator_sfs << ['e', 'creator']
  end

  case creator['agent_type']

  when 'agent_corporate_entity'
    code = '110'
    ind1 = '2'
    sfs = gather_agent_corporate_subfield_mappings(name, relator_sfs, creator)

  when 'agent_person'
    ind1  = name['name_order'] == 'direct' ? '0' : '1'
    code = '100'
    sfs = gather_agent_person_subfield_mappings(name, relator_sfs, creator)

  when 'agent_family'
    code = '100'
    ind1 = '3'
    sfs = gather_agent_family_subfield_mappings(name, relator_sfs, creator)

  end

  df(code, ind1, ind2).with_sfs(*sfs)
end

#handle_relators(relator_sfs, link) ⇒ Object



475
476
477
478
479
480
481
482
483
# File 'backend/app/exporters/models/marc21.rb', line 475

def handle_relators(relator_sfs, link)
  relator = I18n.t("enumerations.linked_agent_archival_record_relators.#{link}")
  relator_sfs << ['4', link]
  unless relator.to_s.include?('translation missing')
    relator_sfs << ['e', relator]
  end

  return relator_sfs
end

#handle_repo_code(repository, *finding_aid_language) ⇒ Object



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
# File 'backend/app/exporters/models/marc21.rb', line 254

def handle_repo_code(repository, *finding_aid_language)
  repo = repository['_resolved']
  return false unless repo

  sfa = repo['org_code'] ? repo['org_code'] : "Repository: #{repo['repo_code']}"

  # ANW-529: options for 852 datafield:
  # 1.) $a => org_code || repo_name
  # 2.) $a => $parent_institution_name && $b => repo_name

  if repo['parent_institution_name']
    subfields_852 = [
                      ['a', repo['parent_institution_name']],
                      ['b', repo['name']]
                    ]
  elsif repo['org_code']
    subfields_852 = [
                      ['a', repo['org_code']],
                    ]
  else
    subfields_852 = [
                      ['a', repo['name']]
                    ]
  end

  df('852', ' ', ' ').with_sfs(*subfields_852)
  df('040', ' ', ' ').with_sfs(['a', repo['org_code']], ['b', finding_aid_language[0]], ['c', repo['org_code']])
  df('049', ' ', ' ').with_sfs(['a', repo['org_code']])
end

#handle_subjects(subjects) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'backend/app/exporters/models/marc21.rb', line 288

def handle_subjects(subjects)
  subjects.each do |link|
    subject = link['_resolved']
    term, *terms = subject['terms']
    code, ind2 =  case term['term_type']
                  when 'uniform_title'
                    ['630', source_to_code(subject['source'])]
                  when 'temporal'
                    ['648', source_to_code(subject['source'])]
                  when 'topical'
                    ['650', source_to_code(subject['source'])]
                  when 'geographic', 'cultural_context'
                    ['651', source_to_code(subject['source'])]
                  when 'genre_form', 'style_period'
                    ['655', source_to_code(subject['source'])]
                  when 'occupation'
                    ['656', '7']
                  when 'function'
                    ['656', '7']
                  else
                    ['650', source_to_code(subject['source'])]
                  end
    sfs = [['a', term['term']]]

    terms.each do |t|
      tag = case t['term_type']
            when 'uniform_title'; 't'
            when 'genre_form', 'style_period'; 'v'
            when 'topical', 'cultural_context'; 'x'
            when 'temporal'; 'y'
            when 'geographic'; 'z'
            end
      sfs << [tag, t['term']]
    end

    if ind2 == '7'
      sfs << ['2', subject['source']]
    end

    ind1 = code == '630' ? "0" : " "
    df!(code, ind1, ind2).with_sfs(*sfs)
  end
end

#handle_title(title, linked_agents, dates) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'backend/app/exporters/models/marc21.rb', line 174

def handle_title(title, linked_agents, dates)
  creator = linked_agents.find {|a| a['role'] == 'creator'}
  date_codes = []

  # process dates first, if defined.
  unless dates.empty?
    dates = [["single", "inclusive", "range"], ["bulk"]].map {|types|
      dates.find {|date| types.include? date['date_type'] }
    }.compact

    dates.each do |date|
      code, val = nil
      code = date['date_type'] == 'bulk' ? 'g' : 'f'
      if date['expression']
        val = date['expression']
      elsif date['end']
        val = "#{date['begin']} - #{date['end']}"
      else
        val = "#{date['begin']}"
      end
      date_codes.push([code, val])
    end
  end

  ind1 = creator.nil? ? "0" : "1"
  if date_codes.length > 0
    # we want to pass in all our date codes as separate subfield tags
    # e.g., with_sfs(['a', title], [code1, val1], [code2, val2]... [coden, valn])
    df('245', ind1, '0').with_sfs(['a', title + ","], *date_codes)
  else
    df('245', ind1, '0').with_sfs(['a', title])
  end
end

#include_unpublished?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'backend/app/exporters/models/marc21.rb', line 84

def include_unpublished?
  @include_unpublished
end

#source_to_code(source) ⇒ Object



284
285
286
# File 'backend/app/exporters/models/marc21.rb', line 284

def source_to_code(source)
  ASpaceMappings::MARC21.get_marc_source_code(source)
end