Commit cdd5dd38 authored by hh1966's avatar hh1966
Browse files

Import container and attachments

parent caec810c
......@@ -45,7 +45,9 @@ module Import
ActiveRecord::Base.transaction do
import_collections
import_samples
import_containers
import_attachments
end
end
......@@ -53,9 +55,21 @@ module Import
@data['Collection'].each do |uuid, fields|
# create the collection
collection = Collection.new(fields)
collection = Collection.new(fields.slice(
"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
}))
collection.save!
# add sample to collection map
# add collection to @instances map
instances!(uuid, collection)
end
end
......@@ -67,24 +81,106 @@ module Import
collection = instance('Collection', collections_sample['collection_id'])
# create the sample
sample = Sample.new(fields.except(
"molecule_id",
"sample_svg_file",
"user_id",
"fingerprint_id",
"molarity_value",
"molarity_unit",
"molecule_name_id",
"molfile_version"
))
sample = Sample.new(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"
).merge({
:created_by => @current_user_id
}))
sample.collections = [collection]
sample.save!
# add sample to instances map
# add sample to the @instances map
instances!(uuid, sample)
end
end
def import_containers
@data['Container'].each do |uuid, fields|
case fields.fetch('container_type')
when 'root', nil
# the root container was created when the containable was imported
containable_type = fields.fetch('containable_type')
containable_uuid = fields.fetch('containable_id')
containable = @instances.fetch(containable_type).fetch(containable_uuid)
container = containable.container
when 'analyses'
# get the analyses container from its parent (root) container
parent = @instances.fetch('Container').fetch(fields.fetch('parent_id'))
container = parent.children.where("container_type = 'analyses'").first()
else
# get the parent container
parent = @instances.fetch('Container').fetch(fields.fetch('parent_id'))
# create the container
container = parent.children.create(fields.slice(
"containable_type",
"name",
"container_type",
"description",
"extended_metadata",
"created_at",
"updated_at"
))
end
# in any case, add container to the @instances map
instances!(uuid, container)
end
end
def import_attachments
@data['Attachment'].each do |uuid, fields|
# look for the attachable for this attachment
attachable_type = fields.fetch('attachable_type')
attachable_uuid = fields.fetch('attachable_id')
attachable = @instances.fetch(attachable_type).fetch(attachable_uuid)
# construct file path
file_path = File.join(@directory, 'attachments', fields.fetch('filename'))
# create the attachment
attachment = attachable.attachments.create(
file_path: file_path,
created_by: @current_user_id,
created_for: @current_user_id,
bucket: attachable.id,
filename: fields.fetch('filename'),
checksum: fields.fetch('checksum'),
content_type: fields.fetch('content_type'),
created_at: fields.fetch('created_at'),
updated_at: fields.fetch('updated_at')
)
# move the attachment to the primary store
primary_store = Rails.configuration.storage.primary_store
attachment.update!(storage: primary_store)
instances!(uuid, attachment)
end
end
def instances!(uuid, instance)
class_name = instance.class.name
......
Supports Markdown
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