Module: JasperRails::Jasper::Rails

Defined in:
lib/jasper-rails.rb

Class Method Summary collapse

Class Method Details

.parameter_value_of(param) ⇒ Object

Returns the value without conversion when it’s converted to Java Types. When isn’t a Rjb class, returns a Java String of it.



131
132
133
134
135
136
137
138
139
# File 'lib/jasper-rails.rb', line 131

def self.parameter_value_of(param)
  # Using Rjb::import('java.util.HashMap').new, it returns an instance of
  # Rjb::Rjb_JavaProxy, so the Rjb_JavaProxy parent is the Rjb module itself.
  if param.class.parent == Rjb
    param
  else
    JavaString.new(param.to_s)
  end
end

.render_pdf(jasper_file, datasource, parameters, options) ⇒ Object



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
# File 'lib/jasper-rails.rb', line 77

def self.render_pdf(jasper_file, datasource, parameters, options)
  options ||= {}
  parameters ||= {}
  jrxml_file  = jasper_file.sub(/\.jasper$/, ".jrxml")

  begin
    # Converting default report params to java HashMap
    jasper_params = HashMap.new
    JasperRails.config[:report_params].each do |k,v|
      jasper_params.put(k, v)
    end

    # Convert the ruby parameters' hash to a java HashMap, but keeps it as
    # default when they already represent a JRB entity.
    # Pay attention that, for now, all other parameters are converted to string!
    parameters.each do |key, value|
      jasper_params.put(JavaString.new(key.to_s), parameter_value_of(value))
    end

    # Compile it, if needed
    if !File.exist?(jasper_file) || (File.exist?(jrxml_file) && File.mtime(jrxml_file) > File.mtime(jasper_file))
      JasperCompileManager.compileReportToFile(jrxml_file, jasper_file)
    end

    # Fill the report
    if datasource
      input_source = InputSource.new
      input_source.setCharacterStream(StringReader.new(datasource.to_xml(options).to_s))
      data_document = silence_warnings do
        # This is here to avoid the "already initialized constant DOCUMENT_POSITION_*" warnings.
        JRXmlUtils._invoke('parse', 'Lorg.xml.sax.InputSource;', input_source)
      end

      jasper_params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, data_document)
      jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params)
    else
      jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params, JREmptyDataSource.new)
    end

    # Export it!
    JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print)
  rescue Exception=>e
    if e.respond_to? 'printStackTrace'
      ::Rails.logger.error e.message
      e.printStackTrace
    else
      ::Rails.logger.error e.message + "\n " + e.backtrace.join("\n ")
    end
    raise e
  end
end