labimotion 1.3.0.rc1 → 1.3.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f43e928ba8d3a20237af9a587380be1a674074d8323f1b41ecedff3d3416c12b
4
- data.tar.gz: 344ae75f2cee3560d9f18e2f167e2321310ae539735cfc74666182868db369fa
3
+ metadata.gz: ee43dfba01f84786e6b1dc59d125c6c8cd3a7594e6f3d9e5179268995b8366dd
4
+ data.tar.gz: e15bf2f064a847b950f3320a2707de508f3c8169a966519280b71f8426a20f3e
5
5
  SHA512:
6
- metadata.gz: 9cfe38fdd248d4fb4f32be1c607605a0a35c6f262dc0e0cb33bd7575895aea6cd177b752f9b888458af7d75dffe45acf05f08a44a352389b0bd9f93a0cc27fc9
7
- data.tar.gz: b7cfc290285914a6b4afddbb604718d5aeace21b8cbda8fa08aa8557e7513c5efa24829a79d39d71e59a937b9ce104623b30afea85c7f70191304b33d68e4a42
6
+ metadata.gz: da54fef539dfc89675a40f1e1de1ab3a58a77f0e23a494912852ab78a29546af83ceeb1ef9e3b1a8f1d77d78adcc55e77a8d986b28f43810966264bdab89c230
7
+ data.tar.gz: db9c675738091bd749bdea7ece8eb3e555b6063694691d81a58832b19be3f92618d61a69228ad8a820c11a4b14d7ff8b77ef817378740e0f2af108aa43867316
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'labimotion/version'
4
+ require 'labimotion/libs/export_element'
5
+
4
6
  module Labimotion
5
7
  # Generic Element API
6
8
  class GenericElementAPI < Grape::API
@@ -41,6 +43,30 @@ module Labimotion
41
43
  end
42
44
  end
43
45
 
46
+ namespace :export do
47
+ desc 'export element'
48
+ params do
49
+ optional :id, type: Integer, desc: 'element id'
50
+ optional :export_format, type: String, desc: 'export format'
51
+ end
52
+ get do
53
+ element = Labimotion::Element.find(params[:id])
54
+ ## byebug
55
+ export = Labimotion::ExportElement.new current_user, element, params[:export_format]
56
+ env['api.format'] = :binary
57
+ content_type 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
58
+ el_filename = export.res_name
59
+ filename = URI.escape(el_filename)
60
+ # header['Content-Disposition'] = "attachment; filename=abc.docx"
61
+ header('Content-Disposition', "attachment; filename=\"#{filename}\"")
62
+
63
+ export.to_docx
64
+ rescue StandardError => e
65
+ Labimotion.log_exception(e, current_user)
66
+ { klass: [] }
67
+ end
68
+ end
69
+
44
70
  namespace :create_element_klass do
45
71
  desc 'create Generic Element Klass'
46
72
  params do
@@ -89,7 +89,7 @@ module Labimotion
89
89
  sk_id = sk_obj["identifier"]
90
90
  ek_obj = data.fetch(Labimotion::Prop::L_ELEMENT_KLASS).fetch(sk_obj["element_klass_id"])
91
91
  element_klass = Labimotion::ElementKlass.find_by(name: ek_obj['name']) if ek_obj.present?
92
- next if element_klass.nil? || ek_obj.nil? # || ek_obj['is_generic'] == true
92
+ next if element_klass.nil? || ek_obj.nil? || ek_obj['is_generic'] == true
93
93
 
94
94
  element_uuid = fields.fetch('element_id')
95
95
  element_type = fields.fetch('element_type')
@@ -99,7 +99,7 @@ module Labimotion
99
99
 
100
100
  if segment_klass.nil?
101
101
  skr = Labimotion::SegmentKlassesRevision.find_by(uuid: fields.fetch('klass_uuid'))
102
- segment_klass = Labimotion::SegmentKlass.find_by(id: skr.segment_klass_id) if skr.present?
102
+ segment_klass = skr.segment_klass if segment_klass.nil? && skr.present?
103
103
  end
104
104
 
105
105
  next if segment_klass.nil? || element.nil?
@@ -133,7 +133,7 @@ module Labimotion
133
133
  data.fetch('Labimotion::Element', {}).each do |uuid, fields|
134
134
  klass_id = fields["element_klass_id"]
135
135
  ek_obj = data.fetch('Labimotion::ElementKlass', {})[klass_id]
136
- ek_id = ek_obj["identifier"] if ek_obj.present?
136
+ ek_id = ek_obj["identifier"]
137
137
  element_klass = Labimotion::ElementKlass.find_by(identifier: ek_id) if ek_id.present?
