Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distribution to CoL data package exporter #3166

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project <em>does not yet</em> adheres to [Semantic Versioning](https://semv

- Subsequent Name Form section in New taxon name [#3460]
- Original form section in New taxon name
- Distribution to Catalogue of Life data package exports [#3148]

### Changed

Expand Down
2 changes: 1 addition & 1 deletion lib/export/coldp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Export
# * Pending handling of both BibTeX and Verbatim
module Coldp

FILETYPES = %w{Description Name Synonym VernacularName}.freeze
FILETYPES = %w{Description Distribution Name Synonym VernacularName}.freeze

# @return [Scope]
# A full set of valid only Otus (= Taxa in CoLDP) that are to be sent.
Expand Down
96 changes: 96 additions & 0 deletions lib/export/coldp/files/distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# taxonID
# areaID
# area
# gazetteer
# status
# referenceID
# remarks
#
module Export::Coldp::Files::Distribution

def self.reference_id(content)
i = content.sources.pluck(:id)
return i.join(',') if i.any?
nil
end

def self.generate(otus, project_members, reference_csv = nil )
CSV.generate(col_sep: "\t") do |csv|

csv << %w{
taxonID
areaID
area
gazetteer
status
referenceID
modified
modifiedBy
remarks
}

otus.each do |o|
o.asserted_distributions.includes(:geographic_area).each do |ad|

ga = ad.geographic_area
if !ga.iso_3166_a3.blank?
gazetteer = 'iso'
area_id = ga.iso_3166_a3
area = ga.iso_3166_a3
elsif !ga.iso_3166_a2.blank?
gazetteer = 'iso'
area_id = ga.iso_3166_a2
area = ga.iso_3166_a2
elsif !ga.tdwgID.blank?
gazetteer = 'tdwg'
if ga.data_origin == 'tdwg_l3' or ga.data_origin == 'tdwg_l4'
area_id = ga.tdwgID.gsub(/^[0-9]{1,2}(.+)$/, '\1') # fixes mismatch in TW vs CoL TDWG level 3 & 4 identifiers
else
area_id = ga.tdwgID
end
area = area_id
else
gazetteer = 'text'
area_id = nil
area = ga.name
end

sources = ad.sources.load
reference_ids = sources.collect{|a| a.id}
csv << [
o.id,
area_id,
area,
gazetteer,
nil,
reference_ids.first, # reference_id: only 1 distribution reference allowed
Export::Coldp.modified(ad[:update_at]), # modified
Export::Coldp.modified_by(ad[:updated_by_id], project_members), # modified_by
nil
]

Export::Coldp::Files::Reference.add_reference_rows(sources, reference_csv, project_members) if reference_csv
end
end

otus.joins("INNER JOIN contents ON contents.otu_id = otus.id
INNER JOIN controlled_vocabulary_terms ON controlled_vocabulary_terms.id = contents.topic_id")
.select("otus.id, contents.text")
.where("controlled_vocabulary_terms.name = 'Distribution text'").distinct.each do |o|
area = o.text

csv << [
o.id,
nil,
area,
'text',
nil,
nil,
Export::Coldp.modified(o[:update_at]),
Export::Coldp.modified_by(o[:updated_by_id], project_members),
nil
]
end
end
end
end