require 'zip' module ProtocolsExporter private def get_guid(id) str1 = '00000000-0000-' str2 = id.to_s str2 = '0' * (19 - str2.size) + str2 str2 = '4' + str2 str2n = str2[0..3] + '-' + str2[4..7] + '-' + str2[8..-1] str1 + str2n end def get_protocol_name(protocol) ## "Inject" module's name protocol_name = if protocol.in_module? && protocol.name.blank? protocol.my_module.name else protocol.name end protocol_name end def generate_envelope_xml(protocols) envelope_xml = "\n" protocols.each do |protocol| protocol_name = get_protocol_name(protocol) envelope_xml << "#{protocol_name}" \ "\n" end envelope_xml << "\n" envelope_xml end def tiny_mce_asset_present?(object) object.tiny_mce_assets.exists? end def get_tiny_mce_assets(text) return unless text regex = Constants::TINY_MCE_ASSET_REGEX tiny_assets_xml = "\n" text.gsub(regex) do |el| match = el.match(regex) img = TinyMceAsset.find_by_id(Base62.decode(match[1])) next unless img img_guid = get_guid(img.id) asset_file_name = "rte-#{img_guid}" \ "#{File.extname(img.image_file_name)}" asset_xml = "\n" asset_xml << "#{img.image_file_name}\n" asset_xml << "#{img.image_content_type}\n" asset_xml << "\n" tiny_assets_xml << asset_xml end tiny_assets_xml << "\n" tiny_assets_xml end def generate_protocol_xml(protocol) protocol_name = get_protocol_name(protocol) protocol_xml = "\n" protocol_xml << "\n" protocol_xml << "#{protocol_name}\n" protocol_xml << "#{protocol.authors}\n" protocol_xml << " \n" if tiny_mce_asset_present?(protocol) && protocol.description protocol_xml << get_tiny_mce_assets(protocol.description) end protocol_xml << "#{protocol.created_at.as_json}\n" protocol_xml << "#{protocol.updated_at.as_json}\n" # Steps if protocol.steps.count > 0 protocol_xml << "\n" protocol.steps.order(:id).each do |step| step_guid = get_guid(step.id) step_xml = "\n" step_xml << "#{step.name}\n" # uses 2 spaces to make more difficult to remove user data on import step_xml << "\n" if tiny_mce_asset_present?(step) step_xml << get_tiny_mce_assets(step.description) end # Assets if step.assets.count > 0 step_xml << "\n" step.assets.order(:id).each do |asset| asset_guid = get_guid(asset.id) asset_file_name = "#{asset_guid}" \ "#{File.extname(asset.file_file_name)}" asset_xml = "\n" asset_xml << "#{asset.file_file_name}\n" asset_xml << "#{asset.file_content_type}\n" asset_xml << "\n" step_xml << asset_xml end step_xml << "\n" end # Tables if step.tables.count > 0 step_xml << "\n" step.tables.order(:id).each do |table| table_xml = "\n" table_xml << "#{table.contents.unpack1('H*')}" \ "\n" table_xml << "\n" step_xml << table_xml end step_xml << "\n" end # Checklists if step.checklists.count > 0 step_xml << "\n" step.checklists.order(:id).each do |checklist| checklist_xml = "\n" checklist_xml << "#{checklist.name}\n" if checklist.checklist_items checklist_xml << "\n" checklist.checklist_items.each do |item| item_xml = "\n" item_xml << "#{item.text}\n" item_xml << "\n" checklist_xml << item_xml end checklist_xml << "\n" end checklist_xml << "\n" step_xml << checklist_xml end step_xml << "\n" end protocol_xml << step_xml protocol_xml << "\n" end protocol_xml << "\n" end protocol_xml << "\n" protocol_xml << '' protocol_xml end def generate_envelope_xsd envelope_xsd = "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd << "\n" envelope_xsd end def generate_eln_xsd eln_xsd = "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "" \ "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" eln_xsd << "\n" end end