labimotion 1.1.4 → 1.2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/lib/labimotion/apis/generic_element_api.rb +32 -38
  3. data/lib/labimotion/collection/export.rb +45 -84
  4. data/lib/labimotion/collection/import.rb +60 -54
  5. data/lib/labimotion/entities/element_revision_entity.rb +10 -10
  6. data/lib/labimotion/entities/eln_element_entity.rb +23 -23
  7. data/lib/labimotion/entities/properties_entity.rb +18 -19
  8. data/lib/labimotion/entities/segment_revision_entity.rb +11 -11
  9. data/lib/labimotion/helpers/element_helpers.rb +14 -4
  10. data/lib/labimotion/helpers/generic_helpers.rb +9 -9
  11. data/lib/labimotion/helpers/segment_helpers.rb +1 -2
  12. data/lib/labimotion/libs/converter.rb +12 -12
  13. data/lib/labimotion/libs/export_dataset.rb +32 -6
  14. data/lib/labimotion/libs/nmr_mapper.rb +27 -27
  15. data/lib/labimotion/libs/properties_handler.rb +95 -0
  16. data/lib/labimotion/libs/sample_association.rb +15 -15
  17. data/lib/labimotion/models/concerns/generic_klass_revisions.rb +1 -1
  18. data/lib/labimotion/models/concerns/generic_revisions.rb +3 -3
  19. data/lib/labimotion/models/concerns/linked_properties.rb +18 -0
  20. data/lib/labimotion/models/concerns/segmentable.rb +17 -12
  21. data/lib/labimotion/models/element.rb +26 -17
  22. data/lib/labimotion/utils/export_utils.rb +196 -0
  23. data/lib/labimotion/utils/field_type.rb +23 -0
  24. data/lib/labimotion/utils/import_utils.rb +244 -59
  25. data/lib/labimotion/utils/prop.rb +27 -0
  26. data/lib/labimotion/utils/search.rb +2 -2
  27. data/lib/labimotion/utils/serializer.rb +22 -22
  28. data/lib/labimotion/version.rb +1 -1
  29. data/lib/labimotion.rb +4 -1
  30. metadata +7 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75c0801b117991ca8b6dc96daebfedbfdbdc7d40e31bb8c78fbc3f1089eaff91
4
- data.tar.gz: 5f67c104d53b3388549064ab3c8e7bebe5df7d39d1075c3d5c42dce5eee9e07e
3
+ metadata.gz: 7724683d3271fa68ecdf774518cc4d10a4d51ee1431c957e0538c5a00e4f689d
4
+ data.tar.gz: b9cc8b469761e732707c0b272c2e814a8215c3c22a7d7ca077d7f5ddca9ed877
5
5
  SHA512:
6
- metadata.gz: a0c036e9a1fc5f3b44130a2e7bf0038f2d0476461ca6a18994564752cf42737486c4d67ec61a0d698af9f28e92bf04c506035954d13b8dbdd2f5a67722c4b694
7
- data.tar.gz: 32721efb59f25ed9c2b5ec853987416e2073ab3612881541a9e1e79d2134076998ed18a29cec4efac778f121e6faa3e27d72f0d44e34653e01e9c79e3bebaf95
6
+ metadata.gz: dd7aaa2347333c05e09eb5e9d0628a8079e2fd4819e46b37e7a45fc40577fae12d2f731a5f1eddd601065c35aa333f312f724bd46b59d93f35f3639f8e34d53b
7
+ data.tar.gz: f222c51a55a96c1d87c1d79a1fb965b8fb6315a2b182a6767b01222eeae665de248f7bdfdc6ca3789e1f2623faf7a1b69b03bcbc1527f51f6b9c03ba99183d56
@@ -287,16 +287,12 @@ module Labimotion
287
287
  scope = list_serialized_elements(params, current_user)
288
288
 
289
289
  reset_pagination_page(scope)
290
- if Labimotion::IS_RAILS5 == true
291
- generic_elements = paginate(scope).map { |s| ElementListPermissionProxy.new(current_user, s, user_ids).serialized }
292
- else
293
- generic_elements = paginate(scope).map do |element|
294
- Labimotion::ElementEntity.represent(
295
- element,
296
- displayed_in_list: true,
297
- detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
298
- )
299
- end
290
+ generic_elements = paginate(scope).map do |element|
291
+ Labimotion::ElementEntity.represent(
292
+ element,
293
+ displayed_in_list: true,
294
+ detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
295
+ )
300
296
  end