138
138
  element_klass = Labimotion::ElementKlass.find_by(uuid: fields.fetch('klass_uuid')) if element_klass.nil?
139
139
 
@@ -55,10 +55,14 @@ module Labimotion
55
55
  field_obj[:obj] = field['value'] ### change to object
56
56
  when Labimotion::FieldType::DRAG_SAMPLE
57
57
  val = field.fetch('value', {})
58
- sample = Sample.find_by(id: val['el_id'])
58
+ instance = Sample.find_by(id: val['el_id'])
59
59
  field_obj[:value] = val['el_label']
60
60
  field_obj[:has_structure] = true
61
- obj_os = OpenStruct.new(sample)
61
+ obj_os = Entities::SampleReportEntity.new(
62
+ instance,
63
+ current_user: @current_user,
64
+ detail_levels: ElementDetailLevelCalculator.new(user: @current_user, element: instance).detail_levels,
65
+ ).serializable_hash
62
66
  field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_os, format: 'png').generate
63
67
  when Labimotion::FieldType::DRAG_MOLECULE
64
68
  val = field.fetch('value', {})
@@ -0,0 +1,251 @@
1
+ # frozen_string_literal: true
2
+ require 'export_table'
3
+ require 'labimotion/version'
4
+ require 'sablon'
5
+
6
+ module Labimotion
7
+ class ExportElement
8
+ def initialize(current_user, element, export_format)
9
+ @current_user = current_user
10
+ @element = element
11
+ @properties = element.properties
12
+ @options = element.properties_release[Labimotion::Prop::SEL_OPTIONS]
13
+ @export_format = export_format
14
+ rescue StandardError => e
15
+ Labimotion.log_exception(e)
16
+ end
17
+
18
+ def build_layers
19
+ objs = []
20
+ @properties[Labimotion::Prop::LAYERS]&.keys&.sort_by { |key| @properties[Labimotion::Prop::LAYERS][key]['position'] }&.each do |key|
21
+ layer = @properties[Labimotion::Prop::LAYERS][key] || {}
22
+
23
+ ## Build fields html
24
+ # field_objs = build_fields_html(layer) if layer[Labimotion::Prop::FIELDS]&.length&.positive?
25
+ # field_html = Sablon.content(:html, field_objs) if field_objs.present?
26
+ field_objs = build_fields(layer)
27
+
28
+ layer_info = {
29
+ label: layer['label'],
30
+ layer: layer['layer'],
31
+ cols: layer['cols'],
32
+ timeRecord: layer['timeRecord'],
33
+ fields: field_objs
34
+ }
35
+ objs.push(layer_info)
36
+ end
37
+ objs
38
+ rescue StandardError => e
39
+ Labimotion.log_exception(e)
40
+ end
41
+
42
+ def build_field(layer, field)
43
+ field_obj = {}
44
+ field_obj[:label] = field['label']
45
+ field_obj[:field] = field['field']
46
+ field_obj[:type] = field['type']
47
+
48
+ field_obj.merge!(field.slice('value', 'value_system'))
49
+ field_obj[:is_table] = false
50
+ field_obj[:not_table] = true
51
+ case field['type']
52
+ when Labimotion::FieldType::DRAG_ELEMENT
53
+ field_obj[:value] = (field['value'] && field['value']['el_label']) || ''
54
+ field_obj[:obj] = field['value'] ### change to object
55
+ when Labimotion::FieldType::DRAG_SAMPLE
56
+ val = field.fetch('value', {})
57
+ instance = Sample.find_by(id: val['el_id'])
58
+ field_obj[:value] = val['el_label']
59
+ field_obj[:has_structure] = true
60
+ obj_os = Entities::SampleReportEntity.new(
61
+ instance,
62
+ current_user: @current_user,
63
+ detail_levels: ElementDetailLevelCalculator.new(user: @current_user, element: instance).detail_levels,
64
+ ).serializable_hash
65
+ obj_final = OpenStruct.new(obj_os)
66
+ field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_final, format: 'png').generate
67
+ when Labimotion::FieldType::DRAG_MOLECULE
68
+ val = field.fetch('value', {})
69
+ obj = Molecule.find_by(id: val['el_id'])
70
+ field_obj[:value] = val['el_label']
71
+ # field_obj[:has_structure] = true
72
+ # field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj, format: 'png').generate
73
+ when Labimotion::FieldType::SELECT
74
+ field_obj[:value] = @options.fetch(field['option_layers'], nil)&.fetch('options', nil)&.find { |ss| ss['key'] == field['value'] }&.fetch('label', nil) || field['value']
75
+ when Labimotion::FieldType::UPLOAD
76
+ files = field.fetch('value', nil)&.fetch('files', [])
77
+ val = files&.map { |file| "#{file['filename']} #{file['label']}" }&.join('\n')
78
+ field_obj[:value] = val
79
+ when Labimotion::FieldType::TABLE
80
+ field_obj[:is_table] = true
81
+ field_obj[:not_table] = false
82
+ tbl = []
83
+ ## tbl_idx = []
84
+ header = {}
85
+
86
+ sub_fields = field.fetch('sub_fields', [])
87
+ sub_fields.each_with_index do |sub_field, idx|
88
+ header["col#{idx}"] = sub_field['col_name']
89
+ end
90
+ tbl.push(header)
91
+ field.fetch('sub_values', []).each do |sub_val|
92
+ data = {}
93
+ sub_fields.each_with_index do |sub_field, idx|
94
+ data["col#{idx}"] = build_table_field(sub_val, sub_field)
95
+ end
96
+ tbl.push(data)
97
+ end
98
+ field_obj[:data] = tbl
99
+ # field_obj[:value] = 'this is a table'
100
+ when Labimotion::FieldType::INPUT_GROUP
101
+ val = []
102
+ field.fetch('sub_fields', [])&.each do |sub_field|
103
+ if sub_field['type'] == Labimotion::FieldType::SYSTEM_DEFINED
104
+ val.push("#{sub_field['value']} #{sub_field['value_system']}")
105
+ else
106
+ val.push(sub_field['value'])
107
+ end
108
+ end
109
+ field_obj[:value] = val.join(' ')
110
+ end
111
+ field_obj
112
+ rescue StandardError => e
113
+ Labimotion.log_exception(e)
114
+ end
115
+
116
+ def build_table_field(sub_val, sub_field)
117
+ return '' if sub_field.fetch('id', nil).nil? || sub_val[sub_field['id']].nil?
118
+
119
+ case sub_field['type']
120
+ when Labimotion::FieldType::SELECT
121
+ sub_val[sub_field['id']]['value']
122
+ when Labimotion::FieldType::SYSTEM_DEFINED
123
+ "#{sub_val[sub_field['id']]['value']} #{sub_val[sub_field['id']]['value_system']}"
124
+ else
125
+ sub_val[sub_field['id']]
126
+ end
127
+ end
128
+
129
+ def build_fields(layer)
130
+ fields = layer[Labimotion::Prop::FIELDS] || []
131
+ field_objs = []
132
+ fields.each do |field|
133
+ next if field['type'] == 'dummy'
134
+
135
+ field_obj = build_field(layer, field)
136
+ field_objs.push(field_obj)
137
+ end
138
+ field_objs
139
+ rescue StandardError => e
140
+ Labimotion.log_exception(e)
141
+ end
142
+
143
+ def to_docx
144
+ # location = Rails.root.join('lib', 'template', 'Labimotion.docx')
145
+ location = Rails.root.join('lib', 'template', 'Labimotion_lines.docx')
146
+ # location = Rails.root.join('lib', 'template', 'Labimotion_img.docx')
147
+ File.exist?(location)
148
+ template = Sablon.template(location)
149
+ layers = build_layers
150
+ context = {
151
+ name: @element.name,
152
+ short_label: @element.short_label,
153
+ date: Time.now.strftime('%d/%m/%Y'),
154
+ author: @current_user.name,
155
+ layers: layers
156
+ }
157
+ tempfile = Tempfile.new('labimotion.docx')
158
+ template.render_to_file File.expand_path(tempfile), context
159
+ content = File.read(tempfile)
160
+ content
161
+ rescue StandardError => e
162
+ Labimotion.log_exception(e)
163
+ ensure
164
+ # Close and delete the temporary file
165
+ tempfile.close
166
+ tempfile.unlink
167
+ end
168
+
169
+
170
+ def res_name
171
+ element_name = @element.short_label
172
+ "#{element_name}_#{Time.now.strftime('%Y%m%d%H%M')}.docx"
173
+ rescue StandardError => e
174
+ Labimotion.log_exception(e)
175
+ end
176
+
177
+ def build_field_html(layer, field, cols)
178
+ case field['type']
179
+ when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
180
+ val = (field['value'] && field['value']['el_label']) || ''
181
+ when Labimotion::FieldType::UPLOAD
182
+ val = (field['value'] && field['value']['files'] && field['value']['files'].first && field['value']['files'].first['filename'] ) || ''
183
+ else
184
+ val = field['value']
185
+ end
186
+ htd = field['hasOwnRow'] == true ? "<td colspan=#{cols}>" : '<td>'
187
+ "#{htd}<b>#{field['label']}: </b><br />#{val}</td>"
188
+ rescue StandardError => e
189
+ Labimotion.log_exception(e)
190
+ end
191
+
192
+ def build_fields_html(layer)
193
+ fields = layer[Labimotion::Prop::FIELDS] || []
194
+ field_objs = []
195
+ cols = layer['cols'] || 0
196
+ field_objs.push('<table style="width: 4000dxa"><tr>')
197
+ fields.each do |field|
198
+ if cols&.zero? || field['hasOwnRow'] == true
199
+ field_objs.push('</tr><tr>')
200
+ cols = layer['cols']
201
+ end
202
+ field_obj = build_field_html(layer, field, layer['cols'])
203
+ field_objs.push(field_obj)
204
+ cols -= 1
205
+ cols = 0 if field['hasOwnRow'] == true
206
+ end
207
+ field_objs.push('</tr></table>')
208
+ field_objs&.join("")&.gsub('<tr></tr>', '')
209
+ rescue StandardError => e
210
+ Labimotion.log_exception(e)
211
+ end
212
+
213
+
214
+ def build_layers_html
215
+ layer_html = []
216
+ first_line = true
217
+ @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
218
+ layer_html.push('</tr></table>') if first_line == false
219
+ layer = @properties[Labimotion::Prop::LAYERS][key] || {}
220
+ fields = layer[Labimotion::Prop::FIELDS] || []
221
+ layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>")
222
+ layer_html.push('<table><tr>')
223
+ cols = layer['cols']
224
+ fields.each do |field|
225
+ if (cols === 0)
226
+ layer_html.push('</tr><tr>')
227
+ end
228
+
229
+ val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value']
230
+ layer_html.push("<td>#{field['label']}: <br />#{val}</td>")
231
+ cols -= 1
232
+ end
233
+ first_line = false
234
+ end
235
+ layer_html.push('</tr></table>')
236
+ layer_html.join('')
237
+ rescue StandardError => e
238
+ Labimotion.log_exception(e)
239
+ end
240
+
241
+ def html_labimotion
242
+ layers_html = build_layers_html
243
+ html_body = <<-HTML.strip
244
+ #{layers_html}
245
+ HTML
246
+ html_body
247
+ rescue StandardError => e
248
+ Labimotion.log_exception(e)
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+ require 'export_table'
3
+ require 'labimotion/version'
4
+ require 'sablon'
5
+
6
+ module Labimotion
7
+ class ExportElement
8
+ def initialize(current_user, element, export_format)
9
+ @current_user = current_user
10
+ @element = element
11
+ @properties = element.properties
12
+ @export_format = export_format
13
+ rescue StandardError => e
14
+ Labimotion.log_exception(e)
15
+ byebug
16
+ end
17
+
18
+ def build_field(layer, field)
19
+ field_obj = {}
20
+ field_obj[:label] = field['label']
21
+ field_obj[:field] = field['field']
22
+ field_obj[:type] = field['type']
23
+
24
+ case field['type']
25
+ when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
26
+ field_obj[:value] = field['value']['el_label']
27
+ else
28
+ field_obj[:value] = field['value']
29
+ end
30
+ field_obj
31
+ rescue StandardError => e
32
+ Labimotion.log_exception(e)
33
+ byebug
34
+ end
35
+
36
+ def build_fields(layer, fields)
37
+ field_objs = []
38
+ fields.each do |field|
39
+ field_obj = build_field(layer, field)
40
+ field_objs.push(field_obj)
41
+ end
42
+ field_objs
43
+ rescue StandardError => e
44
+ Labimotion.log_exception(e)
45
+ byebug
46
+ end
47
+
48
+ def build_layers
49
+ objs = []
50
+ @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
51
+ layer = @properties[Labimotion::Prop::LAYERS][key] || {}
52
+ field_objs = build_fields_html(layer)
53
+ field_html = Sablon.content(:html, field_objs)
54
+ layer_info = {
55
+ label: layer['label'],
56
+ layer: layer['layer'],
57
+ cols: layer['cols'],
58
+ timeRecord: layer['timeRecord'],
59
+ fields: field_html
60
+ }
61
+ objs.push(layer_info)
62
+ end
63
+ objs
64
+ rescue StandardError => e
65
+ Labimotion.log_exception(e)
66
+ byebug
67
+ end
68
+
69
+
70
+ def build_field_html(layer, field)
71
+ case field['type']
72
+ when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
73
+ val = field['value']['el_label']
74
+ else
75
+ val = field['value']
76
+ end
77
+ "<td><b>#{field['label']}: </b><br />#{val}</td>"
78
+ rescue StandardError => e
79
+ Labimotion.log_exception(e)
80
+ byebug
81
+ end
82
+
83
+ def build_fields_html(layer)
84
+ fields = layer[Labimotion::Prop::FIELDS] || []
85
+ field_objs = []
86
+ cols = layer['cols']
87
+ field_objs.push('<table><tr>')
88
+ fields.each do |field|
89
+ field_objs.push('</tr><tr>') if (cols === 0)
90
+ field_obj = build_field_html(layer, field)
91
+ field_objs.push(field_obj)
92
+ cols -= 1
93
+ end
94
+ field_objs.push('</tr></table>')
95
+ field_objs.join("")
96
+ rescue StandardError => e
97
+ Labimotion.log_exception(e)
98
+ byebug
99
+ end
100
+
101
+ def build_layers_html
102
+ layer_html = []
103
+ first_line = true
104
+ @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
105
+ layer_html.push('</tr></table>') if first_line == false
106
+ layer = @properties[Labimotion::Prop::LAYERS][key] || {}
107
+ fields = layer[Labimotion::Prop::FIELDS] || []
108
+ layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>")
109
+ layer_html.push('<table><tr>')
110
+ cols = layer['cols']
111
+ fields.each do |field|
112
+ if (cols === 0)
113
+ layer_html.push("</tr><tr>")
114
+ end
115
+
116
+ val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value']
117
+ layer_html.push("<td>#{field['label']}: <br />#{val}</td>")
118
+ cols -= 1
119
+ end
120
+ first_line = false
121
+ end
122
+ layer_html.push('</tr></table>')
123
+ layer_html.join('')
124
+
125
+
126
+ rescue StandardError => e
127
+ Labimotion.log_exception(e)
128
+ byebug
129
+ end
130
+
131
+ def html_labimotion
132
+ layers_html = build_layers_html
133
+ html_body = <<-HTML.strip
134
+ #{layers_html}
135
+ HTML
136
+ html_body
137
+ rescue StandardError => e
138
+ Labimotion.log_exception(e)
139
+ byebug
140
+ end
141
+
142
+ def to_docx
143
+ location = Rails.root.join('lib', 'template', 'Labimotion.docx')
144
+ File.exist?(location)
145
+ template = Sablon.template(location)
146
+ layers = build_layers
147
+ context = {
148
+ name: @element.name,
149
+ short_label: @element.short_label,
150
+ layers: layers
151
+ }
152
+ output = "Labimotion1.docx"
153
+ template.render_to_file File.expand_path(output), context
154
+ File.read(output)
155
+ rescue StandardError => e
156
+ Labimotion.log_exception(e)
157
+ byebug
158
+ end
159
+ end
160
+ end
@@ -3,5 +3,5 @@
3
3
  ## Labimotion Version
