Module: Export::Dwca::Eml

Defined in:
lib/export/dwca/eml.rb

Overview

Terminology: there are some eml fields, like pubDate, that get populated at dwca creation time.

  • eml with those fields populated is ‘actualized’, otherwise it’s ‘unactualized’

  • Do not mark such fields with ‘STUB’; that way we can tell which non-auto-populated fields the user hasn’t provided a value for.

Constant Summary collapse

EML_PARAMETERS =

Nodes/values whose data must be filled in or deleted before public use. !! You must also update .actualize_*_stub when you add a node here.

['packageId'].freeze
DATASET_PARAMETERS =
['alternateIdentifier', 'pubDate'].freeze
ADDITIONAL_METADATA_PARAMETERS =
['dateStamp'].freeze

Class Method Summary collapse

Class Method Details

.actualize_additional_metadata_stub(parameter, uuid) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/export/dwca/eml.rb', line 89

def self.(parameter, uuid)
  case parameter
  when 'dateStamp'
    DateTime.parse(Time.now.to_s).to_s
  else
    raise TaxonWorks::Error, "Unrecognized additionalMetadata stub node '#{parameter}'!"
  end
end

.actualize_dataset_stub(parameter, uuid) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/export/dwca/eml.rb', line 78

def self.actualize_dataset_stub(parameter, uuid)
  case parameter
  when 'alternateIdentifier'
    uuid
  when 'pubDate'
    Time.new.strftime("%Y-%m-%d")
  else
    raise TaxonWorks::Error, "Unrecognized dataset stub node '#{parameter}'!"
  end
end

.actualize_eml_stub(parameter, uuid) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/export/dwca/eml.rb', line 69

def self.actualize_eml_stub(parameter, uuid)
  case parameter
  when 'packageId'
    uuid
  else
    raise TaxonWorks::Error, "Unrecognized eml stub node '#{parameter}'!"
  end
end

.actualized_eml(doc) ⇒ Object



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
# File 'lib/export/dwca/eml.rb', line 42

def self.actualized_eml(doc)
  uuid = SecureRandom.uuid

  eml_node = doc.at_xpath(
    '//eml:eml', 'eml' => 'eml://ecoinformatics.org/eml-2.1.1'
  )
  EML_PARAMETERS.each do |p|
    eml_node[p] = self.actualize_eml_stub(p, uuid)
  end

  DATASET_PARAMETERS.each do |p|
    fragment = doc.at_xpath("//#{p}")
    if fragment # user may have deleted this parameter
      fragment.content = self.actualize_dataset_stub(p, uuid)
    end
  end

  ADDITIONAL_METADATA_PARAMETERS.each do |p|
    fragment = doc.at_xpath("//#{p}")
    if fragment # user may have deleted this parameter
      fragment.content = self.(p, uuid)
    end
  end

  doc.to_xml
end

.actualized_eml_for(dataset, additional_metadata) ⇒ Object



33
34
35
36
# File 'lib/export/dwca/eml.rb', line 33

def self.actualized_eml_for(dataset, )
  doc = self.eml_for(dataset, )
  self.actualized_eml(doc)
end

.actualized_stub_emlObject



38
39
40
# File 'lib/export/dwca/eml.rb', line 38

def self.actualized_stub_eml
  self.actualized_eml(self.eml_stubbed)
end

.additional_metadata_stubObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/export/dwca/eml.rb', line 204

def self.
  # Do NOT mark fields populated at DWCA create time as STUB; mark all other
  # fields the user needs to fill in with 'STUB'.
  <<~TEXT
    <metadata>
      <gbif>
        <dateStamp></dateStamp>
        <hierarchyLevel>dataset</hierarchyLevel>
        <citation identifier="Identifier STUB">STUB DATASET</citation>
        <resourceLogoUrl>STUB. SOME RESOURCE LOGO URL</resourceLogoUrl>
        <formationPeriod>STUB. SOME FORMATION PERIOD</formationPeriod>
        <livingTimePeriod>STUB. SOME LIVING TIME PERIOD</livingTimePeriod>
        <dc:replaces>STUB. PRIOR IDENTIFIER</dc:replaces>
      </gbif>
    </metadata>
TEXT
end

.dataset_stubObject



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
# File 'lib/export/dwca/eml.rb', line 155

