Class: Labimotion::ExportElement

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/export_element.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_user, element, export_format) ⇒ ExportElement

Returns a new instance of ExportElement.



8
9
10
11
12
13
14
15
16
# File 'lib/labimotion/libs/export_element.rb', line 8

def initialize(current_user, element, export_format)
  @current_user = current_user
  @element = element
  @properties = element.properties
  @options = element.properties_release[Labimotion::Prop::SEL_OPTIONS]
  @export_format = export_format
rescue StandardError => e
  Labimotion.log_exception(e)
end

Instance Method Details

#build_field(layer, field) ⇒ Object



42
43
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
82
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
# File 'lib/labimotion/libs/export_element.rb', line 42

def build_field(layer, field)
  field_obj = {}
  field_obj[:label] = field['label']
  field_obj[:field] = field['field']
  field_obj[:type] = field['type']

  field_obj.merge!(field.slice('value', 'value_system'))
  field_obj[:is_table] = false
  field_obj[:not_table] = true
  case field['type']
  when Labimotion::FieldType::DRAG_ELEMENT
    field_obj[:value] = (field['value'] && field['value']['el_label']) || ''
    field_obj[:obj] = field['value']  ### change to object
  when Labimotion::FieldType::DRAG_SAMPLE
    val = field.fetch('value', {})
    instance = Sample.find_by(id: val['el_id'])
    field_obj[:value] = val['el_label']
    field_obj[:has_structure] = true
    obj_os = Entities::SampleReportEntity.new(
      instance,
      current_user: @current_user,
      detail_levels: ElementDetailLevelCalculator.new(user: @current_user, element: instance).detail_levels,
    ).serializable_hash
    obj_final = OpenStruct.new(obj_os)
    field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_final, format: 'png').generate
  when Labimotion::FieldType::DRAG_MOLECULE
    val = field.fetch('value', {})
    obj = Molecule.find_by(id: val['el_id'])
    field_obj[:value] = val['el_label']
    # field_obj[:has_structure] = true
    # field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj, format: 'png').generate
  when Labimotion::FieldType::SELECT
    field_obj[:value] = @options.fetch(field['option_layers'], nil)&.fetch('options', nil)&.find { |ss| ss['key'] == field['value'] }&.fetch('label', nil) || field['value']
  when Labimotion::FieldType::UPLOAD
    files = field.fetch('value', nil)&.fetch('files', [])
    val = files&.map { |file| "#{file['filename']} #{file['label']}" }&.join('\n')
    field_obj[:value] = val
  when Labimotion::FieldType::TABLE
    field_obj[:is_table] = true
    field_obj[:not_table] = false
    tbl = []
    ## tbl_idx = []
    header = {}

    sub_fields = field.fetch('sub_fields', [])
    sub_fields.each_with_index do |sub_field, idx|
      header["col#{idx}"] = sub_field['col_name']
    end
    tbl.push(header)
    field.fetch('sub_values', []).each do |sub_val|
      data = {}
      sub_fields.each_with_index do |sub_field, idx|
        data["col#{idx}"] = build_table_field(sub_val, sub_field)
      end
      tbl.push(data)
    end
    field_obj[:data] = tbl
    # field_obj[:value] = 'this is a table'
  when Labimotion::FieldType::INPUT_GROUP
    val = []
    field.fetch('sub_fields', [])&.each do |sub_field|
      if sub_field['type'] == Labimotion::FieldType::SYSTEM_DEFINED
        val.push("#{sub_field['value']} #{sub_field['value_system']}")
      else
        val.push(sub_field['value'])
      end
    end
    field_obj[:value] = val.join(' ')
  end
  field_obj
rescue StandardError => e
  Labimotion.log_exception(e)
end

#build_field_html(layer, field, cols) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/labimotion/libs/export_element.rb', line 177

def build_field_html(layer, field, cols)
  case field['type']
  when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
    val = (field['value'] && field['value']['el_label']) || ''
  when Labimotion::FieldType::UPLOAD
    val = (field['value'] && field['value']['files'] && field['value']['files'].first && field['value']['files'].first['filename'] ) || ''
  else
    val = field['value']
  end
  htd = field['hasOwnRow'] == true ? "<td colspan=#{cols}>" : '<td>'
  "#{htd}<b>#{field['label']}: </b><br />#{val}</td>"
rescue StandardError => e
  Labimotion.log_exception(e)
end

#build_fields(layer) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/labimotion/libs/export_element.rb', line 129

def build_fields(layer)
  fields = layer[Labimotion::Prop::FIELDS] || []
  field_objs = []
  fields.each do |field|
    next if field['type'] == 'dummy'

    field_obj = build_field(layer, field)
    field_objs.push(field_obj)
  end
  field_objs
