Module: Export::Coldp::Files::Distribution

Defined in:
lib/export/coldp/files/distribution.rb

Overview

taxonID areaID area gazetteer status referenceID remarks

Class Method Summary collapse

Class Method Details

.generate(otus, project_members, reference_csv = nil) ⇒ Object



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
# File 'lib/export/coldp/files/distribution.rb', line 17

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[:updated_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, contents.updated_at, contents.updated_by_id")
        .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.updated_at),
        Export::Coldp.modified_by(o.updated_by_id, project_members),
        nil
      ]
    end
  end
end

.reference_id(content) ⇒ Object



11
12
13
14
15
# File 'lib/export/coldp/files/distribution.rb', line 11

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