Class: HealthDataStandards::Import::BulkRecordImporter
- Inherits:
-
Object
- Object
- HealthDataStandards::Import::BulkRecordImporter
- Defined in:
- lib/health-data-standards/import/bulk_record_importer.rb
Class Method Summary collapse
- .import(xml_data, provider_map = {}) ⇒ Object
- .import_archive(file, failed_dir = nil) ⇒ Object
- .import_directory(source_dir, failed_dir = nil) ⇒ Object
- .import_file(name, data, failed_dir, provider_map = {}) ⇒ Object
- .import_json(data, provider_map = {}) ⇒ Object
Class Method Details
.import(xml_data, provider_map = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/health-data-standards/import/bulk_record_importer.rb', line 93 def self.import(xml_data, provider_map = {}) doc = xml_data.kind_of?(Nokogiri::XML::Document) ? xml_data : Nokogiri::XML(xml_data) providers = [] root_element_name = doc.root.name if root_element_name == 'ClinicalDocument' doc.root.add_namespace_definition('cda', 'urn:hl7-org:v3') doc.root.add_namespace_definition('sdtc', 'urn:hl7-org:sdtc') if doc.at_xpath("/cda:ClinicalDocument/cda:templateId[@root='2.16.840.1.113883.3.88.11.32.1']") patient_data = C32::PatientImporter.instance.parse_c32(doc) elsif doc.at_xpath("/cda:ClinicalDocument/cda:templateId[@root='2.16.840.1.113883.10.20.22.1.2']") patient_data = CCDA::PatientImporter.instance.parse_ccda(doc) elsif doc.at_xpath("/cda:ClinicalDocument/cda:templateId[@root='2.16.840.1.113883.10.20.24.1.2']") patient_data = Cat1::PatientImporter.instance.parse_cat1(doc) else STDERR.puts("Unable to determinate document template/type of CDA document") return {status: 'error', message: "Document templateId does not identify it as a C32 or CCDA", status_code: 400} end record = Record.update_or_create(patient_data) begin providers = CDA::ProviderImporter.instance.extract_providers(doc, record) rescue Exception => e STDERR.puts "error extracting providers" end else return {status: 'error', message: 'Unknown XML Format', status_code: 400} end record.provider_performances = providers providers.each do |prov| prov.provider.ancestors.each do |ancestor| record.provider_performances.push(ProviderPerformance.new(start_date: prov.start_date, end_date: prov.end_date, provider: ancestor)) end end if record.save return record end end |
.import_archive(file, failed_dir = nil) ⇒ Object
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 |
# File 'lib/health-data-standards/import/bulk_record_importer.rb', line 14 def self.import_archive(file, failed_dir=nil) begin failed_dir ||=File.join(File.dirname(file)) patient_id_list = nil Zip::ZipFile.open(file.path) do |zipfile| zipfile.entries.each do |entry| if entry.name if entry.name.split("/").last == "patient_manifest.txt" patient_id_list = zipfile.read(entry.name) next end end next if entry.directory? data = zipfile.read(entry.name) status = self.import_file(entry.name,data,failed_dir) raise StandardError, status[:message] if status.is_a?(Hash) && status[:status] === 'error' end end missing_patients = [] #if there was a patient manifest, theres a patient id list we need to load if patient_id_list patient_id_list.split("\n").each do |id| patient = Record.where(:medical_record_number => id).first if patient == nil missing_patients << id end end end missing_patients rescue FileUtils.mkdir_p(failed_dir) FileUtils.cp(file,File.join(failed_dir,File.basename(file))) File.open(File.join(failed_dir,"#{file}.error"), 'w') do |f| f.puts($!.) f.puts($!.backtrace) end raise $! end end |
.import_directory(source_dir, failed_dir = nil) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/health-data-standards/import/bulk_record_importer.rb', line 6 def self.import_directory(source_dir, failed_dir=nil) failed_dir ||= File.join(source_dir, '../', 'failed_imports') files = Dir.glob(File.join(source_dir, '*.*')) files.each do |file| self.import_file(file,File.new(file).read,failed_dir) end end |
.import_file(name, data, failed_dir, provider_map = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/health-data-standards/import/bulk_record_importer.rb', line 60 def self.import_file(name,data,failed_dir,provider_map={}) begin ext = File.extname(name) if ext == ".json" self.import_json(data) else self.import(data) end rescue FileUtils.mkdir_p(File.dirname(File.join(failed_dir,name))) File.open(File.join(failed_dir,name),"w") do |f| f.puts(data) end File.open(File.join(failed_dir,"#{name}.error"),"w") do |f| f.puts($!.) f.puts($!.backtrace) end raise $! end end |
.import_json(data, provider_map = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/health-data-standards/import/bulk_record_importer.rb', line 81 def self.import_json(data,provider_map = {}) json = JSON.parse(data,:max_nesting=>100) record = Record.update_or_create(Record.new(json)) providers = record.provider_performances providers.each do |prov| prov.provider.ancestors.each do |ancestor| record.provider_performances.push(ProviderPerformance.new(start_date: prov.start_date, end_date: prov.end_date, provider: ancestor)) end end record.save! end |