Class: JSONReport

Inherits:
JasperReport show all
Defined in:
backend/app/model/reports/json_report.rb

Instance Attribute Summary

Attributes inherited from JasperReport

#data_source, #export_file, #format, #jrprint

Instance Method Summary (collapse)

Methods inherited from JasperReport

compile, finalize, #report, report, report_base, #title

Methods included from ReportManager::Mixin

included

Constructor Details

- (JSONReport) initialize(params)

Returns a new instance of JSONReport



5
6
7
8
9
10
11
# File 'backend/app/model/reports/json_report.rb', line 5

def initialize(params)
  @repo_id = params[:repo_id] if params.has_key?(:repo_id) && params[:repo_id] != ""
  @base_path = File.join(self.class.report_base, self.class.name ) 
  @datasource = Tempfile.new(self.class.name + '.data')
  
  ObjectSpace.define_finalizer( self, self.class.finalize(self) ) 
end

Instance Method Details

- (Object) default_params



29
30
31
32
33
34
35
36
37
38
# File 'backend/app/model/reports/json_report.rb', line 29

def default_params
  params = {} 
  params[JsonQueryExecuterFactory::JSON_DATE_PATTERN] ||= "yyyy-MM-dd"      
  params[JsonQueryExecuterFactory::JSON_NUMBER_PATTERN] ||= "#,##0.##"       
  params[JsonQueryExecuterFactory::JSON_LOCALE] ||= Locale::ENGLISH          
  params[JRParameter::REPORT_LOCALE] ||= ::Locale::US
  params["repositoryId"] = @repo_id
  params["basePath"] = @base_path
  params
end

- (Object) fill(params = {})



40
41
42
43
44
45
46
# File 'backend/app/model/reports/json_report.rb', line 40

def fill( params = {} )
  params.merge!(default_params) 
  params["net.sf.jasperreports.json.source"] = load_datasource
  
  @jrprint =  JasperFillManager.fill_report(report, java.util.HashMap.new(params) )

end

- (Object) load_datasource

there are several ways to attach data to your jasper report. most of them don’t seem to work very well. One that does it to add a file uri to the json.datasource property that’s passed as a param. since this works, it will be the default.



18
19
20
21
22
# File 'backend/app/model/reports/json_report.rb', line 18

def load_datasource
  @datasource.write(query.to_json)
  @datasource.rewind # be kind
  @datasource.path
end

- (Object) query

this is where we load the data. it most likely will be a sequel query



25
26
27
# File 'backend/app/model/reports/json_report.rb', line 25

def query
  { :locations => [] }
end

- (Object) render(format, params = {})



87
88
89
90
91
92
93
94
95
# File 'backend/app/model/reports/json_report.rb', line 87

def render(format, params = {} )
  if format == :json
    load_datasource 
    to_json
  elsif [:pdf, :html, :xlsx, :csv ].include?(format) 
    fill(params)
    self.send("to_#{format.to_s}")
  end
end

- (Object) to_csv



59
60
61
62
63
64
65
66
67
# File 'backend/app/model/reports/json_report.rb', line 59

def to_csv
  exporter = JRCsvExporter.new
  exporter.exporter_input = SimpleExporterInput.new(@jrprint)
  @export_file = Tempfile.new("location.csv")
  exporter.exporter_output = SimpleWriterExporterOutput.new(@export_file.to_outputstream)
  exporter.export_report
  @export_file.rewind 
  @export_file.read.to_java_bytes 
end

- (Object) to_html



52
53
54
55
56
57
# File 'backend/app/model/reports/json_report.rb', line 52

def to_html
  @export_file = Tempfile.new("location.html")
  JasperExportManager.export_report_to_html_file(@jrprint, @export_file.path)
  @export_file.rewind 
  @export_file.read.to_java_bytes 
end

- (Object) to_json



83
84
85
# File 'backend/app/model/reports/json_report.rb', line 83

def to_json
  @datasource.read.to_java_bytes
end

- (Object) to_pdf



48
49
50
# File 'backend/app/model/reports/json_report.rb', line 48

def to_pdf
   JasperExportManager.export_report_to_pdf(@jrprint)
end

- (Object) to_xlsx



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'backend/app/model/reports/json_report.rb', line 69

def to_xlsx
  exporter = JRXlsxExporter.new
  exporter.exporter_input = SimpleExporterInput.new(@jrprint)
  @export_file = Tempfile.new("location.xlsx")
  exporter.exporter_output = SimpleOutputStreamExporterOutput.new(@export_file.to_outputstream)
  configuration = SimpleXlsxReportConfiguration.new 
  configuration.one_page_per_sheet = false 
  exporter.configuration = configuration
  exporter.export_report
  @export_file.rewind 
  @export_file.read.to_java_bytes 

end