Browse Source

Add XML to Zip archive

pull/587/head
Stéphane Meny 3 years ago
parent
commit
866da1a8a8
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 79
      app/services/exports/case_log_export_service.rb

79
app/services/exports/case_log_export_service.rb

@ -63,56 +63,25 @@ module Exports
"core_#{collection_start}_#{collection_start + 1}_#{quarter}_#{base_number_str}_#{increment_str}" "core_#{collection_start}_#{collection_start + 1}_#{quarter}_#{base_number_str}_#{increment_str}"
end end
# { key (archive_filename) => Zip::File}
# { key (archive_filename) => [1,5,6,7,90]
# iterate over case logs (in startdate order)
# generate the XML file when we got all logs (calendar years + quarter) 1zip with 1xml
# take array case logs => 1 XML file
# categorise things
# 1. order them first, select active category, detect change in category
# 2. categorise everything first, then generate file per category
def write_export_data(case_logs) def write_export_data(case_logs)
previous_archive = nil # Order case logs per archive
case_logs_to_export = [] case_logs_per_archive = {}
# First element: add list
# Same archive: add list
# Different: write file (clear list) + add list
# Last element: add list + write file
# each loop add list
# except different archive, write file before
# finally write file at the end
case_logs.each do |case_log| case_logs.each do |case_log|
current_archive = get_archive_name(case_log, 1, 1) archive = get_archive_name(case_log, 1, 1)
if previous_archive.present? && previous_archive != current_archive if case_logs_per_archive.has_key?(archive)
xml_file = export_logs(case_logs_to_export) case_logs_per_archive[archive] << case_log
case_logs_to_export = [] else
case_logs_per_archive[archive] = [case_log]
end end
case_logs_to_export << case_log
previous_archive = current_archive
end end
# Write last archive # Write all archives
xml_file = export_logs(case_logs_to_export) case_logs_per_archive.each do |archive, case_logs_to_export|
xml = build_export_xml(case_logs_to_export)
zip_filenames = [] zip_io = Zip::File.open_buffer(StringIO.new)
case_logs.each do |case_log| zip_io.add("#{archive}.xml", xml)
archive_filename = get_archive_name(case_log, 1, 1) @storage_service.write_file("#{archive}.zip", zip_io.write_buffer)
unless zip_filenames.include?(archive_filename)
zip_io = Zip::File.open_buffer(StringIO.new)
zip_io.add("#{archive_filename}.xml", Tempfile.new)
@storage_service.write_file("#{archive_filename}.zip", zip_io.write_buffer)
end
end end
string_io = build_export_xml_io(case_logs)
file_path = "#{get_folder_name}/#{get_file_name}.xml"
@storage_service.write_file(file_path, string_io)
end end
def retrieve_case_logs def retrieve_case_logs
@ -127,7 +96,7 @@ module Exports
StringIO.new(csv_string) StringIO.new(csv_string)
end end
def build_export_xml_io(case_logs) def build_export_xml(case_logs)
doc = Nokogiri::XML("<forms/>") doc = Nokogiri::XML("<forms/>")
case_logs.each do |case_log| case_logs.each do |case_log|
@ -144,23 +113,11 @@ module Exports
end end
form << doc.create_element("providertype", case_log.owning_organisation.read_attribute_before_type_cast(:provider_type)) form << doc.create_element("providertype", case_log.owning_organisation.read_attribute_before_type_cast(:provider_type))
end end
doc.write_xml_to(StringIO.new, encoding: "UTF-8")
end
def get_folder_name
"core_#{day_as_string}"
end
def get_file_name
"dat_core_#{day_as_string}_#{increment_as_string}"
end
def day_as_string
Time.current.strftime("%Y_%m_%d")
end
def increment_as_string(increment = 1) file = Tempfile.new
sprintf("%04d", increment) doc.write_xml_to(file, encoding: "UTF-8")
file.rewind
file
end end
end end
end end

Loading…
Cancel
Save