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.

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

- (CollectingEvents) initialize(ce_namespace: nil, **args)

Returns a new instance of CollectingEvents



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

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

Instance Attribute Details

- (Object) ce_namespace

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

- (Object) collecting_events

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

- (Object) build



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

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

- (Object) build_collecting_events

process each row for information:



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

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:                          otu,
                                                        biological_collection_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)
    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)}|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

- (Object) preview_collecting_events



15
16
17
18
19
# File 'lib/batch_load/import/collecting_events.rb', line 15

def preview_collecting_events
  @preview_table = {}

  @preview_table
end