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
@@ -6,39 +6,38 @@ module Labimotion
6
6
 
7
7
  # TODO: Refactor this method to something more readable/understandable
8
8
  def properties
9
- (object&.properties.is_a?(Hash) && object.properties['layers']&.keys || []).each do |key|
9
+ (object&.properties.is_a?(Hash) && object.properties[Labimotion::Prop::LAYERS]&.keys || []).each do |key|
10
10
  # layer = object.properties[key]
11
- field_sample_molecules = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'drag_sample' || ss['type'] == 'drag_molecule' }
11
+ 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 }
12
12
  field_sample_molecules.each do |field|
13
- idx = object.properties['layers'][key]['fields'].index(field)
13
+ idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
14
14
  sid = field.dig('value', 'el_id')
15
15
  next unless sid.present?
16
16
 
17
- el = field['type'] == 'drag_sample' ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
17
+ el = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
18
18
  next unless el.present?
19
- next unless object.properties.dig('layers', key, 'fields', idx, 'value').present?
19
+ next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
20
20
 
21
- object.properties['layers'][key]['fields'][idx]['value']['el_label'] = el.short_label if field['type'] == 'drag_sample'
22
- object.properties['layers'][key]['fields'][idx]['value']['el_tip'] = el.short_label if field['type'] == 'drag_sample'
23
- 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)
21
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
22
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
23
+ 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)
24
24
  end
25
25
 
26
- field_tables = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
26
+ field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
27
27
  field_tables.each do |field|
28
- idx = object.properties['layers'][key]['fields'].index(field)
29
- next unless field['sub_values'].present? && field['sub_fields'].present?
28
+ idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
29
+ next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?
30
30
 
31
- field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
32
- object.properties['layers'][key]['fields'][idx] = set_table(field, field_table_molecules, 'Molecule') if field_table_molecules.present?
31
+ field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
32
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE) if field_table_molecules.present?
33
33
 
34
- field_table_samples = field['sub_fields'].select { |ss| ss['type'] == 'drag_sample' }
35
- object.properties['layers'][key]['fields'][idx] = set_table(field, field_table_samples, 'Sample') if field_table_samples.present?
34
+ field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
35
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_samples, Labimotion::Prop::SAMPLE) if field_table_samples.present?
36
36
  end
37
37
  end
38
38
  object.properties
39
39
  end
40
40
 
41
-
42
41
  def set_table(field, field_table_objs, obj)
43
42
  col_ids = field_table_objs.map { |x| x.values[0] }
44
43
  col_ids.each do |col_id|
@@ -46,16 +45,16 @@ module Labimotion
46
45
  next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?
47
46
 
48
47
  find_obj = obj.constantize.find_by(id: sub_value[col_id]['value']['el_id'])
49
- next unless find_obj.present?
48
+ next if find_obj.blank?
50
49
 
51
50
  case obj
52
- when 'Molecule'
51
+ when Labimotion::Prop::MOLECULE
53
52
  sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file)
54
53
  sub_value[col_id]['value']['el_inchikey'] = find_obj.inchikey
55
54
  sub_value[col_id]['value']['el_smiles'] = find_obj.cano_smiles
56
55
  sub_value[col_id]['value']['el_iupac'] = find_obj.iupac_name
57
56
  sub_value[col_id]['value']['el_molecular_weight'] = find_obj.molecular_weight
58
- when 'Sample'
57
+ when Labimotion::Prop::SAMPLE
59
58
  sub_value[col_id]['value']['el_svg'] = find_obj.get_svg_path
60
59
  sub_value[col_id]['value']['el_label'] = find_obj.short_label
61
60
  sub_value[col_id]['value']['el_short_label'] = find_obj.short_label
@@ -9,27 +9,27 @@ module Labimotion
9
9
  end
10
10
 
11
11
  def properties
12
- object.properties['layers']&.keys.each do |key|
13
- field_sample_molecules = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'drag_sample' || ss['type'] == 'drag_molecule' }
12
+ object.properties[Labimotion::Prop::LAYERS]&.keys.each do |key|
13
+ 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 }
14
14
  field_sample_molecules.each do |field|
15
- idx = object.properties['layers'][key]['fields'].index(field)
15
+ idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
16
16
  sid = field.dig('value', 'el_id')
17
17
  next unless sid.present?
18
18
 
19
- el = field['type'] == 'drag_sample' ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
19
+ el = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
20
20
  next unless el.present?
21
- next unless object.properties.dig('layers', key, 'fields', idx, 'value').present?
21
+ next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
22
22
 
23
- object.properties['layers'][key]['fields'][idx]['value']['el_label'] = el.short_label if field['type'] == 'drag_sample'
24
- object.properties['layers'][key]['fields'][idx]['value']['el_tip'] = el.short_label if field['type'] == 'drag_sample'
25
- 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)
23
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
24
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
25
+ 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)
26
26
  end
27
27
 
28
- field_tables = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
28
+ field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
29
29
  field_tables.each do |field|
30
- next unless field['sub_values'].present? && field['sub_fields'].present?
30
+ next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?
31
31
 
32
- field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
32
+ field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
33
33
  next unless field_table_molecules.present?
34
34
 
35
35
  col_ids = field_table_molecules.map { |x| x.values[0] }
@@ -133,10 +133,20 @@ module Labimotion
133
133
  raise e
134
134
  end
135
135
 
136
+ def split_elements(ui_state, current_user)
137
+ col_id = ui_state[:currentCollectionId] || 0
138
+ element_ids = Labimotion::Element.for_user(current_user.id).for_ui_state_with_collection(ui_state[:element], Labimotion::CollectionsElement, col_id)
139
+ klass_id = Labimotion::ElementKlass.find_by(name: ui_state[:element][:name])&.id
140
+ Labimotion::Element.where(id: element_ids, element_klass_id: klass_id).each do |element|
141
+ element.split(current_user, col_id)
142
+ end
143
+ {}
144
+ end
145
+
136
146
  def upload_generics_files(current_user, params)
137
147
  attach_ary = []
138
148
  att_ary = create_uploads(
139
- 'Element',
149
+ Labimotion::Prop::ELEMENT,
140
150
  params[:att_id],
141
151
  params[:elfiles],
142
152
  params[:elInfo],
@@ -146,7 +156,7 @@ module Labimotion
146
156
  (attach_ary << att_ary).flatten! unless att_ary&.empty?
147
157
 
148
158
  att_ary = create_uploads(
149
- 'Segment',
159
+ Labimotion::Prop::SEGMENT,
150
160
  params[:att_id],
151
161
  params[:sefiles],
152
162
  params[:seInfo],
@@ -195,7 +205,7 @@ module Labimotion
195
205
  layer, field = params[:sort_column].split('.')
196
206
 
197
207
  element_klass = Labimotion::ElementKlass.find_by(name: params[:el_type])
198
- allowed_fields = element_klass.properties_release.dig('layers', layer, 'fields')&.pluck('field') || []
208
+ allowed_fields = element_klass.properties_release.dig(Labimotion::Prop::LAYERS, layer, Labimotion::Prop::FIELDS)&.pluck('field') || []
199
209
 
200
210
  if field.in?(allowed_fields)
201
211
  query = ActiveRecord::Base.sanitize_sql(
@@ -258,7 +268,7 @@ module Labimotion
258
268
  layer, field = params[:sort_column].split('.')
259
269
 
260
270
  element_klass = Labimotion::ElementKlass.find_by(name: params[:el_type])
261
- allowed_fields = element_klass.properties_release.dig('layers', layer, 'fields')&.pluck('field') || []
271
+ allowed_fields = element_klass.properties_release.dig(Labimotion::Prop::LAYERS, layer, Labimotion::Prop::FIELDS)&.pluck('field') || []
262
272
 
263
273
  if field.in?(allowed_fields)
264
274
  query = ActiveRecord::Base.sanitize_sql(
@@ -107,9 +107,9 @@ module Labimotion
107
107
 
108
108
  def fetch_properties_uploads(properties)
109
109
  uploads = []
110
- properties['layers'].keys.each do |key|
111
- layer = properties['layers'][key]
112
- field_uploads = layer['fields'].select { |ss| ss['type'] == 'upload' }
110
+ properties[Labimotion::Prop::LAYERS].keys.each do |key|
111
+ layer = properties[Labimotion::Prop::LAYERS][key]
112
+ field_uploads = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::UPLOAD }
113
113
  field_uploads.each do |field|
114
114
  ((field['value'] && field['value']['files']) || []).each do |file|
115
115
  uploads.push({ layer: key, field: field['field'], uid: file['uid'], filename: file['filename'] })
@@ -125,10 +125,10 @@ module Labimotion
125
125
  def update_properties_upload(element, properties, att, pa)
126
126
  return if pa.nil?
127
127
 
128
- idx = properties['layers'][pa[:layer]]['fields'].index { |fl| fl['field'] == pa[:field] }
129
- fidx = properties['layers'][pa[:layer]]['fields'][idx]['value']['files'].index { |fi| fi['uid'] == pa[:uid] }
130
- properties['layers'][pa[:layer]]['fields'][idx]['value']['files'][fidx]['aid'] = att.id
131
- properties['layers'][pa[:layer]]['fields'][idx]['value']['files'][fidx]['uid'] = att.identifier
128
+ idx = properties[Labimotion::Prop::LAYERS][pa[:layer]][Labimotion::Prop::FIELDS].index { |fl| fl['field'] == pa[:field] }
129
+ fidx = properties[Labimotion::Prop::LAYERS][pa[:layer]][Labimotion::Prop::FIELDS][idx]['value']['files'].index { |fi| fi['uid'] == pa[:uid] }
130
+ properties[Labimotion::Prop::LAYERS][pa[:layer]][Labimotion::Prop::FIELDS][idx]['value']['files'][fidx]['aid'] = att.id
131
+ properties[Labimotion::Prop::LAYERS][pa[:layer]][Labimotion::Prop::FIELDS][idx]['value']['files'][fidx]['uid'] = att.identifier
132
132
  element.update_columns(properties: properties)
133
133
  rescue StandardError => e
134
134
  Labimotion.log_exception(e, current_user)
@@ -143,9 +143,9 @@ module Labimotion
143
143
  map_info&.keys&.each do |key|
144
144
  next if map_info[key]['files'].empty?
145
145
 
146
- if type == 'Segment'
146
+ if type == Labimotion::Prop::SEGMENT
147
147
  element = Labimotion::Segment.find_by(element_id: id, segment_klass_id: key)
148
- elsif type == 'Element'
148
+ elsif type == Labimotion::Prop::ELEMENT
149
149
  element = Labimotion::Element.find_by(id: id)
150
150
  end
151
151
  next if element.nil?
@@ -73,8 +73,7 @@ module Labimotion
73
73
  attributes['updated_by'] = current_user.id
74
74
  attributes['sync_by'] = current_user.id
75
75
  attributes['sync_time'] = DateTime.now
76
-
77
- element_klass = Labimotion::ElementKlass.find_by(identifier: response['element_klass']['identifier'])
76
+ element_klass = Labimotion::ElementKlass.find_by(identifier: response['element_klass']['identifier']) if response.dig('element_klass','identifier').present?
78
77
  element_klass = Labimotion::ElementKlass.find_by(name: response['element_klass']['name'], is_generic: false) if element_klass.nil?
79
78
  return { status: 'error', message: "The element [#{response['element_klass']['name']}] does not exist in this instance" } if element_klass.nil?
80
79
 
@@ -247,38 +247,38 @@ module Labimotion
247
247
  end
248
248
 
249
249
  def self.update_ds(dataset, dsr, current_user = nil) # rubocop: disable Metrics/PerceivedComplexity
250
- layers = dataset.properties['layers'] || {}
250
+ layers = dataset.properties[Labimotion::Prop::LAYERS] || {}
251
251
  new_prop = dataset.properties
252
252
  dsr.each do |ds|
253
253
  layer = layers[ds[:layer]]
254
- next if layer.nil? || layer['fields'].nil?
254
+ next if layer.nil? || layer[Labimotion::Prop::FIELDS].nil?
255
255
 
256
- fields = layer['fields'].select{ |f| f['field'] == ds[:field] }
256
+ fields = layer[Labimotion::Prop::FIELDS].select{ |f| f['field'] == ds[:field] }
257
257
  fi = fields&.first
258
- idx = layer['fields'].find_index(fi)
258
+ idx = layer[Labimotion::Prop::FIELDS].find_index(fi)
259
259
  fi['value'] = ds[:value]
260
260
  fi['device'] = ds[:device] || ds[:value]
261
- new_prop['layers'][ds[:layer]]['fields'][idx] = fi
261
+ new_prop[Labimotion::Prop::LAYERS][ds[:layer]][Labimotion::Prop::FIELDS][idx] = fi
262
262
  end
263
- new_prop.dig('layers', 'general', 'fields')&.each_with_index do |fi, idx|
263
+ new_prop.dig(Labimotion::Prop::LAYERS, 'general', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
264
264
  if fi['field'] == 'creator' && current_user.present?
265
265
  fi['value'] = current_user.name
266
266
  fi['system'] = current_user.name
267
- new_prop['layers']['general']['fields'][idx] = fi
267
+ new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
268
268
  end
269
269
  end
270
270
  element = Container.find(dataset.element_id)&.root_element
271
- element.present? && element&.class&.name == 'Sample' && new_prop.dig('layers', 'sample_details', 'fields')&.each_with_index do |fi, idx|
271
+ element.present? && element&.class&.name == 'Sample' && new_prop.dig(Labimotion::Prop::LAYERS, 'sample_details', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
272
272
  if fi['field'] == 'id'
273
273
  fi['value'] = element.id
274
274
  fi['system'] = element.id
275
- new_prop['layers']['sample_details']['fields'][idx] = fi
275
+ new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
276
276
  end
277
277
 
278
278
  if fi['field'] == 'label'
279
279
  fi['value'] = element.short_label
280
280
  fi['system'] = element.short_label
281
- new_prop['layers']['sample_details']['fields'][idx] = fi
281
+ new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
282
282
  end
283
283
  end
284
284
  dataset.properties = new_prop
@@ -344,9 +344,9 @@ module Labimotion
344
344
 
345
345
  def self.metadata(id)
346
346
  att = Attachment.find(id)
347
- return if att.nil? || att.attachable_id.nil? || att.attachable_type != 'Container'
347
+ return if att.nil? || att.attachable_id.nil? || att.attachable_type != Labimotion::Prop::CONTAINER
348
348
 
349
- ds = Labimotion::Dataset.find_by(element_type: 'Container', element_id: att.attachable_id)
349
+ ds = Labimotion::Dataset.find_by(element_type: Labimotion::Prop::CONTAINER, element_id: att.attachable_id)
350
350
  att.update_column(:con_state, Labimotion::ConState::COMPLETED) if ds.present?
351
351
  process_ds(att.id) if ds.nil?
352
352
  end
@@ -18,6 +18,8 @@ module Labimotion
18
18
  element_name = Container.find(id)&.root_element&.short_label
19
19
  ols = ols_name(id)
20
20
  "#{element_name}_#{ols.gsub(' ', '_')}.xlsx"
21
+ rescue StandardError => e
22
+ Labimotion.log_exception(e)
21
23
  end
22
24
 
23
25
  def ols_name(id)
@@ -32,6 +34,9 @@ module Labimotion
32
34
  name = '1H NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000593'
33
35
  name = '13C NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000595'
34
36
  name.slice(0, 26)
37
+ rescue StandardError => e
38
+ Labimotion.log_exception(e)
39
+ 'ols_name'
35
40
  end
36
41
 
37
42
  def description(ds, id)
@@ -56,6 +61,8 @@ module Labimotion
56
61
  sheet.add_row(['Source data', 'The data from Device or Chemotion, cannot be modified once a generic dataset is created'])
57
62
  sheet.add_row([''])
58
63
  sheet.add_row([''])
64
+ rescue StandardError => e
65
+ Labimotion.log_exception(e)
59
66
  end
60
67
 
61
68
  def export(id)
@@ -74,26 +81,32 @@ module Labimotion
74
81
  layer_style = sheet.styles.add_style(b: true, bg_color: 'CEECF5')
75
82
  sheet.add_row(header, style: header_style)
76
83
 
77
- layers = ds.properties['layers'] || {}
84
+ layers = ds.properties[Labimotion::Prop::LAYERS] || {}
85
+ options = ds.properties[Labimotion::Prop::SEL_OPTIONS]
78
86
  layer_keys = layers.keys.sort_by { |key| layers[key]['position'] }
79
87
  layer_keys.each do |key|
80
88
  layer = layers[key]
81
89
  sheet.add_row([layer['label'], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], style: layer_style)
82
- sorted_fields = layer['fields'].sort_by { |obj| obj['position'] }
90
+ sorted_fields = layer[Labimotion::Prop::FIELDS].sort_by { |obj| obj['position'] }
83
91
  sorted_fields.each do |field|
84
92
  next if field['type'] == 'dummy'
85
93
 
86
94
  type = field['type']
87
95
  from_device = field['device'].present? ? 'Device' : ''
88
96
  from_device = field['system'].present? ? 'Chemotion' : from_device
89
- type = "#{field['type']}-#{field['option_layers']}" if field['type'] == 'select' || field['type'] == 'system-defined'
97
+ type = "#{field['type']}-#{field['option_layers']}" if field['type'] == Labimotion::FieldType::SELECT || field['type'] == Labimotion::FieldType::SYSTEM_DEFINED
90
98
 
91
99
  show_value = field['value'] =~ /\A\d+,\d+\z/ ? field['value']&.gsub(',', '.') : field['value']
92
100
  sheet.add_row([' ', field['label'], nil, field['value_system'], field['field'], type, from_device, field['dkey'], nil].freeze)
93
101
 
94
- if %w[system-defined integer].include? field['type']
95
- sheet.rows.last.cells[2].type = :integer
96
- sheet.rows.last.cells[8].type = :integer
102
+ case field['type']
103
+ when Labimotion::FieldType::SELECT
104
+ sheet.rows.last.cells[2].type = :string
105
+ sheet.rows.last.cells[8].type = :string
106
+ show_value = opt_value(field, options) if show_value.present?
107
+ when Labimotion::FieldType::SYSTEM_DEFINED, Labimotion::FieldType::INTEGER
108
+ sheet.rows.last.cells[2].type = :float
109
+ sheet.rows.last.cells[8].type = :float
97
110
  else
98
111
  sheet.rows.last.cells[2].type = :string
99
112
  sheet.rows.last.cells[8].type = :string
@@ -105,6 +118,19 @@ module Labimotion
105
118
  end
106
119
  # sheet.column_widths nil, nil, nil, nil, 0, 0, 0, 0, 0
107
120
  end
121
+ rescue StandardError => e
122
+ Labimotion.log_exception(e)
123
+ end
124
+
125
+ def opt_value(field, options)
126
+ return nil if field.nil? || options.nil? || field['value']&.empty? || field['option_layers']&.empty?
127
+ return nil unless opts = options.fetch(field['option_layers'], nil)&.fetch('options', nil)
128
+
129
+ selected = opts&.find { |ss| ss['key'] == field['value'] }
130
+ selected&.fetch('label', nil) || field['value']
131
+ rescue StandardError => e
132
+ Labimotion.log_exception(e)
133
+ field['value']
108
134
  end
109
135
 
110
136
  def spectra(id)
@@ -109,7 +109,7 @@ module Labimotion
109
109
  return if field['field'] != field_name || value&.empty?
110
110
 
111
111
  field['value'] = value
112
- prop['layers'][layer_name]['fields'][idx] = field
112
+ prop[Labimotion::Prop::LAYERS][layer_name][Labimotion::Prop::FIELDS][idx] = field
113
113
  prop
114
114
  end
115
115
 
@@ -117,14 +117,14 @@ module Labimotion
117
117
  dataset = obj[:dataset]
118
118
  metadata = obj[:metadata]
119
119
  new_prop = dataset.properties
120
- new_prop.dig('layers', 'general', 'fields')&.each_with_index do |fi, idx|
120
+ new_prop.dig(Labimotion::Prop::LAYERS, 'general', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
121
121
  # new_prop = set_data(new_prop, fi, idx, 'general', 'title', metadata['NAME'])
122
122
  if fi['field'] == 'title' && metadata['NAME'].present?
123
123
  ## fi['label'] = fi['label']
124
124
  fi['value'] = metadata['NAME']
125
125
  fi['device'] = metadata['NAME']
126
126
  fi['dkey'] = 'NAME'
127
- new_prop['layers']['general']['fields'][idx] = fi
127
+ new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
128
128
  end
129
129
 
130
130
  if fi['field'] == 'date' && metadata['Date_'].present?
@@ -132,7 +132,7 @@ module Labimotion
132
132
  fi['value'] = metadata['Date_']
133
133
  fi['device'] = metadata['Date_']
134
134
  fi['dkey'] = 'Date_'
135
- new_prop['layers']['general']['fields'][idx] = fi
135
+ new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
136
136
  end
137
137
 
138
138
  if fi['field'] == 'time' && metadata['Time'].present?
@@ -140,130 +140,130 @@ module Labimotion
140
140
  fi['value'] = metadata['Time']
141
141
  fi['device'] = metadata['Time']
142
142
  fi['dkey'] = 'Time'
143
- new_prop['layers']['general']['fields'][idx] = fi
143
+ new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
144
144
  end
145
145
 
146
146
  if fi['field'] == 'creator' && current_user.present?
147
147
  ## fi['label'] = fi['label']
148
148
  fi['value'] = current_user.name
149
- new_prop['layers']['general']['fields'][idx] = fi
149
+ new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
150
150
  end
151
151
  end
152
152
  element = Container.find(id)&.root_element
153
- element.present? && element&.class&.name == 'Sample' && new_prop.dig('layers', 'sample_details',
154
- 'fields')&.each_with_index do |fi, idx|
153
+ element.present? && element&.class&.name == 'Sample' && new_prop.dig(Labimotion::Prop::LAYERS, 'sample_details',
154
+ Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
155
155
  if fi['field'] == 'label'
156
156
  fi['value'] = element.short_label
157
- new_prop['layers']['sample_details']['fields'][idx] = fi
157
+ new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
158
158
  end
159
159
  if fi['field'] == 'id'
160
160
  fi['value'] = element.id
161
- new_prop['layers']['sample_details']['fields'][idx] = fi
161
+ new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
162
162
  end
163
163
  end
164
164
 
165
- new_prop.dig('layers', 'instrument', 'fields')&.each_with_index do |fi, idx|
165
+ new_prop.dig(Labimotion::Prop::LAYERS, 'instrument', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
166
166
  if fi['field'] == 'instrument' && metadata['INSTRUM'].present?
167
167
  ## fi['label'] = fi['label']
168
168
  fi['value'] = metadata['INSTRUM']
169
169
  fi['device'] = metadata['INSTRUM']
170
170
  fi['dkey'] = 'INSTRUM'
171
- new_prop['layers']['instrument']['fields'][idx] = fi
171
+ new_prop[Labimotion::Prop::LAYERS]['instrument'][Labimotion::Prop::FIELDS][idx] = fi
172
172
  end
173
173
  end
174
174
 
175
- new_prop.dig('layers', 'equipment', 'fields')&.each_with_index do |fi, idx|
175
+ new_prop.dig(Labimotion::Prop::LAYERS, 'equipment', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
176
176
  if fi['field'] == 'probehead' && metadata['PROBHD'].present?
177
177
  ## fi['label'] = fi['label']
178
178
  fi['value'] = metadata['PROBHD']
179
179
  fi['device'] = metadata['PROBHD']
180
180
  fi['dkey'] = 'PROBHD'
181
- new_prop['layers']['equipment']['fields'][idx] = fi
181
+ new_prop[Labimotion::Prop::LAYERS]['equipment'][Labimotion::Prop::FIELDS][idx] = fi
182
182
  end
183
183
  end
184
184
 
185
- new_prop.dig('layers', 'sample_preparation', 'fields')&.each_with_index do |fi, idx|
185
+ new_prop.dig(Labimotion::Prop::LAYERS, 'sample_preparation', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
186
186
  if fi['field'] == 'solvent' && metadata['SOLVENT'].present?
187
187
  ## fi['label'] = fi['label']
188
188
  fi['value'] = metadata['SOLVENT']
189
189
  fi['device'] = metadata['SOLVENT']
190
190
  fi['dkey'] = 'SOLVENT'
191
191
  fi['value'] = 'chloroform-D1 (CDCl3)' if metadata['SOLVENT'] == 'CDCl3'
192
- new_prop['layers']['sample_preparation']['fields'][idx] = fi
192
+ new_prop[Labimotion::Prop::LAYERS]['sample_preparation'][Labimotion::Prop::FIELDS][idx] = fi
193
193
  end
194
194
  end
195
195
 
196
- new_prop.dig('layers', 'set', 'fields')&.each_with_index do |fi, idx|
196
+ new_prop.dig(Labimotion::Prop::LAYERS, 'set', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
197
197
  if fi['field'] == 'temperature' && metadata['TE'].present?
198
198
  ## fi['label'] = fi['label']
199
199
  fi['value'] = metadata['TE'].split(/\s+/).first
200
200
  fi['device'] = metadata['TE']
201
201
  fi['dkey'] = 'TE'
202
202
  fi['value_system'] = metadata['TE'].split(/\s+/).last
203
- new_prop['layers']['set']['fields'][idx] = fi
203
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
204
204
  end
205
205
  if fi['field'] == 'ns' && metadata['NS'].present?
206
206
  ## fi['label'] = fi['label']
207
207
  fi['value'] = metadata['NS']
208
208
  fi['device'] = metadata['NS']
209
209
  fi['dkey'] = 'NS'
210
- new_prop['layers']['set']['fields'][idx] = fi
210
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
211
211
  end
212
212
  if fi['field'] == 'PULPROG' && metadata['PULPROG'].present?
213
213
  ## fi['label'] = fi['label']
214
214
  fi['value'] = metadata['PULPROG']
215
215
  fi['device'] = metadata['PULPROG']
216
216
  fi['dkey'] = 'PULPROG'
217
- new_prop['layers']['set']['fields'][idx] = fi
217
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
218
218
  end
219
219
  if fi['field'] == 'td' && metadata['TD'].present?
220
220
  ## fi['label'] = fi['label']
221
221
  fi['value'] = metadata['TD']
222
222
  fi['device'] = metadata['TD']
223
223
  fi['dkey'] = 'TD'
224
- new_prop['layers']['set']['fields'][idx] = fi
224
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
225
225
  end
226
226
  if fi['field'] == 'done' && metadata['D1'].present?
227
227
  ## fi['label'] = fi['label']
228
228
  fi['value'] = metadata['D1']
229
229
  fi['device'] = metadata['D1']
230
230
  fi['dkey'] = 'D1'
231
- new_prop['layers']['set']['fields'][idx] = fi
231
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
232
232
  end
233
233
  if fi['field'] == 'sf' && metadata['SF'].present?
234
234
  ## fi['label'] = fi['label']
235
235
  fi['value'] = metadata['SF']
236
236
  fi['device'] = metadata['SF']
237
237
  fi['dkey'] = 'SF'
238
- new_prop['layers']['set']['fields'][idx] = fi
238
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
239
239
  end
240
240
  if fi['field'] == 'sfoone' && metadata['SFO1'].present?
241
241
  ## fi['label'] = fi['label']
242
242
  fi['value'] = metadata['SFO1']
243
243
  fi['device'] = metadata['SFO1']
244
244
  fi['dkey'] = 'SFO1'
245
- new_prop['layers']['set']['fields'][idx] = fi
245
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
246
246
  end
247
247
  if fi['field'] == 'sfotwo' && metadata['SFO2'].present?
248
248
  ## fi['label'] = fi['label']
249
249
  fi['value'] = metadata['SFO2']
250
250
  fi['device'] = metadata['SFO2']
251
251
  fi['dkey'] = 'SFO2'
252
- new_prop['layers']['set']['fields'][idx] = fi
252
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
253
253
  end
254
254
  if fi['field'] == 'nucone' && metadata['NUC1'].present?
255
255
  ## fi['label'] = fi['label']
256
256
  fi['value'] = metadata['NUC1']
257
257
  fi['device'] = metadata['NUC1']
258
258
  fi['dkey'] = 'NUC1'
259
- new_prop['layers']['set']['fields'][idx] = fi
259
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
260
260
  end
261
261
  if fi['field'] == 'nuctwo' && metadata['NUC2'].present?
262
262
  ## fi['label'] = fi['label']
263
263
  fi['value'] = metadata['NUC2']
264
264
  fi['device'] = metadata['NUC2']
265
265
  fi['dkey'] = 'NUC2'
266
- new_prop['layers']['set']['fields'][idx] = fi
266
+ new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
267
267
  end
268
268
  end
269
269
  dataset.properties = new_prop