Module: JasperRails::Jasper::Rails

Defined in:
lib/jasper-rails.rb

Class Method Summary collapse

Class Method Details

.classpathObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jasper-rails.rb', line 41

def self.classpath
  classpaths = '.'
  Dir["#{File.dirname(__FILE__)}/java/*.jar"].each do |jar|
    classpaths << File::PATH_SEPARATOR + File.expand_path(jar)
  end

  Dir["lib/*.jar"].each do |jar|
    classpaths << File::PATH_SEPARATOR + File.expand_path(jar)
  end
  classpaths
end

.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.



143
144
145
146
147
148
149
150
151
152
# File 'lib/jasper-rails.rb', line 143

def self.parameter_value_of(param)
  javaString                  = Rjb::import 'java.lang.String'
  # 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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
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
# File 'lib/jasper-rails.rb', line 53

def self.render_pdf(jasper_file, datasource, parameters, options)

  if ENV['RAILS_ENV'] == 'development'
    Rjb::load( classpath, ['-Djava.awt.headless=true', '-Djdbc.drivers=org.sqlite.JDBC','-Xms128M', '-Xmx256M'] ) unless Rjb::loaded?
  end
  if ENV['RAILS_ENV'] == 'production'
    # Hack for Amazon Linux
    #ENV['JAVA_HOME'] = "/usr/lib/jvm/java"
    #ENV['LD_LIBRARY_PATH'] = "/usr/lib:/usr/lib/jvm/java/jre/lib/amd64:/usr/lib/jvm/java/jre/lib/amd64/server"
    Rjb::load( classpath, ['-Djava.awt.headless=true', '-Djdbc.drivers=com.mysql.jdbc.Driver','-Xms128M', '-Xmx256M'] ) unless Rjb::loaded?
  end


  # The code below is to workaround declaring constants within methods
  # We would like to delay these till a request is received to workaround Apache Passenger issue
  locale                      = Rjb::import 'java.util.Locale'
  jRException                 = Rjb::import 'net.sf.jasperreports.engine.JRException'
  jasperCompileManager        = Rjb::import 'net.sf.jasperreports.engine.JasperCompileManager'
  jasperExportManager         = Rjb::import 'net.sf.jasperreports.engine.JasperExportManager'
  jasperFillManager           = Rjb::import 'net.sf.jasperreports.engine.JasperFillManager'
  jasperPrint                 = Rjb::import 'net.sf.jasperreports.engine.JasperPrint'
  jRXmlUtils                  = Rjb::import 'net.sf.jasperreports.engine.util.JRXmlUtils'
  jREmptyDataSource           = Rjb::import 'net.sf.jasperreports.engine.JREmptyDataSource'
  jRXPathQueryExecuterFactory = silence_warnings{Rjb::import 'net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory'}
  inputSource                 = Rjb::import 'org.xml.sax.InputSource'
  stringReader                = Rjb::import 'java.io.StringReader'
  hashMap                     = Rjb::import 'java.util.HashMap'
  byteArrayInputStream        = Rjb::import 'java.io.ByteArrayInputStream'
  javaString                  = Rjb::import 'java.lang.String'
  jFreeChart                  = Rjb::import 'org.jfree.chart.JFreeChart'
  javaSystem                  = Rjb::import 'java.lang.System'
  driverManager               = Rjb::import 'java.sql.DriverManager'
  sqlException                = Rjb::import 'java.sql.SQLException'
  if ENV['RAILS_ENV'] == 'development'
    connection                = driverManager.getConnection("jdbc:sqlite:/Users/obrientimothya/Dropbox/development/vle/db/development.sqlite3")
  end
  if ENV['RAILS_ENV'] == 'production'
    connection                = driverManager.getConnection("jdbc:mysql://#{ENV['RDS_HOSTNAME']}:#{ENV['RDS_PORT']}/#{ENV['RDS_DB_NAME']}", ENV['RDS_USERNAME'], ENV['RDS_PASSWORD'])
  end


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

  begin

    # Default report params
    config = {
      :report_params=>{
        "REPORT_LOCALE"    => locale.new('en', 'US'),
        "XML_LOCALE"       => locale.new('en', 'US'),
        "XML_DATE_PATTERN" => 'yyyy-MM-dd'
      }
    }

    # Converting default report params to java HashMap
    jasper_params = hashMap.new
    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

    jasper_print = jasperFillManager.fillReport(jasper_file, jasper_params, connection)
    # 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