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'
Rjb::load( classpath, ['-Djava.awt.headless=true', '-Djdbc.drivers=com.mysql.jdbc.Driver','-Xms128M', '-Xmx256M'] ) unless Rjb::loaded?
end
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
config = {
:report_params=>{
"REPORT_LOCALE" => locale.new('en', 'US'),
"XML_LOCALE" => locale.new('en', 'US'),
"XML_DATE_PATTERN" => 'yyyy-MM-dd'
}
}
jasper_params = hashMap.new
config[:report_params].each do |k,v|
jasper_params.put(k, v)
end
parameters.each do |key, value|
jasper_params.put(javaString.new(key.to_s), parameter_value_of(value))
end
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)
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
|