Commit 676530e3 authored by hh1966's avatar hh1966
Browse files

Remove table_name from Export/Import logic

parent 5bfacf9b
......@@ -21,12 +21,12 @@ module Export
# fetch samples
fetch_many(collection.samples, {
:molecule_name_id => 'molecule_names',
:molecule_id => 'molecules',
:molecule_name_id => 'MoleculeName',
:molecule_id => 'Molecule',
})
fetch_many(collection.collections_samples, {
:collection_id => 'collections',
:sample_id => 'samples',
:collection_id => 'Collection',
:sample_id => 'Sample',
})
# loop over samples and fetch sample properties
......@@ -34,14 +34,14 @@ module Export
fetch_one(sample.fingerprint)
fetch_one(sample.molecule)
fetch_one(sample.molecule_name, {
:molecule_id => 'molecules',
:molecule_id => 'Molecule',
})
fetch_one(sample.well, {
:sample_id => 'samples',
:wellplate_id => 'wellplates',
:sample_id => 'Sample',
:wellplate_id => 'Wellplate',
})
fetch_many(sample.residues, {
:sample_id => 'samples',
:sample_id => 'Sample',
})
# fetch containers, attachments and literature
......@@ -52,8 +52,8 @@ module Export
# fetch reactions
fetch_many(collection.reactions)
fetch_many(collection.collections_reactions, {
:collection_id => 'collections',
:reaction_id => 'reactions',
:collection_id => 'Collection',
:reaction_id => 'Reaction',
})
# loop over reactions and fetch reaction properties
......@@ -68,8 +68,8 @@ module Export
reaction.reactions_product_samples,
].each do |instances|
fetch_many(instances, {
:reaction_id => 'reactions',
:sample_id => 'samples',
:reaction_id => 'Reaction',
:sample_id => 'Sample',
})
end
......@@ -81,8 +81,8 @@ module Export
# fetch wellplates
fetch_many(collection.wellplates)
fetch_many(collection.collections_wellplates, {
:collection_id => 'collections',
:wellplate_id => 'wellplates',
:collection_id => 'Collection',
:wellplate_id => 'Wellplate',
})
# fetch containers and attachments
......@@ -93,16 +93,16 @@ module Export
# fetch screens
fetch_many(collection.screens)
fetch_many(collection.collections_screens, {
:collection_id => 'collections',
:screen_id => 'screens',
:collection_id => 'Collection',
:screen_id => 'Screen',
})
# loop over screens and fetch screen properties
collection.screens.each do |screen|
# fetch relation between wellplates_screens and screen
fetch_many(screen.screens_wellplates, {
:screen_id => 'screens',
:wellplate_id => 'wellplates',
:screen_id => 'Screen',
:wellplate_id => 'Wellplate',
})
# fetch containers and attachments
......@@ -112,8 +112,8 @@ module Export
# fetch research_plans
fetch_many(collection.research_plans)
fetch_many(collection.collections_research_plans, {
:collection_id => 'collections',
:research_plan_id => 'research_plans',
:collection_id => 'Collection',
:research_plan_id => 'ResearchPlan',
})
# loop over research plans and fetch research plan properties
......@@ -121,7 +121,7 @@ module Export
# fetch attachments
# attachments are directrly related to research plans so we don't need fetch_containers
fetch_many(research_plan.attachments, {
:attachable_id => 'research_plans',
:attachable_id => 'ResearchPlan',
})
# add attachments to the list of attachments
......@@ -141,39 +141,39 @@ module Export
end
def fetch_containers(containable)
containable_table = containable.class.table_name
containable_type = containable.class.name
# fetch root container
root_container = containable.container
fetch_one(containable.container, {
:containable_id => containable_table,
:parent_id => 'containers',
:containable_id => containable_type,
:parent_id => 'Container',
})
# fetch analyses container
analyses_container = root_container.children.where("container_type = 'analyses'").first()
fetch_one(analyses_container, {
:containable_id => containable_table,
:parent_id => 'containers',
:containable_id => containable_type,
:parent_id => 'Container',
})
# fetch analysis_containers
analysis_containers = analyses_container.children.where("container_type = 'analysis'")
analysis_containers.each do |analysis_container|
fetch_one(analysis_container, {
:containable_id => containable_table,
:parent_id => 'containers',
:containable_id => containable_type,
:parent_id => 'Container',
})
# fetch attachment containers and attachments
attachment_containers = analysis_container.children.where("container_type = 'dataset'")
attachment_containers.each do |attachment_container|
fetch_one(attachment_container, {
:containable_id => containable_table,
:parent_id => 'containers',
:containable_id => containable_type,
:parent_id => 'Container',
})
fetch_many(attachment_container.attachments, {
:attachable_id => 'containers',
:attachable_id => 'Container',
})
# add attachments to the list of attachments
......@@ -184,15 +184,14 @@ module Export
def fetch_literals(element)
element_type = element.class.name
element_table = element.class.table_name
# a manual query needed since there is no Active Record Associations available
literals = Literal.where("element_id = ? AND element_type = ?", element.id, element_type)
literals.each do |literal|
fetch_one(literal.literature)
fetch_one(literal, {
:literature_id => 'literatures',
:element_id => element_table,
:literature_id => 'Literature',
:element_id => element_type,
})
end
end
......@@ -205,14 +204,13 @@ module Export
def fetch_one(instance, foreign_keys = {})
unless instance.nil?
# get the table_name from the class
# IMPORTANT: these are strings, not symbols
class_name = instance.class.name
table_name = instance.class.table_name
# get the type from the class
# IMPORTANT: this is a strings, not a symbol
type = instance.class.name
# get the uuid and only continue it does not exist yet
uuid = uuid(table_name, instance.id)
unless uuid?(table_name, uuid)
uuid = uuid(type, instance.id)
unless uuid?(type, uuid)
# replace id and foreign_keys with uuids
update = {}
foreign_keys.each do |foreign_key, foreign_table|
......@@ -220,35 +218,35 @@ module Export
end
# append updated json to @data
unless @data.key?(class_name)
@data[class_name] = {}
unless @data.key?(type)
@data[type] = {}
end
@data[class_name][uuid] = instance.as_json().except('id').merge(update)
@data[type][uuid] = instance.as_json().except('id').merge(update)
end
end
end
def uuid(table_name, id)
def uuid(type, id)
unless id.nil?
# create an empty hash for the table_name if it does not exist yet
unless @uuids.key?(table_name)
@uuids[table_name] = {}
# create an empty hash for the type if it does not exist yet
unless @uuids.key?(type)
@uuids[type] = {}
end
# create an uuid for the id if it does not exist yet
unless @uuids[table_name].key?(id)
@uuids[table_name][id] = SecureRandom.uuid
unless @uuids[type].key?(id)
@uuids[type][id] = SecureRandom.uuid
end
@uuids[table_name][id]
@uuids[type][id]
else
nil # return nil
end
end
def uuid?(table_name, id)
@uuids.key?(table_name) and @uuids[table_name].key?(id)
def uuid?(type, id)
@uuids.key?(type) and @uuids[type].key?(id)
end
end
......
......@@ -55,17 +55,17 @@ module Import
end
def import_collections
@data['Collection'].each do |uuid, fields|
@data.fetch('Collection', []).each do |uuid, fields|
# create the collection
collection = Collection.create!(fields.slice(
"label",
"sample_detail_level",
"reaction_detail_level",
"wellplate_detail_level",
"screen_detail_level",
"researchplan_detail_level",
"created_at",
"updated_at"
'label',
'sample_detail_level',
'reaction_detail_level',
'wellplate_detail_level',
'screen_detail_level',
'researchplan_detail_level',
'created_at',
'updated_at'
).merge({
:user_id => @current_user_id
}))
......@@ -76,37 +76,37 @@ module Import
end
def import_samples
@data['Sample'].each do |uuid, fields|
@data.fetch('Sample', []).each do |uuid, fields|
# get the collection for this sample
collections_sample = find_association('CollectionsSample', 'sample_id', uuid)
collections_sample = fetch_association('CollectionsSample', 'sample_id', uuid)
collection_uuid = collections_sample.fetch('collection_id')
collection = @instances.fetch('Collection').fetch(collection_uuid)
# create the sample
sample = Sample.create!(fields.slice(
"name",
"target_amount_value",
"target_amount_unit",
"description",
"molfile",
"molfile_version",
"purity",
"solvent",
"impurities",
"location",
"is_top_secret",
"external_label",
"real_amount_value",
"real_amount_unit",
"imported_readout",
"identifier",
"density",
"melting_point",
"boiling_point",
"xref",
"stereo",
"created_at",
"updated_at"
'name',
'target_amount_value',
'target_amount_unit',
'description',
'molfile',
'molfile_version',
'purity',
'solvent',
'impurities',
'location',
'is_top_secret',
'external_label',
'real_amount_value',
'real_amount_unit',
'imported_readout',
'identifier',
'density',
'melting_point',
'boiling_point',
'xref',
'stereo',
'created_at',
'updated_at'
).merge({
:created_by => @current_user_id,
:collections => [collection]
......@@ -118,33 +118,33 @@ module Import
end
def import_reactions
@data['Reaction'].each do |uuid, fields|
@data.fetch('Reaction', []).each do |uuid, fields|
# get the collection for this reaction
collections_reaction = find_association('CollectionsReaction', 'reaction_id', uuid)
collections_reaction = fetch_association('CollectionsReaction', 'reaction_id', uuid)
collection_uuid = collections_reaction.fetch('collection_id')
collection = @instances.fetch('Collection').fetch(collection_uuid)
# create the sample
reaction = Reaction.create!(fields.slice(
"name",
"description",
"timestamp_start",
"timestamp_stop",
"observation",
"purification",
"dangerous_products",
"tlc_solvents",
"tlc_description",
"rf_value",
"temperature",
"status",
"solvent",
# "short_label",
"role",
"origin",
"duration",
"created_at",
"updated_at"
'name',
'description',
'timestamp_start',
'timestamp_stop',
'observation',
'purification',
'dangerous_products',
'tlc_solvents',
'tlc_description',
'rf_value',
'temperature',
'status',
'solvent',
# 'short_label',
'role',
'origin',
'duration',
'created_at',
'updated_at'
).merge({
:created_by => @current_user_id,
:collections => [collection]
......@@ -160,7 +160,7 @@ module Import
end
def import_containers
@data['Container'].each do |uuid, fields|
@data.fetch('Container', []).each do |uuid, fields|
case fields.fetch('container_type')
when 'root', nil
# the root container was created when the containable was imported
......@@ -178,13 +178,13 @@ module Import
# create the container
container = parent.children.create!(fields.slice(
"containable_type",
"name",
"container_type",
"description",
"extended_metadata",
"created_at",
"updated_at"
'containable_type',
'name',
'container_type',
'description',
'extended_metadata',
'created_at',
'updated_at'
))
end
......@@ -194,7 +194,7 @@ module Import
end
def import_attachments
@data['Attachment'].each do |uuid, fields|
@data.fetch('Attachment', []).each do |uuid, fields|
# get the attachable for this attachment
attachable_type = fields.fetch('attachable_type')
attachable_uuid = fields.fetch('attachable_id')
......@@ -226,7 +226,7 @@ module Import
end
def import_literature
@data['Literal'].each do |uuid, fields|
@data.fetch('Literal', []).each do |uuid, fields|
# get the element for this literal
element_type = fields.fetch('element_type')
element_uuid = fields.fetch('element_id')
......@@ -242,12 +242,12 @@ module Import
rescue KeyError => ex
# create the literature
literature = Literature.create!(literature_fields.slice(
"title",
"url",
"refs",
"doi",
"created_at",
"updated_at"
'title',
'url',
'refs',
'doi',
'created_at',
'updated_at'
))
# add literature to the @instances map
......@@ -257,10 +257,10 @@ module Import
# create the literal
literal = Literal.new(
fields.slice(
"element_type",
"category",
"created_at",
"updated_at"
'element_type',
'category',
'created_at',
'updated_at'
).merge({
:user_id => @current_user_id,
:element => element,
......@@ -274,18 +274,18 @@ module Import
end
def update_instances!(uuid, instance)
class_name = instance.class.name
type = instance.class.name
unless @instances.key?(class_name)
@instances[class_name] = {}
unless @instances.key?(type)
@instances[type] = {}
end
@instances[class_name][uuid] = instance
@instances[type][uuid] = instance
end
def find_association(association_model, id_field, id)
@data[association_model].each do |uuid, fields|
if fields[id_field] == id
def fetch_association(association_type, foreign_key, id)
@data.fetch(association_type).each do |uuid, fields|
if fields.fetch(foreign_key) == id
return fields
end
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment