labimotion 1.3.0.rc1 → 1.3.0.rc3

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: ef750ee91a207edde617086a1dadc141b83613584810eba9435836fe59b2f745
4
+ data.tar.gz: b68dfa74b8701fbd73598d8a2b67109680a70c31b92e780acef89aa968923108
5
5
  SHA512:
6
- metadata.gz: 9cfe38fdd248d4fb4f32be1c607605a0a35c6f262dc0e0cb33bd7575895aea6cd177b752f9b888458af7d75dffe45acf05f08a44a352389b0bd9f93a0cc27fc9
7
- data.tar.gz: b7cfc290285914a6b4afddbb604718d5aeace21b8cbda8fa08aa8557e7513c5efa24829a79d39d71e59a937b9ce104623b30afea85c7f70191304b33d68e4a42
6
+ metadata.gz: 8fe68edfe62dc71133d5340b6242d43a3d4a6dab50ccb77531bb32898f84635d8794b16ac1185672b453dc9a8965782f7c1545ab4f556ab0d744087d7559ff24
7
+ data.tar.gz: 0ed4f34cf80aef0200681e45103ae22bf4b9fe6e02ca6772ee146aee989a40939143b3370b16b787c678c4329990491dabe331394bca851f473a573405c42a11
@@ -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
@@ -13,7 +13,6 @@ module Labimotion
13
13
  @export_format = export_format
14
14
  rescue StandardError => e
15
15
  Labimotion.log_exception(e)
16
- byebug
17
16
  end
18
17
 
19
18
  def build_layers
@@ -38,7 +37,6 @@ module Labimotion
38
37
  objs
39
38
  rescue StandardError => e
40
39
  Labimotion.log_exception(e)
41
- byebug
42
40
  end
43
41
 
44
42
  def build_field(layer, field)
@@ -48,18 +46,24 @@ module Labimotion
48
46
  field_obj[:type] = field['type']
49
47
 
50
48
  field_obj.merge!(field.slice('value', 'value_system'))
51
-
49
+ field_obj[:is_table] = false
50
+ field_obj[:not_table] = true
52
51
  case field['type']
53
52
  when Labimotion::FieldType::DRAG_ELEMENT
54
53
  field_obj[:value] = (field['value'] && field['value']['el_label']) || ''
55
54
  field_obj[:obj] = field['value'] ### change to object
56
55
  when Labimotion::FieldType::DRAG_SAMPLE
57
56
  val = field.fetch('value', {})
58
- sample = Sample.find_by(id: val['el_id'])
57
+ instance = Sample.find_by(id: val['el_id'])
59
58
  field_obj[:value] = val['el_label']
60
59
  field_obj[:has_structure] = true
61
- obj_os = OpenStruct.new(sample)
62
- field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_os, format: 'png').generate
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
63
67
  when Labimotion::FieldType::DRAG_MOLECULE
64
68
  val = field.fetch('value', {})
65
69
  obj = Molecule.find_by(id: val['el_id'])
@@ -73,32 +77,73 @@ module Labimotion
73
77
  val = files&.map { |file| "#{file['filename']} #{file['label']}" }&.join('\n')
74
78
  field_obj[:value] = val
75
79
  when Labimotion::FieldType::TABLE
76
- field_obj[:value] = 'this is a table' ## to be handled
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'
77
100
  when Labimotion::FieldType::INPUT_GROUP
78
- field_obj[:value] = 'this is a input group' ## to be handled
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(' ')
79
110
  end
80
111
  field_obj
81
112
  rescue StandardError => e
82
113
  Labimotion.log_exception(e)
83
- byebug
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
84
127
  end
85
128
 
86
129
  def build_fields(layer)
87
130
  fields = layer[Labimotion::Prop::FIELDS] || []
88
131
  field_objs = []
89
132
  fields.each do |field|
133
+ next if field['type'] == 'dummy'
134
+
90
135
  field_obj = build_field(layer, field)
91
136
  field_objs.push(field_obj)
92
137
  end
93
138
  field_objs
94
139
  rescue StandardError => e
95
140
  Labimotion.log_exception(e)
96
- byebug
97
141
  end
98
142
 
99
143
  def to_docx
100
144
  # location = Rails.root.join('lib', 'template', 'Labimotion.docx')
101
145
  location = Rails.root.join('lib', 'template', 'Labimotion_lines.docx')
146
+ # location = Rails.root.join('lib', 'template', 'Labimotion_img.docx')
102
147
  File.exist?(location)
103
148
  template = Sablon.template(location)
104
149
  layers = build_layers
@@ -109,15 +154,26 @@ module Labimotion
109
154
  author: @current_user.name,