301
297
  { generic_elements: generic_elements }
302
298
  rescue StandardError => e
@@ -324,7 +320,6 @@ module Labimotion
324
320
  attachments: attach_thumbnail(element&.attachments)
325
321
  }
326
322
  else
327
- #byebug
328
323
  {
329
324
  element: Labimotion::ElementEntity.represent(
330
325
  element,
@@ -352,19 +347,12 @@ module Labimotion
352
347
  post do
353
348
  begin
354
349
  element = create_element(current_user, params)
355
- if Labimotion::IS_RAILS5 == true
356
- {
357
- element: ElementPermissionProxy.new(current_user, element, user_ids).serialized,
358
- attachments: attach_thumbnail(element&.attachments)
359
- }
360
- else
361
- present(
362
- element,
363
- with: Labimotion::ElementEntity,
364
- root: :element,
365
- detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
366
- )
367
- end
350
+ present(
351
+ element,
352
+ with: Labimotion::ElementEntity,
353
+ root: :element,
354
+ detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
355
+ )
368
356
  rescue StandardError => e
369
357
  Labimotion.log_exception(e, current_user)
370
358
  raise e
@@ -388,26 +376,32 @@ module Labimotion
388
376
  put do
389
377
  begin
390
378
  element = update_element_by_id(current_user, params)
391
- if Labimotion::IS_RAILS5 == true
392
- {
393
- element: ElementPermissionProxy.new(current_user, element, user_ids).serialized,
394
- attachments: attach_thumbnail(element&.attachments)
395
- }
396
- else
397
- {
398
- element: Labimotion::ElementEntity.represent(
399
- element,
400
- detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
401
- ),
402
- attachments: attach_thumbnail(element&.attachments),
403
- }
404
- end
379
+ {
380
+ element: Labimotion::ElementEntity.represent(
381
+ element,
382
+ detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
383
+ ),
384
+ attachments: attach_thumbnail(element&.attachments),
385
+ }
405
386
  rescue StandardError => e
406
387
  Labimotion.log_exception(e, current_user)
407
388
  raise e
408
389
  end
409
390
  end
410
391
  end
392
+
393
+ namespace :split do
394
+ desc 'split elements'
395
+ params do
396
+ requires :ui_state, type: Hash, desc: 'Selected elements from the UI'
397
+ end
398
+ post do
399
+ split_elements(params[:ui_state], current_user)
400
+ rescue StandardError => e
401
+ Labimotion.log_exception(e, current_user)
402
+ { error: e.message }
403
+ end
404
+ end
411
405
  end
412
406
  end
413
407
  end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'labimotion/utils/export_utils'
3
4
  module Labimotion
4
5
  ## Export
5
6
  class Export
6
-
7
7
  def self.fetch_element_klasses(&fetch_many)
8
8
  klasses = Labimotion::ElementKlass.where(is_active: true)
9
9
  fetch_many.call(klasses, {'created_by' => 'User'})
10
+ rescue StandardError => e
11
+ Labimotion.log_exception(e)
10
12
  end
11
13
 
12
14
  def self.fetch_segment_klasses(&fetch_many)
@@ -15,54 +17,72 @@ module Labimotion
15
17
  'element_klass_id' => 'Labimotion::ElementKlass',
16
18
  'created_by' => 'User'
17
19
  })
20
+ rescue StandardError => e
21
+ Labimotion.log_exception(e)
18
22
  end
19
23
 
20
24
  def self.fetch_dataset_klasses(&fetch_many)
21
25
  klasses = Labimotion::DatasetKlass.where(is_active: true)
22
26
  fetch_many.call(klasses, {'created_by' => 'User'})
27
+ rescue StandardError => e
28
+ Labimotion.log_exception(e)
23
29
  end
24
30
 
25
- def self.fetch_elements(collection, segments, attachments, fetch_many, fetch_one, fetch_containers)
26
- # fetch_many.call(collection.elements, {
27
- # 'element_klass_id' => 'Labimotion::ElementKlass',
28
- # 'created_by' => 'User',
29
- # })
30
- # fetch_many.call(collection.collections_elements, {
31
- # 'collection_id' => 'Collection',
32
- # 'element_id' => 'Labimotion::Element',
33
- # })
31
+ def self.fetch_elements(collection, uuids, fetch_many, fetch_one, fetch_containers)
32
+ attachments = []
34
33
  collection.elements.each do |element|
35
- element, attachments = Labimotion::Export.fetch_properties(data, element, attachments, &fetch_one)
36
34
  fetch_one.call(element, {
37
35
  'element_klass_id' => 'Labimotion::ElementKlass',
38
36
  'created_by' => 'User',
39
37
  })
40
38
  fetch_containers.call(element)
41
- segment, @attachments = Labimotion::Export.fetch_segments(element, attachments, &fetch_one)
42
- segments += segment if segment.present?
43
39
  end
40
+ collection.elements.each do |element|
41
+ element, attachments = Labimotion::ExportUtils.fetch_properties(element, uuids, attachments, &fetch_one)
42
+ fetch_one.call(element, {
43
+ 'element_klass_id' => 'Labimotion::ElementKlass',
44
+ 'created_by' => 'User',
45
+ })
46
+ attachments = Labimotion::Export.fetch_segments(element, uuids, attachments, &fetch_one)
47
+ end
48
+ fetch_many.call(collection.collections_elements, {
49
+ 'collection_id' => 'Collection',
50
+ 'element_id' => 'Labimotion::Element',
51
+ })
44
52
 
45
- [segments, attachments]
53
+ attachments
54
+ rescue StandardError => e
55
+ Labimotion.log_exception(e)
56
+ attachments
57
+ end
46
58
 
59
+ def self.fetch_segments_prop(data, uuids)
60
+ data.fetch(Labimotion::Prop::L_SEGMENT, {}).keys.each do |key|
61
+ segment = data.fetch(Labimotion::Prop::L_SEGMENT, {})[key]
62
+ Labimotion::ExportUtils.fetch_seg_properties(segment, uuids)
63
+ data[Labimotion::Prop::L_SEGMENT][key] = segment
64
+ end
65
+ data
66
+ rescue StandardError => e
67
+ Labimotion.log_exception(e)
68
+ data
47
69
  end
48
70
 
49
- def self.fetch_segments(element, attachments, &fetch_one)
71
+ def self.fetch_segments(element, uuids, attachments, &fetch_one)
50
72
  element_type = element.class.name
51
73
  segments = Labimotion::Segment.where("element_id = ? AND element_type = ?", element.id, element_type)
52
74
  segments.each do |segment|
53
- # segment = fetch_properties(segment)
54
- segment, attachments = Labimotion::Export.fetch_properties(segment, attachments, &fetch_one)
55
- # fetch_one.call(segment.segment_klass.element_klass)
56
- # fetch_one.call(segment.segment_klass, {
57
- # 'element_klass_id' => 'Labimotion::ElementKlass'
58
- # })
75
+ segment, attachments = Labimotion::ExportUtils.fetch_properties(segment, uuids, attachments, &fetch_one)
59
76
  fetch_one.call(segment, {
60
77
  'element_id' => segment.element_type,
61
78
  'segment_klass_id' => 'Labimotion::SegmentKlass',
62
79
  'created_by' => 'User'
63
80
  })
64
81
  end
65
- [segments, attachments]
82
+ attachments
83
+ rescue StandardError => e
84
+ Labimotion.log_exception(e)
85
+ attachments
66
86
  end
67
87
 
68
88
  def self.fetch_datasets(dataset, &fetch_one)
@@ -76,68 +96,9 @@ module Labimotion
76
96
  'dataset_klass_id' => 'Labimotion::DatasetKlass',
77
97
  })
78
98
  [dataset]
