Module: ODDB::OdbaExporter

Defined in:
ext/export/src/oddbdat.rb,
ext/export/src/patent_xls.rb,
src/plugin/oddbdat_export.rb,
ext/export/src/generics_xls.rb,
ext/export/src/csv_exporter.rb,
ext/export/src/odba_exporter.rb,
ext/export/test/test_oddbdat.rb,
ext/export/src/competition_xls.rb,
test/test_plugin/oddbdat_export.rb,
ext/export/test/test_csv_exporter.rb,
ext/export/test/test_generics_xls.rb

Defined Under Namespace

Modules: CsvExporter Classes: AcLimLine, AcLimTable, AcLine, AcOddbLine, AcOddbTable, AcTable, AccompLine, AccompTable, AcmedLine, AcmedTable, AcnamLine, AcnamTable, AcpricealgExfactoryLine, AcpricealgPublicLine, AcpricealgTable, AcscLine, AcscTable, AtcLine, CodesTable, CompLine, CompTable, CompetitionXls, EanLine, EanTable, GalenicFormLine, GenericXls, LimTxtLine, LimTxtTable, LimitationLine, LimitationTable, Line, MCMLine, MCMTable, OddbDatExport, PackageLine, PatentXls, Readme, ScLine, ScTable, StubDRbObject, SubstanceLine, Table, TestAcLimLine, TestAcLimTable, TestAcLine, TestAcOddbLine, TestAcOddbTable, TestAcTable, TestAccompLine, TestAccompTable, TestAcmedLine, TestAcmedTable, TestAcnamLine, TestAcnamTable, TestAcpricealgExfactoryLine, TestAcpricealgPublicLine, TestAcpricealgTable, TestAcscLine, TestAcscTable, TestAtcLine, TestCodesTable, TestCompLine, TestCompTable, TestCsvExporter, TestEanLine, TestEanTable, TestGalenicFormLine, TestGenericXls, TestLimTxtLine, TestLimTxtTable, TestLimitationLine, TestLimitationTable, TestLine, TestMCMLine, TestMCMTable, TestOddbDatExport, TestReadme, TestScLine, TestScTable, TestTable

Constant Summary

DATE =
Date.today.strftime("%Y%m%d%H%M%S")
SERVER_NAME =

ODDB::Text::Table = ‘table’

'server_name/'
AtcClass =

the followings are necessary for TestCodesTable

'atcclass'
GalenicForm =
'galenicform'

Class Method Summary (collapse)

Class Method Details

+ (Object) clear



21
22
23
24
25
26
27
28
29
# File 'ext/export/src/odba_exporter.rb', line 21

def OdbaExporter.clear
begin
  Thread.new { 
    sleep 1
    DRb.thread.exit
  }
end
  nil
end

+ (Object) compress(dir, name)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/export/src/odba_exporter.rb', line 30

def OdbaExporter.compress(dir, name)
  FileUtils.mkdir_p(dir)
  Dir.chdir(dir)
  tmp_name = name + '.tmp'
  gz_name = tmp_name + '.gz'
  zip_name = tmp_name + '.zip'
  gzwriter =   Zlib::GzipWriter.open(gz_name)
  zipwriter = Zip::ZipOutputStream.open(zip_name)
  zipwriter.put_next_entry(name)
  File.open(name, "r") { |fh|
    fh.each { |line|
      gzwriter << line
      zipwriter.puts(line)
    }
  }
  gzwriter.close if(gzwriter)
  zipwriter.close if(zipwriter)
  FileUtils.mv(gz_name, name + '.gz')
  FileUtils.mv(zip_name, name + '.zip')
  name
end

+ (Object) compress_many(dir, name, files)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'ext/export/src/odba_exporter.rb', line 51

def OdbaExporter.compress_many(dir, name, files)
  FileUtils.mkdir_p(dir)
  Dir.chdir(dir)
  tmp_name = name + '.tmp'
  tar_name = tmp_name + '.tar'
  gz_name = tar_name + '.gz'
  File.delete(gz_name) if(File.exist?(gz_name))
  tar_archive = Archive::Tar.new(tar_name)
  tar_archive.create_archive(files.join(" "))
  tar_archive.compress_archive
  FileUtils.mv(gz_name, name + '.tar.gz')

  zip_name = tmp_name + '.zip'
  File.delete(zip_name) if(File.exist?(zip_name))
  Zip::ZipOutputStream.open(zip_name) { |zos|
    files.each { |fname|
      zos.put_next_entry(fname)
      zos.puts File.read(fname)
    }
  }
  FileUtils.mv(zip_name, name + '.zip')
  name
end

+ (Object) export_analysis_csv(odba_ids, dir, name)



