Class: BatchLoad::Import::TaxonifiToTaxonworks

Inherits:
BatchLoad::Import show all
Defined in:
lib/batch_load/import/taxonifi_to_taxonworks.rb

Overview

Batch loading of CSV formatted taxon names via Taxonifi

Instance Attribute Summary (collapse)

Attributes inherited from BatchLoad::Import

#create_attempted, #csv, #errors, #file, #file_errors, #import_level, #processed, #processed_rows, #project, #successful_rows, #total_data_lines, #total_lines, #user, #user_header_map

Instance Method Summary (collapse)

Methods inherited from BatchLoad::Import

#all_objects, #create, #create_attempted?, #import_level_ok?, #line_strict_level_ok?, #processed?, #ready_to_create?, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?

Constructor Details

- (TaxonifiToTaxonworks) initialize(nomenclature_code: nil, parent_taxon_name_id: nil, also_create_otu: false, **args)

Returns a new instance of TaxonifiToTaxonworks



24
25
26
27
28
29
30
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 24

def initialize(nomenclature_code: nil, parent_taxon_name_id: nil, also_create_otu: false, **args)
  @nomenclature_code    = nomenclature_code
  @also_create_otu      = also_create_otu
  @parent_taxon_name_id = parent_taxon_name_id

  super(args)
end

Instance Attribute Details

- (Object) also_create_otu

Whether to create an OTU as well



19
20
21
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 19

def also_create_otu
  @also_create_otu
end

- (Object) name_collection

The Taxonifi Name collection



7
8
9
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 7

def name_collection
  @name_collection
end

- (Object) nomenclature_code

The code (Rank Class) that new names will use



16
17
18
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 16

def nomenclature_code
  @nomenclature_code
end

- (Object) parent_taxon_name

The parent for the top level names



10
11
12
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 10

def parent_taxon_name
  @parent_taxon_name
end

- (Object) parent_taxon_name_id

The id of the parent taxon name, computed automatically as Root if not provided



13
14
15
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 13

def parent_taxon_name_id
  @parent_taxon_name_id
end

- (Object) project_id

Required to handle some defaults



22
23
24
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 22

def project_id
  @project_id
end

Instance Method Details

- (Object) build



52
53
54
55
56
57
58
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 52

def build
  if valid?
    build_name_collection
    build_protonyms
    @processed = true
  end
end

- (Object) build_name_collection (protected)



62
63
64
65
66
67
68
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 62

def build_name_collection
  begin
    @name_collection ||= ::Taxonifi::Lumper.create_name_collection(csv: csv)
  rescue Taxonifi::Assessor::RowAssessor::RowAssessorError
    @file_errors.push "Error assessing a row of data in the inputfile."
  end
end

- (Object) build_protonyms (protected)



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
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 70

def build_protonyms
  if name_collection.nil?
    @file_errors.push "No names were readable in the file."
    return
  end

  parents = {}

  total_lines = 0

  name_collection.collection.each do |n|
    i  = n.row_number + 1
    rp = nil

    if @processed_rows[i]
      rp = @processed_rows[i]
    else
      rp = BatchLoad::RowParse.new
      @processed_rows[i] = rp
    end

    p = Protonym.new(
      name:                          n.name,
      year_of_publication:           n.year.to_s,
      rank_class:                    Ranks.lookup(nomenclature_code, n.rank),
      by:                            @user,
      also_create_otu:               also_create_otu,
      project:                       @project,
      verbatim_author:               (n.parens ? n.author_with_parens : nil),
      taxon_name_authors_attributes: taxon_name_authors_hash(n)
    )

    p.parent = (n.parent.nil? ? parent_taxon_name : parents[n.parent.id])

    rp.objects[:protonyms] ||= []
    rp.objects[:protonyms].push(p)

    parents[n.id] = p

    total_lines = i if total_lines < i
  end

  @total_data_lines = total_lines

  true
end

- (Object) taxon_name_authors_hash(taxonifi_name) (protected)



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 117

def taxon_name_authors_hash(taxonifi_name)
  author_attributes = []
  taxonifi_name.authors.each do |a|
    suffix = a.suffix.join(' ') if !a.suffix.nil?
    author_attributes.push({
                             last_name:  a.last_name,
                             first_name: [a.first_name, a.initials_string].compact.join(' '),
                             suffix:     suffix, # this might not be right, have to 2x check
                           })
  end
  author_attributes
end