obrientimothya-jasper-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27b2536c4d7b51fbabe8928203f7d833144e4adb
4
+ data.tar.gz: d011b05de9b966b9888abf06b79b9c01293d0653
5
+ SHA512:
6
+ metadata.gz: a9af112add8c6d27712b0a58322d0815d135c97eae8a2d718a4a2b6e5bc34b4f4c518f2e6c6a822b184b781b06832ae67c2a6f9dbbeb8af844b5a1641066f3d7
7
+ data.tar.gz: 3197b86a0e15d3e35b78c51582d2f2043e213c6e8da043eb9fcc56ed3bb0e0ec747891f898a40f079823122f780204d121435b1e4b9b6395d60608bb79a9f6cb
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
6
+ .project
7
+ *.jasper
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rjb', '>= 1.4.0'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+
2
+ Copyright (C) 2012 Marlus Saraiva, Rodrigo Maia
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # jasper-rails
2
+
3
+ JasperReports/Rails integration using Rjb (Ruby Java Bridge)
4
+
5
+ # News!
6
+ - Updated to JasperReports 5.0.0
7
+ - Global configuration (default report params)
8
+ - Passing Java parameters directly
9
+
10
+ ## Dependencies
11
+
12
+ You need a Java Virtual Machine installed and set up in order to use this gem.
13
+
14
+ ## Install
15
+
16
+ ```
17
+ gem install jasper-rails
18
+ ```
19
+
20
+ ## Configure
21
+
22
+ Add `jasper-rails` to your Gemfile:
23
+
24
+ ```ruby
25
+ gem "jasper-rails"
26
+ ```
27
+
28
+ Note: If you're running your application on OS X, this is usually all you have to do. On Linux, you might have
29
+ to set the JAVA_HOME. On Windows, good luck!
30
+
31
+ For any issues related to the java/ruby integration, please check out: [http://rjb.rubyforge.org/](http://rjb.rubyforge.org/). You will find here plenty tips that might help you to solve your problem.
32
+
33
+ ## Using jasper-rails
34
+
35
+ 1) Tell your controller that it "responds to" :pdf and it "responds with" some list:
36
+
37
+ Note: This list will become your datasource inside the report.
38
+
39
+ ```ruby
40
+ respond_to :html, :xml, :pdf
41
+
42
+ def index
43
+ @people = Person.all
44
+ respond_with @people
45
+ end
46
+ ```
47
+
48
+ 2) Create your jasper report source file using iReport or any compatible jasper tool.
49
+
50
+ 3) Set the "Query Text" and "The language for the dataset query" properties of your report.
51
+
52
+ Note: If you're using iReport, you can find those properties in the "Properties panel".
53
+ Don't forget to select the report root node in your "Report inspector".
54
+
55
+ Example: If you have a list of people, you should have something like:
56
+ * "Query Text" = /people/person
57
+ * "The language for the dataset query" = xpath
58
+
59
+ 4) Design your report (add fields, groups, images, etc.)
60
+
61
+ 5) Save the report source file in your views folder (just like any other html view file):
62
+
63
+ Example:
64
+
65
+ * app/views/people/index.jrxml
66
+
67
+ ## Parameters
68
+
69
+ ```ruby
70
+ def index
71
+ @people = Person.all
72
+
73
+ # This variable will be available in your report!
74
+ @user = "John Doe"
75
+
76
+ respond_with @people
77
+ end
78
+ ```
79
+
80
+ All you have to do now is to create, in iReport, a parameter called "user" (yes, without the "@") and drop it in your report!
81
+
82
+ Limitation: By now, all parameters that aren't manually converted to Rjb java classes/instances are converted to java String. We intend to change this in the near future.
83
+
84
+ ## Passing Java parameters directly
85
+
86
+ ```ruby
87
+ title = (Rjb::import 'java.lang.String').new("The Beatles")
88
+ @parameter_that_is_a_java_class = (Rjb::import 'java.util.HashMap').new
89
+ @parameter_that_is_a_java_class.put("title", title)
90
+ ```
91
+
92
+ For more information check out issue: [#13](https://github.com/fortesinformatica/jasper-rails/pull/13)
93
+
94
+ ## Global configuration
95
+
96
+ You can change any default report param by setting JasperRails.config[:report_params], e.g., in your "config/application.rb" you might have something like:
97
+
98
+ ```ruby
99
+ config.after_initialize do
100
+ JasperRails.config[:report_params]["REPORT_LOCALE"] = JasperRails::Locale.new('pt', 'BR')
101
+ end
102
+ ```
103
+
104
+ ## RSpec integration
105
+ Check out: [jasper-rails-rspec](http://github.com/fortesinformatica/jasper-rails-rspec).
106
+
107
+ ## DEMO
108
+ For a running example, just clone: [jasper-rails-demo](http://github.com/fortesinformatica/jasper-rails-demo).
109
+
110
+ ## History
111
+ This project was first developed at Grupo Fortes in 2011 and we have been using it in several projects since then.
112
+ Pay attention that all the features it supports right now were based in those project's especific needs. Therefore you might find a lot of
113
+ JasperResports' features that are not yet supported.
114
+
115
+ ## LICENSE
116
+
117
+ Copyright (C) 2012 Marlus Saraiva, Rodrigo Maia
118
+
119
+ Permission is hereby granted, free of charge, to any person obtaining
120
+ a copy of this software and associated documentation files (the
121
+ "Software"), to deal in the Software without restriction, including
122
+ without limitation the rights to use, copy, modify, merge, publish,
123
+ distribute, sublicense, and/or sell copies of the Software, and to
124
+ permit persons to whom the Software is furnished to do so, subject to
125
+ the following conditions:
126
+
127
+ The above copyright notice and this permission notice shall be
128
+ included in all copies or substantial portions of the Software.
129
+
130
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
131
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
132
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
133
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
134
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
135
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
136
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize!
7
+ run Combustion::Application
@@ -0,0 +1,172 @@
1
+ #
2
+ # Copyright (C) 2012 Marlus Saraiva, Rodrigo Maia
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ require "jasper-rails/version"
24
+ require "rails"
25
+ require "rjb"
26
+ require "action_controller/metal/responder"
27
+
28
+ if Mime::Type.lookup_by_extension("pdf").nil?
29
+ Mime::Type.register "application/pdf", :pdf
30
+ end
31
+
32
+ module JasperRails
33
+
34
+ class << self
35
+ attr_accessor :config
36
+ end
37
+
38
+ module Jasper
39
+ module Rails
40
+
41
+ def self.classpath
42
+ classpaths = '.'
43
+ Dir["#{File.dirname(__FILE__)}/java/*.jar"].each do |jar|
44
+ classpaths << File::PATH_SEPARATOR + File.expand_path(jar)
45
+ end
46
+
47
+ Dir["lib/*.jar"].each do |jar|
48
+ classpaths << File::PATH_SEPARATOR + File.expand_path(jar)
49
+ end
50
+ classpaths
51
+ end
52
+
53
+ def self.render_pdf(jasper_file, datasource, parameters, options)
54
+
55
+ if ENV['RAILS_ENV'] == 'development'
56
+ Rjb::load( classpath, ['-Djava.awt.headless=true', '-Djdbc.drivers=org.sqlite.JDBC','-Xms128M', '-Xmx256M'] ) unless Rjb::loaded?
57
+ end
58
+ if ENV['RAILS_ENV'] == 'production'
59
+ # Hack for Amazon Linux
60
+ #ENV['JAVA_HOME'] = "/usr/lib/jvm/java"
61
+ #ENV['LD_LIBRARY_PATH'] = "/usr/lib:/usr/lib/jvm/java/jre/lib/amd64:/usr/lib/jvm/java/jre/lib/amd64/server"
62
+ Rjb::load( classpath, ['-Djava.awt.headless=true', '-Djdbc.drivers=com.mysql.jdbc.Driver','-Xms128M', '-Xmx256M'] ) unless Rjb::loaded?
63
+ end
64
+
65
+
66
+ # The code below is to workaround declaring constants within methods
67
+ # We would like to delay these till a request is received to workaround Apache Passenger issue
68
+ locale = Rjb::import 'java.util.Locale'
69
+ jRException = Rjb::import 'net.sf.jasperreports.engine.JRException'
70
+ jasperCompileManager = Rjb::import 'net.sf.jasperreports.engine.JasperCompileManager'
71
+ jasperExportManager = Rjb::import 'net.sf.jasperreports.engine.JasperExportManager'
72
+ jasperFillManager = Rjb::import 'net.sf.jasperreports.engine.JasperFillManager'
73
+ jasperPrint = Rjb::import 'net.sf.jasperreports.engine.JasperPrint'
74
+ jRXmlUtils = Rjb::import 'net.sf.jasperreports.engine.util.JRXmlUtils'
75
+ jREmptyDataSource = Rjb::import 'net.sf.jasperreports.engine.JREmptyDataSource'
76
+ jRXPathQueryExecuterFactory = silence_warnings{Rjb::import 'net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory'}
77
+ inputSource = Rjb::import 'org.xml.sax.InputSource'
78
+ stringReader = Rjb::import 'java.io.StringReader'
79
+ hashMap = Rjb::import 'java.util.HashMap'
80
+ byteArrayInputStream = Rjb::import 'java.io.ByteArrayInputStream'
81
+ javaString = Rjb::import 'java.lang.String'
82
+ jFreeChart = Rjb::import 'org.jfree.chart.JFreeChart'
83
+ javaSystem = Rjb::import 'java.lang.System'
84
+ driverManager = Rjb::import 'java.sql.DriverManager'
85
+ sqlException = Rjb::import 'java.sql.SQLException'
86
+ if ENV['RAILS_ENV'] == 'development'
87
+ connection = driverManager.getConnection("jdbc:sqlite:/Users/obrientimothya/Dropbox/development/vle/db/development.sqlite3")
88
+ end
89
+ if ENV['RAILS_ENV'] == 'production'
90
+ connection = driverManager.getConnection("jdbc:mysql://#{ENV['RDS_HOSTNAME']}:#{ENV['RDS_PORT']}/#{ENV['RDS_DB_NAME']}", ENV['RDS_USERNAME'], ENV['RDS_PASSWORD'])
91
+ end
92
+
93
+
94
+ options ||= {}
95
+ parameters ||= {}
96
+ jrxml_file = jasper_file.sub(/\.jasper$/, ".jrxml")
97
+
98
+ begin
99
+
100
+ # Default report params
101
+ config = {
102
+ :report_params=>{
103
+ "REPORT_LOCALE" => locale.new('en', 'US'),
104
+ "XML_LOCALE" => locale.new('en', 'US'),
105
+ "XML_DATE_PATTERN" => 'yyyy-MM-dd'
106
+ }
107
+ }
108
+
109
+ # Converting default report params to java HashMap
110
+ jasper_params = hashMap.new
111
+ config[:report_params].each do |k,v|
112
+ jasper_params.put(k, v)
113
+ end
114
+
115
+ # Convert the ruby parameters' hash to a java HashMap, but keeps it as
116
+ # default when they already represent a JRB entity.
117
+ # Pay attention that, for now, all other parameters are converted to string!
118
+ parameters.each do |key, value|
119
+ jasper_params.put(javaString.new(key.to_s), parameter_value_of(value))
120
+ end
121
+
122
+ # Compile it, if needed
123
+ if !File.exist?(jasper_file) || (File.exist?(jrxml_file) && File.mtime(jrxml_file) > File.mtime(jasper_file))
124
+ jasperCompileManager.compileReportToFile(jrxml_file, jasper_file)
125
+ end
126
+
127
+ jasper_print = jasperFillManager.fillReport(jasper_file, jasper_params, connection)
128
+ # Export it!
129
+ jasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print)
130
+ rescue Exception=>e
131
+ if e.respond_to? 'printStackTrace'
132
+ ::Rails.logger.error e.message
133
+ e.printStackTrace
134
+ else
135
+ ::Rails.logger.error e.message + "\n " + e.backtrace.join("\n ")
136
+ end
137
+ raise e
138
+ end
139
+ end
140
+
141
+ # Returns the value without conversion when it's converted to Java Types.
142
+ # When isn't a Rjb class, returns a Java String of it.
143
+ def self.parameter_value_of(param)
144
+ javaString = Rjb::import 'java.lang.String'
145
+ # Using Rjb::import('java.util.HashMap').new, it returns an instance of
146
+ # Rjb::Rjb_JavaProxy, so the Rjb_JavaProxy parent is the Rjb module itself.
147
+ if param.class.parent == Rjb
148
+ param
149
+ else
150
+ javaString.new(param.to_s)
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ class ActionController::Responder
157
+ def to_pdf
158
+ #jasper_file = "#{Rails.root.to_s}/app/views/#{controller.controller_path}/#{controller.action_name}.jasper"
159
+ jasper_file = (@options[:template] && "#{Rails.root.to_s}/app/views/#{controller.controller_path}/#{@options[:template]}") ||
160
+ "#{Rails.root.to_s}/app/views/#{controller.controller_path}/#{controller.action_name}.jasper"
161
+
162
+ params = {}
163
+ controller.instance_variables.each do |v|
164
+ params[v.to_s[1..-1]] = controller.instance_variable_get(v)
165
+ end
166
+
167
+ #controller.send_data Jasper::Rails::render_pdf(jasper_file, resource, params, options), :type => Mime::PDF
168
+ controller.send_data Jasper::Rails::render_pdf(jasper_file, resource, params, options), :type => Mime::PDF, :disposition => 'inline'
169
+ end
170
+ end
171
+
172
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module JasperRails
3
+ VERSION = "0.0.1"
4
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jasper-rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "obrientimothya-jasper-rails"
7
+ s.version = JasperRails::VERSION
8
+ s.authors = ["Marlus Saraiva", "Rodrigo Maia", "Timothy OBrien"]
9
+ s.summary = %q{Custom Rails-Jaspersoft Tool}
10
+ s.description = %q{A FORK of the fortesinformatica/jasper-rails project for a particular project. NOT intended for wide use.}
11
+ s.email = "obrien.timothy.a@gmail.com"
12
+ s.homepage = "https://github.com/obrientimothya/jasper-rails"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency('rjb', '1.4.3')
20
+ s.add_development_dependency 'combustion', '~> 0.3.2'
21
+ s.add_development_dependency "rspec-rails"
22
+ s.add_development_dependency "jasper-rails-rspec"
23
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe PeopleController do
4
+
5
+ describe "GET index" do
6
+ before do
7
+ Person.stub(:all).and_return([
8
+ Person.new(:name=>'john' , :email=>'lennon@beatles.com'),
9
+ Person.new(:name=>'george', :email=>'harrison@beatles.com')
10
+ ])
11
+ end
12
+
13
+ it "should respond success" do
14
+ get :index, :format => :pdf
15
+
16
+ response.should be_success
17
+ end
18
+
19
+ it "should not contain nulls" do
20
+ get :index, :format => :pdf
21
+
22
+ response.should_not contain("null")
23
+ end
24
+
25
+ it "should show all fields in the report" do
26
+ get :index, :format => :pdf
27
+
28
+ response.should contain("john")
29
+ response.should contain("lennon@beatles.com")
30
+ response.should contain("george")
31
+ response.should contain("harrison@beatles.com")
32
+ end
33
+
34
+ it "should clip the text if it's larger than the Text Field" do
35
+ Person.stub(:all).and_return([
36
+ Person.new(:name=>'jonh' , :email=>'a_very_long_text_that_is_larger_than_the_text_field_in_the_report')
37
+ ])
38
+
39
+ get :index, :format => :pdf
40
+
41
+ response.should_not contain("a_very_long_text_that_is_larger_than_the_text_field_in_the_report")
42
+ response.should contain("a_very_long_text_that_is_larger_than_the_text_field_in_the_")
43
+ end
44
+
45
+ it "should show the parameters defined in the controller" do
46
+ get :index, :format => :pdf
47
+
48
+ response.should contain("I'm a parameter. I was defined in the controller")
49
+ end
50
+
51
+ end
52
+
53
+ describe "GET compile_time_error_report" do
54
+
55
+ it "should raise a RuntimeError if the report's design is not valid" do
56
+ expect { get :compile_time_error_report, :format => :pdf }.to raise_error(RuntimeError, /Report design not valid/)
57
+ end
58
+
59
+ end
60
+
61
+ describe "GET runtime_error_report" do
62
+
63
+ it "should raise a RuntimeError if the report could not be filled due to a runtime error" do
64
+ expect { get :runtime_error_report, :format => :pdf }.to raise_error(RuntimeError)
65
+ end
66
+
67
+ end
68
+
69
+ describe "GET nil_datasource" do
70
+
71
+ it "should treat nil datasources" do
72
+ get :nil_datasource, :format => :pdf
73
+
74
+ response.should contain("I'm a parameter. I was defined in the controller")
75
+ end
76
+
77
+ end
78
+
79
+ describe "GET java_parameter" do
80
+
81
+ it "accepts java parameter without converting it to string" do
82
+ get :java_parameter, :format => :pdf
83
+
84
+ response.should contain("The Beatles")
85
+ end
86
+
87
+ end
88
+
89
+ end