79
- end
80
-
81
-
82
- def self.fetch_properties(instance, attachments, &fetch_one)
83
- properties = instance.properties
84
- properties['layers'].keys.each do |key|
85
- layer = properties['layers'][key]
86
-
87
- # field_samples = layer['fields'].select { |ss| ss['type'] == 'drag_sample' } -- TODO for elements
88
- # field_elements = layer['fields'].select { |ss| ss['type'] == 'drag_element' } -- TODO for elements
89
-
90
- field_molecules = layer['fields'].select { |ss| ss['type'] == 'drag_molecule' }
91
- field_molecules.each do |field|
92
- idx = properties['layers'][key]['fields'].index(field)
93
- id = field["value"] && field["value"]["el_id"] unless idx.nil?
94
- mol = Molecule.find(id) unless id.nil?
95
- properties['layers'][key]['fields'][idx]['value']['el_id'] = fetch_one.call(mol) unless mol.nil?
96
- end
97
-
98
- field_samples = layer['fields'].select { |ss| ss['type'] == 'drag_sample' }
99
- field_samples.each do |field|
100
- # idx = properties['layers'][key]['fields'].index(field)
101
- # id = field["value"] && field["value"]["el_id"] unless idx.nil?
102
- # ss = Sample.find(id) unless id.nil?
103
- # properties['layers'][key]['fields'][idx]['value']['el_id'] = fetch_one.call(ss) unless ss.nil?
104
- end
105
-
106
- field_uploads = layer['fields'].select { |ss| ss['type'] == 'upload' }
107
- field_uploads.each do |upload|
108
- idx = properties['layers'][key]['fields'].index(upload)
109
- files = upload["value"] && upload["value"]["files"]
110
- files&.each_with_index do |fi, fdx|
111
- att = Attachment.find(fi['aid'])
112
- attachments += [att]
113
- properties['layers'][key]['fields'][idx]['value']['files'][fdx]['aid'] = fetch_one.call(att, {'attachable_id' => 'Labimotion::Segment'}) unless att.nil?
114
- end
115
- end
116
-
117
- field_tables = properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
118
- field_tables&.each do |field|
119
- next unless field['sub_values'].present? && field['sub_fields'].present?
120
- # field_table_samples = field['sub_fields'].select { |ss| ss['type'] == 'drag_sample' } -- not available yet
121
- # field_table_uploads = field['sub_fields'].select { |ss| ss['type'] == 'upload' } -- not available yet
122
- field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
123
- if field_table_molecules.present?
124
- col_ids = field_table_molecules.map { |x| x.values[0] }
125
- col_ids.each do |col_id|
126
- field['sub_values'].each do |sub_value|
127
- next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?
128
-
129
- svalue = sub_value[col_id]['value']
130
- next unless svalue['el_id'].present? && svalue['el_inchikey'].present?
131
-
132
- tmol = Molecule.find_by(id: svalue['el_id'])
133
- sub_value[col_id]['value']['el_id'] = fetch_one.call(tmol) unless tmol.nil?
134
- end
135
- end
136
- end
137
- end
138
- end
139
- instance.properties = properties
140
- [instance, attachments]
99
+ rescue StandardError => e
100
+ Labimotion.log_exception(e)
101
+ [dataset]
141
102
  end
142
103
  end
143
104
  end
@@ -3,14 +3,13 @@
3
3
  require 'labimotion/utils/import_utils'
4
4
  module Labimotion
5
5
  class Import
6
-
7
6
  def self.import_repo_segment_props(instances, attachments, attachable_uuid, fields)
8
7
  primary_store = Rails.configuration.storage.primary_store
9
8
  attachable = instances.fetch('Labimotion::Segment').fetch(attachable_uuid)
10
9
  attachment = Attachment.where(id: attachments, filename: fields.fetch('identifier')).first
11
10
  attachment.update!(
12
11
  attachable_id: attachable.id,
13
- attachable_type: 'SegmentProps',
12
+ attachable_type: Labimotion::Prop::SEGMENTPROPS,
14
13
  con_state: Labimotion::ConState::NONE,
15
14
  transferred: true,
16
15
  aasm_state: fields.fetch('aasm_state'),
@@ -23,22 +22,25 @@ module Labimotion
23
22
  )
24
23
 
25
24
  properties = attachable.properties
26
- properties['layers'].keys.each do |key|
27
- layer = properties['layers'][key]
28
- field_uploads = layer['fields'].select { |ss| ss['type'] == 'upload' }
25
+ properties[Labimotion::Prop::LAYERS].keys.each do |key|
26
+ layer = properties[Labimotion::Prop::LAYERS][key]
27
+ field_uploads = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::UPLOAD }
29
28
  field_uploads&.each do |upload|
30
- idx = properties['layers'][key]['fields'].index(upload)
29
+ idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(upload)
31
30
  files = upload["value"] && upload["value"]["files"]
32
31
  files&.each_with_index do |fi, fdx|
33
- if properties['layers'][key]['fields'][idx]['value']['files'][fdx]['uid'] == fields.fetch('identifier')
34
- properties['layers'][key]['fields'][idx]['value']['files'][fdx]['aid'] = attachment.id
35
- properties['layers'][key]['fields'][idx]['value']['files'][fdx]['uid'] = attachment.identifier
32
+ if properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'][fdx]['uid'] == fields.fetch('identifier')
33
+ properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'][fdx]['aid'] = attachment.id
34
+ properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'][fdx]['uid'] = attachment.identifier
36
35
  end
37
36
  end
38
37
  end
39
38
  end
40
39
  attachable.update!(properties: properties)
41
40
  attachment
41
+ rescue StandardError => e
42
+ Labimotion.log_exception(e)
43
+ attachment
42
44
  end
43
45
 
44
46
  def self.import_datasets(data, instances, gt, current_user_id, &update_instances)
@@ -75,59 +77,59 @@ module Labimotion
75
77
  update_instances.call(uuid, dataset)
76
78
  end
77
79
  rescue StandardError => e
78
- Rails.logger.error(e.backtrace)
80
+ Labimotion.log_exception(e)
79
81
  raise
80
82
  end
81
83
  end
82
84
 
83
- def self.import_segments(data, instances, gt, current_user_id, &update_instances)
84
- begin
85
- data.fetch('Labimotion::Segment', {}).each do |uuid, fields|
86
- klass_id = fields["segment_klass_id"]
87
- sk_obj = data.fetch('Labimotion::SegmentKlass', {})[klass_id]
88
- sk_id = sk_obj["identifier"]
89
- ek_obj = data.fetch('Labimotion::ElementKlass').fetch(sk_obj["element_klass_id"])
90
- element_klass = Labimotion::ElementKlass.find_by(name: ek_obj['name']) if ek_obj.present?
91
- next if element_klass.nil? || ek_obj.nil? || ek_obj['is_generic'] == true
92
-
93
- element_uuid = fields.fetch('element_id')
94
- element_type = fields.fetch('element_type')
95
- element = instances.fetch(element_type).fetch(element_uuid)
96
- segment_klass = Labimotion::SegmentKlass.find_by(identifier: sk_id) if sk_id.present?
97
- segment_klass = Labimotion::SegmentKlass.find_by(uuid: fields.fetch('klass_uuid')) if segment_klass.nil?
98
-
99
- if segment_klass.nil?
100
- skr = Labimotion::SegmentKlassesRevision.find_by(uuid: fields.fetch('klass_uuid'))
101
- segment_klass = skr.segment_klass if segment_klass.nil? && skr.present?
102
- end
85
+ def self.import_segments(data, instances, attachments, gt, current_user_id, &update_instances)
86
+ data.fetch(Labimotion::Prop::L_SEGMENT, {}).each do |uuid, fields|
87
+ klass_id = fields["segment_klass_id"]
88
+ sk_obj = data.fetch(Labimotion::Prop::L_SEGMENT_KLASS, {})[klass_id]
89
+ sk_id = sk_obj["identifier"]
90
+ ek_obj = data.fetch(Labimotion::Prop::L_ELEMENT_KLASS).fetch(sk_obj["element_klass_id"])
91
+ element_klass = Labimotion::ElementKlass.find_by(name: ek_obj['name']) if ek_obj.present?
92
+ next if element_klass.nil? || ek_obj.nil? || ek_obj['is_generic'] == true
93
+
94
+ element_uuid = fields.fetch('element_id')
95
+ element_type = fields.fetch('element_type')
96
+ element = instances.fetch(element_type).fetch(element_uuid)
97
+ segment_klass = Labimotion::SegmentKlass.find_by(identifier: sk_id) if sk_id.present?
98
+ segment_klass = Labimotion::SegmentKlass.find_by(uuid: fields.fetch('klass_uuid')) if segment_klass.nil?
99
+
100
+ if segment_klass.nil?
101
+ skr = Labimotion::SegmentKlassesRevision.find_by(uuid: fields.fetch('klass_uuid'))
102
+ segment_klass = skr.segment_klass if segment_klass.nil? && skr.present?
103
+ end
103
104
 
104
- next if segment_klass.nil? || element.nil?
105
+ next if segment_klass.nil? || element.nil?
105
106
 
