Translate

Monday 24 March 2014

Using Styles in Jasper Reports

We have been accustomed to use style sheets or CSS in our HTML codes, parallel to that we can use Styles in Jasper Reports also. It provides same utilities like conditional, non -conditional and could be applied on  any element in the Report.

Now how to create styles, there are two methods basically:
1. External Style Sheet (jrtx file)
2. Declaring Internal Styles in the report itself.

The advantages of using external Style sheet is that it provides modularity and re-usability of the style over large number of reports using similar kind of styles. On the other hand, Internal styles would only contains the styles definitions for a particular report.

How to apply styles in the reports(iReport).

1. Locate the Styles node in the Report Inspector.
2. Right Click on the node and add a new Style
3. Then you could define the properties of the Style in the in the properties pane. Properties like background-color,forecolor, outline,.. etc/
4. You could even click on the Style and add a conditional Style and define conditional styles, like alternate coloring and much more.
5. After defining the styles now in the application part, locate the element onto which you want to apply the style.
6. Select the particular style in the property called Style(could be found in the properties pane) of the Element.

I have created a simple tutorial video demonstrating the Styles in Jasper Reports. Click Here

Download the sample demonstrated in the video tutorial from here. The ZIP file contains :
1. The JRXML File
2. The jrtx Style.

The Sample uses the Sample database so no need to worry about the datasource.

Happy Coding.

How to display the Username of the user who logged into JasperServer

Many a times we require the name of the person who logged into the Jasper Server and according to this information we are able to show/hide the elements on report, or simpley to log the user name we can use it.

So how to achieve this ? 


We can create parameters in our report, with a specific name and datatype. Then these parameters will be automatically filled when the report is executed by JasperReports Server.
LoggedInUsername (java.lang.String), with no default value. Then, we can use $P{LoggedInUsername} in a text field, in a query, in a printWhenExpression, etc ...
This is a built in parameter which gets filled with the User Details when report is run on Jasper Server.
Note that the report if run in iReport, would get a null value. So for the correct results it is necessary that we deploy the report onto JasperServer.



Parameter Name
Type
Notes
LoggedInUser
User
Not usable in query input control,
 but is used as parameter to report
LoggedInUsername
String
Of logged in user
LoggedInUserFullName
String
Of logged in user
LoggedInUserEmailAddress
String
Of logged in user
LoggedInUserEnabled
Boolean
Is logged in user enabled?
LoggedInUserExternallyDefined
Boolean
Is logged in user externally defined?
 ie. authenticated externally
LoggedInUserTenantId
String
Of logged in user. Only relevant in 
Pro/Enterprise.
LoggedInUserRoles
Collection<string>
Current set of roles of logged in user. 
Useful for $X parameter
LoggedInUserAttributes
Map<string, string>
Not usable in query input control, but is 
used as parameter to report.
 Empty map if no attributes
LoggedInUserAttributeNames
Collection<string>
User profile attribute names. Useful for
 $X parameters. Empty collection if no
 
attributes
LoggedInUserAttributeValues
Collection
-
LoggedInUserAttribute_<attribute name>
String
Attribute value for matched attribute name (like "att1") on the user. Empty string if no match. Only provided if defined in a query or as a report parameter.

Using the above parameters we could easily get various important information which could help in defining the roles. 

Happy Coding.!!

Thursday 6 March 2014

Cascading Parameters in Jasper Reports

Cascading, what does one mean by saying cascading, literally it means anything that resembles a waterfall or especially seeming to flow. Now where to implement this in Jasper Reports?    

Right, it could be used in parameters, or rather the filters which are dependent on each other. You can find these kind of filters in online shopping websites, wherein we can choose on various filters like if we want to buy the laptop first filter would be Laptop next filter would be the company chosen among the companies which sell Laptops, and then we chose for the specifications like processor,ram etc. each have separate filters but the boundaries of the parameter depends on the previous parameter.

Before getting into the setup of Cascading Parameters in Jasper Report there are few things which we need to know before hand - 
Usage of     $X{IN, feild, parameter} in the SQL Query used in the Where clause, which filters the data based on the selection. Let me go one by one in explaining the components
field - Now this is the field in the database upon which the find will be done
parameter - Now this is the parameter selected by user and will be queried or looked in the field. This enable for multi select also in the parameters.

Now in  order to achieve this in Jasper Reports we need to follow few steps.

  1. First of all you need to design a Jasper Report and identify the parameters to be cascaded
  2. Then we need to define the expression for parameters,  and mind one thing that each of the parameter need to be of type java.util.collection if you want to enable multi- select.
  3. Next and very important thing is that the cascading parameters could not be tested in iReport / JasperSoft Studio, this  needs to be deployed on Jasper Server in order to check the correct functionality. Though could be run in iReport or JasperReports with default values hardcoded.



I have created a working sample which could be simply deployed, and checked for the functionality and working.
The Zip file could be downloaded from here  - LINK TO ZIP FILE

The Zip file contains
1. Cascade Parameter Report (JRXML File)
2. SQL DUMP, could be extracted using MySQL or any other tool
3. Table Creation Script in a notepad file, this will allow you to create the require table in the database.

For setting up the sample report you need to-
1. Create a schema in you database and create the table using the script command
2. Then run the JRXML file using iReport/Jaspersoft Studio
3. Then set the datasource referring to Persons table in the database.
4. Then deploy the JRXML  on the Jasper Server, and set the datasource and input controls. Do remember the names of the parameters used in the report should exactly match the input controls declared on the Jasper Server.

If you miss on some step then you could follow a YouTube video of mine which  will serve as a tutorial.
Below is the video where in you could see all the steps.




Please put your views or questions or suggestions below, I will be more than happy to hear them.Happy Coding!!