104
105
106
107
108
109
110
111
112
113
# File 'ext/export/src/odba_exporter.rb', line 104

def OdbaExporter.export_analysis_csv(odba_ids, dir, name)
  safe_export(dir, name) { |fh|
    fh << "groupcd;poscd;anonymouspos;analysis_description_de;analysis_description_fr;analysis_footnote_de;analysis_footnote_fr;analysis_taxnote_de;analysis_taxnote_fr;analysis_limitation_de;analysis_limitation_fr;analysis_list_title_de;analysis_list_title_fr;lab_areas;taxpoints;finding;analysis_permissions_de;analysis_permissions_fr\n"
    odba_ids.each { |odba_id|
      item = ODBA.cache.fetch(odba_id, nil)
      CsvExporter.dump(CsvExporter::ANALYSIS, item, fh)  
    }
    nil
  }
end

+ (Object) export_competition_xls(comp_id, dir, name, db_path = nil)



74
75
76
77
78
79
80
81
82
# File 'ext/export/src/odba_exporter.rb', line 74

def OdbaExporter.export_competition_xls(comp_id, dir, name, db_path=nil)
  safe_export(dir, name) { |fh|
    exporter = CompetitionXls.new(fh.path, db_path)
    company = ODBA.cache.fetch(comp_id)
    exporter.export_competition(company)
    exporter.close
    nil
  }
end

+ (Object) export_doc_csv(odba_ids, dir, name)



83
84
85
86
87
88
89
90
91
92
# File 'ext/export/src/odba_exporter.rb', line 83

def OdbaExporter.export_doc_csv(odba_ids, dir, name)
  safe_export(dir, name) { |fh|
    fh << "ean13;exam;salutation;title;firstname;name;praxis;addresstype;address_name;lines;address;plz;city;canton;fon;fax;email;language;specialities\n"
    odba_ids.each { |odba_id|
      item = ODBA.cache.fetch(odba_id, nil)
      CsvExporter.dump(CsvExporter::DOCTOR, item, fh)
    }
    nil
  }
end

+ (Object) export_ean13_idx_th_csv(odba_ids, dir, name)



117
118
119
120
121
122
123
124
125
126
# File 'ext/export/src/odba_exporter.rb', line 117

def OdbaExporter.export_ean13_idx_th_csv(odba_ids, dir, name)
  safe_export(dir, name) { |fh|
    fh << "ean13;index_therapeuticus\n"
    odba_ids.each { |odba_id|
      item = ODBA.cache.fetch(odba_id, nil)
      CsvExporter.dump([ :barcode, :index_therapeuticus ], item, fh)  
    }
    nil
  }
end

+ (Object) export_generics_xls(dir, name)



96
97
98
99
100
101
102
103
# File 'ext/export/src/odba_exporter.rb', line 96

def OdbaExporter.export_generics_xls(dir, name)
  safe_export(dir, name) { |fh|
    exporter = GenericXls.new(fh.path)
    exporter.export_generics
    exporter.close
    nil
  }
end

+ (Object) export_idx_th_csv(odba_ids, dir, name)



130
131
132
133
134
135
136
137
138
139
# File 'ext/export/src/odba_exporter.rb', line 130

def OdbaExporter.export_idx_th_csv(odba_ids, dir, name)
  safe_export(dir, name) { |fh|
    fh << "index_therapeuticus;description_de;description_fr;comment_de;comment_fr;limitation_de;limitation_fr\n"
    odba_ids.each { |odba_id|
      item = ODBA.cache.fetch(odba_id, nil)
      CsvExporter.dump(CsvExporter::INDEX_THERAPEUTICUS, item, fh)  
    }
    nil
  }
end

+ (Object) export_migel_csv(odba_ids, dir, name)



143
144
145
146
147
148
149
150
151
152
# File 'ext/export/src/odba_exporter.rb', line 143

def OdbaExporter.export_migel_csv(odba_ids, dir, name)
  safe_export(dir, name) { |fh|
    fh << "migel_code;group_code;group_de;group_fr;group_it;group_limitation_de;group_limitation_fr;group_limitation_it;subgroup_code;subgroup_de;subgroup_fr;subgroup_it;subgroup_limitation_de;subgroup_limitation_fr;subgroup_limitation_it;product_code;product_de;product_fr;product_it;accessory_code;accessory_de;accessory_fr;accessory_it;product_limitation_de;product_limitation_fr;product_limitation_it;price;qty;unit_de;unit_fr;unit_it;limitation_flag;date\n"
    odba_ids.each { |odba_id|
      item = ODBA.cache.fetch(odba_id, nil)
      CsvExporter.dump(CsvExporter::MIGEL, item, fh)
    }
    true
  }
end

+ (Object) export_narcotics_csv(odba_ids, dir, name)



156
157
158
159
160
161
162
163
164
# File 'ext/export/src/odba_exporter.rb', line 156

def OdbaExporter.export_narcotics_csv(odba_ids, dir, name)
  safe_export(dir, name) { |fh|
    odba_ids.each { |odba_id|
      item = ODBA.cache.fetch(odba_id, nil)
      CsvExporter.dump(CsvExporter::NARCOTIC, item, fh)
    }
    nil
  }
end

+ (Object) export_oddbdat(odba_ids, dir, klasses)



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/export/src/odba_exporter.rb', line 165

def OdbaExporter.export_oddbdat(odba_ids, dir, klasses)
  FileUtils.mkdir_p(dir)
  files = klasses.collect { |klass| 
    table = klass.new
    file = Tempfile.new(table.filename, dir)
    [file, table]
  }
  if(odba_ids.nil?)
    files.each { |file, table|
      file.puts table.lines
    }
  else
    odba_ids.each { |odba_id|
      item = ODBA.cache.fetch(odba_id, nil)
      files.each { |file, table|
        file.puts table.lines(item)
      }
      #ODBA.cache.clear
    }
  end
  files.each { |file, table|
    path = File.join(dir, table.filename)
    FileUtils.mv(file.path, path)
  }
  files.collect { |file, table| table.filename }
ensure
  if(files)
    files.each { |file, table| file.close! }
  end
end

+ (Object) export_patent_xls(odba_ids, dir, name)



195
196
197
198
199
200
201
202
203
204
# File 'ext/export/src/odba_exporter.rb', line 195

def OdbaExporter.export_patent_xls(odba_ids, dir, name)
  nil_data = []
  safe_export(dir, name) { |fh|
    exporter = PatentXls.new(fh.path)
    exporter.export(odba_ids, nil_data)
    exporter.close
    nil
  }
  nil_data
end

+ (Object) export_price_history_csv(odba_ids, dir, name)



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'ext/export/src/odba_exporter.rb', line 205

def OdbaExporter.export_price_history_csv(odba_ids, dir, name)
  epoch = Date.new 1979
  safe_export(dir, name) { |fh|
    dates = {}
    packages = odba_ids.collect do |odba_id|
      pack = ODBA.cache.fetch(odba_id, nil)
      pack.prices.each do |type, prices|
        prices.each do |price|
          dates[(time = price.valid_from) ? time.to_date : epoch] = true
        end
      end
      pack
    end
    dates = dates.keys.sort
    if dates.first == epoch
      dates[0] = nil
    end
    head = %w{iksnr ikscd name size barcode pharmacode out_of_trade}
    dates.each do |date|
      datestr = date ? date.strftime('%d.%m.%Y') : 'unknown'
      head.push "#{datestr} (exfactory)", 'authority', 'origin',
                "#{datestr} (public)", 'authority', 'origin'
    end
    CSV::Writer.generate(fh, ';') do |csv| csv << head end
    packages.sort_by do |pack| pack.name end.each do |pack|
      CsvExporter.dump(CsvExporter::PRICE_HISTORY, pack, fh, :dates => dates)
    end
    nil
  }
end

+ (Object) export_yaml(odba_ids, dir, name, opts = {})



235
236
237
238
239
240
241
242
243
244
# File 'ext/export/src/odba_exporter.rb', line 235

def OdbaExporter.export_yaml(odba_ids, dir, name, opts={})
  opts.each do |key, val| Thread.current[key] = val end
  safe_export(dir, name) { |fh|
    odba_ids.each { |odba_id|
      YAML.dump(ODBA.cache.fetch(odba_id, nil), fh)
      fh.puts
    }
    nil
  }
end

+ (Object) remote_safe_export(dir, name, &block)



245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/export/src/odba_exporter.rb', line 245

def OdbaExporter.remote_safe_export(dir, name, &block)
  FileUtils.mkdir_p(dir)
  Tempfile.open(name, dir) { |fh|
    fh.close
    block.call(fh.path)
    newpath = File.join(dir, name)
    FileUtils.mv(fh.path, newpath)
    FileUtils.chmod(0644, newpath)
    compress(dir, name)
  }
  name
end

+ (Object) safe_export(dir, name, &block)



257
258
259
260
261
262
263
264
265
266
267
268
# File 'ext/export/src/odba_exporter.rb', line 257

def OdbaExporter.safe_export(dir, name, &block)
  FileUtils.mkdir_p(dir)
  Tempfile.open(name, dir) { |fh|
    block.call(fh)
    fh.close
    newpath = File.join(dir, name)
    FileUtils.mv(fh.path, newpath)
    FileUtils.chmod(0644, newpath)
    compress(dir, name)
  }
  name
end