Class: OAIDCTermsMapper

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/oai/mappers/oai_dcterms.rb

Instance Method Summary collapse

Instance Method Details

#map_oai_record(record) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'backend/app/lib/oai/mappers/oai_dcterms.rb', line 5

def map_oai_record(record)
  jsonmodel = record.jsonmodel_record
  result = Nokogiri::XML::Builder.new do |xml|

    xml['oai_dcterms'].dcterms('xmlns:dcterms' => 'http://purl.org/dc/terms/',
                               'xmlns:oai_dcterms' => 'http://www.openarchives.org/OAI/2.0/oai_dcterms/',
                               'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                               'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/oai_dcterms/') do
      # Repo name -> publisher
      xml['dcterms'].publisher (jsonmodel['repository']['_resolved']['name'])

      # Parent institution name -> publisher
      if jsonmodel['repository']['_resolved']['parent_institution_name']
        xml['dcterms'].publisher(jsonmodel['repository']['_resolved']['parent_institution_name'])
      end

      # Identifier (own component ID + IDs of parents)
      # TODO: Reuse after implementing 'off' switch for ARK
      merged_identifier = if jsonmodel['jsonmodel_type'] == 'archival_object'
                            ([jsonmodel['component_id']] + jsonmodel['ancestors'].map {|a| a['_resolved']['component_id']}).compact.reverse.uniq.join(".")
                          else
                            (0..3).map {|id| jsonmodel["id_#{id}"]}.compact.join('.')
                          end

      unless merged_identifier.empty?
        xml['dcterms'].identifier(merged_identifier)
      end

      if AppConfig[:arks_enabled] && jsonmodel['ark_name']
        ark_url = jsonmodel['ark_name']['current']

        xml['dcterms'].identifier(ark_url) if ark_url
      end

      # And a second identifier containing the public url - if public is running
      if AppConfig[:enable_public]
        xml['dcterms'].identifier(AppConfig[:public_proxy_url] + jsonmodel['uri'])
      end

      # Creator -- agents linked with role 'creator' that don't have a relator of 'contributor' or 'publisher'
      Array(jsonmodel['linked_agents']).each do |link|
        next unless link['_resolved']['publish']

        if link['role'] == 'creator' && !['ctb' , 'pbl'].include?(link['relator'])
          xml['dcterms'].creator(link['_resolved']['title'])
        end
      end

      # Contributor -- agents linked with role 'creator' and relator of 'contributor'
      Array(jsonmodel['linked_agents']).each do |link|
        next unless link['_resolved']['publish']

        if link['role'] == 'creator' && ['ctb'].include?(link['relator'])
          xml['dcterms'].contributor(link['_resolved']['title'])
        end
      end

      # Publisher -- agents linked with role 'creator' and relator of 'publisher'
      Array(jsonmodel['linked_agents']).each do |link|
        next unless link['_resolved']['publish']

        if link['role'] == 'creator' && ['pbl'].include?(link['relator'])
          xml['dcterms'].publisher(link['_resolved']['title'])
        end
      end

      # Title -- display string
      xml['dcterms'].title(OAIUtils.display_string(jsonmodel))

      # Finding Aid Title
      if jsonmodel['jsonmodel_type'] == 'archival_object'
        xml['dcterms'].alternative(OAIUtils.strip_mixed_content(jsonmodel['resource']['_resolved']['finding_aid_title']))
      else
        xml['dcterms'].alternative(OAIUtils.strip_mixed_content(jsonmodel['finding_aid_title']))
      end

      # Dates
      Array(jsonmodel['dates']).each do |date|
        date_str = if date['expression']
                     date['expression']
                   else
                     [date['begin'], date['end']].compact.join(' -- ')
                   end
        if date['label'] == 'copyright'
          xml['dcterms'].dateCopyrighted(date_str)
        elsif date['label'] == 'publication'
          xml['dcterms'].issued(date_str)
        else
          xml['dcterms'].date(date_str)
        end
      end

      # Extents
      Array(jsonmodel['extents']).each do |extent|
        extent_str = [extent['number'] + ' ' + I18n.t('enumerations.extent_extent_type.' + extent['extent_type'], :default => extent['extent_type']), extent['container_summary']].compact.join('; ')
        xml['dcterms'].extent(extent_str)
      end

      # Physical description and Dimensions notes are also extents
      Array(jsonmodel['notes'])
        .select {|note| ['physdesc', 'dimensions'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].extent(content)
        end
      end

      # Languages
      if (lang_materials = Array(jsonmodel['lang_materials']))
        language_vals = lang_materials.map {|l| l['language_and_script']}.compact
        if !language_vals.empty?
          language_vals.each do |l|
            xml['dcterms'].language(l['language'])
            if l.include?('script')
              xml['dcterms'].language(l['script'])
            end
          end
        end
        language_notes = lang_materials.map {|l| l['notes']}.compact.reject {|e| e == [] }.flatten
        if !language_notes.empty?
          language_notes.each do |note|
            OAIUtils.extract_published_note_content(note).each do |content|
              xml['dcterms'].language(content)
            end
          end
        end
      end

      # Description note types
      Array(jsonmodel['notes'])
        .select {|note| ['langmaterial', 'bioghist', 'scopecontent', 'odd', 'arrangement'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].description(content)
        end
      end

      # Abstract note types
      Array(jsonmodel['notes'])
        .select {|note| ['abstract'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].abstract(content)
        end
      end

      # Relation note types
      Array(jsonmodel['notes'])
        .select {|note| ['originalsloc', 'altformavail', 'separatedmaterial', 'relatedmaterial'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].relation(content)
        end
      end

      # Provenance note types
      Array(jsonmodel['notes'])
        .select {|note| ['custodhist', 'acqinfo'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].provenance(content)
        end
      end

      # Access rights
      Array(jsonmodel['notes'])
        .select {|note| ['accessrestrict'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].accessRights(content)
        end
      end

      # General rights
      Array(jsonmodel['notes'])
        .select {|note| ['userestrict'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].rights(content)
        end
      end

      # Subjects
      Array(jsonmodel['subjects']).each do |subject|
        term_types = subject['_resolved']['terms'].map {|term| term['term_type']}

        if term_types.include?('geographic')
          xml['dcterms'].coverage(subject['_resolved']['title'])
        elsif term_types.include?('genre_form')
          xml['dcterms'].type(subject['_resolved']['title'])
        else
          xml['dcterms'].subject(subject['_resolved']['title'])
        end
      end

      # Subjects continued - Agents as subjects
      Array(jsonmodel['linked_agents']).each do |link|
        next unless link['_resolved']['publish']

        if link['role'] == 'subject'
          xml['dcterms'].subject(link['_resolved']['title'])
        end
      end

      # Physical facet note
      Array(jsonmodel['notes'])
        .select {|note| ['physfacet'].include?(note['type'])}
        .each do |note|
        OAIUtils.extract_published_note_content(note).each do |content|
          xml['dcterms'].type(content)
        end
      end

      # Originating Collection
      if jsonmodel['jsonmodel_type'] == 'archival_object'
        resource_id_str = (0..3).map {|i| jsonmodel['resource']['_resolved']["id_#{i}"]}.compact.join(".")
        resource_str = [jsonmodel['resource']['_resolved']['title'], resource_id_str].join(', ')

        xml['dcterms'].isPartOf(resource_str)
      end
    end
  end

  result.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
end