Class: BatchLoad::Import::DWCA
- Inherits:
-
BatchLoad::Import
- Object
- BatchLoad::Import
- BatchLoad::Import::DWCA
- 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
-
#collecting_events ⇒ Object
Returns the value of attribute collecting_events.
-
#dwca_namespace ⇒ Object
Returns the value of attribute dwca_namespace.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#repo ⇒ Object
Returns the value of attribute repo.
-
#row_objects ⇒ Object
Returns the value of attribute row_objects.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#tasks_ ⇒ Object
Returns the value of attribute tasks_.
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
-
#_setup ⇒ Object
private
only for use in a TaxonWorks rails console.
- #build ⇒ Boolean
-
#build_dwca ⇒ Integer
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/BlockNesting.
- #code_to_name(code) ⇒ nil, GeographicArea private
- #create ⇒ Object
- #dump_hash(objects) ⇒ Object private
-
#initialize(dwca_namespace: nil, **args) ⇒ DWCA
constructor
A new instance of DWCA.
-
#loop_end(line_counter) ⇒ Integer
private
package up the results of the process into a hash.
- #make_ba(row) ⇒ Hash private
-
#make_ce(row) ⇒ Hash
private
rubocop:disable Metrics/MethodLength a full set of symbolized filter names (kees, below) can be found in Utilities::Dates::REGEXP_DATES.keys.
- #make_co(row) ⇒ Hash private
- #make_gr(row) ⇒ Georeference private
- #make_ident(row) ⇒ Hash private
-
#make_notes(row) ⇒ Hash
private
Of notes where key is object type.
- #make_otu(row) ⇒ Otu private
- #make_prsn(row) ⇒ Hash private
- #make_tag(row) ⇒ Hash private
- #make_td(row) ⇒ TaxonDetermination private
- #make_tn(row) ⇒ Hash private
- #preview_dwca ⇒ Hash
- #save_hash(objects) ⇒ Array private
-
#triage(headers, tasks) ⇒ Array
private
intersection of the word list from the tasks hash (per key) and the list of headers.
Methods inherited from BatchLoad::Import
#all_objects, #create_attempted?, #import_level_ok?, #line_strict_level_ok?, #processed?, #ready_to_create?, #save_order, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?
Constructor Details
#initialize(dwca_namespace: nil, **args) ⇒ DWCA
Returns a new instance of DWCA.
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 |
# File 'lib/batch_load/import/dwca.rb', line 17 def initialize(dwca_namespace: nil, **args) @dwca_namespace = dwca_namespace @root = args.delete(:root) @kingdom = args.delete(:kingdom) @cat_no_pred = args.delete(:cat_no_pred) @geo_rem_kw = args.delete(:geo_rem_kw) @repo = args.delete(:repo) @namespace = args.delete(:namespace) @collecting_events = {} @rows = {} @row_objects = {} @parser = ::Biodiversity::Parser @tasks_ = { make_ident: %w(catalognumber occurrenceid), make_prsn: %w(georeferencedby), make_tn: %w(scientificname taxonrank family kingdom), make_td: %w(scientificname basisofrecord individualcount organismquantity organismquantitytype recordedby), # make_otu: %w(scientificname), make_co: %w(individualcount organismquantity organismquantitytype), make_ba: %w(associatedtaxa), make_notes: %w(georeferenceremarks locationremarks occurrenceremarks), make_tag: %w(), make_gr: %w(decimallatitude decimallongitude countrycode stateprovince county municipality locality coordinateuncertaintyinmeters georeferencedby), make_ce: %w(decimallatitude decimallongitude coordinateuncertaintyinmeters eventdate locationremarks recordedby geodeticdatum countrycode stateprovince county municipality locality) }.freeze super(**args) end |
Instance Attribute Details
#collecting_events ⇒ Object
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 |
#dwca_namespace ⇒ Object
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 |
#parser ⇒ Object
Returns the value of attribute parser.
10 11 12 |
# File 'lib/batch_load/import/dwca.rb', line 10 def parser @parser end |
#repo ⇒ Object
Returns the value of attribute repo.
8 9 10 |
# File 'lib/batch_load/import/dwca.rb', line 8 def repo @repo end |
#row_objects ⇒ Object
Returns the value of attribute row_objects.
14 15 16 |
# File 'lib/batch_load/import/dwca.rb', line 14 def row_objects @row_objects end |
#rows ⇒ Object
Returns the value of attribute rows.
13 14 15 |
# File 'lib/batch_load/import/dwca.rb', line 13 def rows @rows end |
#tasks_ ⇒ Object
Returns the value of attribute tasks_.
11 12 13 |
# File 'lib/batch_load/import/dwca.rb', line 11 def tasks_ @tasks_ end |
Instance Method Details
#_setup ⇒ Object (private)
only for use in a TaxonWorks rails console
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/batch_load/import/dwca.rb', line 371 def _setup project = Project.where(name: 'BatchLoad Test').first Current.project_id = project.id user = User.find(185) Current.user_id = user.id @root = Protonym.find_or_create_by(name: 'Root', rank_class: 'NomenclaturalRank', parent_id: nil) @animalia = Protonym.find_or_create_by(name: 'Animalia', parent_id: @root.id, rank_class: Ranks.lookup(:iczn, :kingdom)) end |
#build ⇒ Boolean
312 313 314 315 316 317 |
# File 'lib/batch_load/import/dwca.rb', line 312 def build if valid? build_dwca @processed = true end end |
#build_dwca ⇒ Integer
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/BlockNesting
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/batch_load/import/dwca.rb', line 88 def build_dwca line_counter = 1 # accounting for headers tasks = triage(csv.headers, tasks_) csv.each do |row| @warns = [] @errs = [] @rows[line_counter] = {} @rows[line_counter][:row] = row @row_objects = {} tasks.each {|task| results = send(task, row) @row_objects[task] = results warn = results[:warn] err = results[:err] @errs.push(results.delete(:err)) unless err.blank? @warns.push(results.delete(:warn)) unless warn.blank? } t_n = @row_objects[:make_tn].select {|kee, val| val != nil}.values.first # save the (possible new) taxon_name t_n.save! if t_n.new_record? # associate the taxon_name with the otu otu = Otu.find_or_create_by!(taxon_name: t_n) otu.save! if otu.new_record? @row_objects[:make_otu] = otu t_d = @row_objects[:make_td] c_o = @row_objects[:make_co][:c_o] idents = @row_objects[:make_ident] id_cat_no = idents[:id_cat_no] id_occ_id = idents[:id_occ_id] b_a_s = @row_objects[:make_ba] notes = @row_objects[:make_notes] c_o_notes = notes[:c_o] c_e_notes = notes[:c_e] g_r_notes = notes[:g_r] c_e = @row_objects[:make_ce][:c_e] g_r = @row_objects[:make_gr] ppl = @row_objects[:make_prsn] unless c_e.valid? test_list = c_e.errors.keys if test_list.include?(:start_date_day) or test_list.include?(:start_date_month) or test_list.include?(:start_date_year) or test_list.include?(:end_date_day) or test_list.include?(:end_date_month) or test_list.include?(:end_date_year) @warns.push(c_e.errors.) @warns.push("eventDate (#{c_e.verbatim_date}) does not parse properly.") c_e.start_date_day = nil c_e.start_date_month = nil c_e.start_date_year = nil c_e.end_date_day = nil c_e.end_date_month = nil c_e.end_date_year = nil end end if c_e.valid? # check c_e again c_e.save if c_e.new_record? unless c_e_notes.blank? apply_note = false c_e_notes.each_key {|kee| c_e_n = c_e_notes[kee] if c_e.notes.present? c_e.notes.each {|note| if note.text == c_e_n.text # need to drop the object without warning @row_objects[:make_notes][:c_e].delete(:remark) else apply_note = true end } else apply_note = true end c_e_n.note_object = c_e if apply_note } end else @errs.push(c_e.errors..flatten) # everything else is a fail. line_counter = loop_end(line_counter) next end # associate the collection_object with the collecting_event c_o.collecting_event = c_e # associate the namespace and catalog number with the collection_object if id_cat_no.blank? @warns.push('Missing catalog number.') else c_o.identifiers << id_cat_no end if id_occ_id.blank? # is a warning required? else c_o.identifiers << id_occ_id end c_o.repository = @repo c_o.save! # add the possible biological_associations, new? otu to collection_object unless b_a_s.blank? b_a_s.each_key {|kee| b_a = b_a_s[kee] b_a.biological_association_subject = c_o } end # add notes to collection_object, if required unless c_o_notes.blank? c_o_notes.each_key {|kee| c_o_notes[kee].note_object = c_o } end # associate the collection_object with the otu using the taxon_determiniation t_d.biological_collection_object = c_o t_d.otu = otu t_d.save! # associate the georeference with the collecting_event g_r.collecting_event = c_e # two different types of georeferences: verbatim, and geolocate if g_r.type.include?('Ver') # for verbatim_data, generate a new georeference using the collecting_event a = c_e.generate_verbatim_data_georeference if a.valid? # and replace the pre-built georeference g_r = c_e.georeferences.last @row_objects[:make_gr] = g_r else @warns.push(a.errors.) @row_objects.delete(:make_gr) end else # is a GeoLocate unless g_r.valid? # georeference was (0,0), will be dropped err_txt = 'Georeference::GeoLocate cannot be created, ' if g_r.errors.[:collecting_event_id].present? # what to do here? @warns.push(err_txt + 'duplicate georeference') else @warns.push(err_txt + 'lat/long not valid.') end @row_objects.delete(:make_gr) unless g_r_notes.blank? @warns.push('Georeference::GeoLocate cannot be created, \'georeferenceRemark\' has been dropped.') @row_objects[:make_notes].delete(:g_r) end end end # add a georeferencer as required g_rer = ppl[:g_r] unless g_rer.blank? unless g_r.georeferencers.include?(g_rer) g_r.georeferencers << g_rer end end # add notes to georeference, if required unless g_r_notes.blank? apply_note = false g_r_notes.each_key {|kee| g_r_n = g_r_notes[kee] if g_r.notes.present? g_r.notes.each {|note| if note.text == g_r_n.text # need to drop the object without warning @row_objects[:make_notes][:g_r].delete(:remark) else apply_note = true end } else apply_note = true end g_r_n.note_object = g_r if apply_note } end # either the original, or the ce-created one gets saved # g_r.save begin # make sure all objects for this row get saved @row_objects.each_key {|kee| objects = @row_objects[kee] case objects.class.to_s when 'Array' objects.flatten.each {|object| if object.valid? object.save else @errs.push(object.errors.) end } when 'Hash' @errs.push(save_hash(objects)) else # all other single object classes test = objects.try(:valid?) case test when true objects.save when false @errs.push(objects.errors.) when nil @warns.push('No georeference exists.') end end } #@warns.push("warning #{line_counter}") #@errs.push("error #{line_counter}") if line_counter.even? @warns.flatten! @errs.flatten! # ap@warns end line_counter = loop_end(line_counter) # break if line_counter > 30 end @total_lines = line_counter - 1 end |
#code_to_name(code) ⇒ nil, GeographicArea (private)
775 776 777 |
# File 'lib/batch_load/import/dwca.rb', line 775 def code_to_name(code) code.nil? ? nil : GeographicArea.where(iso_3166_a2: code).try(:first).try(:name) end |
#create ⇒ Object
319 320 321 |
# File 'lib/batch_load/import/dwca.rb', line 319 def create end |
#dump_hash(objects) ⇒ Object (private)
336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/batch_load/import/dwca.rb', line 336 def dump_hash(objects) objects.each_key {|kee| object = objects[kee] unless object.blank? if object.class.to_s == 'Hash' dump_hash(object) else object.destroy end end } end |
#loop_end(line_counter) ⇒ Integer (private)
package up the results of the process into a hash
328 329 330 331 332 333 |
# File 'lib/batch_load/import/dwca.rb', line 328 def loop_end(line_counter) @rows[line_counter][:row_objects] = @row_objects @rows[line_counter][:err] = @errs @rows[line_counter][:warn] = @warns line_counter + 1 end |
#make_ba(row) ⇒ Hash (private)
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/batch_load/import/dwca.rb', line 391 def make_ba(row) # for each associatedTaxa, find or create an otu, and creata a biological association for it, # connecting it to a collection_object retval = {} bas = row['associatedtaxa'] unless bas.blank? taxa = bas.split('|') taxa.each {|bio_assoc| unless bio_assoc.blank? br = BiologicalRelationship.find_or_create_by!(name: 'associated_taxa', project_id: @project_id) otu = Otu.find_or_create_by!(name: bio_assoc, project_id: @project_id) ba = BiologicalAssociation.new(biological_relationship: br, biological_association_object: otu, biological_association_object_type: 'Otu', biological_association_subject_type: 'CollectionObject') retval[bio_assoc] = ba end } end retval end |
#make_ce(row) ⇒ Hash (private)
rubocop:disable Metrics/MethodLength a full set of symbolized filter names (kees, below) can be found in Utilities::Dates::REGEXP_DATES.keys
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/batch_load/import/dwca.rb', line 561 def make_ce(row) ret_val = {} kees = [:yyyy_mm_dd, :mm_dd_yy] d_s = row['eventdate'] v_l = [row['municipality'], row['locality']].select {|name| name.present?}.join(':') date_params = {} unless d_s.blank? trials = Utilities::Dates.hunt_dates(d_s, kees) trials.each_key {|kee| trial = trials[kee] unless trial.blank? case kee when kees[0] type = kee when kees[1] type = kee end sdd, sdm, sdy = trial[:start_date_day], trial[:start_date_month], trial[:start_date_year] edd, edm, edy = trial[:end_date_day], trial[:end_date_month], trial[:end_date_year] date_params[:start_date_day] = sdd unless sdd.blank? date_params[:start_date_month] = sdm unless sdm.blank? date_params[:start_date_year] = sdy unless sdy.blank? date_params[:end_date_day] = edd unless edd.blank? date_params[:end_date_month] = edm unless edm.blank? date_params[:end_date_year] = edy unless edy.blank? break # bail after the first with a date end } end g_a_list = [row['county'], row['stateprovince'], code_to_name(row['countrycode'])] g_a_text = g_a_list.select {|name| name.present?}.join(':') if g_a_text.blank? g_a = nil else g_a_matches = GeographicArea.matching(g_a_text, true) g_a = g_a_matches[g_a_text].try(:first) end hunt_params = {project_id: @project_id, geographic_area: g_a, verbatim_datum: row['geodeticdatum'], verbatim_locality: v_l, verbatim_date: d_s, verbatim_label: row['locationremarks'], verbatim_geolocation_uncertainty: row['coordinateuncertaintyinmeters'], verbatim_latitude: row['decimallatitude'], verbatim_longitude: row['decimallongitude'], verbatim_collectors: row['recordedby']}.merge!(date_params) c_e = CollectingEvent.find_or_initialize_by(hunt_params) ret_val[:c_e] = c_e ret_val.merge!(err: []) ret_val.merge!(warn: []) ret_val end |
#make_co(row) ⇒ Hash (private)
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/batch_load/import/dwca.rb', line 628 def make_co(row) ret_val = {} warn = [] err = [] ic = row['individualcount'] oq = row['organismquantity'] oqt = row['organismquantitytype'] combo = false if ic.present? and (oqt.present? or oq.present?) # warn.push('Choose either individualCount or the combination of organismQuantity and organismQuantityType, # not both.') combo = true end if (ic.to_i > 1) and combo # leave ic alone warn.push('individualCount has been used instead of the combination of organismQuantity and organismQuantityType.') else # set ic according to organismQuantityType case oqt when 'individuals' ic = oq.to_i when 'percentageOfSpecies', 'percentageOfBiovolume', 'percentageOfBiomass', 'percentageCoverage', 'dominScale', 'braunBlanquetScale', 'biomassAFDG', 'biomassG', 'biomassKg', 'biovolumeCubicMicrons', 'biovolumeMl' ic = oq else warn.push("Unrecognized organismQuantityType #{oqt}.") ic = oq end end # ic will cause the selection of 'lot' or 'specimen' c_o = Specimen.new(total: ic) ret_val[:c_o] = c_o ret_val.merge!(err: err) ret_val.merge!(warn: warn) ret_val end |
#make_gr(row) ⇒ Georeference (private)
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/batch_load/import/dwca.rb', line 466 def make_gr(row) geo_by = row['georeferencedby'] # lat, long = (lat.length > 0) ? lat : nil, (long.length > 0) ? long : nil lat, long = row['decimallatitude'], row['decimallongitude'] if lat.nil? and long.nil? uncert = nil else uncert = row['coordinateuncertaintyinmeters'] end geo_by = '' if geo_by.nil? if geo_by.downcase.include?('verbatim') # make a verbatim reference # fill in what we can, the real work is done when the collecting_event is added g_r = Georeference::VerbatimData.new(error_radius: uncert) else # faking a Georeference::GeoLocate: gl_req_params = {country: code_to_name(row['countrycode']), state: row['stateprovince'], county: row['county'], Placename: row['municipality'], Uncert: uncert, Latitude: lat, Longitude: long, locality: row['locality'], gc: geo_by}.stringify_keys req = Georeference::GeoLocate::RequestUI.new(gl_req_params) # 1) new the Georeference, without a collecting_event g_r = Georeference::GeoLocate::GeoLocate.find_or_initialize_by(api_request: req.request_params_string, project_id: @project_id) # 2) save the information from the row in request_hash if g_r.new_record? g_r.api_request = req.request_params_string # 3) build a fake iframe response in the form '52.65|-106.333333|3036|Unavailable' text = "#{lat}|#{long}|#{uncert}|Unavailable" # 4) use that fake to stimulate the parser to create the object g_r.iframe_response = text end end g_r end |
#make_ident(row) ⇒ Hash (private)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/batch_load/import/dwca.rb', line 418 def make_ident(row) ident = {} cat_no = row['catalognumber'].split(' ') unless cat_no.blank? if @namespace.short_name != cat_no[0] @errs.push("Namespace (#{cat_no[0]}) does not match import namespace (#{@namespace.short_name}).") end unless cat_no[1].blank? id = Identifier::Local::CatalogNumber.find_or_initialize_by(namespace: @namespace, project_id: @project_id, identifier: cat_no[1]) ident[:id_cat_no] = id end end occ_id = row['occurrenceid'] unless occ_id.blank? id = Identifier::Global::OccurrenceId.find_or_initialize_by(relation: 'skos:exactMatch', project_id: @project_id, identifier: occ_id) ident[:id_occ_id] = id end ident end |
#make_notes(row) ⇒ Hash (private)
Returns of notes where key is object type.
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/batch_load/import/dwca.rb', line 510 def make_notes(row) ret_val = {} begin notes = {} geo_remark = row['georeferenceremarks'] unless geo_remark.blank? notes[:remark] = Note.new(text: geo_remark, project_id: @project_id) end ret_val[:g_r] = notes unless notes.blank? end begin # collecting event notes notes = {} loc_remark = row['locationremarks'] unless loc_remark.blank? notes[:remark] = Note.new(text: loc_remark, note_object_attribute: :verbatim_locality, project_id: @project_id) end ret_val[:c_e] = notes unless notes.blank? end begin # collection object notes notes = {} occ_remark = row['occurrenceremarks'] unless occ_remark.blank? notes[:remark] = Note.new(text: occ_remark, project_id: @project_id) end ret_val[:c_o] = notes unless notes.blank? end ret_val end |
#make_otu(row) ⇒ Otu (private)
619 620 621 |
# File 'lib/batch_load/import/dwca.rb', line 619 def make_otu(row) Otu.new end |
#make_prsn(row) ⇒ Hash (private)
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/batch_load/import/dwca.rb', line 445 def make_prsn(row) ppl = {} # check for person's name name = row['georeferencedby'] unless name.blank? if name.downcase.include?('verbatim') else # make a person to be used as a 'georeferencer' parsed = Person.parser(name) pr = Person.find_or_create_by!(last_name: parsed[0]['family'], first_name: parsed[0]['given']) ppl[:g_r] = pr end end ppl end |
#make_tag(row) ⇒ Hash (private)
549 550 551 552 553 554 555 |
# File 'lib/batch_load/import/dwca.rb', line 549 def make_tag(row) ret_val = {} ret_val.merge!(err: []) ret_val.merge!(warn: []) ret_val end |
#make_td(row) ⇒ TaxonDetermination (private)
769 770 771 |
# File 'lib/batch_load/import/dwca.rb', line 769 def make_td(row) TaxonDetermination.new end |
#make_tn(row) ⇒ Hash (private)
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'lib/batch_load/import/dwca.rb', line 681 def make_tn(row) ret_val = {species: nil, genus: nil, tribe: nil} this_kingdom = row['kingdom'] if @kingdom.try(:name) != this_kingdom @kingdom = Protonym.find_or_create_by(name: this_kingdom, rank_class: Ranks.lookup(:iczn, :kingdom), project_id: @project_id) if @kingdom.new_record? @kingdom.parent = @root @kingdom.save! ret_val[:kingdom] = @kingdom end end this_family = row['family'] if @family.try(:name) != this_family @family = Protonym.find_or_create_by(name: this_family, rank_class: Ranks.lookup(:iczn, :family), project_id: @project_id) if @family.new_record? @family.parent = @kingdom @family.save! ret_val[:family] = @family end end sn = row['scientificname'] snp = @parser.parse(sn) # find or create Protonym based on exact match of row['scientificname'] and taxon_names.cached # rubocop:disable Rails/SaveBang t_n = Protonym.find_or_create_by(cached: snp[:canonicalName][:simple], project_id: @project_id) this_rank = row['taxonrank'].downcase.to_sym # rubocop:enable Rails/SaveBang if t_n.new_record? case this_rank when :species begin # find or create genus genus_name = snp[:details][0][:genus][:value] @genus = Protonym.find_or_create_by(name: genus_name, parent: @family, rank_class: Ranks.lookup(:iczn, :genus), project_id: @project_id) if @genus.new_record? @genus.save! ret_val[:new_genus] = @genus end end species = snp[:details][0][:specificEpithet] t_n.parent = @genus t_n.rank_class = Ranks.lookup(:iczn, :species) t_n.name = species[:value] # t_n.cached_author_year = snp[:scientificName][:details][0][:species][:authorship] t_n.year_of_publication = (species.dig(:authorship, :basionymAuthorship, :year, :value).try(:scan, /\d+/) || []).first = species.dig(:authorship, :basionymAuthorship, :authors) if species[:authorship].include?('(') = "(#{})" end t_n. = ret_val[:species] = t_n when :genus genus = snp[:details][0][:uninomial] t_n.parent = @family t_n.rank_class = Ranks.lookup(:iczn, :genus) t_n.name = genus[:value] ret_val[:genus] = t_n when :tribe tribe = snp[:details][0][:uninomial] t_n.parent = @family t_n.rank_class = Ranks.lookup(:iczn, :tribe) t_n.name = tribe[:value] ret_val[:tribe] = t_n else raise "Unknown taxonRank #{this_rank}." end # t_n.create_otu else ret_val[this_rank] = t_n end ret_val end |
#preview_dwca ⇒ Hash
47 48 49 50 51 |
# File 'lib/batch_load/import/dwca.rb', line 47 def preview_dwca @preview_table = {} @preview_table end |
#save_hash(objects) ⇒ Array (private)
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/batch_load/import/dwca.rb', line 351 def save_hash(objects) l_errs = [] objects.each_key {|kee| object = objects[kee] unless object.blank? if object.class.to_s == 'Hash' l_errs.push(save_hash(object)) else if object.valid? object.save else l_errs.push(object.errors.) end end end } l_errs.flatten end |
#triage(headers, tasks) ⇒ Array (private)
intersection of the word list from the tasks hash (per key) and the list of headers
794 795 796 |
# File 'lib/batch_load/import/dwca.rb', line 794 def triage(headers, tasks) tasks.select {|kee, vlu| (vlu & headers) == vlu}.keys end |