Class: BatchLoad::Import::CollectingEvents

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

Overview

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

Defined Under Namespace

Classes: CastorInterpreter, GPXInterpreter

Instance Attribute Summary collapse

Attributes inherited from BatchLoad::Import

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

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?, #save_order, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?

Constructor Details

#initialize(ce_namespace: nil, **args) ⇒ CollectingEvents

Returns a new instance of CollectingEvents.

Parameters:

  • args (Hash)


10
11
12
13
14
# File 'lib/batch_load/import/collecting_events.rb', line 10

def initialize(ce_namespace: nil, **args)
  @collecting_events = {}
  @ce_namespace      = ce_namespace
  super(**args)
end

Instance Attribute Details

#ce_namespaceObject

Returns the value of attribute ce_namespace.



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

def ce_namespace
  @ce_namespace
end

#collecting_eventsObject

Returns the value of attribute collecting_events.



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

def collecting_events
  @collecting_events
end

Instance Method Details

#buildBoolean

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/batch_load/import/collecting_events.rb', line 94

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

#build_collecting_eventsInteger

rubocop:disable Metrics/MethodLength process each row for information:

Returns:

  • (Integer)


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

def build_collecting_events
  i = 1 # accounting for headers
  # identifier namespace
  header0 = csv.headers[0] # should be 'collection_object_identifier_namespace_short_name'
  header1 = csv.headers[1] # should be 'collection_object_identifier_identifier'
  header5 = csv.headers[5] # should be 'collecting_event_identifier_namespace_short_name'
  header6 = csv.headers[6] # should be 'collecting_event_identifier_identifier'
  header7 = csv.headers[7] # should be 'collecting_event_identifier_type'
  csv.each do |row|
    co_namespace = row[header0]
    co_id        = row[header1]
    next if (co_namespace.nil? or co_id.nil?) # no namespace to search!
    # find a namespace (ns1) with a short_name of row[headers[0]] (id1)
    # find a collection_object which has an identifier which has a namespace (ns1), and a cached of
    # (ns1.short_name + ' ' + identifier.identifier)
    # ns1 = Namespace.where(short_name: id1).first
    long = row['longitude'] # longitude
    lat = row['latitude'] # latitude
    method = row['method']
    error = (row['error'].to_s + ' ' + row['georeference_error_units'].to_s).strip
    ce_namespace = row[header5]
    co = CollectionObject.joins(:identifiers).where(identifiers: {cached: "#{co_namespace} #{co_id}"}).first
    otu = Otu.find_or_create_by!(name: row['otu'])
    td = TaxonDetermination.find_or_create_by!(
      otu:,
      taxon_determination_object: co)
    ce = CollectingEvent.find_or_create_by!(
      verbatim_locality: row['verbatim_location'],
      verbatim_geolocation_uncertainty: error,
      verbatim_date:                    row['verbatim_date'],
      start_date_day:                   row['start_date_day'],
      start_date_month:                 row['start_date_month'],
      start_date_year:                  row['start_date_year'],
      end_date_day:                     row['end_date_day'],
      end_date_month:                   row['end_date_month'],
      end_date_year:                    row['end_date_year'],
      verbatim_longitude:               long,
      verbatim_latitude:                lat,
      verbatim_method:                  method,
      verbatim_label:                   row['verbatim_label'])
    ce.save!
    case method.downcase
      when 'geolocate'
        # faking a Georeference::GeoLocate:
        #   1) create the Georeference, using the newly created collecting_event
        gr = Georeference::GeoLocate.create!(collecting_event: ce)
        #   2) build a fake iframe response in the form '52.65|-106.333333|3036|Unavailable'
        text = "#{lat}|#{long}|#{Utilities::Geo.distance_in_meters(error).to_f}|Unavailable"
        #   3) use that fake to stimulate the parser to create the object
        gr.iframe_response = text
        gr.save
      else
        # nothing to do?
    end unless method.nil?

    ns_ce = Namespace.where(short_name: ce_namespace).first
    ce_id = Identifier.new(namespace:  ns_ce,
                           type:       'Identifier::' + row[header7],
                           identifier: row[header6])
    ce.identifiers << ce_id
    co.collecting_event = ce
    i                   += 1
  end
  @total_lines = i - 1
end

#preview_collecting_eventsHash

Returns:

  • (Hash)


17
18
19
20
21
# File 'lib/batch_load/import/collecting_events.rb', line 17

def preview_collecting_events
  @preview_table = {}

  @preview_table
end