labimotion 0.3.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/lib/labimotion/api.rb +19 -0
  3. data/lib/labimotion/apis/generic_dataset_api.rb +3 -4
  4. data/lib/labimotion/apis/generic_element_api.rb +8 -56
  5. data/lib/labimotion/apis/labimotion_hub_api.rb +4 -13
  6. data/lib/labimotion/apis/segment_api.rb +3 -4
  7. data/lib/labimotion/entities/element_entity.rb +0 -1
  8. data/lib/labimotion/entities/generic_public_entity.rb +0 -2
  9. data/lib/labimotion/helpers/dataset_helpers.rb +6 -14
  10. data/lib/labimotion/helpers/element_helpers.rb +10 -30
  11. data/lib/labimotion/helpers/generic_helpers.rb +13 -13
  12. data/lib/labimotion/helpers/segment_helpers.rb +8 -20
  13. data/lib/labimotion/libs/converter.rb +27 -53
  14. data/lib/labimotion/libs/export_dataset.rb +3 -8
  15. data/lib/labimotion/libs/nmr_mapper.rb +15 -39
  16. data/lib/labimotion/libs/nmr_mapper_repo.rb +278 -0
  17. data/lib/labimotion/libs/template_hub.rb +4 -8
  18. data/lib/labimotion/models/concerns/attachment_converter.rb +9 -10
  19. data/lib/labimotion/models/concerns/datasetable.rb +1 -1
  20. data/lib/labimotion/models/concerns/segmentable.rb +4 -3
  21. data/lib/labimotion/models/concerns/workflow.rb +0 -9
  22. data/lib/labimotion/models/element.rb +8 -17
  23. data/lib/labimotion/models/element_klass.rb +8 -0
  24. data/lib/labimotion/models/segment_klass.rb +0 -2
  25. data/lib/labimotion/{collection → utils}/export.rb +1 -32
  26. data/lib/labimotion/{collection → utils}/import.rb +71 -53
  27. data/lib/labimotion/version.rb +3 -7
  28. data/lib/labimotion.rb +2 -5
  29. metadata +10 -10
  30. data/lib/labimotion/models/hub_log.rb +0 -8
  31. data/lib/labimotion/utils/import_utils.rb +0 -116
@@ -14,38 +14,11 @@ require 'labimotion/utils/utils'
14
14
 
15
15
  module Labimotion
16
16
  class Converter
17
+
17
18
  def self.logger
18
19
  @@converter_logger ||= Logger.new(Rails.root.join('log/converter.log')) # rubocop:disable Style/ClassVars
19
20
  end
20
21
 
