Class: BatchLoad::Import::DWCA

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

Overview

TODO: Originally transliterated from Import::CollectingEvents: Remove this to-do after successful operation.

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_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

- (DWCA) initialize(dwca_namespace: nil, **args)

Returns a new instance of DWCA



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

def initialize(dwca_namespace: nil, **args)
  @collecting_events = {}
  @rows              = []
  @row_objects       = {}
  @dwca_namespace    = dwca_namespace
  @parser            = ScientificNameParser.new
  @tasks_            = {
    make_tn:  %w(scientificName taxonRank family kingdom),
    make_td:  %w(otherPile),
    make_otu: %w(pileMaker),
    make_co:  %w(pileMaker catalogNumber basisOfRecord individualCount organismQuantity organismQuantityType recordedBy),
    make_ce:  %w(pileMaker verbatimLocality eventDate decimalLatitude decimalLongitude countryCode recordedBy locationRemarks)
  }.freeze

  pre_load

  super(args)
end

Instance Attribute Details

- (Object) collecting_events

Returns the value of attribute collecting_events



5
6
7
# File 'lib/batch_load/import/dwca.rb', line 5

def collecting_events
  @collecting_events
end

- (Object) dwca_namespace

Returns the value of attribute dwca_namespace



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

def dwca_namespace
  @dwca_namespace
end

- (Object) parser

Returns the value of attribute parser



9
10
11
# File 'lib/batch_load/import/dwca.rb', line 9

def parser
  @parser
end

- (Object) row_objects

Returns the value of attribute row_objects



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

def row_objects
  @row_objects
end

- (Object) rows

Returns the value of attribute rows



12
13
14
# File 'lib/batch_load/import/dwca.rb', line 12

def rows
  @rows
end

- (Object) tasks_

Returns the value of attribute tasks_



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

def tasks_
  @tasks_
end

Instance Method Details

- (Object) _setup (private)

only for use in a TaxonWorks rails console



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/batch_load/import/dwca.rb', line 100

def _setup
  $project    = Project.where(name: 'BatchLoad Test').first
  $project_id = $project.id
  $user       = User.find(185)
  $user_id    = $user.id
  @root       = Protonym.find_or_create_by(name:       'Root',
                                           rank_class: 'NomenclaturalRank',
                                           parent_id:  nil,
                                           project_id: $project_id)
  @animalia   = Protonym.find_or_create_by(name:       'Animalia',
                                           parent_id:  @root.id,
                                           rank_class: NomenclaturalRank::Iczn::HigherClassificationGroup::Kingdom,
                                           project_id: $project_id)
end

- (Object) build



86
87
88
89
90
91
# File 'lib/batch_load/import/dwca.rb', line 86

def build
  if valid?
    build_dwca
    @processed = true
  end
end

- (Object) build_dwca

process each row for information:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/batch_load/import/dwca.rb', line 72

def build_dwca
  line_counter = 1 # accounting for headers

  tasks = triage(csv.headers, tasks_)
  csv.each do |row|
    @row_objects = {}
    tasks.each {|task|
      @row_objects[task] = send(task, row)
    }
    line_counter += 1
  end
  @total_lines = line_counter - 1
end

- (Object) create



93
94
95
# File 'lib/batch_load/import/dwca.rb', line 93

def create

end

- (Object) make_ce(row) (private)



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/batch_load/import/dwca.rb', line 127

def make_ce(row)
  lat, long = row['decimalLatitude'], row['decimalLongitude']
  c_e       = CollectingEvent.new(verbatim_latitude:  (lat.length > 0) ? lat : nil,
                                  verbatim_longitude: (long.length > 0) ? long : nil,
                                  verbatim_locality:  row['verbatimLocality'],
                                  verbatim_date:      row['eventDate'],
                                  verbatim_label:     row['locationRemarks']
  )
  c_e.save!
  c_e
end

- (Object) make_co(row) (private)



143
144
145
# File 'lib/batch_load/import/dwca.rb', line 143

def make_co(row)
  'CollectionObject'
end

- (Object) make_otu(row) (private)



139
140
141
# File 'lib/batch_load/import/dwca.rb', line 139

def make_otu(row)
  'Otu'
end

- (Object) make_td(row) (private)



174
175
176
# File 'lib/batch_load/import/dwca.rb', line 174

def make_td(row)
  'TaxonDetermination'
end

- (Object) make_tn(row) (private)



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/batch_load/import/dwca.rb', line 147

def make_tn(row)
  this_kingdom = row['kingdom']
  unless this_kingdom.name == @kingdom.name
    @kingdom = Protonym.find_or_create_by(name:       @kingdom.name,
                                          parent_id:  @root.id,
                                          rank_class: NomenclaturalRank::Iczn::HigherClassificationGroup::Kingdom,
                                          project_id: sessions_current_project_id)
  end

  this_family = row['family']
  unless @family.name == this_family
    @family = Protonym.find_or_create_by(name:       this_family,
                                         parent_id:  @kingdom.id,
                                         rank_class: Ranks.lookup(:iczn, 'family'),
                                         project_id: sessions_current_project_id)
  end
  sn  = row['scientificName']
  snp = @parser.parse(sn)

  t_n = TaxonName.new(name:            snp[:scientificName][:canonical],
                      parent_id:       @family.id,
                      rank_class:      Ranks.lookup(:iczn, taxonRank),
                      also_create_otu: true)
  t_n.save!
  t_n
end

- (Object) pre_load (private)

what to do before you try to load the entire file



116
117
118
119
120
121
122
123
124
125
# File 'lib/batch_load/import/dwca.rb', line 116

def pre_load
  @root    = Protonym.find_or_create_by(name:       'Root',
                                        rank_class: 'NomenclaturalRank',
                                        parent_id:  nil,
                                        project_id: sessions_current_project_id)
  @kingdom = Protonym.find_or_create_by(name:       'Animalia',
                                        parent_id:  @root.id,
                                        rank_class: NomenclaturalRank::Iczn::HigherClassificationGroup::Kingdom,
                                        project_id: sessions_current_project_id)
end

- (Object) preview_dwca



34
35
36
37
38
# File 'lib/batch_load/import/dwca.rb', line 34

def preview_dwca
  @preview_table = {}

  @preview_table
end

- (Object) triage(headers, tasks) (private)

2.3.3 :057 > headers

=> ["a", "b", "c", "d", "e", "f"]
2.3.3 :058 > mthds
=> {:foo=>["a", "b"], :bar=>["d", "e"], :blorf=>["f", "z"]}
2.3.3 :059 > mthds.select{|k,v| v & headers == v}
=> {:foo=>["a", "b"], :bar=>["d", "e"]}
2.3.3 :060 > mthds.select{|k,v| v & headers == v}.keys
=> [:foo, :bar]
2.3.3 :061 >


189
190
191
# File 'lib/batch_load/import/dwca.rb', line 189

def triage(headers, tasks)
  tasks.select {|kee, vlu| vlu & headers == vlu}.keys
end