From 085fd2b03bd87b232bb4d78b7067bebfaea60abb Mon Sep 17 00:00:00 2001 From: Geoff Ower Date: Mon, 24 Oct 2022 14:27:26 -0500 Subject: [PATCH 1/4] Close #3148 --- lib/export/coldp.rb | 2 +- lib/export/coldp/files/distribution.rb | 90 ++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 lib/export/coldp/files/distribution.rb diff --git a/lib/export/coldp.rb b/lib/export/coldp.rb index 766a24dbeb..ca2fefb835 100644 --- a/lib/export/coldp.rb +++ b/lib/export/coldp.rb @@ -10,7 +10,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. diff --git a/lib/export/coldp/files/distribution.rb b/lib/export/coldp/files/distribution.rb new file mode 100644 index 0000000000..58b05bee89 --- /dev/null +++ b/lib/export/coldp/files/distribution.rb @@ -0,0 +1,90 @@ +# 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, reference_csv = nil ) + CSV.generate(col_sep: "\t") do |csv| + + csv << %w{ + taxonID + areaID + area + gazetteer + status + referenceID + remarks + } + + otus.joins(:asserted_distributions).distinct.each do |o| + o.asserted_distributions.each do |ad| + + ga = GeographicArea.find(ad.geographic_area_id) + 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, # only 1 distribution reference allowed + nil + ] + + Export::Coldp::Files::Reference.add_reference_rows(sources, reference_csv) 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, + nil + ] + end + end + end +end From 7722b47d125c33bc500df39dfc551d2216c1cc68 Mon Sep 17 00:00:00 2001 From: Geoff Ower Date: Wed, 14 Dec 2022 15:55:40 -0600 Subject: [PATCH 2/4] Simplify --- lib/export/coldp/files/distribution.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/export/coldp/files/distribution.rb b/lib/export/coldp/files/distribution.rb index 58b05bee89..e7c31bcb26 100644 --- a/lib/export/coldp/files/distribution.rb +++ b/lib/export/coldp/files/distribution.rb @@ -27,10 +27,10 @@ def self.generate(otus, reference_csv = nil ) remarks } - otus.joins(:asserted_distributions).distinct.each do |o| - o.asserted_distributions.each do |ad| + otus.each do |o| + o.asserted_distributions.includes(:geographic_area).each do |ad| - ga = GeographicArea.find(ad.geographic_area_id) + ga = ad.geographic_area if !ga.iso_3166_a3.blank? gazetteer = 'iso' area_id = ga.iso_3166_a3 From ed48c7826f2c4fd4c64d53c4aa1de91a37bc4279 Mon Sep 17 00:00:00 2001 From: Geoff Ower Date: Wed, 16 Aug 2023 10:09:12 -0500 Subject: [PATCH 3/4] Add modified and modified by --- lib/export/coldp/files/distribution.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/export/coldp/files/distribution.rb b/lib/export/coldp/files/distribution.rb index e7c31bcb26..c66c57f8ec 100644 --- a/lib/export/coldp/files/distribution.rb +++ b/lib/export/coldp/files/distribution.rb @@ -14,7 +14,7 @@ def self.reference_id(content) nil end - def self.generate(otus, reference_csv = nil ) + def self.generate(otus, project_members, reference_csv = nil ) CSV.generate(col_sep: "\t") do |csv| csv << %w{ @@ -24,6 +24,8 @@ def self.generate(otus, reference_csv = nil ) gazetteer status referenceID + modified + modifiedBy remarks } @@ -61,11 +63,13 @@ def self.generate(otus, reference_csv = nil ) area, gazetteer, nil, - reference_ids.first, # only 1 distribution reference allowed + 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) if reference_csv + Export::Coldp::Files::Reference.add_reference_rows(sources, reference_csv, project_members) if reference_csv end end @@ -82,6 +86,8 @@ def self.generate(otus, reference_csv = nil ) 'text', nil, nil, + Export::Coldp.modified(o[:update_at]), + Export::Coldp.modified_by(o[:updated_by_id], project_members), nil ] end From 31b73436f9edf5806f1ac8750294dfc74fdf524b Mon Sep 17 00:00:00 2001 From: Geoff Ower Date: Wed, 16 Aug 2023 10:11:13 -0500 Subject: [PATCH 4/4] Add distribution to COLDP exports (Close #3148) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5efe4389a0..86eabe5be0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project does not yet 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