Class: Lilco::MilkAnalysesExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/lilco/milk_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, deprecated?, exchanger_name, export, exporters, find, find_and_import, find_by, 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


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
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/exchangers/lilco/milk_analyses_exchanger.rb', line 3

def import
  analyser_attributes = YAML.load_file(File.join(File.dirname(__FILE__), 'organization.yml'))

  unless analyser = Entity.find_by(siret_number: analyser_attributes[: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

  trans_inhib = {
    'NEG' => 'negative',
    'POS' => 'positive'
  }

  rows.each do |row|
    r = OpenStruct.new(
      year: row[0],
      month: row[1],
      order: row[2],
      reference_number: (row[0].to_s + row[1].to_s.rjust(2, '0') + row[2].to_s.rjust(2, '0')),
      at: Date.civil(row[0].to_i, row[1].to_i, row[2].to_i * 9),
      germes: (row[3].blank? ? 0 : row[3].to_i).in_thousand_per_milliliter,
      inhib: trans_inhib[row[4]] || 'negative',
      mg: (row[5].blank? ? 0 : row[5].to_d / 100).in_gram_per_liter,
      mp: (row[6].blank? ? 0 : row[6].to_d / 100).in_gram_per_liter,
      cells: (row[7].blank? ? 0 : row[7].to_i).in_thousand_per_milliliter,
      buty: (row[8].blank? ? 0 : row[8].tr(',', '.').to_i).in_unity_per_liter,
      cryo: (row[9].blank? ? 0 : row[9].tr(',', '.').to_d).in_celsius,
      lipo: (row[10].blank? ? 0 : row[10].tr(',', '.').to_d).in_thousand_per_hectogram,
      igg: (row[11].blank? ? 0 : row[11].to_d).in_unity_per_liter,
      uree: (row[12].blank? ? 0 : row[12].to_i).in_milligram_per_liter,
      salmon: row[13],
      listeria: row[14],
      staph: row[15],
      coli: row[16],
      pseudo: row[17],
      ecoli: row[18]
    )

    unless analysis = Analysis.where(reference_number: r.reference_number, analyser: analyser).first
      analysis = Analysis.create!(
        reference_number: r.reference_number,
        nature: 'cow_milk_analysis',
        analyser: analyser,
        analysed_at: r.at,
        sampled_at: r.at
      )

      analysis.read!(:total_bacteria_concentration, r.germes) if r.germes.to_f > 0
      analysis.read!(:inhibitors_presence, r.inhib)
      analysis.read!(:fat_matters_concentration, r.mg) if r.mg.to_f > 0
      analysis.read!(:protein_matters_concentration, r.mp) if r.mp.to_f > 0
      analysis.read!(:somatic_cell_concentration, r.cells) if r.cells.to_f > 0
      analysis.read!(:clostridial_spores_concentration, r.buty) if r.buty.to_f > 0
      analysis.read!(:freezing_point_temperature, r.cryo) if r.cryo.to_f > 0
      analysis.read!(:lipolysis, r.lipo) if r.lipo.to_f > 0
      analysis.read!(:immunoglobulins_concentration, r.igg) if r.igg.to_f > 0
      analysis.read!(:urea_concentration, r.uree) if r.uree.to_f > 0

    end

    w.check_point
  end
end