2019-04-19 20:44:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-06-10 16:18:55 +08:00
|
|
|
class ProtocolsImporter
|
2017-01-24 16:52:38 +08:00
|
|
|
include RenamingUtil
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2022-06-10 16:18:55 +08:00
|
|
|
def initialize(user, team)
|
|
|
|
@user = user
|
|
|
|
@team = team
|
|
|
|
end
|
|
|
|
|
2023-02-27 20:22:01 +08:00
|
|
|
def import_new_protocol(protocol_json)
|
2016-07-21 19:11:15 +08:00
|
|
|
remove_empty_inputs(protocol_json)
|
|
|
|
protocol = Protocol.new(
|
2019-04-19 20:44:40 +08:00
|
|
|
name: protocol_json['name'],
|
|
|
|
authors: protocol_json['authors'],
|
2023-02-27 20:22:01 +08:00
|
|
|
protocol_type: :in_repository_draft,
|
2022-06-10 16:18:55 +08:00
|
|
|
added_by: @user,
|
|
|
|
team: @team
|
2016-07-21 19:11:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Try to rename record
|
2019-04-19 20:44:40 +08:00
|
|
|
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
|
2022-06-10 16:18:55 +08:00
|
|
|
populate_protocol(protocol, protocol_json)
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
protocol
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2022-06-10 16:18:55 +08:00
|
|
|
def import_into_existing(protocol, protocol_json)
|
2016-07-21 19:11:15 +08:00
|
|
|
# Firstly, destroy existing protocol's contents
|
2019-04-19 20:44:40 +08:00
|
|
|
protocol.tiny_mce_assets.destroy_all
|
2020-07-02 23:03:29 +08:00
|
|
|
protocol.destroy_contents
|
2016-07-21 19:11:15 +08:00
|
|
|
protocol.reload
|
|
|
|
|
|
|
|
# Alright, now populate the protocol
|
2022-06-10 16:18:55 +08:00
|
|
|
populate_protocol(protocol, protocol_json)
|
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
|
|
|
|
|
2022-06-10 16:18:55 +08:00
|
|
|
def create_in_step!(step, new_orderable)
|
2023-12-19 17:39:16 +08:00
|
|
|
new_orderable.save!
|
2022-06-10 16:18:55 +08:00
|
|
|
|
2023-12-19 17:39:16 +08:00
|
|
|
step.step_orderable_elements.create!(
|
|
|
|
position: step.step_orderable_elements.length,
|
|
|
|
orderable: new_orderable
|
|
|
|
)
|
2022-06-10 16:18:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def populate_protocol(protocol, protocol_json)
|
2016-07-21 19:11:15 +08:00
|
|
|
protocol.reload
|
2022-06-10 16:18:55 +08:00
|
|
|
protocol.description = populate_rte(protocol_json, protocol)
|
2022-08-09 16:13:43 +08:00
|
|
|
protocol.name = protocol_json['name'].presence
|
2019-04-19 20:44:40 +08:00
|
|
|
protocol.save!
|
2016-07-21 19:11:15 +08:00
|
|
|
asset_ids = []
|
|
|
|
step_pos = 0
|
2016-09-20 19:41:29 +08:00
|
|
|
# Check if protocol has steps
|
2019-04-19 20:44:40 +08:00
|
|
|
protocol_json['steps']&.values&.each do |step_json|
|
|
|
|
step = Step.create!(
|
|
|
|
name: step_json['name'],
|
|
|
|
position: step_pos,
|
|
|
|
completed: false,
|
2022-06-10 16:18:55 +08:00
|
|
|
user: @user,
|
|
|
|
last_modified_by: @user,
|
2019-04-19 20:44:40 +08:00
|
|
|
protocol: protocol
|
|
|
|
)
|
|
|
|
# need step id to link image to step
|
2022-07-01 17:35:04 +08:00
|
|
|
step_description_text = step.step_texts.create
|
|
|
|
step_description_text.update(text: populate_rte(step_json, step_description_text))
|
2022-06-10 16:18:55 +08:00
|
|
|
create_in_step!(step, step_description_text)
|
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
step.save!
|
|
|
|
step_pos += 1
|
|
|
|
|
|
|
|
step_json['checklists']&.values&.each do |checklist_json|
|
2022-06-10 16:18:55 +08:00
|
|
|
checklist = Checklist.new(
|
2019-04-19 20:44:40 +08:00
|
|
|
name: checklist_json['name'],
|
|
|
|
step: step,
|
2022-06-10 16:18:55 +08:00
|
|
|
created_by: @user,
|
|
|
|
last_modified_by: @user
|
2017-11-22 23:28:14 +08:00
|
|
|
)
|
2022-06-10 16:18:55 +08:00
|
|
|
|
|
|
|
create_in_step!(step, checklist)
|
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
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,
|
2022-06-10 16:18:55 +08:00
|
|
|
created_by: @user,
|
|
|
|
last_modified_by: @user,
|
2019-04-19 20:44:40 +08:00
|
|
|
checklist: checklist
|
|
|
|
)
|
|
|
|
item_pos += 1
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
2019-04-19 20:44:40 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
step_json['tables']&.values&.each do |table_json|
|
2022-06-10 16:18:55 +08:00
|
|
|
step_table = StepTable.new(
|
2019-04-19 20:44:40 +08:00
|
|
|
step: step,
|
2022-06-10 16:18:55 +08:00
|
|
|
table: Table.new(
|
|
|
|
name: table_json['name'],
|
|
|
|
contents: Base64.decode64(table_json['contents']),
|
|
|
|
created_by: @user,
|
|
|
|
last_modified_by: @user,
|
|
|
|
team: @team
|
|
|
|
)
|
2019-04-19 20:44:40 +08:00
|
|
|
)
|
2022-06-10 16:18:55 +08:00
|
|
|
|
|
|
|
create_in_step!(step, step_table)
|
2019-04-19 20:44:40 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
next unless step_json['assets']
|
|
|
|
|
|
|
|
step_json['assets']&.values&.each do |asset_json|
|
|
|
|
asset = Asset.new(
|
2022-06-10 16:18:55 +08:00
|
|
|
created_by: @user,
|
|
|
|
last_modified_by: @user,
|
|
|
|
team: @team
|
2019-04-19 20:44:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Decode the file bytes
|
2024-07-29 18:09:21 +08:00
|
|
|
asset.attach_file_version(io: StringIO.new(Base64.decode64(asset_json['bytes'])),
|
2019-07-19 16:53:33 +08:00
|
|
|
filename: asset_json['fileName'],
|
|
|
|
content_type: asset_json['fileType'],
|
|
|
|
metadata: JSON.parse(asset_json['fileMetadata'] || '{}'))
|
2019-04-19 20:44:40 +08:00
|
|
|
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|
|
2024-02-05 23:24:06 +08:00
|
|
|
Asset.find(asset_id).post_process_file
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_empty_inputs(obj)
|
|
|
|
obj.keys.each do |key|
|
2019-04-19 20:44:40 +08:00
|
|
|
if obj[key] == ''
|
2016-07-21 19:11:15 +08:00
|
|
|
obj[key] = nil
|
2019-04-19 20:44:40 +08:00
|
|
|
elsif obj[key].is_a? Hash
|
2016-07-21 19:11:15 +08:00
|
|
|
# Recursive call
|
|
|
|
remove_empty_inputs(obj[key])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-22 23:28:14 +08:00
|
|
|
# create tiny_mce assets and change the inport tokens
|
2022-06-10 16:18:55 +08:00
|
|
|
def populate_rte(object_json, object)
|
2019-04-19 20:44:40 +08:00
|
|
|
return populate_rte_legacy(object_json) unless object_json['descriptionAssets']
|
|
|
|
|
2020-07-27 22:39:39 +08:00
|
|
|
description = TinyMceAsset.update_old_tinymce(object_json['description'], nil, true)
|
2019-04-19 20:44:40 +08:00
|
|
|
object_json['descriptionAssets'].values.each do |tiny_mce_img_json|
|
2017-11-22 23:28:14 +08:00
|
|
|
tiny_mce_img = TinyMceAsset.new(
|
2019-04-19 20:44:40 +08:00
|
|
|
object: object,
|
2022-06-10 16:18:55 +08:00
|
|
|
team_id: @team.id,
|
2019-04-19 20:44:40 +08:00
|
|
|
saved: true
|
2017-11-22 23:28:14 +08:00
|
|
|
)
|
|
|
|
tiny_mce_img.save!
|
2019-07-09 16:28:15 +08:00
|
|
|
|
|
|
|
# Decode the file bytes
|
2019-07-19 16:53:33 +08:00
|
|
|
file = StringIO.new(Base64.decode64(tiny_mce_img_json['bytes']))
|
2021-01-28 21:43:45 +08:00
|
|
|
to_blob = ActiveStorage::Blob.create_and_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)
|
2022-07-01 17:35:04 +08:00
|
|
|
|
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!(' ]]-->', '')
|
2017-11-22 23:28:14 +08:00
|
|
|
end
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
|
|
|
# handle import from legacy exports
|
2019-04-19 20:44:40 +08:00
|
|
|
def populate_rte_legacy(object_json)
|
|
|
|
return unless object_json['description']&.present?
|
|
|
|
|
|
|
|
object_json['description'].gsub(Constants::TINY_MCE_ASSET_REGEX, '')
|
|
|
|
.gsub(' ]]-->', '')
|
2017-11-22 23:28:14 +08:00
|
|
|
end
|
2016-08-12 22:31:40 +08:00
|
|
|
end
|