Class: ReportGenerator

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/reports/report_generator.rb

Overview

java_import org.xhtmlrenderer.pdf.ITextRenderer

Constant Summary collapse

INVALID_CHARS =
{
  '"' => '"',
  '&' => '&',
  "'" => ''',
  '<' => '&lt;',
  '>' => '&gt;'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report) ⇒ ReportGenerator

Returns a new instance of ReportGenerator.



13
14
15
16
17
# File 'backend/app/lib/reports/report_generator.rb', line 13

def initialize(report)
  @report = report
  @sub_report_data_stack = []
  @sub_report_code_stack = []
end

Instance Attribute Details

#reportObject

Returns the value of attribute report



9
10
11
# File 'backend/app/lib/reports/report_generator.rb', line 9

def report
  @report
end

#sub_report_code_stackObject

Returns the value of attribute sub_report_code_stack



11
12
13
# File 'backend/app/lib/reports/report_generator.rb', line 11

def sub_report_code_stack
  @sub_report_code_stack
end

#sub_report_data_stackObject

Returns the value of attribute sub_report_data_stack



10
11
12
# File 'backend/app/lib/reports/report_generator.rb', line 10

def sub_report_data_stack
  @sub_report_data_stack
end

Instance Method Details

#clean_invalid_xml_chars(text) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'backend/app/lib/reports/report_generator.rb', line 124

def clean_invalid_xml_chars(text)
  control_chars = (0...32).map(&:chr)
  valid_control_chars = [0x9, 0xA, 0xD].map(&:chr)
  text.gsub!(/[#{control_chars.join("")}]/) do |ch|
    if valid_control_chars.include?(ch)
      ch
    else
      ''
    end
  end
end

#do_render(file) ⇒ Object



136
137
138
139
# File 'backend/app/lib/reports/report_generator.rb', line 136

def do_render(file)
  renderer = ERB.new(File.read(template_path(file)))
  renderer.result(binding)
end

#format_sub_report(contents) ⇒ Object



141
142
143
144
145
146
147
148
# File 'backend/app/lib/reports/report_generator.rb', line 141

def format_sub_report(contents)
  sub_report_code_stack.push(contents.pop)
  sub_report_data_stack.push(contents)
  render = do_render('top_level_subreport.erb')
  sub_report_code_stack.pop
  sub_report_data_stack.pop
  render
end

#generate(file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'backend/app/lib/reports/report_generator.rb', line 19

def generate(file)
  case (report.format)
  when 'json'
    generate_json(file)
  when 'html'
    generate_html(file)
  when 'pdf'
    generate_pdf(file)
  when 'rtf'
    generate_rtf(file)
  else
    generate_csv(file)
  end
end

#generate_csv(file) ⇒ Object



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
# File 'backend/app/lib/reports/report_generator.rb', line 68

def generate_csv(file)
  results = report.get_content
  CSV.open(file.path, 'wb') do |csv|
    report.info.each do |key, value|
      csv << [key, value]
    end
    csv << []

    begin
      rows = []
      rows.push(results[0].keys)

      results.each do |result|
        row = []
        result.each do |_key, value|
          row.push(value.is_a?(Array) ? ASUtils.to_json(value) : value)
        end
        rows.push(row)
      end

      data = nil
      if report.expand_csv
        data = CsvReportExpander.new(rows, report.job).expand_csv
      else
        data = rows
      end

      data.each do |row|
        csv << row
      end
    rescue NoMethodError
      csv << ['No results found.']
    end
  end
end

#generate_html(file) ⇒ Object



43
44
45
# File 'backend/app/lib/reports/report_generator.rb', line 43

def generate_html(file)
  file.write(do_render('report.erb'))
end

#generate_json(file) ⇒ Object



34
35
36
37
38
39
40
41
# File 'backend/app/lib/reports/report_generator.rb', line 34

def generate_json(file)
  data = {}
  data[:info] = nil
  data[:records] = report.get_content
  data[:info] = report.info
  json = ASUtils.to_json(data)
  file.write(json)
end

#generate_pdf(file) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'backend/app/lib/reports/report_generator.rb', line 47

def generate_pdf(file)
  output_stream = java.io.FileOutputStream.new(file.path)
  xml = ASUtils.tempfile('xml_report_')
  xml.write(clean_invalid_xml_chars(do_render('report.erb')))
  xml.close
  renderer = org.xhtmlrenderer.pdf.ITextRenderer.new

  renderer.set_document(java.io.File.new(xml.path))
  renderer.layout

  renderer.create_pdf(output_stream)

  xml.unlink
  output_stream.close
end

#generate_rtf(file) ⇒ Object



63
64
65
66
# File 'backend/app/lib/reports/report_generator.rb', line 63

def generate_rtf(file)
  rtf = RtfGenerator.new(self).generate
  file.write(rtf.to_rtf)
end

#identifier(record) ⇒ Object



193
194
195
# File 'backend/app/lib/reports/report_generator.rb', line 193

def identifier(record)
  [t('identifier_prefix', nil), record[report.identifier_field]].compact.join(' ') if report.identifier_field
end

#rtf_subreport(value) {|value| ... } ⇒ Object

Yields:

  • (value)


177
178
179
180
181
182
183
# File 'backend/app/lib/reports/report_generator.rb', line 177

def rtf_subreport(value)
  sub_report_code_stack.push(value.pop)
  sub_report_data_stack.push(value)
  yield(value)
  sub_report_code_stack.pop
  sub_report_data_stack.pop
end

#show_in_list(value) ⇒ Object



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
# File 'backend/app/lib/reports/report_generator.rb', line 150

def show_in_list(value)
  # organize list values (needed for multiple report types within a single list)
  rendered = ''
  list = {}
  report_data = []
  value.each do |v|
    if v.is_a?(String)
      # this is the report type, assign the currently collected data to it
      list[v] = report_data.dup
      report_data = []
    else
      # this is the data, collect it
      report_data << v
    end
  end

  list.each do |type, data|
    sub_report_code_stack.push(type)
    sub_report_data_stack.push(data)
    rendered += do_render('nested_subreport.erb')
    sub_report_code_stack.pop
    sub_report_data_stack.pop
  end

  rendered
end

#t(key, default = '') ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'backend/app/lib/reports/report_generator.rb', line 197

def t(key, default='')
  subreport_code = sub_report_code_stack.empty? ? nil : sub_report_code_stack.last
  special_translation = report.special_translation(key, subreport_code)
  if special_translation
    special_translation
  else
    if default == ''
      fallback = key
    else
      fallback = default
    end
    global = I18n.t("reports.translation_defaults.#{key}", :default => fallback)
    if sub_report_code_stack.empty?
      I18n.t("reports.#{report.code}.#{key}", :default => global)
    else
      I18n.t("reports.#{sub_report_code_stack.last}.#{key}", :default => global)
    end
  end
end

#template_path(file) ⇒ Object



185
186
187
188
189
190
191
# File 'backend/app/lib/reports/report_generator.rb', line 185

def template_path(file)
  if File.exist?(File.join('app', 'views', 'reports', file))
    return File.join('app', 'views', 'reports', file)
  end

  StaticAssetFinder.new('reports').find(file)
end

#xml_clean!(data) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'backend/app/lib/reports/report_generator.rb', line 112

def xml_clean!(data)
  if data.is_a?(Array)
    data.each {|item| xml_clean!(item)}
  elsif data.is_a?(Hash)
    data.each {|_key, value| xml_clean!(value)}
  elsif data.is_a?(String)
    data.gsub!(/[#{INVALID_CHARS.keys.join('')}]/) do |ch|
      INVALID_CHARS[ch]
    end
  end
end