2016-07-21 19:11:15 +08:00
|
|
|
module ProtocolsImporter
|
2017-01-24 16:52:38 +08:00
|
|
|
include RenamingUtil
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-09-25 20:39:43 +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"],
|
|
|
|
description: protocol_json["description"],
|
|
|
|
authors: protocol_json["authors"],
|
|
|
|
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
|
|
|
|
if protocol.invalid? then
|
|
|
|
rename_record(protocol, :name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Okay, now save the protocol
|
|
|
|
protocol.save!
|
|
|
|
|
|
|
|
# Protocol is saved, populate it
|
2017-03-22 23:24:48 +08:00
|
|
|
populate_protocol(protocol, protocol_json, user, team)
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
return protocol
|
|
|
|
end
|
|
|
|
|
2017-03-22 23:24:48 +08:00
|
|
|
def import_into_existing(protocol, protocol_json, user, team)
|
2016-07-21 19:11:15 +08:00
|
|
|
# Firstly, destroy existing protocol's contents
|
|
|
|
protocol.destroy_contents(user)
|
|
|
|
protocol.reload
|
|
|
|
|
|
|
|
# Alright, now populate the protocol
|
2017-03-22 23:24:48 +08:00
|
|
|
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
|
|
|
|
|
2017-03-22 23:24:48 +08:00
|
|
|
def populate_protocol(protocol, protocol_json, user, team)
|
2016-07-21 19:11:15 +08:00
|
|
|
protocol.reload
|
|
|
|
asset_ids = []
|
|
|
|
step_pos = 0
|
2016-09-20 19:41:29 +08:00
|
|
|
# Check if protocol has steps
|
2016-09-22 22:36:37 +08:00
|
|
|
if protocol_json['steps']
|
|
|
|
protocol_json['steps'].values.each do |step_json|
|
2017-11-22 23:28:14 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
if step_json["checklists"]
|
|
|
|
step_json["checklists"].values.each do |checklist_json|
|
|
|
|
checklist = Checklist.create!(
|
|
|
|
name: checklist_json["name"],
|
|
|
|
step: step,
|
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user
|
|
|
|
)
|
|
|
|
if checklist_json["items"]
|
|
|
|
item_pos = 0
|
|
|
|
checklist_json["items"].values.each do |item_json|
|
|
|
|
item = ChecklistItem.create!(
|
|
|
|
text: item_json["text"],
|
|
|
|
checked: false,
|
|
|
|
position: item_pos,
|
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user,
|
|
|
|
checklist: checklist
|
|
|
|
)
|
|
|
|
item_pos += 1
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-22 23:28:14 +08:00
|
|
|
if step_json['tables']
|
|
|
|
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
|
|
|
end
|
|
|
|
|
2017-11-22 23:28:14 +08:00
|
|
|
if 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
|
|
|
|
asset.file = StringIO.new(Base64.decode64(asset_json["bytes"]))
|
|
|
|
asset.file_file_name = asset_json["fileName"]
|
|
|
|
asset.file_content_type = asset_json["fileType"]
|
|
|
|
asset.save!
|
|
|
|
asset_ids << asset.id
|
|
|
|
|
|
|
|
StepAsset.create!(
|
|
|
|
step: step,
|
|
|
|
asset: asset
|
|
|
|
)
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
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] == ""
|
|
|
|
obj[key] = nil
|
|
|
|
elsif obj[key].kind_of? Hash
|
|
|
|
# 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
|
|
|
|
def populate_rte(step_json, step, team)
|
|
|
|
return populate_rte_legacy(step_json) unless step_json['descriptionAssets']
|
|
|
|
description = step_json['description']
|
|
|
|
step_json['descriptionAssets'].values.each do |tiny_mce_img_json|
|
|
|
|
tiny_mce_img = TinyMceAsset.new(
|
|
|
|
reference: step,
|
|
|
|
team_id: team.id
|
|
|
|
)
|
|
|
|
# Decode the file bytes
|
|
|
|
tiny_mce_img.image = StringIO.new(
|
|
|
|
Base64.decode64(tiny_mce_img_json['bytes'])
|
|
|
|
)
|
|
|
|
tiny_mce_img.image_content_type = tiny_mce_img_json['fileType']
|
|
|
|
tiny_mce_img.save!
|
2017-11-24 18:45:07 +08:00
|
|
|
description.gsub!("[~tiny_mce_id:#{tiny_mce_img_json['tokenId']}]",
|
2017-11-22 23:28:14 +08:00
|
|
|
"[~tiny_mce_id:#{tiny_mce_img.id}]")
|
2018-01-05 22:30:11 +08:00
|
|
|
.gsub!(' ]]-->', '')
|
2017-11-22 23:28:14 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
|
|
|
# handle import from legacy exports
|
|
|
|
def populate_rte_legacy(step_json)
|
|
|
|
return unless step_json['description'] && step_json['description'].present?
|
2017-11-25 00:04:01 +08:00
|
|
|
step_json['description'].gsub(Constants::TINY_MCE_ASSET_REGEX, '')
|
2018-01-05 22:30:11 +08:00
|
|
|
.gsub(' ]]-->', '')
|
2017-11-22 23:28:14 +08:00
|
|
|
end
|
2016-08-12 22:31:40 +08:00
|
|
|
end
|