Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
hh1966
chemotion_eln_server
Commits
676530e3
Commit
676530e3
authored
Mar 12, 2019
by
hh1966
Browse files
Remove table_name from Export/Import logic
parent
5bfacf9b
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/export/export_collections.rb
View file @
676530e3
...
...
@@ -21,12 +21,12 @@ module Export
# fetch samples
fetch_many
(
collection
.
samples
,
{
:molecule_name_id
=>
'
m
olecule
_n
ame
s
'
,
:molecule_id
=>
'
m
olecule
s
'
,
:molecule_name_id
=>
'
M
olecule
N
ame'
,
:molecule_id
=>
'
M
olecule'
,
})
fetch_many
(
collection
.
collections_samples
,
{
:collection_id
=>
'
c
ollection
s
'
,
:sample_id
=>
'
s
ample
s
'
,
:collection_id
=>
'
C
ollection'
,
:sample_id
=>
'
S
ample'
,
})
# 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
=>
'
m
olecule
s
'
,
:molecule_id
=>
'
M
olecule'
,
})
fetch_one
(
sample
.
well
,
{
:sample_id
=>
'
s
ample
s
'
,
:wellplate_id
=>
'
w
ellplate
s
'
,
:sample_id
=>
'
S
ample'
,
:wellplate_id
=>
'
W
ellplate'
,
})
fetch_many
(
sample
.
residues
,
{
:sample_id
=>
'
s
ample
s
'
,
:sample_id
=>
'
S
ample'
,
})
# fetch containers, attachments and literature
...
...
@@ -52,8 +52,8 @@ module Export
# fetch reactions
fetch_many
(
collection
.
reactions
)
fetch_many
(
collection
.
collections_reactions
,
{
:collection_id
=>
'
c
ollection
s
'
,
:reaction_id
=>
'
r
eaction
s
'
,
:collection_id
=>
'
C
ollection'
,
:reaction_id
=>
'
R
eaction'
,
})
# 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
=>
'
r
eaction
s
'
,
:sample_id
=>
'
s
ample
s
'
,
:reaction_id
=>
'
R
eaction'
,
:sample_id
=>
'
S
ample'
,
})
end
...
...
@@ -81,8 +81,8 @@ module Export
# fetch wellplates
fetch_many
(
collection
.
wellplates
)
fetch_many
(
collection
.
collections_wellplates
,
{
:collection_id
=>
'
c
ollection
s
'
,
:wellplate_id
=>
'
w
ellplate
s
'
,
:collection_id
=>
'
C
ollection'
,
:wellplate_id
=>
'
W
ellplate'
,
})
# fetch containers and attachments
...
...
@@ -93,16 +93,16 @@ module Export
# fetch screens
fetch_many
(
collection
.
screens
)
fetch_many
(
collection
.
collections_screens
,
{
:collection_id
=>
'
c
ollection
s
'
,
:screen_id
=>
'
s
creen
s
'
,
:collection_id
=>
'
C
ollection'
,
:screen_id
=>
'
S
creen'
,
})
# 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
=>
'
s
creen
s
'
,
:wellplate_id
=>
'
w
ellplate
s
'
,
:screen_id
=>
'
S
creen'
,
:wellplate_id
=>
'
W
ellplate'
,
})
# 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
=>
'
c
ollection
s
'
,
:research_plan_id
=>
'
r
esearch
_p
lan
s
'
,
:collection_id
=>
'
C
ollection'
,
:research_plan_id
=>
'
R
esearch
P
lan'
,
})
# 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
=>
'
r
esearch
_p
lan
s
'
,
:attachable_id
=>
'
R
esearch
P
lan'
,
})
# add attachments to the list of attachments
...
...
@@ -141,39 +141,39 @@ module Export
end
def
fetch_containers
(
containable
)
containable_t
abl
e
=
containable
.
class
.
table_
name
containable_t
yp
e
=
containable
.
class
.
name
# fetch root container
root_container
=
containable
.
container
fetch_one
(
containable
.
container
,
{
:containable_id
=>
containable_t
abl
e
,
:parent_id
=>
'
c
ontainer
s
'
,
:containable_id
=>
containable_t
yp
e
,
:parent_id
=>
'
C
ontainer'
,
})
# fetch analyses container
analyses_container
=
root_container
.
children
.
where
(
"container_type = 'analyses'"
).
first
()
fetch_one
(
analyses_container
,
{
:containable_id
=>
containable_t
abl
e
,
:parent_id
=>
'
c
ontainer
s
'
,
:containable_id
=>
containable_t
yp
e
,
:parent_id
=>
'
C
ontainer'
,
})
# 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_t
abl
e
,
:parent_id
=>
'
c
ontainer
s
'
,
:containable_id
=>
containable_t
yp
e
,
:parent_id
=>
'
C
ontainer'
,
})
# 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_t
abl
e
,
:parent_id
=>
'
c
ontainer
s
'
,
:containable_id
=>
containable_t
yp
e
,
:parent_id
=>
'
C
ontainer'
,
})
fetch_many
(
attachment_container
.
attachments
,
{
:attachable_id
=>
'
c
ontainer
s
'
,
:attachable_id
=>
'
C
ontainer'
,
})
# 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
=>
'
l
iterature
s
'
,
:element_id
=>
element_t
abl
e
,
:literature_id
=>
'
L
iterature'
,
:element_id
=>
element_t
yp
e
,
})
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
(
t
able_nam
e
,
instance
.
id
)
unless
uuid?
(
t
able_nam
e
,
uuid
)
uuid
=
uuid
(
t
yp
e
,
instance
.
id
)
unless
uuid?
(
t
yp
e
,
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_nam
e
)
@data
[
class_nam
e
]
=
{}
unless
@data
.
key?
(
typ
e
)
@data
[
typ
e
]
=
{}
end
@data
[
class_nam
e
][
uuid
]
=
instance
.
as_json
().
except
(
'id'
).
merge
(
update
)
@data
[
typ
e
][
uuid
]
=
instance
.
as_json
().
except
(
'id'
).
merge
(
update
)
end
end
end
def
uuid
(
t
able_nam
e
,
id
)
def
uuid
(
t
yp
e
,
id
)
unless
id
.
nil?
# create an empty hash for the t
able_nam
e if it does not exist yet
unless
@uuids
.
key?
(
t
able_nam
e
)
@uuids
[
t
able_nam
e
]
=
{}
# create an empty hash for the t
yp
e if it does not exist yet
unless
@uuids
.
key?
(
t
yp
e
)
@uuids
[
t
yp
e
]
=
{}
end
# create an uuid for the id if it does not exist yet
unless
@uuids
[
t
able_nam
e
].
key?
(
id
)
@uuids
[
t
able_nam
e
][
id
]
=
SecureRandom
.
uuid
unless
@uuids
[
t
yp
e
].
key?
(
id
)
@uuids
[
t
yp
e
][
id
]
=
SecureRandom
.
uuid
end
@uuids
[
t
able_nam
e
][
id
]
@uuids
[
t
yp
e
][
id
]
else
nil
# return nil
end
end
def
uuid?
(
t
able_nam
e
,
id
)
@uuids
.
key?
(
t
able_nam
e
)
and
@uuids
[
t
able_nam
e
].
key?
(
id
)
def
uuid?
(
t
yp
e
,
id
)
@uuids
.
key?
(
t
yp
e
)
and
@uuids
[
t
yp
e
].
key?
(
id
)
end
end
...
...
lib/import/import_collections.rb
View file @
676530e3
...
...
@@ -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
=
f
ind
_association
(
'CollectionsSample'
,
'sample_id'
,
uuid
)
collections_sample
=
f
etch
_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
=
f
ind
_association
(
'CollectionsReaction'
,
'reaction_id'
,
uuid
)
collections_reaction
=
f
etch
_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_nam
e
=
instance
.
class
.
name
typ
e
=
instance
.
class
.
name
unless
@instances
.
key?
(
class_nam
e
)
@instances
[
class_nam
e
]
=
{}
unless
@instances
.
key?
(
typ
e
)
@instances
[
typ
e
]
=
{}
end
@instances
[
class_nam
e
][
uuid
]
=
instance
@instances
[
typ
e
][
uuid
]
=
instance
end
def
f
ind
_association
(
association_
model
,
id_field
,
id
)
@data
[
association_
model
]
.
each
do
|
uuid
,
fields
|
if
fields
[
id_field
]
==
id
def
f
etch
_association
(
association_
type
,
foreign_key
,
id
)
@data
.
fetch
(
association_
type
)
.
each
do
|
uuid
,
fields
|
if
fields
.
fetch
(
foreign_key
)
==
id
return
fields
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment