labimotion 1.5.0.rc7 → 1.5.0.rc8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 933a7ef2942093d53c89df803dd802efeb4f0a0241193de7094d51c26fcfa8ff
4
- data.tar.gz: 5792273bc537ccf951ee3c7ab5ef575f23079b63acc8ce3f2d923fb445181d26
3
+ metadata.gz: 5a54a18e3c64bb9446ecf6c57aa2755d9ab0921a2571aa734615faa40f46d19d
4
+ data.tar.gz: 03a66fbf2e025ac855831349888ab941b7a2059a02557c473c4654eaceb62601
5
5
  SHA512:
6
- metadata.gz: 2b50364418e278a654814ef2afa8c29bf2c9a2a1d4d134fb690573e317676b2fe9cdfa2afbc5cce8d65a5317fcf45a7f7a427791c36b19cc3f4d85a043055048
7
- data.tar.gz: 04a7949b73b943642f8c2e4e4541d4ee3695e27aded5fc447818c5bfaefd2ba8ced4626f9bd045eef9765fe75c7d5f42016f1afe2b202cd0454bcd5be89c94fe
6
+ metadata.gz: fb9af61e157bc3c89b8b32d3fe51aa580d6a684c8348029e6b291e65d0f1876f48279f09bd291dc40194cb2a1c84cc84c6ea3beac5092f34ce1b2d6fd64db52b
7
+ data.tar.gz: cde31727f46f71e4803cc933f56e1f73785f87f9ab928a010299dc7c33dbdd504907cc31aa73bdc81000bf1774a60095a23860ae4e7d6b0ffdd29046612615da
@@ -74,10 +74,8 @@ module Labimotion
74
74
  # end
75
75
  vocabularies = Labimotion::VocabularyHandler.load_app_vocabularies
76
76
  merged_data = Labimotion::FieldKlassEntity.represent(vocabularies, serializable: true)
77
- # byebug
78
77
  # list = Labimotion::Vocabulary.all.sort_by { |e| e.name }
79
78
  # present list, with: Labimotion::VocabularyEntity, root: 'klass'
80
- # byebug
81
79
  merged_data
82
80
  rescue StandardError => e
83
81
  Labimotion.log_exception(e, current_user)
@@ -13,78 +13,5 @@ module Labimotion
13
13
  object&.dataset_klass&.label
14
14
  end
15
15
 
16
-
17
- # def root_element
18
- # object&.element&.root_element
19
- # end
20
-
21
- # def properties
22
- # prop = object&.properties || {}
23
- # prop[Labimotion::Prop::LAYERS].keys.each do |key|
24
- # layer = prop[Labimotion::Prop::LAYERS][key] || {}
25
- # return prop if layer.empty? || layer[Labimotion::Prop::FIELDS].nil?
26
-
27
- # field_vocs = layer[Labimotion::Prop::FIELDS].select { |ss| ss['is_voc'] == true }
28
- # field_vocs.each do |field|
29
- # idx = prop[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
30
- # next if idx.nil?
31
-
32
- # case field['source']
33
- # when Labimotion::Prop::ELEMENT
34
- # if field['identifier'] == 'element.name'
35
- # # byebug
36
- # prop[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = root_element&.name
37
- # # dic[field.identifier] = currentElement.name
38
-
39
- # end
40
- # when Labimotion::Prop::SEGMENT
41
- # segs = root_element&.segments&.select { |ss| field['source_id'] == ss.segment_klass&.identifier }
42
- # next if segs.empty? || field['layer_id'].blank? || field['field_id'].blank?
43
-
44
- # seg = segs&.first
45
- # seg_fields = seg.properties.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
46
- # seg_field = seg_fields&.first
47
- # prop[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = seg_field['value']
48
- # when Labimotion::Prop::DATASET
49
- # # byebug
50
- # dk = DatasetKlass.find_by(identifier: field["source_id"])
51
- # dk["ols_term_id"]
52
- # # byebug
53
- # # sel_ana = root_element.analyses.select { |ana| ana.extended_metadata["kind"].split("|")&.first&.strip == dk["ols_term_id"] }
54
- # anas = root_element.analyses.select { |ana| ana.extended_metadata["kind"].split("|")&.first&.strip == dk["ols_term_id"] }
55
- # anas.each do |ana|
56
- # cdss = ana.children
57
- # cdss.each do |cds|
58
- # next unless cds.dataset.present?
59
- # ds_prop = cds.dataset.properties
60
- # ds_fields = ds_prop.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
61
- # ds_field = ds_fields&.first
62
- # # byebug
63
- # if prop[Labimotion::Prop::LAYERS][key].present? && ds_field['value'].present?
64
- # prop[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = ds_field['value']
65
- # end
66
- # end
67
- # end
68
-
69
- # # sel_ana.dataset
70
- # # if root_element.analyses&.first&.extended_metadata["kind"].split("|")&.first&.strip == dk["ols_term_id"]
71
- # # byebug
72
- # # end
73
- # # root_element.analyses.select { |ss| ss.dataset_klass_id == dk.id }
74
- # end
75
- # end
76
-
77
- # # layer[Labimotion::Prop::FIELDS].each do |field|
78
- # # end
79
- # end
80
-
81
- # # byebug
82
- # # layers = properties['layers']
83
-
84
- # # properties['vocabularies'].each_with_object({}) do |v, h|
85
- # # h[v['name']] = v['value']
86
- # # end
87
- # prop
88
- # end
89
16
  end
90
17
  end
@@ -1,109 +1,163 @@
1
1
  require 'labimotion/entities/application_entity'
2
2
 
3
- # Entity module
4
3
  module Labimotion
5
4
  class PropertiesEntity < Labimotion::ApplicationEntity
6
-
7
- # TODO: Refactor this method to something more readable/understandable
8
5
  def properties
6
+ process_layers do |key, layer|
7
+ process_fields(key, layer)
8
+ end
9
+ object.properties
10
+ end
11
+
12
+ private
13
+
14
+ def process_layers
9
15
  (object&.properties.is_a?(Hash) && object.properties[Labimotion::Prop::LAYERS]&.keys || []).each do |key|
16
+ yield(key, object.properties[Labimotion::Prop::LAYERS][key])
17
+ end
18
+ end
10
19
 
20
+ def process_fields(key, layer)
21
+ process_sample_and_molecule_fields(key, layer)
22
+ process_reaction_fields(key, layer)
23
+ process_table_fields(key, layer)
24
+ process_voc_fields(key, layer)
25
+ end
11
26
 
12
- 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 }
13
- field_sample_molecules.each do |field|
14
- idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
15
- sid = field.dig('value', 'el_id')
16
- next unless sid.present?
27
+ def process_sample_and_molecule_fields(key, layer)
28
+ select_fields(layer, [Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_MOLECULE]).each do |field, idx|
29
+ update_sample_or_molecule_field(key, field, idx)
30
+ end
31
+ end
17
32
 
18
- el = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
19
- next unless el.present?
20
- next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
33
+ def process_reaction_fields(key, layer)
34
+ select_fields(layer, [Labimotion::FieldType::SYS_REACTION]).each do |field, idx|
35
+ update_reaction_field(key, field, idx)
36
+ end
37
+ end
21
38
 
22
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
23
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
24
- 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 || 'nosvg')
25
- end
39
+ def process_table_fields(key, layer)
40
+ select_fields(layer, [Labimotion::FieldType::TABLE]).each do |field, idx|
41
+ update_table_field(key, field, idx)
42
+ end
43
+ end
26
44
 
27
- field_sys_reactions = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::SYS_REACTION }
28
- field_sys_reactions.each do |field|
29
- idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
30
- sid = field.dig('value', 'el_id')
31
- next unless sid.present?
45
+ def process_voc_fields(key, layer)
46
+ select_fields(layer, nil, true).each do |field, idx|
47
+ update_voc_field(key, field, idx)
48
+ end
49
+ end
32
50
 
33
- el = Reaction.find_by(id: sid)
34
- if el.blank?
35
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = 'ERROR'
36
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = ''
37
- end
51
+ def select_fields(layer, types = nil, is_voc = false)
52
+ fields = layer[Labimotion::Prop::FIELDS]
53
+ fields = fields.select { |f| types.include?(f['type']) } if types
54
+ fields = fields.select { |f| f['is_voc'] == true } if is_voc
55
+ fields.map { |f| [f, fields.index(f)] }
56
+ end
38
57
 
39
- next unless el.present?
40
- next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
58
+ def update_sample_or_molecule_field(key, field, idx)
59
+ sid = field.dig('value', 'el_id')
60
+ return unless sid.present?
41
61
 
42
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label
43
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label
44
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = el.reaction_svg_file
45
- end
62
+ el = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
63
+ return unless el.present? && object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
46
64
 
47
- field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
48
- field_tables.each do |field|
49
- idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
50
- next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?
65
+ update_field_value(key, idx, {
66
+ 'el_label' => el.short_label,
67
+ 'el_tip' => el.short_label,
68
+ 'el_svg' => field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el&.molecule_svg_file || 'nosvg')
69
+ })
70
+ end
51
71
 
52
- field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
53
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE) if field_table_molecules.present?
72
+ def update_reaction_field(key, field, idx)
73
+ sid = field.dig('value', 'el_id')
74
+ return unless sid.present?
54
75
 
55
- field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
56
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_samples, Labimotion::Prop::SAMPLE) if field_table_samples.present?
57
- end
76
+ el = Reaction.find_by(id: sid)
77
+ if el.blank?
78
+ update_field_value(key, idx, { 'el_tip' => 'ERROR', 'el_svg' => '' })
79
+ return
80
+ end
58
81
 
59
- field_vocs = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['is_voc'] == true }
60
- field_vocs.each do |field|
61
- idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
62
- next if idx.nil?
63
-
64
- case object.class.name
65
- when Labimotion::Prop::L_ELEMENT
66
- root_element = object
67
- when Labimotion::Prop::L_SEGMENT
68
- root_element = object&.element
69
- when Labimotion::Prop::L_DATASET
70
- root_element = object&.element&.root_element
71
- end
82
+ return unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
72
83
 
73
- case field['source']
74
- when Labimotion::Prop::ELEMENT
75
- if field['identifier'] == 'element.name'
76
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = root_element&.name
77
- end
78
- when Labimotion::Prop::SEGMENT
79
- segs = root_element&.segments&.select { |ss| field['source_id'] == ss.segment_klass&.identifier }
80
- next if segs.empty? || field['layer_id'].blank? || field['field_id'].blank?
81
-
82
- seg = segs&.first
83
- seg_fields = seg.properties.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
84
- seg_field = seg_fields&.first
85
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = seg_field['value']
86
- when Labimotion::Prop::DATASET
87
- dk = DatasetKlass.find_by(identifier: field["source_id"])
88
- dk["ols_term_id"]
89
- anas = root_element.analyses.select { |ana| ana.extended_metadata["kind"].split("|")&.first&.strip == dk["ols_term_id"] }
90
- anas.each do |ana|
91
- cdss = ana.children
92
- cdss.each do |cds|
93
- next unless cds.dataset.present?
94
-
95
- ds_prop = cds.dataset.properties
96
- ds_fields = ds_prop.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
97
- ds_field = ds_fields&.first
98
- if object.properties[Labimotion::Prop::LAYERS][key].present? && ds_field['value'].present?
99
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = ds_field['value']
100
- end
101
- end
102
- end
84
+ update_field_value(key, idx, {
85
+ 'el_label' => el.short_label,
86
+ 'el_tip' => el.short_label,
87
+ 'el_svg' => el.reaction_svg_file
88
+ })
89
+ end
90
+
91
+ def update_table_field(key, field, idx)
92
+ return unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?
93
+
94
+ field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
95
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE) if field_table_molecules.present?
96
+
97
+ field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
98
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_samples, Labimotion::Prop::SAMPLE) if field_table_samples.present?
99
+ end
100
+
101
+ def update_voc_field(key, field, idx)
102
+ root_element = get_root_element
103
+
104
+ case field['source']
105
+ when Labimotion::Prop::ELEMENT
106
+ update_element_voc_field(key, field, idx, root_element)
107
+ when Labimotion::Prop::SEGMENT
108
+ update_segment_voc_field(key, field, idx, root_element)
109
+ when Labimotion::Prop::DATASET
110
+ update_dataset_voc_field(key, field, idx, root_element)
111
+ end
112
+ end
113
+
114
+ def get_root_element
115
+ case object.class.name
116
+ when Labimotion::Prop::L_ELEMENT
117
+ object
118
+ when Labimotion::Prop::L_SEGMENT
119
+ object&.element
120
+ when Labimotion::Prop::L_DATASET
121
+ object&.element&.root_element
122
+ end
123
+ end
124
+
125
+ def update_element_voc_field(key, field, idx, root_element)
126
+ if field['identifier'] == 'element.name'
127
+ update_field_value(key, idx, root_element&.name)
128
+ end
129
+ end
130
+
131
+ def update_segment_voc_field(key, field, idx, root_element)
132
+ segs = root_element&.segments&.select { |ss| field['source_id'] == ss.segment_klass&.identifier }
133
+ return if segs.empty? || field['layer_id'].blank? || field['field_id'].blank?
134
+
135
+ seg = segs&.first
136
+ seg_fields = seg.properties.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
137
+ seg_field = seg_fields&.first
138
+ update_field_value(key, idx, seg_field['value'])
139
+ end
140
+
141
+ def update_dataset_voc_field(key, field, idx, root_element)
142
+ dk = DatasetKlass.find_by(identifier: field["source_id"])
143
+ dk["ols_term_id"]
144
+ anas = root_element.analyses.select { |ana| ana.extended_metadata["kind"].split("|")&.first&.strip == dk["ols_term_id"] }
145
+ anas.each do |ana|
146
+ ana.children.each do |cds|
147
+ next unless cds.dataset.present?
148
+
149
+ ds_prop = cds.dataset.properties
150
+ ds_fields = ds_prop.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
151
+ ds_field = ds_fields&.first
152
+ if object.properties[Labimotion::Prop::LAYERS][key].present? && ds_field['value'].present?
153
+ update_field_value(key, idx, ds_field['value'])
103
154
  end
104
155
  end
105
156
  end
106
- object.properties
157
+ end
158
+
159
+ def update_field_value(key, idx, value)
160
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = value
107
161
  end
108
162
 
109
163
  def set_table(field, field_table_objs, obj)
@@ -117,23 +171,34 @@ module Labimotion
117
171
 
118
172
  case obj
119
173
  when Labimotion::Prop::MOLECULE
120
- sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file) if find_obj&.molecule_svg_file&.present?
121
- sub_value[col_id]['value']['el_inchikey'] = find_obj.inchikey
122
- sub_value[col_id]['value']['el_smiles'] = find_obj.cano_smiles
123
- sub_value[col_id]['value']['el_iupac'] = find_obj.iupac_name
124
- sub_value[col_id]['value']['el_molecular_weight'] = find_obj.molecular_weight
174
+ update_molecule_sub_value(sub_value, col_id, find_obj)
125
175
  when Labimotion::Prop::SAMPLE
126
- sub_value[col_id]['value']['el_svg'] = find_obj.get_svg_path
127
- sub_value[col_id]['value']['el_label'] = find_obj.short_label
128
- sub_value[col_id]['value']['el_short_label'] = find_obj.short_label
129
- sub_value[col_id]['value']['el_name'] = find_obj.name
130
- sub_value[col_id]['value']['el_external_label'] = find_obj.external_label
131
- sub_value[col_id]['value']['el_molecular_weight'] = find_obj.decoupled ? find_obj.molecular_mass : find_obj.molecule.molecular_weight
176
+ update_sample_sub_value(sub_value, col_id, find_obj)
132
177
  end
133
178
  end
134
179
  end
135
180
  field
136
181
  end
137
182
 
183
+ def update_molecule_sub_value(sub_value, col_id, find_obj)
184
+ sub_value[col_id]['value'].merge!({
185
+ 'el_svg' => File.join('/images', 'molecules', find_obj.molecule_svg_file),
186
+ 'el_inchikey' => find_obj.inchikey,
187
+ 'el_smiles' => find_obj.cano_smiles,
188
+ 'el_iupac' => find_obj.iupac_name,
189
+ 'el_molecular_weight' => find_obj.molecular_weight
190
+ })
191
+ end
192
+
193
+ def update_sample_sub_value(sub_value, col_id, find_obj)
194
+ sub_value[col_id]['value'].merge!({
195
+ 'el_svg' => find_obj.get_svg_path,
196
+ 'el_label' => find_obj.short_label,
197
+ 'el_short_label' => find_obj.short_label,
198
+ 'el_name' => find_obj.name,
199
+ 'el_external_label' => find_obj.external_label,
200
+ 'el_molecular_weight' => find_obj.decoupled ? find_obj.molecular_mass : find_obj.molecule.molecular_weight
201
+ })
202
+ end
138
203
  end
139
- end
204
+ end
@@ -17,13 +17,11 @@ module Labimotion
17
17
  # end
18
18
 
19
19
  expose :voc do |obj|
20
- ## byebug
21
20
  voc = (obj[:properties] && obj[:properties]['voc']) || {}
22
21
 
23
22
  case voc['source']
24
23
  when Labimotion::Prop::ELEMENT
25
24
  if voc['identifier'] == 'element.name'
26
- # byebug
27
25
  # voc['source_name'] = ElementKlass.find_by(identifier: voc['source_id'])&.name
28
26
  end
29
27
  when Labimotion::Prop::SEGMENT
@@ -28,7 +28,7 @@ module Labimotion
28
28
  dsr = []
29
29
  ols = nil
30
30
  Zip::File.open(att.attachment_attacher.file.url) do |zip_file|
31
- res = Labimotion::Converter.collect_metadata(zip_file) if att.filename.split('.')&.last == 'zip'
31
+ res = Labimotion::Converter.collect_metadata(zip_file, current_user) if att.filename.split('.')&.last == 'zip'
32
32
  ols = res[:o] unless res&.dig(:o).nil?
33
33
  dsr.push(res[:d]) unless res&.dig(:d).nil?
34
34
  end
@@ -92,7 +92,7 @@ module Labimotion
92
92
  { a: oa, f: folder }
93
93
  end
94
94
 
95
- def self.collect_metadata(zip_file) # rubocop: disable Metrics/PerceivedComplexity
95
+ def self.collect_metadata(zip_file, current_user = {}) # rubocop: disable Metrics/PerceivedComplexity
96
96
  dsr = []
97
97
  ols = nil
98
98
  zip_file.each do |entry|
@@ -114,7 +114,7 @@ module Labimotion
114
114
  { d: dsr, o: ols }
115
115
  end
116
116
 
117
- def self.handle_response(oat, response) # rubocop: disable Metrics/PerceivedComplexity
117
+ def self.handle_response(oat, response, current_user = {}) # rubocop: disable Metrics/PerceivedComplexity
118
118
  dsr = []
119
119
  ols = nil
120
120
 
@@ -123,10 +123,9 @@ module Labimotion
123
123
  tmp_file.write(response.parsed_response)
124
124
  tmp_file.rewind
125
125
 
126
- name = response&.headers && response&.headers['content-disposition']&.split('=')&.last
126
+ # name = response&.headers && response&.headers['content-disposition']&.split('=')&.last
127
127
  filename = oat.filename
128
- name = "#{File.basename(filename, '.*')}.zip" if name.nil?
129
-
128
+ name = "#{File.basename(filename, '.*')}.zip"
130
129
  att = Attachment.new(
131
130
  filename: name,
132
131
  file_path: tmp_file.path,
@@ -140,7 +139,7 @@ module Labimotion
140
139
 
141
140
  att.save! if att.valid?
142
141
 
143
- process_ds(att.id)
142
+ process_ds(att.id, current_user)
144
143
  rescue StandardError => e
145
144
  raise e
146
145
  ensure
@@ -148,7 +147,7 @@ module Labimotion
148
147
  end
149
148
  end
150
149
 
151
- def self.process(data)
150
+ def self.process(data, current_user = {}) # rubocop: disable Metrics/PerceivedComplexity
152
151
  return data[:a].con_state if data[:a]&.attachable_type != 'Container'
153
152
  response = nil
154
153
  begin
@@ -165,25 +164,25 @@ module Labimotion
165
164
  )
166
165
  end
167
166
  if response.ok?
168
- Labimotion::Converter.handle_response(data[:a], response)
167
+ Labimotion::Converter.handle_response(data[:a], response, current_user)
169
168
  Labimotion::ConState::PROCESSED
170
169
  else
171
170
  Labimotion::Converter.logger.error ["Converter Response Error: id: [#{data[:a]&.id}], filename: [#{data[:a]&.filename}], response: #{response}"].join($INPUT_RECORD_SEPARATOR)
172
171
  Labimotion::ConState::ERROR
173
172
  end
174
173
  rescue StandardError => e
175
- Labimotion::Converter.logger.error ["process fail: #{id}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
174
+ Labimotion::Converter.logger.error ["process fail: #{data[:a]&.id}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
176
175
  Labimotion::ConState::ERROR
177
176
  ensure
178
177
  FileUtils.rm_f(ofile)
179
178
  end
180
179
  end
181
180
 
182
- def self.jcamp_converter(id)
181
+ def self.jcamp_converter(id, current_user = {})
183
182
  data = Labimotion::Converter.vor_conv(id)
184
183
  return if data.nil?
185
184
 
186
- Labimotion::Converter.process(data)
185
+ Labimotion::Converter.process(data, current_user)
187
186
  rescue StandardError => e
188
187
  Labimotion::Converter.logger.error ["jcamp_converter fail: #{id}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
189
188
  Labimotion::ConState::ERROR
@@ -243,27 +242,7 @@ module Labimotion
243
242
  fi['device'] = ds[:device] || ds[:value]
244
243
  new_prop[Labimotion::Prop::LAYERS][ds[:layer]][Labimotion::Prop::FIELDS][idx] = fi
245
244
  end
246
- new_prop.dig(Labimotion::Prop::LAYERS, 'general', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
247
- if fi['field'] == 'creator' && current_user.present?
248
- fi['value'] = current_user.name
249
- fi['system'] = current_user.name
250
- new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
251
- end
252
- end
253
245
  element = Container.find(dataset.element_id)&.root_element
254
- element.present? && element&.class&.name == 'Sample' && new_prop.dig(Labimotion::Prop::LAYERS, 'sample_details', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
255
- if fi['field'] == 'id'
256
- fi['value'] = element.id
257
- fi['system'] = element.id
258
- new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
259
- end
260
-
261
- if fi['field'] == 'label'
262
- fi['value'] = element.short_label
263
- fi['system'] = element.short_label
264
- new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
265
- end
266
- end
267
246
  new_prop = Labimotion::VocabularyHandler.update_vocabularies(new_prop, current_user, element)
268
247
  dataset.properties = new_prop
269
248
  dataset.save!
@@ -326,13 +305,13 @@ module Labimotion
326
305
  res
327
306
  end
328
307
 
329
- def self.metadata(id)
308
+ def self.metadata(id, current_user)
330
309
  att = Attachment.find(id)
331
310
  return if att.nil? || att.attachable_id.nil? || att.attachable_type != Labimotion::Prop::CONTAINER
332
311
 
333
312
  ds = Labimotion::Dataset.find_by(element_type: Labimotion::Prop::CONTAINER, element_id: att.attachable_id)
334
313
  att.update_column(:con_state, Labimotion::ConState::COMPLETED) if ds.present?
335
- process_ds(att.id) if ds.nil?
314
+ process_ds(att.id, current_user) if ds.nil?
336
315
  end
337
316
  end
338
317
  end
@@ -115,5 +115,17 @@
115
115
  "voc": {
116
116
  "source": "Element"
117
117
  }
118
+ },
119
+ {
120
+ "id": 3,
121
+ "identifier": "element.class",
122
+ "name": "Element Class",
123
+ "label": "Element Class",
124
+ "field_type": "text",
125
+ "opid": 9,
126
+ "source": "Element",
127
+ "voc": {
128
+ "source": "Element"
129
+ }
118
130
  }
119
131
  ]
@@ -13,7 +13,7 @@ module Labimotion
13
13
  result = is_brucker_binary(id)
14
14
  if result[:is_bagit] == true
15
15
  att.update_column(:con_state, Labimotion::ConState::CONVERTED)
16
- Labimotion::Converter.metadata(id)
16
+ Labimotion::Converter.metadata(id, current_user)
17
17
  Labimotion::ConState::COMPLETED
18
18
  elsif result[:metadata] == nil
19
19
  Labimotion::ConState::NONE
@@ -2,95 +2,111 @@
2
2
 
3
3
  module Labimotion
4
4
  class VocabularyHandler
5
+ class << self
6
+ def update_vocabularies(properties, current_user, element)
7
+ properties[Labimotion::Prop::LAYERS].each do |key, layer|
8
+ update_layer_vocabularies(layer, key, properties, current_user, element)
9
+ end
10
+ properties
11
+ rescue StandardError => e
12
+ Labimotion.log_exception(e, current_user)
13
+ properties
14
+ end
15
+
16
+ def load_all_vocabularies
17
+ load_from_files + load_from_database
18
+ end
19
+
20
+ def load_app_vocabularies
21
+ load_from_files
22
+ end
23
+
24
+ private
5
25
 
6
- def self.update_vocabularies(properties, current_user, element)
7
- properties[Labimotion::Prop::LAYERS].keys.each do |key|
8
- layer = properties[Labimotion::Prop::LAYERS][key]
9
- field_vocabularies = layer[Labimotion::Prop::FIELDS].select { |ss| ss['is_voc'] == true && ss['opid'] == 9}
26
+ def update_layer_vocabularies(layer, key, properties, current_user, element)
27
+ field_vocabularies = layer[Labimotion::Prop::FIELDS].select { |field| field['is_voc'] && field['opid'] == 9 }
10
28
  field_vocabularies.each do |field|
11
- idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
12
- val = ''
13
- orig_val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']
14
- case field['source']
15
- when 'System'
16
- val = Time.now.strftime('%d/%m/%Y %H:%M') if field['identifier'] == 'dateTime.update'
17
- val = Time.now.strftime('%d/%m/%Y %H:%M') if field['identifier'] == 'dateTime.create' && orig_val.blank?
18
- when 'User'
19
- val = current_user.name if field['identifier'] == 'user.name' && current_user.present?
20
- when 'Element'
21
- val = element.id.to_s if field['identifier'] == 'element.id'
22
- val = element.short_label if element.has_attribute?(:short_label) && field['identifier'] == 'element.short_label'
23
- when 'Segment'
24
- segments = element.segments.joins(:segment_klass).find_by('segment_klasses.identifier = ?', field['source_id'])
25
- next if segments.nil?
29
+ idx = layer[Labimotion::Prop::FIELDS].index(field)
30
+ val = get_vocabulary_value(field, current_user, element)
31
+ update_field_value(properties, key, idx, val) if val.present?
32
+ end
33
+ end
34
+
35
+ def get_vocabulary_value(field, current_user, element)
36
+ case field['source']
37
+ when 'System'
38
+ get_system_value(field)
39
+ when 'User'
40
+ get_user_value(field, current_user)
41
+ when 'Element'
42
+ get_element_value(field, element)
43
+ when 'Segment'
44
+ get_segment_value(field, element)
45
+ when 'Dataset'
46
+ # TODO: Implement Dataset logic here
47
+ nil
48
+ end
49
+ end
50
+
51
+ def get_system_value(field)
52
+ current_time = Time.now.strftime('%d/%m/%Y %H:%M')
53
+ case field['identifier']
54
+ when 'dateTime.update'
55
+ current_time
56
+ when 'dateTime.create'
57
+ current_time if field['value'].blank?
58
+ end
59
+ end
60
+
61
+ def get_user_value(field, current_user)
62
+ return current_user.name if field['identifier'] == 'user.name' && current_user.present?
63
+ end
26
64
 
27
- seg_prop = segments.properties
28
- fields = seg_prop[Labimotion::Prop::LAYERS][field['layer_id']][Labimotion::Prop::FIELDS].select { |ss| ss['field'] == field['field_id'] }
29
- val = fields.first['value']
30
- when 'Dataset'
31
- # byebug
65
+ def get_element_value(field, element)
66
+ case field['identifier']
67
+ when 'element.id'
68
+ element.id.to_s
69
+ when 'element.short_label'
70
+ element.short_label if element.has_attribute?(:short_label)
71
+ when 'element.class'
72
+ if element.class.name === 'Labimotion::Element'
73
+ element.element_klass.label
74
+ else
75
+ element.class.name
32
76
  end
33
- # case field['identifier']
34
- # when 'dateTime.now'
35
- # val = Time.now.strftime('%d/%m/%Y %H:%M')
36
- # when 'user.name'
37
- # val = current_user.name
38
- # when 'element.id'
39
- # val = element.id.to_s
40
- # when 'element.short_label'
41
- # val = element.short_label
42
- # end
43
- # ontology = {
44
- # id: "ncit:class:http://purl.obolibrary.org/obo/#{field['term_id']}",
45
- # iri: "http://purl.obolibrary.org/obo/#{field['term_id']}",
46
- # type: "class",
47
- # label: field['name'],
48
- # obo_id: field['term_id'],
49
- # short_form: field['term_id'],
50
- # description: field['label'],
51
- # }
52
- # properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['ontology'] = ontology if ontology.present?
53
- properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = val if val.present?
54
- ## properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['placeholder'] = val if val.present?
55
77
  end
56
78
  end
57
- properties
58
- rescue StandardError => e
59
- Labimotion.log_exception(e, current_user)
60
- properties
61
- end
62
79
 
63
- def self.load_all_vocabularies
64
- file_data = load_from_files
65
- db_data = load_from_database
66
- file_data + db_data
67
- end
80
+ def get_segment_value(field, element)
81
+ segments = element.segments.joins(:segment_klass).find_by('segment_klasses.identifier = ?', field['source_id'])
82
+ return if segments.nil?
68
83
 
69
- def self.load_app_vocabularies
70
- load_from_files
71
- end
84
+ seg_prop = segments.properties
85
+ fields = seg_prop[Labimotion::Prop::LAYERS][field['layer_id']][Labimotion::Prop::FIELDS]
86
+ fields.find { |ss| ss['field'] == field['field_id'] }&.dig('value')
87
+ end
72
88
 
73
- def self.load_from_files
74
- merged_data = []
75
- # byebug
76
- sys_json = File.join(__dir__, 'data', 'vocab', 'System.json')
77
- # byebug
78
- file_content = File.read(sys_json)
79
- json_data = JSON.parse(file_content)
80
- merged_data.concat(json_data)
89
+ def update_field_value(properties, key, idx, val)
90
+ properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = val
91
+ end
81
92
 
82
- std_json = File.join(__dir__, 'data', 'vocab', 'Standard.json')
83
- file_content = File.read(std_json)
84
- json_data = JSON.parse(file_content)
85
- merged_data.concat(json_data)
86
- merged_data
87
- end
93
+ def load_from_files
94
+ merged_data = []
95
+ merged_data.concat(load_json_file('System.json'))
96
+ merged_data.concat(load_json_file('Standard.json'))
97
+ merged_data
98
+ end
88
99
 
89
- def self.load_from_database
90
- vocabularies = Labimotion::Vocabulary.all.sort_by(&:name)
91
- Labimotion::VocabularyEntity.represent(vocabularies, serializable: true)
92
- end
100
+ def load_json_file(filename)
101
+ file_path = File.join(__dir__, 'data', 'vocab', filename)
102
+ file_content = File.read(file_path)
103
+ JSON.parse(file_content)
104
+ end
93
105
 
94
- private_class_method :load_from_files, :load_from_database
106
+ def load_from_database
107
+ vocabularies = Labimotion::Vocabulary.all.sort_by(&:name)
108
+ Labimotion::VocabularyEntity.represent(vocabularies, serializable: true)
109
+ end
110
+ end
95
111
  end
96
- end
112
+ end
@@ -36,7 +36,7 @@ module Labimotion
36
36
  self.con_state = Labimotion::Converter.jcamp_converter(id, current_user)
37
37
  update_column(:con_state, con_state)
38
38
  when Labimotion::ConState::CONVERTED
39
- Labimotion::Converter.metadata(id)
39
+ Labimotion::Converter.metadata(id, current_user)
40
40
  end
41
41
  end
42
42
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  ## Labimotion Version
4
4
  module Labimotion
5
- VERSION = '1.5.0.rc7'
5
+ VERSION = '1.5.0.rc8'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: labimotion
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0.rc7
4
+ version: 1.5.0.rc8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chia-Lin Lin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-10-11 00:00:00.000000000 Z
12
+ date: 2024-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -123,7 +123,10 @@ files:
123
123
  homepage: https://github.com/LabIMotion/labimotion
124
124
  licenses:
125
125
  - AGPL-3.0
126
- metadata: {}
126
+ metadata:
127
+ homepage_uri: https://github.com/LabIMotion/labimotion
128
+ source_code_uri: https://github.com/LabIMotion/labimotion
129
+ bug_tracker_uri: https://github.com/LabIMotion/labimotion/discussions
127
130
  post_install_message:
128
131
  rdoc_options: []
129
132
  require_paths: