Class: ODDB::CsvExportPlugin

Inherits:
Plugin show all
Defined in:
src/plugin/csv_export.rb

Constant Summary

EXPORT_SERVER =
DRbObject.new(nil, EXPORT_URI)
EXPORT_DIR =
File.join(ARCHIVE_PATH, 'downloads')
ODDB_RECIPIENTS =
[ "produktion@seconag.com" ]
ODDB_RECIPIENTS_EXTENDED =
[ "metamaxi@postmail.ch" ]

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Method Summary (collapse)

Methods inherited from Plugin

#initialize, #l10n_sessions, #recipients, #resolve_link, #update_rss_feeds

Methods included from HttpFile

#http_body, #http_file

Constructor Details

This class inherits a constructor from ODDB::Plugin

Instance Method Details

- (Object) _export_drugs(export_name, keys)



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
# File 'src/plugin/csv_export.rb', line 46

def _export_drugs(export_name, keys)
  session = SessionStub.new(@app)
  session.language = 'de'
  session.lookandfeel = LookandfeelBase.new(session)
  model = @app.atc_classes.values.sort_by { |atc| atc.code }
  name = "#{export_name}.csv"
  @file_path = path = File.join(EXPORT_DIR, name)
  exporter = View::Drugs::CsvResult.new(model, session)
  exporter.to_csv_file(keys, path, :packages)
  dups = exporter.duplicates
  @counts = exporter.counts
  @counts['duplicates'] = dups.size
  unless(dups.empty?)
    log = Log.new(@@today)
    log.report = sprintf "CSV-Export includes %i duplicates:\n%s",
                         dups.size, dups.join("\n")
    log.notify("CSV-Export includes %i duplicates" % dups.size)
  end
  EXPORT_SERVER.compress(EXPORT_DIR, name)
  backup = @app.log_group(:bsv_sl).newest_date.strftime("#{export_name}.%Y-%m-%d.csv")
  backup_dir = File.expand_path('../../data/csv', File.dirname(__FILE__))
  backup_path = File.join(backup_dir, backup)
  unless(File.exist? backup_path)
    FileUtils.mkdir_p(backup_dir)
    FileUtils.cp(path, backup_path)
  end
rescue
  puts $!.message
  puts $!.backtrace
  raise
end

- (Object) export_analysis



12
13
14
15
16
# File 'src/plugin/csv_export.rb', line 12

def export_analysis
  ids = @app.analysis_positions.sort_by { |pos|
    pos.code }.collect { |pos| pos.odba_id }
  EXPORT_SERVER.export_analysis_csv(ids, EXPORT_DIR, 'analysis.csv')
end

- (Object) export_doctors



17
18
19
20
# File 'src/plugin/csv_export.rb', line 17

def export_doctors
  ids = @app.doctors.values.collect { |item| item.odba_id }
  EXPORT_SERVER.export_doc_csv(ids, EXPORT_DIR, 'doctors.csv')
end

- (Object) export_drugs



21
22
23
24
25
26
27
28
29
30
31
32
# File 'src/plugin/csv_export.rb', line 21

def export_drugs
  @options = { :iconv => 'ISO-8859-1//TRANSLIT//IGNORE' }
  recipients.concat self.class::ODDB_RECIPIENTS
  _export_drugs 'oddb', [ :rectype, :iksnr, :ikscd, :ikskey, :barcode,
    :bsv_dossier, :pharmacode, :name_base, :galenic_form,
    :most_precise_dose, :size, :numerical_size, :price_exfactory,
    :price_public, :company_name, :ikscat, :sl_entry, :introduction_date,
    :limitation, :limitation_points, :limitation_text, :lppv,
    :registration_date, :expiration_date, :inactive_date, :export_flag,
    :casrn, :generic_type, :has_generic, :deductible, :out_of_trade,
    :c_type, :index_therapeuticus, :ith_swissmedic, :narcotic, :vaccine ]
end

- (Object) export_drugs_extended



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'src/plugin/csv_export.rb', line 33

def export_drugs_extended
  @options = { :compression => 'zip' }
  recipients.concat self.class::ODDB_RECIPIENTS_EXTENDED
  _export_drugs 'oddb2', [ :rectype, :iksnr, :ikscd, :ikskey, :barcode,
    :bsv_dossier, :pharmacode, :name_base, :galenic_form_de, :galenic_form_fr,
    :most_precise_dose, :size, :numerical_size_extended, :price_exfactory,
    :price_public, :company_name, :ikscat, :sl_entry, :introduction_date,
    :limitation, :limitation_points, :limitation_text, :lppv,
    :registration_date, :expiration_date, :inactive_date, :export_flag,
    :casrn, :generic_type, :has_generic, :deductible, :out_of_trade,
    :c_type, :route_of_administration, :galenic_group_de, 
    :galenic_group_fr ]
end

- (Object) export_index_therapeuticus



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'src/plugin/csv_export.rb', line 77

def export_index_therapeuticus
  @options = { :iconv => 'ISO-8859-1//TRANSLIT//IGNORE' }
  recipients.concat self.class::ODDB_RECIPIENTS
  ids = @app.indices_therapeutici.sort.collect { |code, idx| idx.odba_id }
  files = []
  @file_path = File.join EXPORT_DIR, 'idx_th.csv'
  files.push EXPORT_SERVER.export_idx_th_csv(ids, EXPORT_DIR, 'idx_th.csv')
  ids = @app.packages.sort_by { |pac| pac.ikskey }.collect { |pac| 
    pac.odba_id }
  files.push EXPORT_SERVER.export_ean13_idx_th_csv(ids, EXPORT_DIR, 
                                                   'ean13_idx_th.csv')
  EXPORT_SERVER.compress_many(EXPORT_DIR, 'index_therapeuticus', files)
end

- (Object) export_migel



90
91
92
93
94
# File 'src/plugin/csv_export.rb', line 90

def export_migel
  ids = @app.migel_products.sort_by { |product| 
    product.migel_code }.collect { |product| product.odba_id }
  EXPORT_SERVER.export_migel_csv(ids, EXPORT_DIR, 'migel.csv')
end

- (Object) export_narcotics



95
96
97
98
99
100
101
# File 'src/plugin/csv_export.rb', line 95

def export_narcotics
  ids = @app.narcotics.values.sort_by { |narcotic| 
    narcotic.substance.to_s }.collect { |narcotic| 
    narcotic.odba_id }
  EXPORT_SERVER.export_narcotics_csv(ids, EXPORT_DIR,
    'narcotics.csv')
end

- (Object) export_price_history



102
103
104
105
106
107
# File 'src/plugin/csv_export.rb', line 102

def export_price_history
  ids = @app.packages.select do |pac| pac.has_price? end.collect do |pac|
    pac.odba_id
  end
  EXPORT_SERVER.export_price_history_csv(ids, EXPORT_DIR, 'price_history.csv')
end

- (Object) log_info



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'src/plugin/csv_export.rb', line 108

def log_info
  hash = super
  if @file_path
    @options ||= {}
    path = @file_path
    type = "text/csv"
    if comp = @options[:compression]
      path = @file_path + "." << comp
      type = "application/#{comp}"
    end
    if iconv = @options[:iconv]
      type = [ type, iconv ]
    end
    hash.store(:files, { path => type })
    hash.store(:recipients, recipients)
  end
  hash
end

- (Object) report



126
127
128
129
130
131
132
133
134
# File 'src/plugin/csv_export.rb', line 126

def report
  report = ''
  if @counts
    @counts.sort.collect do |key, val|
      report << sprintf("%-32s %5i\n", key, val)
    end
  end
  report
end