2019-01-16 23:11:38 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
require 'digest'
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
class TeamImporter
|
|
|
|
def initialize
|
|
|
|
@user_mappings = {}
|
|
|
|
@notification_mappings = {}
|
|
|
|
@repository_mappings = {}
|
2020-11-13 21:13:26 +08:00
|
|
|
@project_folder_mappings = {}
|
2018-03-06 18:26:30 +08:00
|
|
|
@project_mappings = {}
|
|
|
|
@repository_column_mappings = {}
|
|
|
|
@experiment_mappings = {}
|
|
|
|
@my_module_group_mappings = {}
|
|
|
|
@my_module_mappings = {}
|
|
|
|
@protocol_mappings = {}
|
|
|
|
@protocol_keyword_mappings = {}
|
|
|
|
@step_mappings = {}
|
|
|
|
@asset_mappings = {}
|
|
|
|
@tag_mappings = {}
|
|
|
|
@result_text_mappings = {}
|
|
|
|
@repository_row_mappings = {}
|
2018-06-14 20:40:43 +08:00
|
|
|
@repository_list_item_mappings = {}
|
2020-01-07 21:12:48 +08:00
|
|
|
@repository_checklist_item_mappings = {}
|
|
|
|
@repository_status_item_mappings = {}
|
2018-03-06 18:26:30 +08:00
|
|
|
@result_mappings = {}
|
|
|
|
@checklist_mappings = {}
|
|
|
|
@table_mappings = {}
|
2019-10-30 22:47:11 +08:00
|
|
|
@report_mappings = {}
|
2022-11-08 21:41:11 +08:00
|
|
|
@label_template_mappings = {}
|
|
|
|
@step_text_mappings = {}
|
2018-03-06 18:26:30 +08:00
|
|
|
|
|
|
|
@project_counter = 0
|
|
|
|
@repository_counter = 0
|
|
|
|
@repository_row_counter = 0
|
|
|
|
@protocol_counter = 0
|
|
|
|
@step_counter = 0
|
|
|
|
@report_counter = 0
|
2022-08-18 19:50:04 +08:00
|
|
|
@label_template_counter = 0
|
2018-03-06 18:26:30 +08:00
|
|
|
@my_module_counter = 0
|
|
|
|
@notification_counter = 0
|
|
|
|
@result_counter = 0
|
|
|
|
@asset_counter = 0
|
|
|
|
@user_counter = 0
|
|
|
|
@mce_asset_counter = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_from_dir(import_dir)
|
|
|
|
MyModule.skip_callback(:create, :before, :create_blank_protocol)
|
2019-04-15 21:13:10 +08:00
|
|
|
Activity.skip_callback(:create, :before, :add_user)
|
|
|
|
Activity.skip_callback(:initialize, :after, :init_default_values)
|
2021-10-14 19:21:36 +08:00
|
|
|
@user_roles = UserRole.all
|
2018-03-06 18:26:30 +08:00
|
|
|
@import_dir = import_dir
|
|
|
|
team_json = JSON.parse(File.read("#{@import_dir}/team_export.json"))
|
2018-03-13 00:00:44 +08:00
|
|
|
team = Team.new(team_json['team'].slice(*Team.column_names))
|
2018-03-06 18:26:30 +08:00
|
|
|
team.id = nil
|
|
|
|
team.transaction(isolation: :serializable) do
|
|
|
|
team_creator_id = team.created_by_id
|
|
|
|
team.created_by_id = nil
|
2018-03-13 00:00:44 +08:00
|
|
|
team_last_modified_by = team.last_modified_by_id
|
|
|
|
team.last_modified_by_id = nil
|
2019-04-15 21:13:10 +08:00
|
|
|
team.without_templates = true
|
2022-11-08 21:41:11 +08:00
|
|
|
team.skip_user_assignments = true
|
2018-03-06 18:26:30 +08:00
|
|
|
team.save!
|
2022-11-08 21:41:11 +08:00
|
|
|
@team_id = team.id
|
2018-03-06 18:26:30 +08:00
|
|
|
|
|
|
|
create_users(team_json['users'], team)
|
2022-11-08 21:41:11 +08:00
|
|
|
puts 'Assigning users to the team...'
|
|
|
|
create_user_assignments(team_json['user_assignments'], team)
|
2018-03-06 18:26:30 +08:00
|
|
|
|
|
|
|
# Find new id of the first admin in the team
|
|
|
|
@admin_id = @user_mappings[team_json['default_admin_id']]
|
|
|
|
|
|
|
|
create_notifications(team_json['notifications'])
|
|
|
|
create_protocol_keywords(team_json['protocol_keywords'], team)
|
|
|
|
create_protocols(team_json['protocols'], nil, team)
|
2020-11-13 21:13:26 +08:00
|
|
|
create_project_folders(team_json['project_folders'], team)
|
2018-03-06 18:26:30 +08:00
|
|
|
create_projects(team_json['projects'], team)
|
2018-03-13 00:00:44 +08:00
|
|
|
create_repositories(team_json['repositories'], team)
|
2022-08-18 19:50:04 +08:00
|
|
|
create_label_templates(team_json['label_templates'], team)
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
|
|
|
|
# Second run, we needed it because of some models should be created
|
|
|
|
|
|
|
|
team_json['users'].each do |user_json|
|
|
|
|
user_json['user_notifications'].each do |user_notification_json|
|
|
|
|
user_notification = UserNotification.new(user_notification_json)
|
|
|
|
user_notification.id = nil
|
|
|
|
user_notification.user_id = find_user(user_notification.user_id)
|
|
|
|
user_notification.notification_id =
|
|
|
|
@notification_mappings[user_notification.notification_id]
|
|
|
|
next if user_notification.notification_id.blank?
|
2019-07-26 18:40:36 +08:00
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
user_notification.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
user_json['repository_table_states'].each do |rep_tbl_state_json|
|
|
|
|
rep_tbl_state = RepositoryTableState.new(rep_tbl_state_json)
|
|
|
|
rep_tbl_state.id = nil
|
|
|
|
rep_tbl_state.user_id = find_user(rep_tbl_state.user_id)
|
|
|
|
rep_tbl_state.repository_id =
|
|
|
|
@repository_mappings[rep_tbl_state.repository_id]
|
|
|
|
rep_tbl_state.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
team_json['projects'].each do |project_json|
|
2018-07-20 22:10:45 +08:00
|
|
|
create_reports(project_json['reports'], team)
|
2018-03-06 18:26:30 +08:00
|
|
|
project_json['experiments'].each do |experiment_json|
|
|
|
|
experiment_json['my_modules'].each do |my_module_json|
|
|
|
|
create_task_connections(my_module_json['outputs'])
|
|
|
|
end
|
|
|
|
end
|
2019-04-15 21:13:10 +08:00
|
|
|
create_activities(project_json['activities'], team)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
team_json['repositories'].each do |repository_json|
|
|
|
|
repository_json['repository_rows'].each do |repository_row_json|
|
2018-06-14 21:20:57 +08:00
|
|
|
create_repository_cells(repository_row_json['repository_cells'], team)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
2020-06-03 23:02:33 +08:00
|
|
|
repository_json['repository_snapshots'].each do |repository_snapshot_json|
|
|
|
|
repository_snapshot_json['repository_rows'].each do |repository_snapshot_row_json|
|
|
|
|
create_repository_cells(repository_snapshot_row_json['repository_cells'], team)
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
|
2019-04-15 21:13:10 +08:00
|
|
|
create_activities(team_json['activities'], team)
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
create_tiny_mce_assets(team_json['tiny_mce_assets'], team)
|
|
|
|
update_smart_annotations(team)
|
|
|
|
|
|
|
|
# restoring team's creator
|
|
|
|
team.created_by_id = find_user(team_creator_id)
|
2018-03-13 00:00:44 +08:00
|
|
|
team.last_modified_by_id = find_user(team_last_modified_by)
|
2018-03-06 18:26:30 +08:00
|
|
|
team.save!
|
|
|
|
|
|
|
|
puts "Imported users: #{@user_counter}"
|
|
|
|
puts "Imported notifications: #{@notification_counter}"
|
|
|
|
puts "Imported projects: #{@project_counter}"
|
|
|
|
puts "Imported reports: #{@report_counter}"
|
|
|
|
puts "Imported repositories: #{@repository_counter}"
|
|
|
|
puts "Imported repository rows: #{@repository_row_counter}"
|
|
|
|
puts "Imported tasks: #{@my_module_counter}"
|
|
|
|
puts "Imported protocols: #{@protocol_counter}"
|
|
|
|
puts "Imported steps: #{@step_counter}"
|
|
|
|
puts "Imported results: #{@result_counter}"
|
|
|
|
puts "Imported assets: #{@asset_counter}"
|
|
|
|
puts "Imported tinyMCE assets: #{@mce_asset_counter}"
|
2022-08-18 19:50:04 +08:00
|
|
|
puts "Imported label templates: #{@label_template_counter}"
|
2018-03-06 18:26:30 +08:00
|
|
|
|
|
|
|
MyModule.set_callback(:create, :before, :create_blank_protocol)
|
2019-04-15 21:13:10 +08:00
|
|
|
Activity.set_callback(:create, :before, :add_user)
|
|
|
|
Activity.set_callback(:initialize, :after, :init_default_values)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-25 17:50:08 +08:00
|
|
|
def import_experiment_template_from_dir(import_dir, project_id,
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id)
|
|
|
|
# Remove callbacks that can cause problems when importing
|
|
|
|
MyModule.skip_callback(:create, :before, :create_blank_protocol)
|
|
|
|
|
|
|
|
@import_dir = import_dir
|
|
|
|
@is_template = true
|
|
|
|
|
|
|
|
# Parse the experiment file and save it to DB
|
2020-01-07 21:12:48 +08:00
|
|
|
project = Project.find_by(id: project_id)
|
2019-01-25 17:50:08 +08:00
|
|
|
experiment_json = JSON.parse(File.read("#{@import_dir}/experiment.json"))
|
|
|
|
|
2019-01-28 22:37:24 +08:00
|
|
|
# Handle situation when experiment with same name already exists
|
2019-01-25 17:50:08 +08:00
|
|
|
exp_name = experiment_json.dig('experiment', 'name')
|
|
|
|
if project.experiments.where(name: exp_name).present?
|
|
|
|
experiment_names = project.experiments.map(&:name)
|
|
|
|
i = 1
|
2019-01-30 17:56:22 +08:00
|
|
|
i += 1 while experiment_names.include?("#{exp_name} (#{i})")
|
|
|
|
experiment_json['experiment']['name'] = "#{exp_name} (#{i})"
|
2019-01-16 22:54:03 +08:00
|
|
|
end
|
2019-01-25 17:50:08 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
ActiveRecord::Base.no_touching do
|
|
|
|
experiment = create_experiment(experiment_json, project, user_id)
|
|
|
|
|
|
|
|
# Create connections for modules
|
|
|
|
experiment_json['my_modules'].each do |my_module_json|
|
|
|
|
create_task_connections(my_module_json['outputs'])
|
|
|
|
end
|
2019-01-16 22:54:03 +08:00
|
|
|
|
2019-01-25 17:50:08 +08:00
|
|
|
update_smart_annotations_in_project(project)
|
2021-12-03 00:14:01 +08:00
|
|
|
|
2021-12-02 23:54:43 +08:00
|
|
|
# handle the permissions for newly created experiment
|
|
|
|
user = User.find(user_id)
|
2023-08-11 21:31:29 +08:00
|
|
|
UserAssignments::GenerateUserAssignmentsJob.perform_now(experiment, user.id)
|
2021-12-03 00:14:01 +08:00
|
|
|
experiment.my_modules.find_each do |my_module|
|
2023-08-11 21:31:29 +08:00
|
|
|
UserAssignments::GenerateUserAssignmentsJob.perform_now(my_module, user.id)
|
2021-12-02 23:54:43 +08:00
|
|
|
end
|
2019-01-25 17:50:08 +08:00
|
|
|
puts "Imported experiment: #{experiment.id}"
|
|
|
|
return experiment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
2019-01-16 22:54:03 +08:00
|
|
|
# Reset callbacks
|
|
|
|
MyModule.set_callback(:create, :before, :create_blank_protocol)
|
|
|
|
end
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def update_smart_annotations(team)
|
|
|
|
team.projects.each do |pr|
|
2019-01-25 17:50:08 +08:00
|
|
|
update_smart_annotations_in_project(pr)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
team.protocols.where(my_module: nil).each do |protocol|
|
|
|
|
protocol.steps.each do |step|
|
|
|
|
step.step_comments.each do |comment|
|
|
|
|
comment.save! if update_annotation(comment.message)
|
|
|
|
end
|
|
|
|
step.save! if update_annotation(step.description)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
team.repositories.each do |rep|
|
|
|
|
rep.repository_rows.find_each do |row|
|
|
|
|
row.repository_cells.each do |cell|
|
2020-01-07 21:12:48 +08:00
|
|
|
cell.value.save! if update_annotation(cell.value.formatted)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-25 17:50:08 +08:00
|
|
|
def update_smart_annotations_in_project(project)
|
|
|
|
project.project_comments.each do |comment|
|
|
|
|
comment.save! if update_annotation(comment.message)
|
|
|
|
end
|
|
|
|
project.experiments.each do |exp|
|
|
|
|
exp.save! if update_annotation(exp.description)
|
|
|
|
exp.my_modules.each do |task|
|
|
|
|
task.task_comments.each do |comment|
|
|
|
|
comment.save! if update_annotation(comment.message)
|
|
|
|
end
|
|
|
|
task.save! if update_annotation(task.description)
|
|
|
|
task.protocol.steps.each do |step|
|
|
|
|
step.step_comments.each do |comment|
|
|
|
|
comment.save! if update_annotation(comment.message)
|
|
|
|
end
|
|
|
|
step.save! if update_annotation(step.description)
|
|
|
|
end
|
|
|
|
task.results.each do |res|
|
|
|
|
res.result_comments.each do |comment|
|
|
|
|
comment.save! if update_annotation(comment.message)
|
|
|
|
end
|
|
|
|
next unless res.result_text
|
2019-07-26 18:40:36 +08:00
|
|
|
|
2019-01-25 17:50:08 +08:00
|
|
|
res.save! if update_annotation(res.result_text.text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
# Returns true if text was updated
|
|
|
|
def update_annotation(text)
|
|
|
|
return false if text.nil?
|
2019-07-26 18:40:36 +08:00
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
updated = false
|
2018-06-27 21:17:02 +08:00
|
|
|
%w(prj exp tsk rep_item).each do |name|
|
2018-03-06 18:26:30 +08:00
|
|
|
text.scan(/~#{name}~\w+\]/).each do |text_match|
|
2018-03-12 05:11:12 +08:00
|
|
|
orig_id_encoded = text_match.match(/~#{name}~(\w+)\]/)[1]
|
2018-03-06 18:26:30 +08:00
|
|
|
orig_id = orig_id_encoded.base62_decode
|
2018-03-13 00:00:44 +08:00
|
|
|
new_id =
|
2018-03-06 18:26:30 +08:00
|
|
|
case name
|
|
|
|
when 'prj'
|
2018-03-13 00:00:44 +08:00
|
|
|
@project_mappings[orig_id]
|
2018-03-06 18:26:30 +08:00
|
|
|
when 'exp'
|
2018-03-13 00:00:44 +08:00
|
|
|
@experiment_mappings[orig_id]
|
2018-03-06 18:26:30 +08:00
|
|
|
when 'tsk'
|
2018-03-13 00:00:44 +08:00
|
|
|
@my_module_mappings[orig_id]
|
2018-06-19 17:58:22 +08:00
|
|
|
when 'rep_item'
|
|
|
|
@repository_row_mappings[orig_id]
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
2018-03-13 00:00:44 +08:00
|
|
|
next unless new_id
|
2019-07-26 18:40:36 +08:00
|
|
|
|
2018-03-13 00:00:44 +08:00
|
|
|
new_id_encoded = new_id.base62_encode
|
2018-03-06 18:26:30 +08:00
|
|
|
text.sub!("~#{name}~#{orig_id_encoded}]", "~#{name}~#{new_id_encoded}]")
|
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
text.scan(/\[@[\w+-@?! ]+~\w+\]/).each do |user_match|
|
2018-03-12 05:11:12 +08:00
|
|
|
orig_id_encoded = user_match.match(/\[@[\w+-@?! ]+~(\w+)\]/)[1]
|
2018-03-06 18:26:30 +08:00
|
|
|
orig_id = orig_id_encoded.base62_decode
|
2018-03-13 00:00:44 +08:00
|
|
|
next unless @user_mappings[orig_id]
|
2019-07-26 18:40:36 +08:00
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
new_id_encoded = @user_mappings[orig_id].base62_encode
|
|
|
|
text.sub!("~#{orig_id_encoded}]", "~#{new_id_encoded}]")
|
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
updated
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_task_connections(connections_json)
|
|
|
|
connections_json.each do |connection_json|
|
|
|
|
connection = Connection.new(connection_json)
|
|
|
|
connection.id = nil
|
|
|
|
connection.input_id = @my_module_mappings[connection.input_id]
|
|
|
|
connection.output_id = @my_module_mappings[connection.output_id]
|
|
|
|
connection.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-18 19:50:04 +08:00
|
|
|
def create_label_templates(label_templates_json, team)
|
|
|
|
# Destroy default templates generated on team creation
|
|
|
|
team.label_templates.where.not(type: 'FluicsLabelTemplate').destroy_all
|
|
|
|
label_templates_json.each do |template_json|
|
|
|
|
template = LabelTemplate.new(template_json)
|
2022-11-08 21:41:11 +08:00
|
|
|
orig_template_id = template.id
|
2022-08-18 19:50:04 +08:00
|
|
|
template.id = nil
|
|
|
|
template.team_id = team.id
|
|
|
|
template.created_by_id = find_user(template.created_by_id) if template.created_by_id
|
|
|
|
template.last_modified_by_id = find_user(template.last_modified_by_id) if template.last_modified_by_id
|
|
|
|
template.save!
|
|
|
|
|
|
|
|
@label_template_counter += 1
|
2022-11-08 21:41:11 +08:00
|
|
|
@label_template_mappings[orig_template_id] = template.id
|
2022-08-18 19:50:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-15 21:13:10 +08:00
|
|
|
def create_activities(activities_json, team)
|
2018-03-06 18:26:30 +08:00
|
|
|
activities_json.each do |activity_json|
|
|
|
|
activity = Activity.new(activity_json)
|
|
|
|
activity.id = nil
|
2019-04-15 21:13:10 +08:00
|
|
|
activity.owner_id = find_user(activity.owner_id)
|
|
|
|
activity.team_id = team.id
|
2018-03-06 18:26:30 +08:00
|
|
|
activity.project_id = @project_mappings[activity.project_id]
|
|
|
|
activity.experiment_id =
|
|
|
|
@experiment_mappings[activity.experiment_id]
|
2019-04-15 21:13:10 +08:00
|
|
|
if activity.subject_id.present?
|
|
|
|
if activity.subject_type == 'Team'
|
|
|
|
activity.subject_id = team.id
|
2020-04-09 18:33:04 +08:00
|
|
|
elsif activity.subject_type == 'RepositoryBase'
|
|
|
|
activity.subject_id = @repository_mappings[activity.subject_id]
|
2019-04-15 21:13:10 +08:00
|
|
|
else
|
|
|
|
mappings = instance_variable_get("@#{activity.subject_type.underscore}_mappings")
|
|
|
|
activity.subject_id = mappings[activity.subject_id]
|
|
|
|
end
|
|
|
|
end
|
2020-01-22 19:10:58 +08:00
|
|
|
if activity.values&.dig(:message_items).present?
|
2019-04-15 21:13:10 +08:00
|
|
|
activity.values['message_items'].each_value do |item|
|
2019-05-07 19:22:55 +08:00
|
|
|
next unless item['type']
|
|
|
|
|
2019-04-15 21:13:10 +08:00
|
|
|
mappings = instance_variable_get("@#{item['type'].underscore}_mappings")
|
2019-05-07 19:22:55 +08:00
|
|
|
next unless mappings
|
|
|
|
|
2019-04-15 21:13:10 +08:00
|
|
|
item['id'] = mappings[item['id']]
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
activity.my_module_id = @my_module_mappings[activity.my_module_id]
|
|
|
|
activity.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_tiny_mce_assets(tmce_assets_json, team)
|
|
|
|
tmce_assets_json.each do |tiny_mce_asset_json|
|
2019-07-26 18:40:36 +08:00
|
|
|
tiny_mce_asset = TinyMceAsset.new(tiny_mce_asset_json['tiny_mce_asset'])
|
|
|
|
tiny_mce_asset_blob = tiny_mce_asset_json['tiny_mce_asset_blob']
|
2018-03-06 18:26:30 +08:00
|
|
|
# Try to find and load file
|
2019-09-12 23:21:48 +08:00
|
|
|
tiny_mce_file = File.open(File.join(@import_dir,
|
|
|
|
'tiny_mce_assets',
|
|
|
|
tiny_mce_asset.id.to_s,
|
|
|
|
tiny_mce_asset_blob['filename']))
|
|
|
|
orig_tmce_id = tiny_mce_asset.id
|
|
|
|
tiny_mce_asset.id = nil
|
|
|
|
if tiny_mce_asset.object_id.present?
|
|
|
|
mappings = instance_variable_get("@#{tiny_mce_asset.object_type.underscore}_mappings")
|
|
|
|
tiny_mce_asset.object_id = mappings[tiny_mce_asset.object_id]
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
2019-09-12 23:21:48 +08:00
|
|
|
tiny_mce_asset.team = team
|
|
|
|
tiny_mce_asset.save!
|
|
|
|
tiny_mce_asset.image.attach(io: tiny_mce_file, filename: File.basename(tiny_mce_file))
|
|
|
|
@mce_asset_counter += 1
|
2020-01-07 21:12:48 +08:00
|
|
|
next if tiny_mce_asset.object_id.blank?
|
2019-09-12 23:21:48 +08:00
|
|
|
|
|
|
|
object = tiny_mce_asset.object
|
|
|
|
object_field = Extends::RICH_TEXT_FIELD_MAPPINGS[object.class.name]
|
|
|
|
encoded_id = Base62.encode(tiny_mce_asset.id)
|
|
|
|
object.public_send(object_field).sub!("data-mce-token=\"#{Base62.encode(orig_tmce_id)}\"",
|
|
|
|
"data-mce-token=\"#{encoded_id}\"")
|
|
|
|
# Check for old fields
|
|
|
|
new_asset_format = "<img src=\"\" class=\"img-responsive\" data-mce-token=\"#{encoded_id}\"/>"
|
|
|
|
object.public_send(object_field).sub!("[~tiny_mce_id:#{orig_tmce_id}]",
|
|
|
|
new_asset_format)
|
|
|
|
object.save!
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_users(users_json, team)
|
|
|
|
puts 'Creating users...'
|
|
|
|
users_json.each do |user_json|
|
2018-03-13 00:00:44 +08:00
|
|
|
user = User.new(user_json['user'].slice(*User.column_names))
|
2018-03-06 18:26:30 +08:00
|
|
|
orig_user_id = user.id
|
|
|
|
user.id = nil
|
|
|
|
user.password = user_json['user']['encrypted_password']
|
|
|
|
user.current_team_id = team.id
|
2018-03-13 00:00:44 +08:00
|
|
|
user.invited_by_id = @user_mappings[user.invited_by_id]
|
2019-11-04 21:53:59 +08:00
|
|
|
user.save!
|
2019-07-26 18:40:36 +08:00
|
|
|
if user_json['user']['avatar']
|
2019-11-04 21:53:59 +08:00
|
|
|
avatar_filename = user_json['user']['avatar']['filename']
|
|
|
|
avatar_path = File.join(@import_dir, 'avatars', orig_user_id.to_s, avatar_filename)
|
2018-12-06 01:25:11 +08:00
|
|
|
if File.exist?(avatar_path)
|
2019-11-06 21:41:51 +08:00
|
|
|
avatar = File.open(avatar_path)
|
|
|
|
user.avatar.attach(io: avatar, filename: avatar_filename)
|
2018-12-06 01:25:11 +08:00
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
@user_counter += 1
|
|
|
|
user.update_attribute('encrypted_password',
|
|
|
|
user_json['user']['encrypted_password'])
|
|
|
|
@user_mappings[orig_user_id] = user.id
|
|
|
|
user_json['user_identities'].each do |user_identity_json|
|
|
|
|
user_identity = UserIdentity.new(user_identity_json)
|
|
|
|
user_identity.id = nil
|
|
|
|
user_identity.user_id = user.id
|
|
|
|
user_identity.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_notifications(notifications_json)
|
|
|
|
puts 'Creating notifications...'
|
|
|
|
notifications_json.each do |notification_json|
|
|
|
|
notification = Notification.new(notification_json)
|
|
|
|
next if notification.type_of.blank?
|
2019-07-26 18:40:36 +08:00
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
orig_notification_id = notification.id
|
|
|
|
notification.id = nil
|
|
|
|
notification.generator_user_id = find_user(notification.generator_user_id)
|
|
|
|
notification.save!
|
|
|
|
@notification_mappings[orig_notification_id] = notification.id
|
|
|
|
@notification_counter += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-03 23:02:33 +08:00
|
|
|
def create_repositories(repositories_json, team, snapshots = false)
|
2018-03-06 18:26:30 +08:00
|
|
|
puts 'Creating repositories...'
|
|
|
|
repositories_json.each do |repository_json|
|
2020-06-03 23:02:33 +08:00
|
|
|
if snapshots
|
|
|
|
repository = RepositorySnapshot.new(repository_json['repository'])
|
|
|
|
repository.my_module_id = @my_module_mappings[repository.my_module_id]
|
|
|
|
repository.parent_id = @repository_mappings[repository.parent_id]
|
|
|
|
else
|
|
|
|
repository = Repository.new(repository_json['repository'])
|
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
orig_repository_id = repository.id
|
|
|
|
repository.id = nil
|
|
|
|
repository.team = team
|
|
|
|
repository.created_by_id = find_user(repository.created_by_id)
|
2020-11-13 21:13:26 +08:00
|
|
|
repository.archived_by_id = find_user(repository.archived_by_id)
|
|
|
|
repository.restored_by_id = find_user(repository.restored_by_id)
|
|
|
|
repository.discarded_by_id = find_user(repository.discarded_by_id)
|
2022-11-08 21:41:11 +08:00
|
|
|
repository.skip_user_assignments = true
|
2018-03-06 18:26:30 +08:00
|
|
|
repository.save!
|
2022-11-08 21:41:11 +08:00
|
|
|
|
|
|
|
create_user_assignments(repository_json['user_assignments'], repository)
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
@repository_mappings[orig_repository_id] = repository.id
|
|
|
|
@repository_counter += 1
|
|
|
|
repository_json['repository_columns'].each do |repository_column_json|
|
2018-06-14 20:40:43 +08:00
|
|
|
repository_column = RepositoryColumn.new(
|
|
|
|
repository_column_json['repository_column']
|
|
|
|
)
|
2018-03-06 18:26:30 +08:00
|
|
|
orig_rep_col_id = repository_column.id
|
|
|
|
repository_column.id = nil
|
|
|
|
repository_column.repository = repository
|
|
|
|
repository_column.created_by_id =
|
|
|
|
find_user(repository_column.created_by_id)
|
|
|
|
repository_column.save!
|
|
|
|
@repository_column_mappings[orig_rep_col_id] = repository_column.id
|
2020-01-07 21:12:48 +08:00
|
|
|
case repository_column.data_type
|
|
|
|
when 'RepositoryListValue'
|
|
|
|
repository_column_json['repository_list_items'].each do |list_item|
|
|
|
|
created_by_id = find_user(repository_column.created_by_id)
|
|
|
|
repository_list_item = RepositoryListItem.new(data: list_item['data'])
|
|
|
|
repository_list_item.repository_column = repository_column
|
|
|
|
repository_list_item.created_by_id = created_by_id
|
|
|
|
repository_list_item.last_modified_by_id = created_by_id
|
|
|
|
repository_list_item.save!
|
|
|
|
@repository_list_item_mappings[list_item['id']] =
|
|
|
|
repository_list_item.id
|
|
|
|
end
|
|
|
|
when 'RepositoryChecklistValue'
|
|
|
|
repository_column_json['repository_checklist_items'].each do |checklist_item|
|
|
|
|
created_by_id = find_user(repository_column.created_by_id)
|
|
|
|
repository_checklist_item = RepositoryChecklistItem.new(data: checklist_item['data'])
|
|
|
|
repository_checklist_item.repository_column = repository_column
|
|
|
|
repository_checklist_item.created_by_id = created_by_id
|
|
|
|
repository_checklist_item.last_modified_by_id = created_by_id
|
|
|
|
repository_checklist_item.save!
|
|
|
|
@repository_checklist_item_mappings[checklist_item['id']] =
|
|
|
|
repository_checklist_item.id
|
|
|
|
end
|
|
|
|
when 'RepositoryStatusValue'
|
|
|
|
repository_column_json['repository_status_items'].each do |status_item|
|
|
|
|
created_by_id = find_user(repository_column.created_by_id)
|
|
|
|
repository_status_item = RepositoryStatusItem.new(
|
|
|
|
status: status_item['status'],
|
|
|
|
icon: status_item['icon']
|
|
|
|
)
|
|
|
|
repository_status_item.repository_column = repository_column
|
|
|
|
repository_status_item.created_by_id = created_by_id
|
|
|
|
repository_status_item.last_modified_by_id = created_by_id
|
|
|
|
repository_status_item.save!
|
|
|
|
@repository_status_item_mappings[status_item['id']] =
|
|
|
|
repository_status_item.id
|
|
|
|
end
|
2018-06-14 20:40:43 +08:00
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
create_repository_rows(repository_json['repository_rows'], repository)
|
2020-06-03 23:02:33 +08:00
|
|
|
|
|
|
|
create_repositories(repository_json['repository_snapshots'], team, true) unless snapshots
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_repository_rows(repository_rows_json, repository)
|
|
|
|
repository_rows_json.each do |repository_row_json|
|
|
|
|
repository_row =
|
|
|
|
RepositoryRow.new(repository_row_json['repository_row'])
|
|
|
|
orig_rep_row_id = repository_row.id
|
|
|
|
repository_row.id = nil
|
|
|
|
repository_row.repository = repository
|
|
|
|
repository_row.created_by_id = find_user(repository_row.created_by_id)
|
2020-11-13 21:13:26 +08:00
|
|
|
repository_row.archived_by_id = find_user(repository_row.archived_by_id)
|
|
|
|
repository_row.restored_by_id = find_user(repository_row.restored_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
repository_row.last_modified_by_id =
|
|
|
|
find_user(repository_row.last_modified_by_id)
|
|
|
|
repository_row.save!
|
|
|
|
@repository_row_mappings[orig_rep_row_id] = repository_row.id
|
|
|
|
@repository_row_counter += 1
|
|
|
|
repository_row_json['my_module_repository_rows'].each do |mm_rep_row_json|
|
|
|
|
mm_rep_row = MyModuleRepositoryRow.new(mm_rep_row_json)
|
|
|
|
mm_rep_row.id = nil
|
|
|
|
mm_rep_row.repository_row = repository_row
|
|
|
|
mm_rep_row.my_module_id = @my_module_mappings[mm_rep_row.my_module_id]
|
|
|
|
mm_rep_row.assigned_by_id = find_user(mm_rep_row.assigned_by_id)
|
2018-03-13 00:00:44 +08:00
|
|
|
mm_rep_row.save!
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-14 21:20:57 +08:00
|
|
|
def create_repository_cells(repository_cells_json, team)
|
2018-03-06 18:26:30 +08:00
|
|
|
repository_cells_json.each do |repository_cell_json|
|
|
|
|
repository_cell =
|
|
|
|
RepositoryCell.new(repository_cell_json['repository_cell'])
|
|
|
|
repository_cell.id = nil
|
|
|
|
repository_cell.repository_column_id =
|
|
|
|
@repository_column_mappings[repository_cell.repository_column_id]
|
|
|
|
repository_cell.repository_row_id =
|
|
|
|
@repository_row_mappings[repository_cell.repository_row_id]
|
2018-06-14 20:40:43 +08:00
|
|
|
create_cell_value(repository_cell,
|
2018-06-14 21:20:57 +08:00
|
|
|
repository_cell_json,
|
|
|
|
team)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_protocol_keywords(protocol_keywords_json, team)
|
|
|
|
puts 'Creating keywords...'
|
|
|
|
protocol_keywords_json.each do |protocol_keyword_json|
|
|
|
|
protocol_keyword = ProtocolKeyword.new(protocol_keyword_json)
|
|
|
|
orig_pr_keyword_id = protocol_keyword.id
|
|
|
|
protocol_keyword.id = nil
|
|
|
|
protocol_keyword.team = team
|
|
|
|
protocol_keyword.save!
|
|
|
|
@protocol_keyword_mappings[orig_pr_keyword_id] = protocol_keyword.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-13 21:13:26 +08:00
|
|
|
def create_project_folders(project_folders_json, team)
|
|
|
|
puts 'Creating project folders...'
|
|
|
|
project_folders_json.each do |project_folder_json|
|
|
|
|
project_folder = ProjectFolder.new(project_folder_json)
|
|
|
|
orig_project_folder_id = project_folder.id
|
|
|
|
project_folder.id = nil
|
|
|
|
project_folder.parent_folder_id = nil
|
|
|
|
project_folder.team = team
|
|
|
|
project_folder.save!
|
|
|
|
@project_folder_mappings[orig_project_folder_id] = project_folder.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update parent relations
|
|
|
|
project_folders_json.each do |project_folder_json|
|
|
|
|
next unless project_folder_json['parent_folder_id']
|
|
|
|
|
|
|
|
ProjectFolder.find(@project_folder_mappings[project_folder_json['id'].to_i])
|
|
|
|
.update!(parent_folder_id: @project_folder_mappings[project_folder_json['parent_folder_id'].to_i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
def create_projects(projects_json, team)
|
|
|
|
puts 'Creating projects...'
|
|
|
|
projects_json.each do |project_json|
|
|
|
|
project = Project.new(project_json['project'])
|
|
|
|
orig_project_id = project.id
|
|
|
|
project.id = nil
|
|
|
|
project.team = team
|
|
|
|
project.created_by_id = find_user(project.created_by_id)
|
|
|
|
project.last_modified_by_id = find_user(project.last_modified_by_id)
|
|
|
|
project.archived_by_id = find_user(project.archived_by_id)
|
|
|
|
project.restored_by_id = find_user(project.restored_by_id)
|
2021-11-22 18:17:39 +08:00
|
|
|
project.skip_user_assignments = true
|
2020-11-13 21:13:26 +08:00
|
|
|
project.project_folder_id = @project_folder_mappings[project.project_folder_id]
|
2018-03-06 18:26:30 +08:00
|
|
|
project.save!
|
|
|
|
@project_mappings[orig_project_id] = project.id
|
|
|
|
@project_counter += 1
|
2021-10-14 19:21:36 +08:00
|
|
|
puts 'Creating project user_assignments...'
|
|
|
|
create_user_assignments(project_json['user_assignments'], project)
|
2018-03-06 18:26:30 +08:00
|
|
|
puts 'Creating user_projects...'
|
|
|
|
project_json['user_projects'].each do |user_project_json|
|
|
|
|
user_project = UserProject.new(user_project_json)
|
|
|
|
user_project.id = nil
|
|
|
|
user_project.project = project
|
|
|
|
user_project.user_id = find_user(user_project.user_id)
|
|
|
|
user_project.assigned_by_id = find_user(user_project.assigned_by_id)
|
|
|
|
user_project.save!
|
|
|
|
end
|
|
|
|
puts 'Creating project_comments...'
|
|
|
|
project_json['project_comments'].each do |project_comment_json|
|
|
|
|
project_comment = ProjectComment.new(project_comment_json)
|
|
|
|
project_comment.id = nil
|
|
|
|
project_comment.user_id = find_user(project_comment.user_id)
|
|
|
|
project_comment.last_modified_by_id =
|
|
|
|
find_user(project_comment.last_modified_by_id)
|
|
|
|
project_comment.project = project
|
|
|
|
project_comment.save!
|
|
|
|
end
|
|
|
|
puts 'Creating tags...'
|
|
|
|
project_json['tags'].each do |tag_json|
|
|
|
|
tag = Tag.new(tag_json)
|
|
|
|
orig_tag_id = tag.id
|
|
|
|
tag.id = nil
|
|
|
|
tag.project = project
|
|
|
|
tag.created_by_id = find_user(tag.created_by_id)
|
|
|
|
tag.last_modified_by_id = find_user(tag.last_modified_by_id)
|
|
|
|
tag.save!
|
|
|
|
@tag_mappings[orig_tag_id] = tag.id
|
|
|
|
end
|
|
|
|
|
|
|
|
create_experiments(project_json['experiments'], project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_experiments(experiments_json, project)
|
|
|
|
puts('Creating experiments...')
|
|
|
|
experiments_json.each do |experiment_json|
|
2019-01-16 22:54:03 +08:00
|
|
|
create_experiment(experiment_json, project)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
def create_experiment(experiment_json, project, user_id = nil)
|
|
|
|
experiment = Experiment.new(experiment_json['experiment'])
|
|
|
|
orig_experiment_id = experiment.id
|
|
|
|
experiment.id = nil
|
|
|
|
experiment.project = project
|
|
|
|
experiment.created_by_id =
|
|
|
|
user_id || find_user(experiment.created_by_id)
|
|
|
|
experiment.last_modified_by_id =
|
|
|
|
user_id || find_user(experiment.last_modified_by_id)
|
|
|
|
experiment.archived_by_id = find_user(experiment.archived_by_id)
|
|
|
|
experiment.restored_by_id = find_user(experiment.restored_by_id)
|
2021-11-22 18:17:39 +08:00
|
|
|
experiment.skip_user_assignments = true
|
2019-01-16 22:54:03 +08:00
|
|
|
experiment.save!
|
|
|
|
@experiment_mappings[orig_experiment_id] = experiment.id
|
2021-10-14 19:21:36 +08:00
|
|
|
|
|
|
|
create_user_assignments(experiment_json['user_assignments'], experiment)
|
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
experiment_json['my_module_groups'].each do |my_module_group_json|
|
|
|
|
my_module_group = MyModuleGroup.new(my_module_group_json)
|
|
|
|
orig_module_group_id = my_module_group.id
|
|
|
|
my_module_group.id = nil
|
|
|
|
my_module_group.experiment = experiment
|
|
|
|
my_module_group.created_by_id =
|
|
|
|
user_id || find_user(my_module_group.created_by_id)
|
|
|
|
my_module_group.save!
|
|
|
|
@my_module_group_mappings[orig_module_group_id] = my_module_group.id
|
|
|
|
end
|
|
|
|
create_my_modules(experiment_json['my_modules'], experiment, user_id)
|
|
|
|
experiment
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_my_modules(my_modules_json, experiment, user_id = nil)
|
2018-03-06 18:26:30 +08:00
|
|
|
puts('Creating my_modules...')
|
|
|
|
my_modules_json.each do |my_module_json|
|
2020-08-06 16:50:11 +08:00
|
|
|
my_module = MyModule.new(my_module_json['my_module'].except('my_module_status_name'))
|
2018-03-06 18:26:30 +08:00
|
|
|
orig_my_module_id = my_module.id
|
|
|
|
my_module.id = nil
|
|
|
|
my_module.my_module_group_id =
|
|
|
|
@my_module_group_mappings[my_module.my_module_group_id]
|
2019-01-16 22:54:03 +08:00
|
|
|
my_module.created_by_id =
|
|
|
|
user_id || find_user(my_module.created_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
my_module.last_modified_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(my_module.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
my_module.archived_by_id = find_user(my_module.archived_by_id)
|
|
|
|
my_module.restored_by_id = find_user(my_module.restored_by_id)
|
|
|
|
my_module.experiment = experiment
|
2021-11-22 18:17:39 +08:00
|
|
|
my_module.skip_user_assignments = true
|
2020-08-06 16:50:11 +08:00
|
|
|
|
|
|
|
# Find matching status from default flow
|
|
|
|
default_flow = MyModuleStatusFlow.global.first
|
2020-08-21 17:58:12 +08:00
|
|
|
|
|
|
|
if default_flow.present?
|
|
|
|
status = default_flow.my_module_statuses.find_by(name: my_module_json['my_module']['my_module_status_name'])
|
|
|
|
status ||= default_flow.initial_status
|
|
|
|
my_module.my_module_status = status
|
|
|
|
end
|
2020-08-06 16:50:11 +08:00
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
my_module.save!
|
|
|
|
@my_module_mappings[orig_my_module_id] = my_module.id
|
|
|
|
@my_module_counter += 1
|
|
|
|
|
2021-10-14 19:21:36 +08:00
|
|
|
create_user_assignments(my_module_json['user_assignments'], my_module)
|
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
unless @is_template
|
|
|
|
my_module_json['my_module_tags'].each do |my_module_tag_json|
|
|
|
|
my_module_tag = MyModuleTag.new(my_module_tag_json)
|
|
|
|
my_module_tag.id = nil
|
|
|
|
my_module_tag.my_module = my_module
|
|
|
|
my_module_tag.tag_id = @tag_mappings[my_module_tag.tag_id]
|
|
|
|
my_module_tag.created_by_id =
|
|
|
|
user_id || find_user(my_module_tag.created_by_id)
|
|
|
|
my_module_tag.save!
|
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
my_module_json['task_comments'].each do |task_comment_json|
|
|
|
|
task_comment = TaskComment.new(task_comment_json)
|
|
|
|
task_comment.id = nil
|
2019-01-16 22:54:03 +08:00
|
|
|
task_comment.user_id = user_id || find_user(task_comment.user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
task_comment.last_modified_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(task_comment.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
task_comment.my_module = my_module
|
|
|
|
task_comment.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
my_module_json['user_my_modules'].each do |user_module_json|
|
|
|
|
user_module = UserMyModule.new(user_module_json)
|
|
|
|
user_module.id = nil
|
|
|
|
user_module.my_module = my_module
|
2019-01-16 22:54:03 +08:00
|
|
|
user_module.user_id = user_id || find_user(user_module.user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
user_module.assigned_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(user_module.assigned_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
user_module.save!
|
|
|
|
end
|
2019-01-16 22:54:03 +08:00
|
|
|
create_protocols(my_module_json['protocols'],
|
|
|
|
my_module, nil, user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
create_results(my_module_json['results'], my_module, user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
def create_protocols(protocols_json, my_module = nil, team = nil,
|
|
|
|
user_id = nil)
|
2018-03-06 18:26:30 +08:00
|
|
|
puts 'Creating protocols...'
|
|
|
|
protocols_json.each do |protocol_json|
|
|
|
|
protocol = Protocol.new(protocol_json['protocol'])
|
|
|
|
orig_protocol_id = protocol.id
|
2020-05-27 18:10:48 +08:00
|
|
|
if protocol.name
|
|
|
|
protocol_name_unique = false
|
2020-05-27 22:11:10 +08:00
|
|
|
original_name = protocol.name
|
|
|
|
counter = 0
|
2020-05-27 18:10:48 +08:00
|
|
|
until protocol_name_unique
|
2020-05-27 22:11:10 +08:00
|
|
|
counter += 1
|
2020-05-27 18:10:48 +08:00
|
|
|
protocol_exist = if protocol.protocol_type == :in_repository_public
|
|
|
|
Protocol.where(protocol_type: protocol.protocol_type)
|
|
|
|
.where(team: team)
|
|
|
|
.find_by(name: protocol.name)
|
|
|
|
else
|
|
|
|
Protocol.where(protocol_type: protocol.protocol_type)
|
|
|
|
.where(team: team)
|
|
|
|
.where(added_by_id: find_user(protocol.added_by_id))
|
|
|
|
.find_by(name: protocol.name)
|
|
|
|
end
|
|
|
|
if protocol_exist
|
2020-05-27 22:11:10 +08:00
|
|
|
protocol.name = original_name + "(#{counter})"
|
2020-05-27 18:10:48 +08:00
|
|
|
else
|
|
|
|
protocol_name_unique = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
protocol.id = nil
|
|
|
|
protocol.added_by_id = find_user(protocol.added_by_id)
|
2022-06-07 00:21:57 +08:00
|
|
|
protocol.added_by_id ||= my_module.present? ? my_module.created_by_id : team.created_by_id
|
2019-01-16 22:54:03 +08:00
|
|
|
protocol.team = team || my_module.experiment.project.team
|
2018-03-06 18:26:30 +08:00
|
|
|
protocol.archived_by_id = find_user(protocol.archived_by_id)
|
|
|
|
protocol.restored_by_id = find_user(protocol.restored_by_id)
|
|
|
|
protocol.my_module = my_module unless protocol.my_module_id.nil?
|
2022-10-12 20:27:21 +08:00
|
|
|
protocol.skip_user_assignments = true
|
2019-07-26 18:40:36 +08:00
|
|
|
protocol.parent_id = @protocol_mappings[protocol.parent_id] unless protocol.parent_id.nil?
|
2018-03-06 18:26:30 +08:00
|
|
|
protocol.save!
|
2022-11-08 21:41:11 +08:00
|
|
|
|
|
|
|
create_user_assignments(protocol_json['user_assignments'], protocol) if protocol.in_repository?
|
|
|
|
|
2018-03-06 18:26:30 +08:00
|
|
|
@protocol_counter += 1
|
|
|
|
@protocol_mappings[orig_protocol_id] = protocol.id
|
|
|
|
|
|
|
|
protocol_json['protocol_protocol_keywords'].each do |pp_keyword_json|
|
|
|
|
pp_keyword = ProtocolProtocolKeyword.new(pp_keyword_json)
|
|
|
|
pp_keyword.id = nil
|
|
|
|
pp_keyword.protocol = protocol
|
|
|
|
pp_keyword.protocol_keyword_id =
|
|
|
|
@protocol_keyword_mappings[pp_keyword.protocol_keyword_id]
|
|
|
|
pp_keyword.save!
|
|
|
|
end
|
2019-01-16 22:54:03 +08:00
|
|
|
create_steps(protocol_json['steps'], protocol, user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
def create_steps(steps_json, protocol, user_id = nil)
|
2018-03-06 18:26:30 +08:00
|
|
|
puts('Creating steps...')
|
|
|
|
steps_json.each do |step_json|
|
|
|
|
step = Step.new(step_json['step'])
|
|
|
|
orig_step_id = step.id
|
|
|
|
step.id = nil
|
2019-01-16 22:54:03 +08:00
|
|
|
step.user_id = user_id || find_user(step.user_id)
|
|
|
|
step.last_modified_by_id =
|
|
|
|
user_id || find_user(step.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
step.protocol_id = protocol.id
|
|
|
|
step.save!
|
|
|
|
@step_mappings[orig_step_id] = step.id
|
|
|
|
@step_counter += 1
|
|
|
|
|
|
|
|
step_json['step_comments'].each do |step_comment_json|
|
|
|
|
step_comment = StepComment.new(step_comment_json)
|
|
|
|
step_comment.id = nil
|
2019-01-16 22:54:03 +08:00
|
|
|
step_comment.user_id = user_id || find_user(step_comment.user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
step_comment.last_modified_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(step_comment.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
step_comment.step = step
|
|
|
|
step_comment.save!
|
|
|
|
end
|
|
|
|
|
2022-06-08 21:31:48 +08:00
|
|
|
step_json['step_orderable_elements'].each do |element_json|
|
|
|
|
if element_json['step_text']
|
|
|
|
orderable = StepText.new(element_json['step_text'])
|
2022-11-08 21:41:11 +08:00
|
|
|
orig_step_text_id = orderable.id
|
2022-06-08 21:31:48 +08:00
|
|
|
orderable.step_id = step.id
|
|
|
|
orderable.id = nil
|
|
|
|
orderable.save!
|
2022-11-08 21:41:11 +08:00
|
|
|
@step_text_mappings[orig_step_text_id] = orderable.id
|
2022-06-08 21:31:48 +08:00
|
|
|
elsif element_json['table']
|
|
|
|
table = Table.new(element_json['table'])
|
|
|
|
orig_table_id = table.id
|
|
|
|
table.id = nil
|
|
|
|
table.created_by_id = user_id || find_user(table.created_by_id)
|
|
|
|
table.last_modified_by_id =
|
|
|
|
user_id || find_user(table.last_modified_by_id)
|
|
|
|
table.team = protocol.team
|
|
|
|
table.contents = Base64.decode64(table.contents)
|
|
|
|
table.data_vector = Base64.decode64(table.data_vector)
|
|
|
|
table.save!
|
|
|
|
@table_mappings[orig_table_id] = table.id
|
|
|
|
orderable = StepTable.create!(step: step, table: table)
|
|
|
|
elsif element_json['checklist']
|
|
|
|
orderable = create_step_checklist(element_json['checklist'], step, user_id)
|
|
|
|
end
|
|
|
|
StepOrderableElement.create!(
|
|
|
|
position: element_json['position'],
|
|
|
|
step: step,
|
|
|
|
orderable: orderable
|
|
|
|
)
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
step_json['assets'].each do |asset_json|
|
2019-01-16 22:54:03 +08:00
|
|
|
asset = create_asset(asset_json, protocol.team, user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
StepAsset.create!(step: step, asset: asset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-16 22:54:03 +08:00
|
|
|
def create_results(results_json, my_module, user_id = nil)
|
2018-03-06 18:26:30 +08:00
|
|
|
puts('Creating results...')
|
|
|
|
results_json.each do |result_json|
|
|
|
|
result = Result.new(result_json['result'])
|
|
|
|
orig_result_id = result.id
|
|
|
|
result.id = nil
|
|
|
|
result.my_module = my_module
|
2019-01-16 22:54:03 +08:00
|
|
|
result.user_id = user_id || find_user(result.user_id)
|
|
|
|
result.last_modified_by_id =
|
|
|
|
user_id || find_user(result.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
result.archived_by_id = find_user(result.archived_by_id)
|
|
|
|
result.restored_by_id = find_user(result.restored_by_id)
|
|
|
|
|
|
|
|
if result_json['table'].present?
|
|
|
|
table = Table.new(result_json['table'])
|
|
|
|
orig_table_id = table.id
|
|
|
|
table.id = nil
|
2019-01-16 22:54:03 +08:00
|
|
|
table.created_by_id = user_id || find_user(table.created_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
table.last_modified_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(table.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
table.team = my_module.experiment.project.team
|
2018-03-13 00:00:44 +08:00
|
|
|
table.contents = Base64.decode64(table.contents)
|
|
|
|
table.data_vector = Base64.decode64(table.data_vector)
|
2018-03-06 18:26:30 +08:00
|
|
|
table.save!
|
|
|
|
@table_mappings[orig_table_id] = table.id
|
|
|
|
result.table = table
|
|
|
|
end
|
|
|
|
|
|
|
|
if result_json['asset'].present?
|
|
|
|
asset = create_asset(result_json['asset'],
|
2019-01-16 22:54:03 +08:00
|
|
|
my_module.experiment.project.team,
|
|
|
|
user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
result.asset = asset
|
|
|
|
end
|
|
|
|
|
|
|
|
if result_json['result_text'].present?
|
|
|
|
result_text = ResultText.new(result_json['result_text'])
|
|
|
|
orig_result_text_id = result_text.id
|
|
|
|
result_text.id = nil
|
|
|
|
result_text.result = result
|
|
|
|
result_text.save!
|
|
|
|
@result_text_mappings[orig_result_text_id] = result_text.id
|
|
|
|
end
|
|
|
|
|
|
|
|
result.save!
|
|
|
|
@result_mappings[orig_result_id] = result.id
|
|
|
|
@result_counter += 1
|
|
|
|
|
|
|
|
result_json['result_comments'].each do |result_comment_json|
|
|
|
|
result_comment = ResultComment.new(result_comment_json)
|
|
|
|
result_comment.id = nil
|
2019-01-16 22:54:03 +08:00
|
|
|
result_comment.user_id =
|
|
|
|
user_id || find_user(result_comment.user_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
result_comment.last_modified_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(result_comment.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
result_comment.result = result
|
|
|
|
result_comment.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns asset object
|
2019-01-16 22:54:03 +08:00
|
|
|
def create_asset(asset_json, team, user_id = nil)
|
2019-10-28 23:03:42 +08:00
|
|
|
asset = Asset.new(asset_json['asset'])
|
2019-07-26 18:40:36 +08:00
|
|
|
asset_blob = asset_json['asset_blob']
|
2019-08-14 15:06:12 +08:00
|
|
|
### Fix for support templates
|
2019-10-28 23:03:42 +08:00
|
|
|
asset_file_name = asset_blob['filename']
|
2019-09-12 23:21:48 +08:00
|
|
|
file = File.open("#{@import_dir}/assets/#{asset.id}/#{asset_file_name}")
|
|
|
|
orig_asset_id = asset.id
|
|
|
|
asset.id = nil
|
|
|
|
asset.created_by_id = user_id || find_user(asset.created_by_id)
|
|
|
|
asset.last_modified_by_id =
|
|
|
|
user_id || find_user(asset.last_modified_by_id)
|
|
|
|
asset.team = team
|
|
|
|
asset.in_template = true if @is_template
|
|
|
|
asset.save!
|
|
|
|
asset.file.attach(io: file, filename: File.basename(file))
|
|
|
|
asset.post_process_file(team)
|
|
|
|
@asset_mappings[orig_asset_id] = asset.id
|
|
|
|
@asset_counter += 1
|
2018-03-06 18:26:30 +08:00
|
|
|
asset
|
|
|
|
end
|
|
|
|
|
2022-06-08 21:31:48 +08:00
|
|
|
def create_step_checklist(checklist_json, step, user_id = nil)
|
2018-03-06 18:26:30 +08:00
|
|
|
checklist = Checklist.new(checklist_json['checklist'])
|
|
|
|
orig_checklist_id = checklist.id
|
|
|
|
checklist.id = nil
|
|
|
|
checklist.step = step
|
2019-01-16 22:54:03 +08:00
|
|
|
checklist.created_by_id =
|
|
|
|
user_id || find_user(checklist.created_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
checklist.last_modified_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(checklist.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
checklist.save!
|
|
|
|
@checklist_mappings[orig_checklist_id] = checklist.id
|
|
|
|
checklist_json['checklist_items'].each do |checklist_item_json|
|
|
|
|
checklist_item = ChecklistItem.new(checklist_item_json)
|
|
|
|
checklist_item.id = nil
|
|
|
|
checklist_item.checklist = checklist
|
2019-01-16 22:54:03 +08:00
|
|
|
checklist_item.created_by_id =
|
|
|
|
user_id || find_user(checklist_item.created_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
checklist_item.last_modified_by_id =
|
2019-01-16 22:54:03 +08:00
|
|
|
user_id || find_user(checklist_item.last_modified_by_id)
|
2018-03-06 18:26:30 +08:00
|
|
|
checklist_item.save!
|
|
|
|
end
|
2022-06-08 21:31:48 +08:00
|
|
|
checklist
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
|
|
|
|
2018-07-20 22:10:45 +08:00
|
|
|
def create_reports(reports_json, team)
|
2018-03-06 18:26:30 +08:00
|
|
|
reports_json.each do |report_json|
|
|
|
|
report_el_parent_mappings = {}
|
|
|
|
report_element_mappings = {}
|
|
|
|
report = Report.new(report_json['report'])
|
2019-10-30 22:47:11 +08:00
|
|
|
orig_report_id = report.id
|
2018-03-06 18:26:30 +08:00
|
|
|
report.id = nil
|
|
|
|
report.project_id = @project_mappings[report.project_id]
|
|
|
|
report.user_id = find_user(report.user_id)
|
|
|
|
report.last_modified_by_id = find_user(report.last_modified_by_id)
|
2018-07-20 22:10:45 +08:00
|
|
|
report.team_id = team.id
|
2022-11-08 21:41:11 +08:00
|
|
|
report.skip_user_assignments = true
|
2018-03-06 18:26:30 +08:00
|
|
|
report.save!
|
2022-11-08 21:41:11 +08:00
|
|
|
|
|
|
|
create_user_assignments(report_json['user_assignments'], report)
|
|
|
|
|
2019-10-30 22:47:11 +08:00
|
|
|
@report_mappings[orig_report_id] = report.id
|
2018-03-06 18:26:30 +08:00
|
|
|
@report_counter += 1
|
|
|
|
report_json['report_elements'].each do |report_element_json|
|
|
|
|
report_element = ReportElement.new(report_element_json)
|
|
|
|
orig_report_element_id = report_element.id
|
|
|
|
report_element.id = nil
|
|
|
|
orig_parent_id = report_element.parent_id
|
|
|
|
report_element.parent_id = nil
|
|
|
|
report_element.report = report
|
|
|
|
if report_element.project_id
|
|
|
|
report_element.project_id =
|
|
|
|
@project_mappings[report_element.project_id]
|
2018-03-13 00:00:44 +08:00
|
|
|
end
|
|
|
|
if report_element.my_module_id
|
2018-03-06 18:26:30 +08:00
|
|
|
report_element.my_module_id =
|
|
|
|
@my_module_mappings[report_element.my_module_id]
|
2018-03-13 00:00:44 +08:00
|
|
|
end
|
2019-07-26 18:40:36 +08:00
|
|
|
report_element.step_id = @step_mappings[report_element.step_id] if report_element.step_id
|
|
|
|
report_element.result_id = @result_mappings[report_element.result_id] if report_element.result_id
|
2018-03-13 00:00:44 +08:00
|
|
|
if report_element.checklist_id
|
2018-03-06 18:26:30 +08:00
|
|
|
report_element.checklist_id =
|
|
|
|
@checklist_mappings[report_element.checklist_id]
|
2018-03-13 00:00:44 +08:00
|
|
|
end
|
2019-07-26 18:40:36 +08:00
|
|
|
report_element.asset_id = @asset_mappings[report_element.asset_id] if report_element.asset_id
|
|
|
|
report_element.table_id = @table_mappings[report_element.table_id] if report_element.table_id
|
2018-03-13 00:00:44 +08:00
|
|
|
if report_element.experiment_id
|
2018-03-06 18:26:30 +08:00
|
|
|
report_element.experiment_id =
|
|
|
|
@experiment_mappings[report_element.experiment_id]
|
2018-03-13 00:00:44 +08:00
|
|
|
end
|
|
|
|
if report_element.repository_id
|
2018-03-06 18:26:30 +08:00
|
|
|
report_element.repository_id =
|
|
|
|
@repository_mappings[report_element.repository_id]
|
|
|
|
end
|
|
|
|
report_element.save!
|
|
|
|
report_element_mappings[orig_report_element_id] = report_element.id
|
|
|
|
report_el_parent_mappings[report_element.id] = orig_parent_id
|
|
|
|
end
|
|
|
|
report_el_parent_mappings.each do |k, v|
|
2020-01-07 21:12:48 +08:00
|
|
|
re = ReportElement.find_by(id: k)
|
2018-03-06 18:26:30 +08:00
|
|
|
re.parent_id = report_element_mappings[v]
|
|
|
|
re.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_user(user_id)
|
|
|
|
return nil if user_id.nil?
|
2019-07-26 18:40:36 +08:00
|
|
|
|
|
|
|
@user_mappings[user_id] || @admin_id
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|
2018-06-14 20:40:43 +08:00
|
|
|
|
|
|
|
def find_list_item_id(list_item_id)
|
|
|
|
@repository_list_item_mappings[list_item_id]
|
|
|
|
end
|
|
|
|
|
2020-01-07 21:12:48 +08:00
|
|
|
def find_checklist_item_id(checklist_item_id)
|
|
|
|
@repository_checklist_item_mappings[checklist_item_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_status_item_id(status_item_id)
|
|
|
|
@repository_status_item_mappings[status_item_id]
|
|
|
|
end
|
|
|
|
|
2021-10-14 19:21:36 +08:00
|
|
|
def create_user_assignments(user_assignments_json, assignable)
|
|
|
|
return if user_assignments_json.blank?
|
|
|
|
|
|
|
|
user_assignments_json.each do |user_assignment_json|
|
|
|
|
user_assignment = UserAssignment.new
|
|
|
|
user_assignment.assignable = assignable
|
|
|
|
user_assignment.user_role = @user_roles.find { |role| role.name == user_assignment_json['role_name'] }
|
|
|
|
user_assignment.user_id = find_user(user_assignment_json['user_id'])
|
2021-11-17 20:33:29 +08:00
|
|
|
user_assignment.assigned = user_assignment_json['assigned']
|
2021-10-14 19:21:36 +08:00
|
|
|
user_assignment.assigned_by_id = find_user(user_assignment_json['assigned_by_id'])
|
2022-11-08 21:41:11 +08:00
|
|
|
user_assignment.team_id = @team_id
|
2021-10-14 19:21:36 +08:00
|
|
|
user_assignment.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-14 21:20:57 +08:00
|
|
|
def create_cell_value(repository_cell, value_json, team)
|
|
|
|
cell_json = value_json['repository_value']
|
2018-06-14 20:40:43 +08:00
|
|
|
case repository_cell.value_type
|
|
|
|
when 'RepositoryListValue'
|
2018-06-14 21:20:57 +08:00
|
|
|
list_item_id = find_list_item_id(cell_json['repository_list_item_id'])
|
2018-06-14 20:40:43 +08:00
|
|
|
repository_value = RepositoryListValue.new(
|
|
|
|
repository_list_item_id: list_item_id.to_i
|
|
|
|
)
|
|
|
|
when 'RepositoryAssetValue'
|
2018-06-14 21:20:57 +08:00
|
|
|
asset = create_asset(value_json['repository_value_asset'], team)
|
|
|
|
repository_value = RepositoryAssetValue.new(asset: asset)
|
2020-01-07 21:12:48 +08:00
|
|
|
when 'RepositoryChecklistValue'
|
|
|
|
repository_value = RepositoryChecklistValue.new(cell_json)
|
2020-06-03 23:02:33 +08:00
|
|
|
value_json['repository_value_checklist'].each do |item|
|
|
|
|
item_id = find_checklist_item_id(item['repository_checklist_item_id']).to_i
|
|
|
|
repository_value.repository_checklist_items << RepositoryChecklistItem.find(item_id)
|
|
|
|
end
|
2020-01-07 21:12:48 +08:00
|
|
|
when 'RepositoryStatusValue'
|
|
|
|
list_item_id = find_status_item_id(cell_json['repository_status_item_id'])
|
|
|
|
repository_value = RepositoryStatusValue.new(
|
|
|
|
repository_status_item_id: list_item_id.to_i
|
|
|
|
)
|
|
|
|
else
|
|
|
|
value_type = repository_cell.repository_column.data_type
|
|
|
|
repository_value = value_type.constantize.new(cell_json)
|
2018-06-14 20:40:43 +08:00
|
|
|
end
|
|
|
|
repository_value.id = nil
|
2018-06-14 21:20:57 +08:00
|
|
|
repository_value.created_by_id = find_user(cell_json['created_by_id'])
|
2018-06-14 20:40:43 +08:00
|
|
|
repository_value.last_modified_by_id =
|
2018-06-14 21:20:57 +08:00
|
|
|
find_user(cell_json['last_modified_by_id'])
|
2018-06-14 20:40:43 +08:00
|
|
|
repository_value.repository_cell = repository_cell
|
2020-01-07 21:12:48 +08:00
|
|
|
repository_cell.value = repository_value
|
2018-06-14 20:40:43 +08:00
|
|
|
repository_value.save!
|
|
|
|
end
|
2018-03-06 18:26:30 +08:00
|
|
|
end
|