106
- ## segment_klass = Labimotion::ImportUtils.create_segment_klass(sk_obj, segment_klass, element_klass, current_user_id)
107
+ ## segment_klass = Labimotion::ImportUtils.create_segment_klass(sk_obj, segment_klass, element_klass, current_user_id)
107
108
 
108
- segment = Labimotion::Segment.create!(
109
- fields.slice(
110
- 'properties', 'properties_release'
111
- ).merge(
112
- created_by: current_user_id,
113
- element: element,
114
- segment_klass: segment_klass,
115
- uuid: SecureRandom.uuid,
116
- klass_uuid: skr&.uuid || segment_klass.uuid
117
- )
109
+ segment = Labimotion::Segment.create!(
110
+ fields.slice(
111
+ 'properties', 'properties_release'
112
+ ).merge(
113
+ created_by: current_user_id,
114
+ element: element,
115
+ segment_klass: segment_klass,
116
+ uuid: SecureRandom.uuid,
117
+ klass_uuid: skr&.uuid || segment_klass.uuid
118
118
  )
119
-
120
- properties = Labimotion::ImportUtils.properties_handler(data, segment.properties)
121
- segment.update!(properties: properties)
122
- update_instances.call(uuid, segment)
123
- end
124
- rescue StandardError => e
125
- Rails.logger.error(e.backtrace)
126
- raise
119
+ )
120
+ properties = Labimotion::ImportUtils.properties_handler(data, instances, attachments, segment, nil)
121
+ segment.update!(properties: properties)
122
+ update_instances.call(uuid, segment)
127
123
  end
124
+
125
+ Labimotion::ImportUtils.process_ai(data, instances)
126
+ rescue StandardError => e
127
+ Labimotion.log_exception(e)
128
+ raise
128
129
  end
129
130
 
130
- def self.import_elements(data, instances, gt, current_user_id, fetch_many, &update_instances)
131
+ def self.import_elements(data, instances, attachments, gt, current_user_id, fetch_many, &update_instances)
132
+ elements = {}
131
133
  data.fetch('Labimotion::Element', {}).each do |uuid, fields|
132
134
  klass_id = fields["element_klass_id"]
133
135
  ek_obj = data.fetch('Labimotion::ElementKlass', {})[klass_id]
@@ -154,14 +156,18 @@ module Labimotion
154
156
  klass_uuid: ekr&.uuid || element_klass.uuid
155
157
  )
156
158
  )
157
-
158
- properties = Labimotion::ImportUtils.properties_handler(data, element.properties)
159
+ elements[uuid] = element
160
+ end
161
+ elements.keys.each do |uuid, element|
162
+ element = elements[uuid]
163
+ properties = Labimotion::ImportUtils.properties_handler(data, instances, attachments, element, elements)
159
164
  element.update!(properties: properties)
160
165
  update_instances.call(uuid, element)
161
166
  element.container = Container.create_root_container
167
+ element.save!
162
168
  end
163
169
  rescue StandardError => e
164
- Rails.logger.error(e.backtrace)
170
+ Labimotion.log_exception(e)
165
171
  raise
166
172
  end
167
173
  end
@@ -10,27 +10,27 @@ module Labimotion
10
10
  end
11
11
 
12
12
  def properties
13
- object.properties['layers']&.keys.each do |key|
14
- field_sample_molecules = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'drag_sample' || ss['type'] == 'drag_molecule' }
13
+ object.properties[Labimotion::Prop::LAYERS]&.keys.each do |key|
14
+ field_sample_molecules = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE || ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
15
15
  field_sample_molecules.each do |field|
16
- idx = object.properties['layers'][key]['fields'].index(field)
16
+ idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
17
17
  sid = field.dig('value', 'el_id')
18
18
  next unless sid.present?
19
19
 
20
- el = field['type'] == 'drag_sample' ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
20
+ el = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
21
21
  next unless el.present?
22
- next unless object.properties.dig('layers', key, 'fields', idx, 'value').present?
22
+ next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
23
23
 
24
- object.properties['layers'][key]['fields'][idx]['value']['el_label'] = el.short_label if field['type'] == 'drag_sample'
25
- object.properties['layers'][key]['fields'][idx]['value']['el_tip'] = el.short_label if field['type'] == 'drag_sample'
26
- object.properties['layers'][key]['fields'][idx]['value']['el_svg'] = field['type'] == 'drag_sample' ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
24
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
25
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
26
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
27
27
  end
