Class: Labimotion::ExportElement
- Inherits:
-
Object
- Object
- Labimotion::ExportElement
- Defined in:
- lib/labimotion/libs/export_element.rb
Instance Method Summary collapse
- #build_field(layer, field) ⇒ Object
- #build_field_html(layer, field, cols) ⇒ Object
- #build_fields(layer) ⇒ Object
- #build_fields_html(layer) ⇒ Object
- #build_layers ⇒ Object
- #build_layers_html ⇒ Object
- #build_table_field(sub_val, sub_field) ⇒ Object
- #html_labimotion ⇒ Object
-
#initialize(current_user, element, export_format) ⇒ ExportElement
constructor
A new instance of ExportElement.
- #res_name ⇒ Object
- #sample_url ⇒ Object
- #to_docx ⇒ Object
Constructor Details
#initialize(current_user, element, export_format) ⇒ ExportElement
Returns a new instance of ExportElement.
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 |
# File 'lib/labimotion/libs/export_element.rb', line 10 def initialize(current_user, element, export_format) @current_user = current_user @element = element @parent = case element.class.name when 'Labimotion::Segment' element&.element when 'Labimotion::Dataset' element&.element&.root_element end @element_klass = case element.class.name when 'Labimotion::Element' element.element_klass when 'Labimotion::Segment' element.segment_klass when 'Labimotion::Dataset' element.dataset_klass end @name = @element.instance_of?(Labimotion::Element) ? element.name : @parent&.name @short_label = @element.instance_of?(Labimotion::Element) ? element.short_label : @parent&.short_label @element_name = "#{@element_klass.label}_#{@short_label}".gsub(/\s+/, '') @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
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/labimotion/libs/export_element.rb', line 74 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', nil) if val.present? 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 end when Labimotion::FieldType::SYS_REACTION val = field.fetch('value', nil) if val.present? instance = Reaction.find_by(id: val['el_id']) field_obj[:value] = val['el_label'] field_obj[:has_structure] = true obj_os = Entities::ReactionReportEntity.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::DiagramReaction.new(obj: obj_final, format: 'png').generate end when Labimotion::FieldType::DRAG_MOLECULE val = field.fetch('value', nil) if val.present? obj = Molecule.find_by(id: val['el_id']) field_obj[:value] = val['el_label'] end 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(' ') when Labimotion::FieldType::WF_NEXT if field['value'].present? && field['wf_options'].present? field_obj[:value] = field['wf_options'].find { |ss| ss['key'] == field['value'] }&.fetch('label', nil)&.split('(')&.first&.strip end when Labimotion::FieldType::SYSTEM_DEFINED _field = field unit = Labimotion::Units::FIELDS.find { |o| o[:field] == _field['option_layers'] }&.fetch(:units, []).find { |u| u[:key] == _field['value_system'] }&.fetch(:label, '') val = _field['value'].to_s + ' ' + unit val = Sablon.content(:html, "<div>" + val + "</div>") if val.include? '<' field_obj[:value] = val when Labimotion::FieldType::TEXT_FORMULA field.fetch('text_sub_fields', []).each do |sub_field| va = @properties['layers'][sub_field.fetch('layer','')]['fields'].find { |f| f['field'] == sub_field.fetch('field','') }&.fetch('value',nil) field_obj[:value] = (field_obj[:value] || '') + va.to_s + sub_field.fetch('separator','') if va.present? end # field_obj[:value] = (field['value'] && field['value']['el_label']) || '' # field_obj[:obj] = field['value'] ### change to object else field_obj[:value] = field['value'] end field_obj rescue StandardError => e Labimotion.log_exception(e) end |
#build_field_html(layer, field, cols) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/labimotion/libs/export_element.rb', line 263 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
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/labimotion/libs/export_element.rb', line 213 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
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/labimotion/libs/export_element.rb', line 278 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_layers ⇒ Object
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 |
# File 'lib/labimotion/libs/export_element.rb', line 45 def build_layers objs = [] @properties[Labimotion::Prop::LAYERS]&.keys&.sort_by do |key| [ @properties[Labimotion::Prop::LAYERS].fetch(key, nil)&.fetch('position', 0) || 0, @properties[Labimotion::Prop::LAYERS].fetch(key, nil)&.fetch('wf_position', 0) || 0 ] end&.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_html ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/labimotion/libs/export_element.rb', line 300 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
182 183 184 185 186 187 188 189 190 191 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 182 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::DRAG_SAMPLE val = sub_val[sub_field['id']]['value'] || {} label = val['el_label'].present? ? "Short Label: [#{val['el_label']}] \n" : '' name = val['el_name'].present? ? "Name: [#{val['el_name']}] \n" : '' ext = val['el_external_label'].present? ? "Ext. Label: [#{val['el_external_label']}] \n" : '' mass = val['el_molecular_weight'].present? ? "Mass: [#{val['el_molecular_weight']}] \n" : '' url = val['el_id'].present? ? "#{sample_url}/#{val['el_id']}" : '' "#{label}#{name}#{ext}#{mass}#{url}" when Labimotion::FieldType::DRAG_MOLECULE val = sub_val[sub_field['id']]['value'] || {} smile = val['el_smiles'].present? ? "SMILES: [#{val['el_smiles']}] \n" : '' inchikey = val['el_inchikey'].present? ? "InChiKey:[#{val['el_inchikey']}] \n" : '' iupac = val['el_iupac'].present? ? "IUPAC:[#{val['el_iupac']}] \n" : '' mass = val['el_molecular_weight'].present? ? "MASS: [#{val['el_molecular_weight']}] \n" : '' "#{smile}#{inchikey}#{iupac}#{mass}" when Labimotion::FieldType::SELECT sub_val[sub_field['id']]['value'] when Labimotion::FieldType::SYSTEM_DEFINED unit = Labimotion::Units::FIELDS.find { |o| o[:field] == sub_field['option_layers'] }&.fetch(:units, [])&.find { |u| u[:key] == sub_val[sub_field['id']]['value_system'] }&.fetch(:label, '') val = sub_val[sub_field['id']]['value'].to_s + ' ' + unit val = Sablon.content(:html, "<div>" + val + "</div>") if val.include? '<' val else sub_val[sub_field['id']] end end |
#html_labimotion ⇒ Object
327 328 329 330 331 332 333 334 335 |
# File 'lib/labimotion/libs/export_element.rb', line 327 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_name ⇒ Object
257 258 259 260 261 |
# File 'lib/labimotion/libs/export_element.rb', line 257 def res_name "#{@element_name}_#{Time.now.strftime('%Y%m%d%H%M')}.docx" rescue StandardError => e Labimotion.log_exception(e) end |
#sample_url ⇒ Object
39 40 41 42 43 |
# File 'lib/labimotion/libs/export_element.rb', line 39 def sample_url host = ENV['PUBLIC_URL'] || 'http://localhost:3000' api = 'mydb/collection/all/sample' "#{host}/#{api}" end |
#to_docx ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/labimotion/libs/export_element.rb', line 227 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 = { label: @element_klass.label, desc: @element_klass.desc, name: @name, parent_klass: @parent.present? ? "#{@parent&.class&.name&.split('::')&.last}: " : '', short_label: @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.(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 |