def self.dataset_stub
  # Do NOT mark fields populated at DWCA create time as STUB; mark all other
  # fields the user needs to fill in with 'STUB'.
  <<~TEXT
    <alternateIdentifier></alternateIdentifier>
    <title xmlns:lang="en">STUB YOUR TITLE HERE</title>
    <creator>
      <individualName>
        <givenName>STUB</givenName>
        <surName>STUB</surName>
      </individualName>
      <organizationName>STUB</organizationName>
      <electronicMailAddress>STUB_EMAIL@EXAMPLE.COM</electronicMailAddress>
    </creator>
    <metadataProvider>
      <organizationName>STUB</organizationName>
      <electronicMailAddress>STUB_EMAIL@EXAMPLE.COM</electronicMailAddress>
      <onlineUrl>STUB</onlineUrl>
    </metadataProvider>
    <associatedParty>
      <organizationName>STUB</organizationName>
      <address>
        <deliveryPoint>STUB - SEE address above for other fields</deliveryPoint>
      </address>
      <role>distributor</role>
    </associatedParty>
    <pubDate>STUB</pubDate>
    <language>eng</language>
    <abstract>
      <para>STUB. Abstract text here.</para>
    </abstract>
    <intellectualRights>
      <para>STUB. License here.</para>
    </intellectualRights>
    <contact>
      <organizationName>STUB</organizationName>
      <address>
        <deliveryPoint>STUB</deliveryPoint>
        <city>STUB</city>
        <administrativeArea>STUB</administrativeArea>
        <postalCode>STUB</postalCode>
        <country>STUB</country>
      </address>
      <electronicMailAddress>STUB_EMAIL@EXAMPLE.COM</electronicMailAddress>
      <onlineUrl>STUB</onlineUrl>
    </contact>
TEXT
end

.eml_for(dataset, additional_metadata) ⇒ Object

!! dataset and additional_metadata should be valid xml, otherwise the result may not be what you expect.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/export/dwca/eml.rb', line 104

def self.eml_for(dataset, )
  dataset ||= ''
   ||= ''
  builder = self.eml_template

  doc = builder.doc

  # DocumentFragment basically never leaves xml errors, it just 'fixes' things.
  dataset_fragment =
    Nokogiri::XML::DocumentFragment.parse(dataset)
   =
    Nokogiri::XML::DocumentFragment.parse()

  doc.at_xpath('//dataset') << dataset_fragment
  doc.at_xpath('//additionalMetadata') << 

  doc
end

.eml_stubbedObject



98
99
100
# File 'lib/export/dwca/eml.rb', line 98

def self.eml_stubbed
  self.eml_for(self.dataset_stub, self.)
end

.eml_templateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/export/dwca/eml.rb', line 15

def self.eml_template
  Nokogiri::XML::Builder.new(encoding: 'utf-8', namespace_inheritance: false) do |xml|
    xml['eml'].eml(
      'xmlns:eml' => 'eml://ecoinformatics.org/eml-2.1.1',
      'xmlns:dc' => 'http://purl.org/dc/terms/',
      'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
      'xsi:schemaLocation' => 'eml://ecoinformatics.org/eml-2.1.1 http://rs.gbif.org/schema/eml-gbif-profile/1.1/eml-gbif-profile.xsd',
      'packageId' => 'STUB',
      'system' => 'https://taxonworks.org',
      'scope' => 'system',
      'xml:lang' => 'en'
    ) {
      xml.dataset
      xml.
    }
  end
end

.still_stubbed?(dataset, additional_metadata) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/export/dwca/eml.rb', line 151

def self.still_stubbed?(dataset, )
  dataset.include?('STUB') || .include?('STUB')
end

.validate_fragments(dataset, additional_metadata) ⇒ Object



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
# File 'lib/export/dwca/eml.rb', line 123

def self.validate_fragments(dataset, )
  # TODO: check that no STUB strings remain
  # Embed the fragments in eml_template so that:
  # * they get namespace definitions
  # * Nokogiri catches syntax errors (DocumentFragment generally just fixes
  #   things)
  eml_template_one_line = self.eml_template.to_xml(
    save_with: Nokogiri::XML::Node::SaveOptions::AS_XML | # single line
      Nokogiri::XML::Node::SaveOptions::NO_DECLARATION # skip <xml></xml>
  )

  dataset_xml = Nokogiri::XML::Document.parse(
    eml_template_one_line.sub(
      '<dataset/>',
        "<dataset>#{dataset}</dataset>"
    )
  )

   = Nokogiri::XML::Document.parse(
    eml_template_one_line.sub(
      '<additionalMetadata/>',
        "<additionalMetadata>#{}</additionalMetadata>"
    )
  )

  [dataset_xml.errors, .errors]
end