Class: Labimotion::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/collection/import.rb

Class Method Summary collapse

Class Method Details

.import_datasets(data, instances, gt, current_user_id, &update_instances) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/labimotion/collection/import.rb', line 44

def self.import_datasets(data, instances, gt, current_user_id, &update_instances)
  begin
    data.fetch('Labimotion::Dataset', {}).each do |uuid, fields|
      klass_id = fields['dataset_klass_id']
      dk_obj = data.fetch('Labimotion::DatasetKlass', {})[klass_id]
      dk_id = dk_obj['identifier']
      element_uuid = fields.fetch('element_id')
      element_type = fields.fetch('element_type')
      element = instances.fetch(element_type).fetch(element_uuid)

      dataset_klass = Labimotion::DatasetKlass.find_by(identifier: dk_id) if dk_id.present?
      next if gt == true && dataset_klass.nil?

      dkr = Labimotion::DatasetKlassesRevision.find_by(uuid: fields.fetch('klass_uuid'))
      dataset_klass = dkr.dataset_klass if dataset_klass.nil? && dkr.present?
      next if dataset_klass.nil? || dataset_klass.ols_term_id != dk_obj['ols_term_id']

      dataset_klass = Labimotion::DatasetKlass.find_by(ols_term_id: dk_obj['ols_term_id']) if dataset_klass.nil?
      next if dataset_klass.nil?

      dataset = Labimotion::Dataset.create!(
        fields.slice(
          'properties', 'properties_release'
        ).merge(
          ## created_by: current_user_id,
          element: element,
          dataset_klass: dataset_klass,
          uuid: SecureRandom.uuid,
          klass_uuid: dkr&.uuid || dataset_klass.uuid
        )
      )
      update_instances.call(uuid, dataset)
    end
  rescue StandardError => e
    Rails.logger.error(e.backtrace)
    raise
  end
end

.import_elements(data, instances, gt, current_user_id, fetch_many, &update_instances) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/labimotion/collection/import.rb', line 130

def self.import_elements(data, instances, gt, current_user_id, fetch_many, &update_instances)
  data.fetch('Labimotion::Element', {}).each do |uuid, fields|
    klass_id = fields["element_klass_id"]
    ek_obj = data.fetch('Labimotion::ElementKlass', {})[klass_id]
    ek_id = ek_obj["identifier"]
    element_klass = Labimotion::ElementKlass.find_by(identifier: ek_id) if ek_id.present?
    element_klass = Labimotion::ElementKlass.find_by(uuid: fields.fetch('klass_uuid')) if element_klass.nil?

    if element_klass.nil?
      ekr = Labimotion::ElementKlassesRevision.find_by(uuid: fields.fetch('klass_uuid'))
      element_klass = ekr.element_klass if element_klass.nil? && ekr.present?
    end
    next if element_klass.nil?

    element = Labimotion::Element.create!(
      fields.slice(
        'name', 'properties', 'properties_release'
      ).merge(
        created_by: current_user_id,
        element_klass: element_klass,
        collections: fetch_many.call(
          'Collection', 'Labimotion::CollectionsElement', 'element_id', 'collection_id', uuid
        ),
        uuid: SecureRandom.uuid,
        klass_uuid: ekr&.uuid || element_klass.uuid
      )
    )

    properties = Labimotion::ImportUtils.properties_handler(data, element.properties)
    element.update!(properties: properties)
    update_instances.call(uuid, element)
    element.container = Container.create_root_container
  end
rescue StandardError => e
  Rails.logger.error(e.backtrace)
  raise
end

.import_repo_segment_props(instances, attachments, attachable_uuid, fields) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/labimotion/collection/import.rb', line 7

def self.import_repo_segment_props(instances, attachments, attachable_uuid, fields)
  primary_store = Rails.configuration.storage.primary_store
  attachable = instances.fetch('Labimotion::Segment').fetch(attachable_uuid)
  attachment = Attachment.where(id: attachments, filename: fields.fetch('identifier')).first
  attachment.update!(
    attachable_id: attachable.id,
    attachable_type: 'SegmentProps',
    con_state: Labimotion::ConState::NONE,
    transferred: true,
    aasm_state: fields.fetch('aasm_state'),
    filename: fields.fetch('filename'),
    content_type: fields.fetch('content_type'),
    storage: primary_store
    # checksum: fields.fetch('checksum'),
    # created_at: fields.fetch('created_at'),
    # updated_at: fields.fetch('updated_at')
  )

  properties = attachable.properties
  properties['layers'].keys.each do |key|
    layer = properties['layers'][key]
    field_uploads = layer['fields'].select { |ss| ss['type'] == 'upload' }
    field_uploads&.each do |upload|
      idx = properties['layers'][key]['fields'].index(upload)
      files = upload["value"] && upload["value"]["files"]
      files&.each_with_index do |fi, fdx|
        if properties['layers'][key]['fields'][idx]['value']['files'][fdx]['uid'] == fields.fetch('identifier')
          properties['layers'][key]['fields'][idx]['value']['files'][fdx]['aid'] = attachment.id
          properties['layers'][key]['fields'][idx]['value']['files'][fdx]['uid'] = attachment.identifier
        end
      end
    end
  end
  attachable.update!(properties: properties)
  attachment
end

.import_segments(data, instances, gt, current_user_id, &update_instances) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/labimotion/collection/import.rb', line 83

def self.import_segments(data, instances, gt, current_user_id, &update_instances)
  begin
    data.fetch('Labimotion::Segment', {}).each do |uuid, fields|
      klass_id = fields["segment_klass_id"]
      sk_obj = data.fetch('Labimotion::SegmentKlass', {})[klass_id]
      sk_id = sk_obj["identifier"]
      ek_obj = data.fetch('Labimotion::ElementKlass').fetch(sk_obj["element_klass_id"])
      element_klass = Labimotion::ElementKlass.find_by(name: ek_obj['name']) if ek_obj.present?
      next if element_klass.nil? || ek_obj.nil? || ek_obj['is_generic'] == true

      element_uuid = fields.fetch('element_id')
      element_type = fields.fetch('element_type')
      element = instances.fetch(element_type).fetch(element_uuid)
      segment_klass = Labimotion::SegmentKlass.find_by(identifier: sk_id) if sk_id.present?
      segment_klass = Labimotion::SegmentKlass.find_by(uuid: fields.fetch('klass_uuid')) if segment_klass.nil?

      if segment_klass.nil?
        skr = Labimotion::SegmentKlassesRevision.find_by(uuid: fields.fetch('klass_uuid'))
        segment_klass = skr.segment_klass if segment_klass.nil? && skr.present?
      end

      next if segment_klass.nil? || element.nil?

      ## segment_klass = Labimotion::ImportUtils.create_segment_klass(sk_obj, segment_klass, element_klass, current_user_id)

      segment = Labimotion::Segment.create!(
        fields.slice(
          'properties', 'properties_release'
        ).merge(
          created_by: current_user_id,
          element: element,
          segment_klass: segment_klass,
          uuid: SecureRandom.uuid,
          klass_uuid: skr&.uuid || segment_klass.uuid
        )
      )

      properties = Labimotion::ImportUtils.properties_handler(data, segment.properties)
      segment.update!(properties: properties)
      update_instances.call(uuid, segment)
    end
  rescue StandardError => e
    Rails.logger.error(e.backtrace)
    raise
  end
end