rescue StandardError => e
  Labimotion.log_exception(e)
end

#build_fields_html(layer) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/labimotion/libs/export_element.rb', line 192

def build_fields_html(layer)
  fields = layer[Labimotion::Prop::FIELDS] || []
  field_objs = []
  cols = layer['cols'] || 0
  field_objs.push('<table style="width: 4000dxa"><tr>')
  fields.each do |field|
    if cols&.zero? || field['hasOwnRow'] == true
      field_objs.push('</tr><tr>')
      cols = layer['cols']
    end
    field_obj = build_field_html(layer, field, layer['cols'])
    field_objs.push(field_obj)
    cols -= 1
    cols = 0 if field['hasOwnRow'] == true
  end
  field_objs.push('</tr></table>')
  field_objs&.join("")&.gsub('<tr></tr>', '')
rescue StandardError => e
  Labimotion.log_exception(e)
end

#build_layersObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/labimotion/libs/export_element.rb', line 18

def build_layers
  objs = []
  @properties[Labimotion::Prop::LAYERS]&.keys&.sort_by { |key| @properties[Labimotion::Prop::LAYERS][key]['position'] }&.each do |key|
    layer = @properties[Labimotion::Prop::LAYERS][key] || {}

    ## Build fields html
    # field_objs = build_fields_html(layer) if layer[Labimotion::Prop::FIELDS]&.length&.positive?
    # field_html = Sablon.content(:html, field_objs) if field_objs.present?
    field_objs = build_fields(layer)

    layer_info = {
      label: layer['label'],
      layer: layer['layer'],
      cols: layer['cols'],
      timeRecord: layer['timeRecord'],
      fields: field_objs
    }
    objs.push(layer_info)
  end
  objs
rescue StandardError => e
  Labimotion.log_exception(e)
end

#build_layers_htmlObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/labimotion/libs/export_element.rb', line 214

def build_layers_html
  layer_html = []
  first_line = true
  @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
    layer_html.push('</tr></table>') if first_line == false
    layer = @properties[Labimotion::Prop::LAYERS][key] || {}
    fields = layer[Labimotion::Prop::FIELDS] || []
    layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>")
    layer_html.push('<table><tr>')
    cols = layer['cols']
    fields.each do |field|
      if (cols === 0)
        layer_html.push('</tr><tr>')
      end

      val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value']
      layer_html.push("<td>#{field['label']}: <br />#{val}</td>")
      cols -= 1
    end
    first_line = false
  end
  layer_html.push('</tr></table>')
  layer_html.join('')
rescue StandardError => e
  Labimotion.log_exception(e)
end

#build_table_field(sub_val, sub_field) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/labimotion/libs/export_element.rb', line 116

def build_table_field(sub_val, sub_field)
  return '' if sub_field.fetch('id', nil).nil? || sub_val[sub_field['id']].nil?

  case sub_field['type']
  when Labimotion::FieldType::SELECT
    sub_val[sub_field['id']]['value']
  when Labimotion::FieldType::SYSTEM_DEFINED
    "#{sub_val[sub_field['id']]['value']} #{sub_val[sub_field['id']]['value_system']}"
  else
    sub_val[sub_field['id']]
  end
end

#html_labimotionObject



241
242
243
244
245
246
247
248
249
# File 'lib/labimotion/libs/export_element.rb', line 241

def html_labimotion
  layers_html = build_layers_html
  html_body = <<-HTML.strip
  #{layers_html}
  HTML
  html_body
rescue StandardError => e
  Labimotion.log_exception(e)
end

#res_nameObject



170
171
172
173
174
175
# File 'lib/labimotion/libs/export_element.rb', line 170

def res_name
  element_name = @element.short_label
  "#{element_name}_#{Time.now.strftime('%Y%m%d%H%M')}.docx"
rescue StandardError => e
  Labimotion.log_exception(e)
end

#to_docxObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/labimotion/libs/export_element.rb', line 143

def to_docx
  # location = Rails.root.join('lib', 'template', 'Labimotion.docx')
  location = Rails.root.join('lib', 'template', 'Labimotion_lines.docx')
  # location = Rails.root.join('lib', 'template', 'Labimotion_img.docx')
  File.exist?(location)
  template = Sablon.template(location)
  layers = build_layers
  context = {
    name: @element.name,
    short_label: @element.short_label,
    date: Time.now.strftime('%d/%m/%Y'),
    author: @current_user.name,
    layers: layers
  }
  tempfile = Tempfile.new('labimotion.docx')
  template.render_to_file File.expand_path(tempfile), context
  content = File.read(tempfile)
  content
rescue StandardError => e
  Labimotion.log_exception(e)
ensure
  # Close and delete the temporary file
  tempfile.close
  tempfile.unlink
end