28
28
 
29
- field_tables = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
29
+ field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
30
30
  field_tables.each do |field|
31
31
  next unless field['sub_values'].present? && field['sub_fields'].present?
32
32
 
33
- field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
33
+ field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
34
34
  next unless field_table_molecules.present?
35
35
 
36
36
  col_ids = field_table_molecules.map { |x| x.values[0] }
@@ -24,47 +24,47 @@ module Labimotion
24
24
  private
25
25
 
26
26
  def properties
27
- (object.properties['layers']&.keys || []).each do |key|
27
+ (object.properties[Labimotion::Prop::LAYERS]&.keys || []).each do |key|
28
28
  # layer = object.properties[key]
29
- field_sample_molecules = object.properties['layers'][key]['fields'].select do |ss|
30
- ss['type'] == 'drag_sample' || ss['type'] == 'drag_molecule'
29
+ field_sample_molecules = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select do |ss|
30
+ ss['type'] == Labimotion::FieldType::DRAG_SAMPLE || ss['type'] == Labimotion::FieldType::DRAG_MOLECULE
31
31
  end
32
32
  field_sample_molecules.each do |field|
33
- idx = object.properties['layers'][key]['fields'].index(field)
33
+ idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
34
34
  sid = field.dig('value', 'el_id')
35
35
  next unless sid.present?
36
36
 
37
- el = field['type'] == 'drag_sample' ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
37
+ el = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
38
38
  next unless el.present?
39
- next unless object.properties.dig('layers', key, 'fields', idx, 'value').present?
39
+ next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
40
40
 
41
- if field['type'] == 'drag_sample'
42
- object.properties['layers'][key]['fields'][idx]['value']['el_label'] =
41
+ if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
42
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] =
43
43
  el.short_label
44
44
  end
45
- if field['type'] == 'drag_sample'
46
- object.properties['layers'][key]['fields'][idx]['value']['el_tip'] =
45
+ if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
46
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] =
47
47
  el.short_label
48
48
  end
49
- object.properties['layers'][key]['fields'][idx]['value']['el_svg'] =
50
- field['type'] == 'drag_sample' ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
49
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] =
50
+ field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
51
51
  end
52
52
 
53
- field_tables = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
53
+ field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
54
54
  field_tables.each do |field|
55
- idx = object.properties['layers'][key]['fields'].index(field)
56
- next unless field['sub_values'].present? && field['sub_fields'].present?
55
+ idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
56
+ next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?
57
57
 
58
- field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
58
+ field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
59
59
  if field_table_molecules.present?
60
- object.properties['layers'][key]['fields'][idx] =
61
- set_table(field, field_table_molecules, 'Molecule')
60
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] =
61
+ set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE)
62
62
  end
63
63
 
64
- field_table_samples = field['sub_fields'].select { |ss| ss['type'] == 'drag_sample' }
64
+ field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
65
65
  if field_table_samples.present?
66
- object.properties['layers'][key]['fields'][idx] =
67
- set_table(field, field_table_samples, 'Sample')
66
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] =
67
+ set_table(field, field_table_samples, Labimotion::Prop::SAMPLE)
68
68
  end
69
69
  end
70
70
  end
@@ -87,13 +87,13 @@ module Labimotion
87
87
  next unless find_obj.present?
88
88
 
89
89
  case obj
90
- when 'Molecule'
90
+ when Labimotion::Prop::MOLECULE
91
91
  sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file)
92
92
  sub_value[col_id]['value']['el_inchikey'] = find_obj.inchikey
93
93
  sub_value[col_id]['value']['el_smiles'] = find_obj.cano_smiles
94
94
  sub_value[col_id]['value']['el_iupac'] = find_obj.iupac_name
95
95
  sub_value[col_id]['value']['el_molecular_weight'] = find_obj.molecular_weight
96
- when 'Sample'
96
+ when Labimotion::Prop::SAMPLE
97
97
  sub_value[col_id]['value']['el_svg'] = find_obj.get_svg_path
98
98
  sub_value[col_id]['value']['el_label'] = find_obj.short_label
99
99
  sub_value[col_id]['value']['el_short_label'] = find_obj.short_label