110
155
  layers: layers
111
156
  }
112
- output = "Labimotion1.docx"
113
- template.render_to_file File.expand_path(output), context
114
- File.read(output)
157
+ tempfile = Tempfile.new('labimotion.docx')
158
+ template.render_to_file File.expand_path(tempfile), context
159
+ content = File.read(tempfile)
160
+ content
115
161
  rescue StandardError => e
116
162
  Labimotion.log_exception(e)
117
- byebug
163
+ ensure
164
+ # Close and delete the temporary file
165
+ tempfile.close
166
+ tempfile.unlink
118
167
  end
119
168
 
120
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
+
121
177
  def build_field_html(layer, field, cols)
122
178
  case field['type']
123
179
  when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
@@ -131,7 +187,6 @@ module Labimotion
131
187
  "#{htd}<b>#{field['label']}: </b><br />#{val}</td>"
132
188
  rescue StandardError => e
133
189
  Labimotion.log_exception(e)
134
- byebug
135
190
  end
136
191
 
137
192
  def build_fields_html(layer)
@@ -153,7 +208,6 @@ module Labimotion
153
208
  field_objs&.join("")&.gsub('<tr></tr>', '')
154
209
  rescue StandardError => e
155
210
  Labimotion.log_exception(e)
156
- byebug
157
211
  end
158
212
 
159
213
 
@@ -179,11 +233,9 @@ module Labimotion
179
233
  first_line = false
180
234
  end
181
235
  layer_html.push('</tr></table>')
182
- byebug
183
236
  layer_html.join('')
184
237
  rescue StandardError => e
185
238
  Labimotion.log_exception(e)
186
- byebug
187
239
  end
188
240
 
189
241
  def html_labimotion
@@ -194,7 +246,6 @@ module Labimotion
194
246
  html_body
195
247
  rescue StandardError => e
196
248
  Labimotion.log_exception(e)
197
- byebug
198
249
  end
199
250
  end
200
251
  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.rc3'
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.rc3
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,8 +66,7 @@ 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
70
- - lib/labimotion/libs/export_element_bak1.rb
69
+ - lib/labimotion/libs/export_element.rb
71
70
  - lib/labimotion/libs/nmr_mapper.rb
72
71
  - lib/labimotion/libs/properties_handler.rb
73
72
  - lib/labimotion/libs/sample_association.rb
@@ -1,118 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'export_table'
3
- require 'labimotion/version'
4
- require 'sablon'
5
-
6
- module Labimotion
7
- class ExportElementBak1
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
- def build_layers
48
- objs = []
49
- @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
50
- layer = @properties[Labimotion::Prop::LAYERS][key] || {}
51
- fields = layer[Labimotion::Prop::FIELDS] || []
52
- field_objs = build_fields(layer, fields)
53
-
54
- layer_info = {
55
- label: layer['label'],
56
- layer: layer['layer'],
57
- cols: layer['cols'],
58
- fields: field_objs
59
- }
60
- objs.push(layer_info)
61
- end
62
- objs
63
- rescue StandardError => e
64
- Labimotion.log_exception(e)
65
- byebug
66
- end
67
-
68
- def html_sample
69
- html_body = <<-HTML.strip
70
- <p style="text-align: right; background-color: #FFFF00">
71
- Right aligned content with a yellow background color.
72
- </p>
73
- <div>
74
- <span style="color: #123456">Inline styles</span> are possible as well
75
- </div>
76
- <table>
77
- <tr>
78
- <th>Company</th>
79
- <th>Contact</th>
80
- <th>Country</th>
81
- </tr>
82
- <tr>
83
- <td>Alfreds Futterkiste</td>
84
- <td>Maria Anders</td>
85
- <td>Germany</td>
86
- </tr>
87
- </table>
88
-
89
- HTML
90
- html_body
91
- rescue StandardError => e
92
- Labimotion.log_exception(e)
93
- byebug
94
- end
95
-
96
- def to_docx
97
- location = Rails.root.join('lib', 'template', 'Labimotion.docx')
98
- File.exist?(location)
99
- template = Sablon.template(location)
100
- layers = build_layers
101
- byebug
102
- html_body = html_sample
103
- html_content = Sablon.content(:html, html_body)
104
- context = {
105
- name: @element.name,
106
- short_label: @element.short_label,
107
- layers: layers,
108
- html_sample: html_content
109
- }
110
- output = "Labimotion1.docx"
111
- template.render_to_file File.expand_path(output), context
112
- File.read(output)
113
- rescue StandardError => e
114
- Labimotion.log_exception(e)
115
- byebug
116
- end
117
- end
118
- end