Module: ODDB::OdbaExporter

Defined in:
ext/export/src/oddbdat.rb,
ext/export/src/patent_xls.rb,
ext/export/src/csv_exporter.rb,
ext/export/src/generics_xls.rb,
ext/export/src/odba_exporter.rb,
ext/export/src/competition_xls.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, PackageLine, PatentXls, Readme, ScLine, ScTable, SubstanceLine, Table, TestGenericXls

Class Method Summary collapse

Class Method Details

.clearObject



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

.compress(dir, name) ⇒ Object



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

.compress_many(dir, name, files) ⇒ Object



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

.export_analysis_csv(odba_ids, dir, name) ⇒ Object



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

def OdbaExporter.export_analysis_csv(odba_ids, dir, name)
	safe_export(dir, name) { |fh|
		fh << <<-HEAD
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
		HEAD
		odba_ids.each { |odba_id|
			item = ODBA.cache.fetch(odba_id, nil)
			CsvExporter.dump(CsvExporter::ANALYSIS, item, fh)	
		}
		nil
	}
end

.export_competition_xls(comp_id, dir, name, db_path = nil) ⇒ Object



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

.export_doc_csv(odba_ids, dir, name) ⇒ Object



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

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

.export_ean13_idx_th_csv(odba_ids, dir, name) ⇒ Object



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

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

.export_generics_xls(dir, name) ⇒ Object



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

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

.export_idx_th_csv(odba_ids, dir, name) ⇒ Object



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

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

.export_migel_csv(odba_ids, dir, name) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/export/src/odba_exporter.rb', line 139

def OdbaExporter.export_migel_csv(odba_ids, dir, name)
	safe_export(dir, name) { |fh|
		fh << <<-HEAD
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
		HEAD
		odba_ids.each { |odba_id|
			item = ODBA.cache.fetch(odba_id, nil)
			CsvExporter.dump(CsvExporter::MIGEL, item, fh)
		}
		true
	}
end

.export_narcotics_csv(odba_ids, dir, name) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'ext/export/src/odba_exporter.rb', line 151

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

.export_oddbdat(odba_ids, dir, klasses) ⇒ Object



160
161
162
163
164
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
# File 'ext/export/src/odba_exporter.rb', line 160

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

.export_patent_xls(odba_ids, dir, name) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'ext/export/src/odba_exporter.rb', line 190

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

.export_price_history_csv(odba_ids, dir, name) ⇒ Object



200
201
202
203
204
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
# File 'ext/export/src/odba_exporter.rb', line 200

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

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



230
231
232
233
234
235
236
237
238
239
# File 'ext/export/src/odba_exporter.rb', line 230

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

.remote_safe_export(dir, name, &block) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
# File 'ext/export/src/odba_exporter.rb', line 240

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

.safe_export(dir, name, &block) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/export/src/odba_exporter.rb', line 252

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