scinote-web/app/utilities/protocols_importer.rb

184 lines
5.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-07-21 19:11:15 +08:00
module ProtocolsImporter
include RenamingUtil
2016-07-21 19:11:15 +08:00
def import_new_protocol(protocol_json, team, type, user)
2016-07-21 19:11:15 +08:00
remove_empty_inputs(protocol_json)
protocol = Protocol.new(
name: protocol_json['name'],
authors: protocol_json['authors'],
2016-07-21 19:11:15 +08:00
protocol_type: (type == :public ? :in_repository_public : :in_repository_private),
published_on: (type == :public ? Time.now : nil),
added_by: user,
2017-01-25 22:00:14 +08:00
team: team
2016-07-21 19:11:15 +08:00
)
# Try to rename record
rename_record(protocol, :name) if protocol.invalid?
2016-07-21 19:11:15 +08:00
# Okay, now save the protocol
protocol.save!
# Protocol is saved, populate it
populate_protocol(protocol, protocol_json, user, team)
2016-07-21 19:11:15 +08:00
protocol
2016-07-21 19:11:15 +08:00
end
def import_into_existing(protocol, protocol_json, user, team)
2016-07-21 19:11:15 +08:00
# Firstly, destroy existing protocol's contents
protocol.tiny_mce_assets.destroy_all
2016-07-21 19:11:15 +08:00
protocol.destroy_contents(user)
protocol.reload
# Alright, now populate the protocol
populate_protocol(protocol, protocol_json, user, team)
2016-07-21 19:11:15 +08:00
protocol.reload
2016-08-12 22:31:40 +08:00
# Unlink the protocol
protocol.unlink
protocol
2016-07-21 19:11:15 +08:00
end
private
def populate_protocol(protocol, protocol_json, user, team)
2016-07-21 19:11:15 +08:00
protocol.reload
protocol.description = populate_rte(protocol_json, protocol, team)
protocol.save!
2016-07-21 19:11:15 +08:00
asset_ids = []
step_pos = 0
# Check if protocol has steps
protocol_json['steps']&.values&.each do |step_json|
step = Step.create!(
name: step_json['name'],
position: step_pos,
completed: false,
user: user,
last_modified_by: user,
protocol: protocol
)
# need step id to link image to step
step.description = populate_rte(step_json, step, team)
step.save!
step_pos += 1
step_json['checklists']&.values&.each do |checklist_json|
checklist = Checklist.create!(
name: checklist_json['name'],
step: step,
created_by: user,
last_modified_by: user
)
next unless checklist_json['items']
item_pos = 0
checklist_json['items'].values.each do |item_json|
ChecklistItem.create!(
text: item_json['text'],
checked: false,
position: item_pos,
created_by: user,
last_modified_by: user,
checklist: checklist
)
item_pos += 1
2016-07-21 19:11:15 +08:00
end
end
2016-07-21 19:11:15 +08:00
step_json['tables']&.values&.each do |table_json|
table = Table.create!(
name: table_json['name'],
contents: Base64.decode64(table_json['contents']),
created_by: user,
last_modified_by: user,
team: team
)
StepTable.create!(
step: step,
table: table
)
end
2016-07-21 19:11:15 +08:00
next unless step_json['assets']
step_json['assets']&.values&.each do |asset_json|
asset = Asset.new(
created_by: user,
last_modified_by: user,
team: team
)
# Decode the file bytes
2019-07-19 16:53:33 +08:00
asset.file.attach(io: StringIO.new(Base64.decode64(asset_json['bytes'])),
filename: asset_json['fileName'],
content_type: asset_json['fileType'],
metadata: JSON.parse(asset_json['fileMetadata'] || '{}'))
asset.save!
asset_ids << asset.id
StepAsset.create!(
step: step,
asset: asset
)
2016-07-21 19:11:15 +08:00
end
end
# Post process assets
asset_ids.each do |asset_id|
2017-01-25 17:24:50 +08:00
Asset.find(asset_id).post_process_file(protocol.team)
2016-07-21 19:11:15 +08:00
end
end
def remove_empty_inputs(obj)
obj.keys.each do |key|
if obj[key] == ''
2016-07-21 19:11:15 +08:00
obj[key] = nil
elsif obj[key].is_a? Hash
2016-07-21 19:11:15 +08:00
# Recursive call
remove_empty_inputs(obj[key])
end
end
end
# create tiny_mce assets and change the inport tokens
def populate_rte(object_json, object, team)
return populate_rte_legacy(object_json) unless object_json['descriptionAssets']
2019-08-14 15:06:25 +08:00
description = TinyMceAsset.update_old_tinymce(object_json['description'], nil)
object_json['descriptionAssets'].values.each do |tiny_mce_img_json|
tiny_mce_img = TinyMceAsset.new(
object: object,
team_id: team.id,
saved: true
)
tiny_mce_img.save!
# Decode the file bytes
2019-07-19 16:53:33 +08:00
file = StringIO.new(Base64.decode64(tiny_mce_img_json['bytes']))
2019-10-07 22:21:11 +08:00
to_blob = ActiveStorage::Blob.create_after_upload!(io: file,
2019-07-19 16:53:33 +08:00
filename: tiny_mce_img_json['fileName'],
content_type: tiny_mce_img_json['fileType'],
metadata: JSON.parse(tiny_mce_img_json['fileMetadata'] || '{}'))
2019-10-07 22:21:11 +08:00
tiny_mce_img.image.attach(to_blob)
2019-05-13 16:22:30 +08:00
if description.gsub!("data-mce-token=\"#{tiny_mce_img_json['tokenId']}\"",
"data-mce-token=\"#{Base62.encode(tiny_mce_img.id)}\"")
else
description.gsub!("data-mce-token=\"#{Base62.encode(tiny_mce_img_json['tokenId'].to_i)}\"",
2019-05-29 21:41:54 +08:00
"data-mce-token=\"#{Base62.encode(tiny_mce_img.id)}\"")
2019-05-13 16:22:30 +08:00
end
2019-05-29 21:41:54 +08:00
description.gsub!(' ]]--&gt;', '')
end
description
end
# handle import from legacy exports
def populate_rte_legacy(object_json)
return unless object_json['description']&.present?
object_json['description'].gsub(Constants::TINY_MCE_ASSET_REGEX, '')
.gsub(' ]]--&gt;', '')
end
2016-08-12 22:31:40 +08:00
end