4
4
  module Labimotion
5
5
  IS_RAILS5 = false
6
- VERSION = '1.3.0.rc1'
6
+ VERSION = '1.3.0.rc2'
7
7
  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.3.0.rc1
4
+ version: 1.3.0.rc2
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-03-27 00:00:00.000000000 Z
12
+ date: 2024-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -66,7 +66,9 @@ files:
66
66
  - lib/labimotion/helpers/segment_helpers.rb
67
67
  - lib/labimotion/libs/converter.rb
68
68
  - lib/labimotion/libs/export_dataset.rb
69
- - lib/labimotion/libs/export_element1.rb
69
+ - lib/labimotion/libs/export_element copy.rb
70
+ - lib/labimotion/libs/export_element.rb
71
+ - lib/labimotion/libs/export_element2.rb
70
72
  - lib/labimotion/libs/export_element_bak1.rb
71
73
  - lib/labimotion/libs/nmr_mapper.rb
72
74
  - lib/labimotion/libs/properties_handler.rb
@@ -104,7 +106,7 @@ files:
104
106
  - lib/labimotion/utils/serializer.rb
105
107
  - lib/labimotion/utils/utils.rb
106
108
  - lib/labimotion/version.rb
107
- homepage: https://github.com/LabIMotion/labimotion
109
+ homepage: https://gitlab.kit.edu/kit/labimotion/labimotion
108
110
  licenses:
109
111
  - AGPL-3.0
110
112
  metadata: {}