Class: Labimotion::ImportUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/utils/import_utils.rb

Class Method Summary collapse

Class Method Details

.create_segment_klass(sk_obj, segment_klass, element_klass, current_user_id) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/labimotion/utils/import_utils.rb', line 310

def self.create_segment_klass(sk_obj, segment_klass, element_klass, current_user_id)
  return if segment_klass.present? || element_klass.nil? || sk_obj.nil?

  segment_klass = Labimotion::SegmentKlass.create!(sk_obj.slice(
      'label',
      'desc',
      'properties_template',
      'is_active',
      'place',
      'properties_release',
      'uuid',
      'identifier',
      'sync_time'
    ).merge(
      element_klass: element_klass,
      created_by: current_user_id,
      released_at: DateTime.now
    )
  )

  segment_klass
end

.proc_element(layer, key, data, instances, properties, elements) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/labimotion/utils/import_utils.rb', line 186

def self.proc_element(layer, key, data, instances, properties, elements)
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_ELEMENT }
  fields.each do |field|
    idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
    id = field["value"] && field["value"]["el_id"] unless idx.nil?
    att_el = (elements && elements[id]) || instances.fetch(Labimotion::Prop::L_ELEMENT, nil)&.fetch(id, nil)
    if att_el.nil?
      properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = {}
    else
      val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']
      val = proc_assign_element(val, att_el)
      properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = val
    end
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end

.proc_molecule(layer, key, data, properties) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/labimotion/utils/import_utils.rb', line 236

def self.proc_molecule(layer, key, data, properties)
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
  fields.each do |field|
    idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
    next if idx.nil? || field.fetch('value', nil).nil?

    next unless field.fetch('value', nil).is_a?(Hash)

    id = field.fetch('value', nil)&.fetch('el_id', nil) unless idx.nil?
    molfile = data.fetch(Labimotion::Prop::MOLECULE, nil)&.fetch(id, nil)&.fetch('molfile', nil) unless id.nil?
    next if molfile.nil?

    mol = Molecule.find_or_create_by_molfile(molfile)
    val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']
    properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = proc_assign_molecule(val, mol)
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end

.proc_reaction(layer, key, data, instances, properties) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/labimotion/utils/import_utils.rb', line 154

def self.proc_reaction(layer, key, data, instances, properties)
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::SYS_REACTION }
  fields.each do |field|
    idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
    id = field["value"] && field["value"]["el_id"] unless idx.nil?
    reaction = instances.fetch(Labimotion::Prop::REACTION, nil)&.fetch(id, nil)
    val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']
    val = proc_assign_reaction(val, reaction)
    properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = val
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end

.proc_sample(layer, key, data, instances, properties) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/labimotion/utils/import_utils.rb', line 170

def self.proc_sample(layer, key, data, instances, properties)
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
  fields.each do |field|
    idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
    id = field["value"] && field["value"]["el_id"] unless idx.nil?
    sample = instances.fetch(Labimotion::Prop::SAMPLE, nil)&.fetch(id, nil)
    val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']
    val = proc_assign_sample(val, sample)
    properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = val
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end

.proc_table(layer, key, data, instance, properties) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/labimotion/utils/import_utils.rb', line 259

def self.proc_table(layer, key, data, instance, properties)
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
  fields&.each do |field|
    tidx = layer[Labimotion::Prop::FIELDS].index(field)
    next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?

    proc_table_prop(layer, key, data, instance, properties, field, tidx, Labimotion::FieldType::DRAG_MOLECULE)
    proc_table_prop(layer, key, data, instance, properties, field, tidx, Labimotion::FieldType::DRAG_SAMPLE)
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end

.proc_upload(layer, key, data, instances, attachments, element) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/labimotion/utils/import_utils.rb', line 206

def self.proc_upload(layer, key, data, instances, attachments, element)
  properties = element.properties
  upload_type = upload_type(element)
  return if upload_type.nil?

  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::UPLOAD }
  fields.each do |field|
    idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
    next if idx.nil?

    files = field["value"] && field["value"]["files"]
    files&.each_with_index do |fi, fdx|
      aid = properties['layers'][key]['fields'][idx]['value']['files'][fdx]['aid']
      uid = properties['layers'][key]['fields'][idx]['value']['files'][fdx]['uid']
      att = data.fetch('Attachment', nil)&.fetch(aid, nil)
      attachment = Attachment.find_by('id IN (?) AND filename LIKE ? ', attachments, uid << '%')
      next if attachment.nil? || att.nil?

      attachment.update!(attachable_id: element.id, attachable_type: upload_type, transferred: true, aasm_state: att['aasm_state'], filename: att['filename'])
      val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'][fdx]
      val = proc_assign_upload(val, attachment)
      properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['files'][fdx] = val
    end
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end

.process_ai(data, instances) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/labimotion/utils/import_utils.rb', line 291

def self.process_ai(data, instances)
  ai_has_changed = false
  instances&.fetch(Labimotion::Prop::L_ELEMENT, nil)&.each do |uuid, element|
    properties = element.properties
    properties.fetch(Labimotion::Prop::LAYERS, nil)&.keys&.each do |key|
      layer = properties[Labimotion::Prop::LAYERS][key]
      layer.fetch('ai', nil)&.each_with_index do |ai_key, idx|
        ana = instances.fetch(Labimotion::Prop::CONTAINER)&.fetch(ai_key, nil)
        properties[Labimotion::Prop::LAYERS][key]['ai'][idx] = ana.id unless ana.nil?
        ai_has_changed = true
      end
    end
    element.update_columns(properties: properties) if ai_has_changed
  end
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end

.properties_handler(data, instances, attachments, elmenet, elements) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/labimotion/utils/import_utils.rb', line 274

def self.properties_handler(data, instances, attachments, elmenet, elements)
  properties = elmenet.properties
  properties.fetch(Labimotion::Prop::LAYERS, nil)&.keys&.each do |key|
    layer = properties[Labimotion::Prop::LAYERS][key]
    properties = proc_molecule(layer, key, data, properties)
    properties = proc_upload(layer, key, data, instances, attachments, elmenet)
    properties = proc_reaction(layer, key, data, instances, properties)
    properties = proc_sample(layer, key, data, instances, properties)
    properties = proc_element(layer, key, data, instances, properties, elements) # unless elements.nil?
    properties = proc_table(layer, key, data, instances, properties)
  end
  properties
rescue StandardError => e
  Labimotion.log_exception(e)
  properties
end