Class: BatchLoad::Import::CollectingEvents::CastorInterpreter

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

Instance Attribute Summary

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(**args) ⇒ CastorInterpreter

Returns a new instance of CastorInterpreter.

Parameters:

  • args (Hash)


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

def initialize(**args)
  @collecting_events = {}
  super(**args)
end

Instance Method Details

#buildBoolean

Returns:

  • (Boolean)


121
122
123
124
125
126
# File 'lib/batch_load/import/collecting_events/castor_interpreter.rb', line 121

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

#build_collecting_eventsInteger

rubocop:disable Metrics/MethodLength

Returns:

  • (Integer)


12
13
14
15
16
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
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
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
# File 'lib/batch_load/import/collecting_events/castor_interpreter.rb', line 12

def build_collecting_events
  # DRMFieldNumbers DRMFN
  namespace_drm_field_numbers = Namespace.find_by(name: 'DRMFieldNumbers')

  @total_data_lines = 0
  i = 0

  # locality code DRM/JSS should be fully parsed
  # all have guid identifier, all but NONE have field numbers identifiers
  # for all locality codes put collecting_info into verbatim fields
  csv.each do |row|
    i += 1
    parse_result = BatchLoad::RowParse.new
    parse_result.objects[:collecting_event] = []

    @processed_rows[i] = parse_result

    next if row['locality_code_prefix'] == 'NABemb'

    begin # processing
      # Text for identifiers
      ce_identifier_castor_text = row['guid']
      ce_identifier_drm_field_numbers_text = "#{row['locality_code_prefix']}#{row['locality_code_string']}" if row['locality_code_prefix'] != 'NONE'

      # Identifiers
      ce_identifier_castor = {
        type: 'Identifier::Global::Uri',
        identifier: ce_identifier_castor_text
      }

      ce_identifier_drm_field_numbers = {
        namespace: namespace_drm_field_numbers,
        type: 'Identifier::Local::FieldNumber',
        identifier: ce_identifier_drm_field_numbers_text
      }

      # Collecting event
      ce_identifiers = []
      ce_identifiers.push(ce_identifier_castor)             if ce_identifier_castor_text.present?
      ce_identifiers.push(ce_identifier_drm_field_numbers)  if ce_identifier_drm_field_numbers_text.present?

      ce_attributes = {
        verbatim_locality:                row['verbatim_location'],
        verbatim_geolocation_uncertainty: (row['error'].to_s + ' ' + row['georeference_error_units'].to_s).strip,
        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:               row['longitude'],
        verbatim_latitude:                row['latitude'],
        verbatim_method:                  row['method'],
        field_notes:                      row['field_notes'],
        verbatim_collectors:              row['verbatim_collectors'],
        verbatim_habitat:                 row['verbatim_habitat'],
        minimum_elevation:                row['minimum_elevation'],
        maximum_elevation:                row['maximum_elevation'],
        identifiers_attributes:           ce_identifiers
      }

      ce = CollectingEvent.new(ce_attributes)

      # Assign geographic area to collecting event
      county = row['county']
      state_province = row['state_province']
      country = row['country']

      country = 'United States' if country == 'USA'

      geographic_area_params = []

      if county.present?
        county = county.split.map(&:capitalize).join(' ')
        geographic_area_params.push(county)
      end

      if state_province.present?
        state_province = state_province.split.map(&:capitalize).join(' ')
        geographic_area_params.push(state_province)
      end

      if country.present?
        country = country.split.map(&:capitalize).join(' ')
        geographic_area_params.push(country)
      end

      if geographic_area_params.length > 0
        geographic_areas = GeographicArea.find_by_self_and_parents(geographic_area_params)

        if geographic_areas.any?
          geographic_area = geographic_areas.first
          ce.geographic_area = geographic_area
        end
      end

      parse_result.objects[:collecting_event].push(ce)
      @total_data_lines += 1 if ce.present?
    #rescue
       # ....
    end
  end

  @total_lines = i
end