Class: AgroSystemes::WaterAnalysesExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/agro_systemes/water_analyses_exchanger.rb

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, exchanger_name, export, exporters, find, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject


2
3
4
5
6
7
8
9
10
11
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
# File 'app/exchangers/agro_systemes/water_analyses_exchanger.rb', line 2

def import
  here = Pathname.new(__FILE__).dirname
  analyser_attributes = YAML.load_file(here.join('entity.yml'))

  land_parcels_transcode = {}

  unless (analyser = Entity.find_by(analyser_attributes.slice(:siret_number)))
    analyser = Entity.create!(analyser_attributes)
  end

  begin
    rows = CSV.read(file, encoding: 'CP1252', col_sep: "\t", headers: true)
  rescue
    raise NotWellFormedFileError
  end
  w.count = rows.size

  rows.each do |row|
    r = OpenStruct.new(
      code_distri: (row[0].blank? ? nil : row[0].to_s),
      reference_number: row[6].to_s,
      at: (row[7].blank? ? nil : Date.civil(*row[7].to_s.split(/\//).reverse.map(&:to_i))),
      water_work_number: row[8].blank? ? nil : land_parcels_transcode[row[8]],
      potential_hydrogen: row[9].blank? ? nil : row[9].to_d,
      nitrogen_concentration: row[10].blank? ? nil : row[10].to_d.in_percent,
      sampled_at: (row[12].blank? ? nil : Date.civil(*row[12].to_s.split(/\//).reverse.map(&:to_i))),
      geolocation: (row[13].blank? ? nil : row[13].to_s)
    )
    unless (analysis = Analysis.find_by(reference_number: r.reference_number, analyser: analyser))
      analysis = Analysis.create!(
        reference_number: r.reference_number,
        nature: 'water_analysis',
        analyser: analyser,
        sampled_at: r.sampled_at,
        analysed_at: r.at
      )

      analysis.read!(:potential_hydrogen, r.potential_hydrogen) if r.potential_hydrogen
      analysis.read!(:nitrogen_concentration, r.nitrogen_concentration) if r.nitrogen_concentration
    end

    # if an lan_parcel exist, link to analysis
    if (water = Matter.of_variety('water').first)
      analysis.product = water
      analysis.save!
      water.read!(:potential_hydrogen, r.potential_hydrogen, at: r.sampled_at) if r.potential_hydrogen
      water.read!(:nitrogen_concentration, r.nitrogen_concentration, at: r.sampled_at) if r.nitrogen_concentration
    end

    # if a georeading exist, link to analysis
    if (georeading = Georeading.find_by(number: r.geolocation))
      analysis.geolocation = georeading.content
      analysis.save!
    end

    w.check_point
  end
end