21
- def self.process_ds(id, current_user = {})
22
- att = Attachment.find_by(id: id, con_state: Labimotion::ConState::CONVERTED)
23
- return if att.nil? || att.attachable_id.nil? || att.attachable_type != 'Container'
24
-
25
- dsr = []
26
- ols = nil
27
- if Labimotion::IS_RAILS5 == true
28
- Zip::File.open(att.store.path) do |zip_file|
29
- res = Labimotion::Converter.collect_metadata(zip_file) if att.filename.split('.')&.last == 'zip'
30
- ols = res[:o] unless res&.dig(:o).nil?
31
- dsr.push(res[:d]) unless res&.dig(:d).nil?
32
- end
33
- else
34
- Zip::File.open(att.attachment_attacher.file.url) do |zip_file|
35
- res = Labimotion::Converter.collect_metadata(zip_file) if att.filename.split('.')&.last == 'zip'
36
- ols = res[:o] unless res&.dig(:o).nil?
37
- dsr.push(res[:d]) unless res&.dig(:d).nil?
38
- end
39
- end
40
- dsr.flatten!
41
- dataset = build_ds(att.attachable_id, ols)
42
- update_ds(dataset, dsr, current_user) if dataset.present?
43
- att.update_column(:con_state, Labimotion::ConState::COMPLETED)
44
- rescue StandardError => e
45
- Labimotion::Converter.logger.error ["Att ID: #{att&.id}, OLS: #{ols}", "DSR: #{dsr}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
46
- raise e
47
- end
48
-
49
22
  def self.uri(api_name)
50
23
  url = Rails.configuration.converter.url
51
24
  "#{url}#{api_name}"
@@ -152,7 +125,17 @@ module Labimotion
152
125
  primary_store = Rails.configuration.storage.primary_store
153
126
  att.update!(storage: primary_store)
154
127
  end
155
- process_ds(att.id)
128
+
129
+ Zip::File.open(tmp_file.path) do |zip_file|
130
+ res = Labimotion::Converter.collect_metadata(zip_file) if name.split('.')&.last == 'zip'
131
+ ols = res[:o] unless res&.dig(:o).nil?
132
+ dsr.push(res[:d]) unless res&.dig(:d).nil?
133
+ end
134
+
135
+ dsr.flatten!
136
+ if dsr.length.positive? && name.split('.')&.last == 'zip'
137
+ Labimotion::Converter.ts('write', att.attachable_id, ols: ols, info: dsr)
138
+ end
156
139
  rescue StandardError => e
157
140
  raise e
158
141
  ensure
@@ -161,7 +144,8 @@ module Labimotion
161
144
  end
162
145
 
163
146
  def self.process(data)
164
- return data[:a].con_state if data[:a]&.attachable_type != 'Container'
147
+ return if data[:a]&.attachable_type != 'Container'
148
+
165
149
  response = nil
166
150
  begin
167
151
  ofile = Rails.root.join(data[:f], data[:a].filename)
@@ -182,36 +166,37 @@ module Labimotion
182
166
  end
183
167
  if response.ok?
184
168
  Labimotion::Converter.handle_response(data[:a], response)
185
- Labimotion::ConState::PROCESSED
186
169
  else
187
170
  Labimotion::Converter.logger.error ["Converter Response Error: id: [#{data[:a]&.id}], filename: [#{data[:a]&.filename}], response: #{response}"].join($INPUT_RECORD_SEPARATOR)
188
- Labimotion::ConState::ERROR
189
171
  end
172
+ response
190
173
  rescue StandardError => e
191
- Labimotion::Converter.logger.error ["process fail: #{id}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
192
- Labimotion::ConState::ERROR
174
+ raise e
193
175
  ensure
194
176
  FileUtils.rm_f(ofile)
195
177
  end
196
178
  end
197
179
 
198
180
  def self.jcamp_converter(id)
199
- data = Labimotion::Converter.vor_conv(id)
200
- return if data.nil?
181
+ resp = nil
182
+ begin
183
+ data = Labimotion::Converter.vor_conv(id)
184
+ return if data.nil?
201
185
 
202
- Labimotion::Converter.process(data)
203
- rescue StandardError => e
204
- Labimotion::Converter.logger.error ["jcamp_converter fail: #{id}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
205
- Labimotion::ConState::ERROR
186
+ resp = Labimotion::Converter.process(data)
187
+ resp&.success? ? Labimotion::ConState::PROCESSED : Labimotion::ConState::ERROR
188
+ rescue StandardError => e
189
+ Labimotion::ConState::ERROR
190
+ Labimotion::Converter.logger.error ["jcamp_converter fail: #{id}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
191
+ end
206
192
  end
207
193
 
208
194
  def self.generate_ds(att_id, current_user = {})
209
195
  dsr_info = Labimotion::Converter.fetch_dsr(att_id)
210
196
  begin
211
197
  return unless dsr_info && dsr_info[:info]&.length.positive?
212
-
213
198
  dataset = Labimotion::Converter.build_ds(att_id, dsr_info[:ols])
214
- Labimotion::Converter.update_ds(dataset, dsr_info[:info], current_user) if dataset.present?
199
+ Labimotion::Converter.update_ds(dataset, dsr_info[:info], current_user)
215
200
  rescue StandardError => e
216
201
  Labimotion::Converter.logger.error ["Att ID: #{att_id}, OLS: #{dsr_info[:ols]}", "DSR: #{dsr_info[:info]}", e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR)
217
202
  ensure
@@ -248,8 +233,6 @@ module Labimotion
248
233
  new_prop = dataset.properties
249
234
  dsr.each do |ds|
250
235
  layer = layers[ds[:layer]]
251
- next if layer.nil? || layer['fields'].nil?
252
-
253
236
  fields = layer['fields'].select{ |f| f['field'] == ds[:field] }
254
237
  fi = fields&.first
255
238
  idx = layer['fields'].find_index(fi)
@@ -339,15 +322,6 @@ module Labimotion
339
322
  end
340
323
  res
341
324
  end
342
-
343
- def self.metadata(id)
344
- att = Attachment.find(id)
345
- return if att.nil? || att.attachable_id.nil? || att.attachable_type != 'Container'
346
-
347
- ds = Labimotion::Dataset.find_by(element_type: 'Container', element_id: att.attachable_id)
348
- att.update_column(:con_state, Labimotion::ConState::COMPLETED) if ds.present?
349
- process_ds(att.id) if ds.nil?
350
- end
351
325
  end
352
326
  end
353
327
 
@@ -27,7 +27,7 @@ module Labimotion
27
27
  name = ds.dataset_klass.label
28
28
 
29
29
  match = name.match(/\((.*?)\)/)
30
- name = match && match.length > 1 ? match[1] : name
30
+ name = match && match.length > 2 ? match[1] : name
31
31
 
32
32
  name = '1H NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000593'
33
33
  name = '13C NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000595'
@@ -99,14 +99,9 @@ module Labimotion
99
99
  wb = @xfile.workbook
100
100
  gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
101
101
  cds = Container.find(id)
102
- cds_csv = cds.attachments.where(aasm_state: 'csv')
103
- csv_length = cds_csv.length
104
- return if csv_length.zero?
105
- cds_csv.each_with_index do |att, idx|
102
+ cds.attachments.where(aasm_state: 'csv').each do |att|
106
103
  name = File.basename(att.filename, '.csv')
107
- name = name.slice(0, (25 - csv_length.to_s.length - 1))
108
- sheet_name = "#{name}_#{idx}"
109
- sheet = @xfile.workbook.add_worksheet(name: sheet_name)
104
+ sheet = @xfile.workbook.add_worksheet(name: name)
110
105
 
111
106
  if Labimotion::IS_RAILS5 == true
112
107
  File.open(att.store.path) do |fi|
@@ -6,24 +6,8 @@ require 'labimotion/utils/utils'
6
6
  module Labimotion
7
7
  ## NmrMapper
8
8
  class NmrMapper
9
- def self.process_ds(id, current_user = {})
10
- att = Attachment.find_by(id: id, con_state: Labimotion::ConState::NMR)
11
- return if att.nil?
12
-
13
- content = is_brucker_binary(id)
14
- if content.nil?
15
- Labimotion::ConState::NONE
16
- else
17
- data = process(att, id, content)
18
- generate_ds(id, att.attachable_id, data, current_user)
19
- Labimotion::ConState::COMPLETED
20
- end
21
- end
22
-
23
9
  def self.is_brucker_binary(id)
24
- att = Attachment.find_by(id: id, con_state: Labimotion::ConState::NMR)
25
- return if att.nil?
26
-
10
+ att = Attachment.find(id)
27
11
  if Labimotion::IS_RAILS5 == true
28
12
  Zip::File.open(att.store.path) do |zip_file|
29
13
  zip_file.each do |entry|
@@ -48,9 +32,8 @@ module Labimotion
48
32
  nil
49
33
  end
50
34
 
51
- def self.process(att, id, content)
52
- return if att.nil? || content.nil?
53
-
35
+ def self.process(id, content)
36
+ att = Attachment.find(id)
54
37
  lines = content.split("\n").reject(&:empty?)
55
38
  metadata = {}
56
39
  lines.map do |ln|
@@ -59,33 +42,26 @@ module Labimotion
59
42
  end
60
43
  ols = 'CHMO:0000593' if metadata['NUC1'] == '1H'
61
44
  ols = 'CHMO:0000595' if metadata['NUC1'] == '13C'
62
-
63
- { content: { metadata: metadata, ols: ols } }
64
- # if content.present? && att.present?
65
- # Labimotion::NmrMapper.ts('write', att.attachable_id,
66
- # content: { metadata: metadata, ols: ols })
67
- # end
45
+ if content.present? && att.present?
46
+ Labimotion::NmrMapper.ts('write', att.attachable_id,
47
+ content: { metadata: metadata, ols: ols })
48
+ end
68
49
  end
69
50
 
70
51
  def self.fetch_content(id)
71
- atts = Attachment.where(attachable_id: id)
72
- return if atts.nil?
73
-
74
- atts.each do |att|
75
- content = Labimotion::NmrMapper.ts('read', att.id)
76
- return content if content.present?
77
- end
52
+ Labimotion::NmrMapper.ts('read', id)
78
53
  end
79
54
 
55
+ def self.generate_ds(id, current_user = {})
56
+ data = Labimotion::NmrMapper.fetch_content(id)
57
+ return if data.nil?
80
58
 
81
- def self.generate_ds(id, cid, data, current_user = {})
82
- return if data.nil? || cid.nil?
83
-
84
- obj = Labimotion::NmrMapper.build_ds(cid, data[:content])
59
+ obj = Labimotion::NmrMapper.build_ds(id, data[:content])
85
60
  return if obj.nil? || obj[:ols].nil?
86
61
 
87
- Labimotion::NmrMapper.update_ds_1h(cid, obj, current_user) if obj[:ols] == 'CHMO:0000593'
88
- Labimotion::NmrMapper.update_ds_1h(cid, obj, current_user) if obj[:ols] == 'CHMO:0000595'
62
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000593'
63
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000595'
64
+ Labimotion::NmrMapper.clean(id)
89
65
  end
90
66
 
91
67
  def self.update_ds_13c(id, obj)
@@ -0,0 +1,278 @@
1
+ # frozen_string_literal: true
2
+ require 'labimotion/version'
3
+ require 'labimotion/utils/utils'
4
+
5
+ module Labimotion
6
+ class NmrMapperRepo
7
+ def self.is_brucker_binary(id)
8
+ att = Attachment.find(id)
9
+ if Labimotion::IS_RAILS5 == true
10
+ Zip::File.open(att.store.path) do |zip_file|
11
+ zip_file.each do |entry|
12
+ if entry.name.include?('/pdata/') && entry.name.include?('parm.txt')
13
+ metadata = entry.get_input_stream.read.force_encoding('UTF-8')
14
+ return metadata
15
+ end
16
+ end
17
+ end
18
+ else
19
+ if att&.attachment_attacher&.file&.url
20
+ Zip::File.open(att.attachment_attacher.file.url) do |zip_file|
21
+ zip_file.each do |entry|
22
+ if entry.name.include?('/pdata/') && entry.name.include?('parm.txt')
23
+ metadata = entry.get_input_stream.read.force_encoding('UTF-8')
24
+ return metadata
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ nil
32
+ end
33
+
34
+ def self.process(id, content)
35
+ att = Attachment.find(id)
36
+ lines = content.split("\n").reject(&:empty?)
37
+ metadata = {}
38
+ lines.map do |ln|
39
+ arr = ln.split(/\s+/)
40
+ metadata[arr[0]] = arr[1..-1].join(' ') if arr.length > 1
41
+ end
42
+ ols = 'CHMO:0000593' if metadata['NUC1'] == '1H'
43
+ ols = 'CHMO:0000595' if metadata['NUC1'] == '13C'
44
+ if content.present? && att.present?
45
+ Labimotion::NmrMapper.ts('write', att.attachable_id,
46
+ content: { metadata: metadata, ols: ols })
47
+ end
48
+ end
49
+
50
+ def self.fetch_content(id)
51
+ Labimotion::NmrMapper.ts('read', id)
52
+ end
53
+
54
+ def self.generate_ds(id, current_user = {})
55
+ data = Labimotion::NmrMapper.fetch_content(id)
56
+ return if data.nil?
57
+
58
+ obj = Labimotion::NmrMapper.build_ds(id, data[:content])
59
+ return if obj.nil? || obj[:ols].nil?
60
+
61
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000593'
62
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000595'
63
+ Labimotion::NmrMapper.clean(id)
64
+ end
65
+
66
+ def self.update_ds_13c(id, obj)
67
+ # dataset = obj[:dataset]
68
+ # metadata = obj[:metadata]
69
+ # new_prop = dataset.properties
70
+
71
+ # dataset.properties = new_prop
72
+ # dataset.save!
73
+ end
74
+
75
+ def self.set_data(prop, field, idx, layer_name, field_name, value)
76
+ return if field['field'] != field_name || value&.empty?
77
+
78
+ field['value'] = value
79
+ prop['layers'][layer_name]['fields'][idx] = field
80
+ prop
81
+ end
82
+
83
+ def self.update_ds_1h(id, obj, current_user)
84
+ dataset = obj[:dataset]
85
+ metadata = obj[:metadata]
86
+ new_prop = dataset.properties
87
+ new_prop.dig('layers', 'general', 'fields')&.each_with_index do |fi, idx|
88
+ # new_prop = set_data(new_prop, fi, idx, 'general', 'title', metadata['NAME'])
89
+ if fi['field'] == 'title' && metadata['NAME'].present?
90
+ ## fi['label'] = fi['label']
91
+ fi['value'] = metadata['NAME']
92
+ fi['device'] = metadata['NAME']
93
+ fi['dkey'] = 'NAME'
94
+ new_prop['layers']['general']['fields'][idx] = fi
95
+ end
96
+
97
+ if fi['field'] == 'date' && metadata['Date_'].present?
98
+ ## fi['label'] = fi['label']
99
+ fi['value'] = metadata['Date_']
100
+ fi['device'] = metadata['Date_']
101
+ fi['dkey'] = 'Date_'
102
+ new_prop['layers']['general']['fields'][idx] = fi
103
+ end
104
+
105
+ if fi['field'] == 'time' && metadata['Time'].present?
106
+ ## fi['label'] = fi['label']
107
+ fi['value'] = metadata['Time']
108
+ fi['device'] = metadata['Time']
109
+ fi['dkey'] = 'Time'
110
+ new_prop['layers']['general']['fields'][idx] = fi
111
+ end
112
+
113
+ if fi['field'] == 'creator' && current_user.present?
114
+ ## fi['label'] = fi['label']
115
+ fi['value'] = current_user.name
116
+ new_prop['layers']['general']['fields'][idx] = fi
117
+ end
118
+ end
119
+ element = Container.find(id)&.root_element
120
+ element.present? && element&.class&.name == 'Sample' && new_prop.dig('layers', 'sample_details', 'fields')&.each_with_index do |fi, idx|
121
+ if fi['field'] == 'label'
122
+ fi['value'] = element.short_label
123
+ new_prop['layers']['sample_details']['fields'][idx] = fi
124
+ end
125
+ if fi['field'] == 'id'
126
+ fi['value'] = element.id
127
+ new_prop['layers']['sample_details']['fields'][idx] = fi
128
+ end
129
+ end
130
+
131
+ new_prop.dig('layers', 'instrument', 'fields')&.each_with_index do |fi, idx|
132
+ if fi['field'] == 'instrument' && metadata['INSTRUM'].present?
133
+ # fi['label'] = fi['label']
134
+ fi['value'] = metadata['INSTRUM']
135
+ fi['device'] = metadata['INSTRUM']
136
+ fi['dkey'] = 'INSTRUM'
137
+ new_prop['layers']['instrument']['fields'][idx] = fi
138
+ end
139
+ end
140
+
141
+
142
+ new_prop.dig('layers', 'equipment', 'fields')&.each_with_index do |fi, idx|
143
+ if fi['field'] == 'probehead' && metadata['PROBHD'].present?
144
+ # fi['label'] = fi['label']
145
+ fi['value'] = metadata['PROBHD']
146
+ fi['device'] = metadata['PROBHD']
147
+ fi['dkey'] = 'PROBHD'
148
+ new_prop['layers']['equipment']['fields'][idx] = fi
149
+ end
150
+ end
151
+
152
+ new_prop.dig('layers', 'sample_preparation', 'fields')&.each_with_index do |fi, idx|
153
+ if fi['field'] == 'solvent' && metadata['SOLVENT'].present?
154
+ # fi['label'] = fi['label']
155
+ fi['value'] = metadata['SOLVENT']
156
+ fi['device'] = metadata['SOLVENT']
157
+ fi['dkey'] = 'SOLVENT'
158
+ fi['value'] = 'chloroform-D1 (CDCl3)' if metadata['SOLVENT'] == 'CDCl3'
159
+ new_prop['layers']['sample_preparation']['fields'][idx] = fi
160
+ end
161
+ end
162
+
163
+
164
+ new_prop.dig('layers', 'set', 'fields')&.each_with_index do |fi, idx|
165
+ if fi['field'] == 'temperature' && metadata['TE'].present?
166
+ # fi['label'] = fi['label']
167
+ fi['value'] = metadata['TE'].split(/\s+/).first
168
+ fi['device'] = metadata['TE']
169
+ fi['dkey'] = 'TE'
170
+ fi['value_system'] = metadata['TE'].split(/\s+/).last
171
+ new_prop['layers']['set']['fields'][idx] = fi
172
+ end
173
+ if fi['field'] == 'ns' && metadata['NS'].present?
174
+ # fi['label'] = fi['label']
175
+ fi['value'] = metadata['NS']
176
+ fi['device'] = metadata['NS']
177
+ fi['dkey'] = 'NS'
178
+ new_prop['layers']['set']['fields'][idx] = fi
179
+ end
180
+ if fi['field'] == 'PULPROG' && metadata['PULPROG'].present?
181
+ # fi['label'] = fi['label']
182
+ fi['value'] = metadata['PULPROG']
183
+ fi['device'] = metadata['PULPROG']
184
+ fi['dkey'] = 'PULPROG'
185
+ new_prop['layers']['set']['fields'][idx] = fi
186
+ end
187
+ if fi['field'] == 'td' && metadata['TD'].present?
188
+ # fi['label'] = fi['label']
189
+ fi['value'] = metadata['TD']
190
+ fi['device'] = metadata['TD']
191
+ fi['dkey'] = 'TD'
192
+ new_prop['layers']['set']['fields'][idx] = fi
193
+ end
194
+ if fi['field'] == 'done' && metadata['D1'].present?
195
+ # fi['label'] = fi['label']
196
+ fi['value'] = metadata['D1']
197
+ fi['device'] = metadata['D1']
198
+ fi['dkey'] = 'D1'
199
+ new_prop['layers']['set']['fields'][idx] = fi
200
+ end
201
+ if fi['field'] == 'sf' && metadata['SF'].present?
202
+ ## fi['label'] = fi['label']
203
+ fi['value'] = metadata['SF']
204
+ fi['device'] = metadata['SF']
205
+ fi['dkey'] = 'SF'
206
+ new_prop['layers']['set']['fields'][idx] = fi
207
+ end
208
+ if fi['field'] == 'sfoone' && metadata['SFO1'].present?
209
+ ## fi['label'] = fi['label']
210
+ fi['value'] = metadata['SFO1']
211
+ fi['device'] = metadata['SFO1']
212
+ fi['dkey'] = 'SFO1'
213
+ new_prop['layers']['set']['fields'][idx] = fi
214
+ end
215
+ if fi['field'] == 'sfotwo' && metadata['SFO2'].present?
216
+ ## fi['label'] = fi['label']
217
+ fi['value'] = metadata['SFO2']
218
+ fi['device'] = metadata['SFO2']
219
+ fi['dkey'] = 'SFO2'
220
+ new_prop['layers']['set']['fields'][idx] = fi
221
+ end
222
+ if fi['field'] == 'nucone' && metadata['NUC1'].present?
223
+ ## fi['label'] = fi['label']
224
+ fi['value'] = metadata['NUC1']
225
+ fi['device'] = metadata['NUC1']
226
+ fi['dkey'] = 'NUC1'
227
+ new_prop['layers']['set']['fields'][idx] = fi
228
+ end
229
+ if fi['field'] == 'nuctwo' && metadata['NUC2'].present?
230
+ ## fi['label'] = fi['label']
231
+ fi['value'] = metadata['NUC2']
232
+ fi['device'] = metadata['NUC2']
233
+ fi['dkey'] = 'NUC2'
234
+ new_prop['layers']['set']['fields'][idx] = fi
235
+ end
236
+ end
237
+ dataset.properties = new_prop
238
+ dataset.save!
239
+ end
240
+
241
+
242
+ def self.ts(method, identifier, params = nil)
243
+ Rails.cache.send(method, "#{Labimotion::NmrMapper.new.class.name}#{identifier}", params)end
244
+
245
+ def self.clean(id)
246
+ Labimotion::NmrMapper.ts('delete', id)
247
+ end
248
+
249
+ def self.build_ds(id, content)
250
+ ds = Container.find_by(id: id)
251
+ return if ds.nil? || content.nil?
252
+
253
+ ols = content[:ols]
254
+ metadata = content[:metadata]
255
+
256
+ return if ols.nil? || metadata.nil?
257
+
258
+ klass = Labimotion::DatasetKlass.find_by(ols_term_id: ols)
259
+ return if klass.nil?
260
+
261
+ uuid = SecureRandom.uuid
262
+ props = klass.properties_release
263
+ props['uuid'] = uuid
264
+ props['pkg'] = Labimotion::Utils.pkg(props['pkg'])
265
+ props['klass'] = 'Dataset'
266
+ dataset = Labimotion::Dataset.create!(
267
+ uuid: uuid,
268
+ dataset_klass_id: klass.id,
269
+ element_type: 'Container',
270
+ element_id: ds.id,
271
+ properties: props,
272
+ properties_release: klass.properties_release,
273
+ klass_uuid: klass.uuid
274
+ )
275
+ { dataset: dataset, metadata: metadata, ols: ols }
276
+ end
277
+ end
278
+ end
@@ -43,15 +43,11 @@ module Labimotion
43
43
  error!('Cannot connect to Chemotion Repository', 401)
44
44
  end
45
45
 
46
- def self.fetch_identifier(klass, identifier, origin)
47
- body = { klass: klass, identifier: identifier, origin: origin }
48
- response = HTTParty.post(
49
- uri('fetch'),
50
- body: body,
51
- timeout: 10
52
- )
46
+ def self.fetch_identifier(klass, identifier)
47
+ # body = { klass: klass, identifier: identifier }
48
+ response = HTTParty.get("#{uri('fetch')}?klass=#{klass}&identifier=#{identifier}", timeout: 10)
53
49
  # response.parsed_response if response.code == 200
54
- JSON.parse(response.body) if response.code == 201
50
+ JSON.parse(response.body) if response.code == 200
55
51
  rescue StandardError => e
56
52
  Labimotion.log_exception(e)
57
53
  error!('Cannot connect to Chemotion Repository', 401)
@@ -10,7 +10,6 @@ module Labimotion
10
10
  after_update :exec_converter
11
11
  def init_converter
12
12
  return if self.has_attribute?(:con_state) == false || con_state.present?
13
-
14
13
  if Rails.configuration.try(:converter).try(:url) && ACCEPTED_FORMATS.include?(File.extname(filename&.downcase))
15
14
  self.con_state = Labimotion::ConState::WAIT
16
15
  end
@@ -23,19 +22,19 @@ module Labimotion
23
22
  end
24
23
 
25
24
  def exec_converter
26
- return if self.has_attribute?(:con_state) == false || self.con_state.nil? || self.con_state == Labimotion::ConState::NONE
27
-
28
- return if attachable_id.nil? && self.con_state != Labimotion::ConState::WAIT
29
-
25
+ #return if attachable_id.nil?
26
+ return if self.has_attribute?(:con_state) == false || con_state.nil? || con_state == Labimotion::ConState::NONE
30
27
  case con_state
31
28
  when Labimotion::ConState::NMR
32
- self.con_state = Labimotion::NmrMapper.process_ds(id)
33
- update_column(:con_state, con_state)
29
+ content = Labimotion::NmrMapperRepo.is_brucker_binary(id)
30
+ if content.nil?
31
+ self.con_state = Labimotion::ConState::NONE
32
+ else
33
+ Labimotion::NmrMapperRepo.process(id, content)
34
+ self.con_state = Labimotion::ConState::PROCESSED
35
+ end
34
36
  when Labimotion::ConState::WAIT
35
37
  self.con_state = Labimotion::Converter.jcamp_converter(id)
36
- update_column(:con_state, con_state)
37
- when Labimotion::ConState::CONVERTED
38
- Labimotion::Converter.metadata(id)
39
38
  end
40
39
  end
41
40
  end
@@ -23,7 +23,7 @@ module Labimotion
23
23
  uuid = SecureRandom.uuid
24
24
  props = args[:properties]
25
25
  props['pkg'] = Labimotion::Utils.pkg(props['pkg'])
26
- props['identifier'] = klass.identifier if klass.identifier.present?
26
+ props['identifier'] = klass.identifier
27
27
  props['uuid'] = uuid
28
28
  props['klass'] = 'Dataset'
29
29
 
@@ -41,13 +41,14 @@ module Labimotion
41
41
 
42
42
  def save_segments(**args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
43
43
  return if args[:segments].nil?
44
+
44
45
  segments = []
45
- args[:segments]&.each do |seg|
46
+ args[:segments].each do |seg|
46
47
  klass = Labimotion::SegmentKlass.find_by(id: seg['segment_klass_id'])
47
48
  uuid = SecureRandom.uuid
48
49
  props = seg['properties']
49
50
  props['pkg'] = Labimotion::Utils.pkg(props['pkg'])
50
- props['identifier'] = klass.identifier if klass.identifier.present?
51
+ props['identifier'] = identifier
51
52
  props['uuid'] = uuid
52
53
  props['klass'] = 'Segment'
53
54
  segment = Labimotion::Segment.find_by(element_type: Labimotion::Utils.element_name(self.class.name), element_id: self.id, segment_klass_id: seg['segment_klass_id'])
@@ -58,7 +59,7 @@ module Labimotion
58
59
  next if segment.present?
59
60
 
60
61
  props['klass_uuid'] = klass.uuid
61
- segment = Labimotion::Segment.create!(properties_release: klass.properties_release, segment_klass_id: seg['segment_klass_id'], element_type: self.class.name, element_id: self.id, properties: props, created_by: args[:current_user_id], uuid: uuid, klass_uuid: klass.uuid)
62
+ segment = Labimotion::Segment.create!(properties_release: klass.properties_release, segment_klass_id: seg['segment_klass_id'], element_type: Labimotion::Utils.element_name(self.class.name), element_id: self.id, properties: props, created_by: args[:current_user_id], uuid: uuid, klass_uuid: klass.uuid)
62
63
  segments.push(segment)
63
64
  end
64
65
  segments
@@ -23,14 +23,5 @@ module Labimotion
23
23
  end
24
24
  properties
25
25
  end
26
-
27
- def migrate_workflow
28
- return if properties_template.nil? || properties_release.nil?
29
-
30
- return if properties_template['flow'].nil? && properties_release['flow'].nil?
31
-
32
- update_column(:properties_template, split_workflow(properties_template)) if properties_template['flow']
33
- update_column(:properties_release, split_workflow(properties_release)) if properties_release['flow']
34
- end
35
26
  end
36
27
  end