2016-03-09 15:21:02 +00:00
|
|
|
module Projects
|
|
|
|
module ImportExport
|
|
|
|
class ProjectTreeRestorer
|
2016-03-09 17:42:04 +00:00
|
|
|
attr_reader :project
|
2016-03-09 15:21:02 +00:00
|
|
|
|
2016-03-10 14:35:44 +00:00
|
|
|
def initialize(path:, user:)
|
2016-03-09 15:21:02 +00:00
|
|
|
@path = path
|
2016-03-09 17:42:04 +00:00
|
|
|
@user = user
|
2016-03-09 15:21:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
|
|
|
json = IO.read(@path)
|
2016-03-10 17:43:57 +00:00
|
|
|
@tree_hash = ActiveSupport::JSON.decode(json)
|
2016-04-11 16:30:54 +00:00
|
|
|
@project_members = @tree_hash.delete('project_members')
|
2016-03-10 17:43:57 +00:00
|
|
|
create_relations
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-03-11 09:25:34 +00:00
|
|
|
def members_map
|
2016-03-11 11:55:50 +00:00
|
|
|
@members ||= Projects::ImportExport::MembersMapper.map(
|
2016-04-11 16:30:54 +00:00
|
|
|
exported_members: @project_members, user: @user, project_id: project.id)
|
2016-03-10 17:43:57 +00:00
|
|
|
end
|
|
|
|
|
2016-03-14 17:32:56 +00:00
|
|
|
def create_relations(relation_list = default_relation_list, tree_hash = @tree_hash)
|
|
|
|
relation_list.each do |relation|
|
2016-04-08 16:13:19 +00:00
|
|
|
if relation.is_a?(Hash)
|
2016-04-11 16:30:54 +00:00
|
|
|
create_sub_relations(relation, tree_hash)
|
2016-03-14 17:32:56 +00:00
|
|
|
end
|
2016-04-11 16:30:54 +00:00
|
|
|
relation_key = relation.is_a?(Hash) ? relation.keys.first : relation
|
|
|
|
relation_hash = create_relation(relation_key, tree_hash[relation_key.to_s])
|
|
|
|
project.update_attribute(relation_key, relation_hash)
|
2016-04-08 16:13:19 +00:00
|
|
|
# relation_hash = nil
|
|
|
|
# # FIXME
|
|
|
|
# # next if tree_hash[relation.to_s].blank?
|
|
|
|
# if (relation.is_a?(Hash) && relation.values.first[:include])
|
|
|
|
# #TODO name stuff properly
|
|
|
|
# relation_sym = relation.keys.first
|
|
|
|
# #TODO remove sub-relation hashes from here so we can save the parent relation first
|
|
|
|
# relation_hash = create_relation(relation_sym, tree_hash[relation_sym.to_s])
|
|
|
|
# sub_relations = []
|
|
|
|
# sub_relation = relation.values.first[:include]
|
|
|
|
# sub_relation_hash_list = tree_hash[relation.keys.first.to_s]
|
|
|
|
# sub_relation_hash_list.each do |sub_relation_hash|
|
|
|
|
# sub_relations << create_relation(relation, sub_relation_hash[relation.to_s])
|
|
|
|
# end
|
|
|
|
# relation_hash.update_attribute(sub_relation, sub_relations)
|
|
|
|
# end
|
|
|
|
# relation_hash ||= create_relation(relation, tree_hash[relation.to_s])
|
|
|
|
# project.update_attribute(relation, relation_hash)
|
2016-03-14 17:32:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_relation_list
|
2016-04-11 16:30:54 +00:00
|
|
|
Projects::ImportExport::ImportExportReader.tree.reject { |model| model.is_a?(Hash) && model[:project_members] }
|
2016-03-09 15:21:02 +00:00
|
|
|
end
|
|
|
|
|
2016-03-10 17:43:57 +00:00
|
|
|
def project
|
|
|
|
@project ||= create_project
|
|
|
|
end
|
2016-03-09 15:21:02 +00:00
|
|
|
|
2016-03-10 17:43:57 +00:00
|
|
|
def create_project
|
|
|
|
project_params = @tree_hash.reject { |_key, value| value.is_a?(Array) }
|
|
|
|
project = Projects::ImportExport::ProjectFactory.create(
|
2016-03-11 11:55:50 +00:00
|
|
|
project_params: project_params, user: @user)
|
2016-03-10 15:21:17 +00:00
|
|
|
project.save
|
|
|
|
project
|
|
|
|
end
|
|
|
|
|
2016-04-11 16:30:54 +00:00
|
|
|
def create_sub_relations(relation, tree_hash)
|
|
|
|
# TODO refactor this
|
|
|
|
relation_key = relation.keys.first
|
|
|
|
tree_hash[relation_key.to_s].each do |relation_item|
|
|
|
|
relation.values.each do |sub_relation|
|
|
|
|
relation_hash = relation_item[sub_relation.to_s]
|
|
|
|
next if relation_hash.blank?
|
|
|
|
sub_relation_object = Projects::ImportExport::RelationFactory.create(
|
|
|
|
relation_sym: sub_relation, relation_hash: relation_hash, members_map: members_map)
|
|
|
|
relation_item[sub_relation.to_s] = sub_relation_object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-10 17:43:57 +00:00
|
|
|
def create_relation(relation, relation_hash_list)
|
2016-03-14 17:32:56 +00:00
|
|
|
[relation_hash_list].flatten.map do |relation_hash|
|
2016-03-09 17:42:04 +00:00
|
|
|
Projects::ImportExport::RelationFactory.create(
|
2016-03-11 17:17:57 +00:00
|
|
|
relation_sym: relation, relation_hash: relation_hash.merge('project_id' => project.id), members_map: members_map)
|
2016-03-09 17:42:04 +00:00
|
|
|
end
|
2016-03-09 15:21:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|