USING PARMETERS IN iREPORT

USING PARMETERS IN iREPORT

 

Most basic knowledge any new learner would have of word parameter -“ parameter is a limit or boundary which defines the scope of a particular process or activity ”. And technically we can define it as – “A numerical or other measurable factor forming one of a set that defines a system or sets the conditions of its operation. ” Or call it “filter” in simple words.

But in jasper-reports they represent the best way communication channel between the report engine and the execution environment (which is your application). After designing the report and checking its preview, what designer will provide as solution to the user for his desired criteria for report generation at run time, the answer is parameter. So, here are basic steps using which you can add parameters to your report to enhance its reporting experience for user. Add parameter to the report from report inspector, and manage its addition and removal from the report .

1Fig 1.1

Right click on the “parameter 1” as shown in Fig 1.1, and rename the parameter as per your convenience. Now, pay attention to its properties to modify it .

2Fig 1.2

In properties you will notice fields as shown in Fig 1.2 , after naming it and defining its class in “parameter class” which is basically its data type, you need to provide “default value expression ” which gets executed only when value of parameter is not provided by the user at run time. Check box of “use as prompt” for pop up to get value from user , which will appear as shown in Fig 1.3.

3

Fig: 1.3

Next question is how parameter gets executed in SQL query of report?

 

Parameters can be used in SQL queries to filter records in a where condition or to add/replace pieces of raw SQL or even to pass the entire SQL string to execute.

In the first case the parameters are used as standard SQL parameters, in example:
SELECT * FROM DEPARTMENT WHERE DEPARTMENT_ID = $P{parameter1}
Syntax of parameter in query : $P{parameter1}
In above example the entered  value get evaluated to display the details of row containing values of DEPARTMENT_ID entered by user through parameter. Result will get displayed as Fig: 1.4
In this user will be solely responsible for the correct execution of report as only entered value will be used in the execution of query.

4Fig 1.4

Thanks,

Nisha Sahu

 

 

Passing values as parameters to Subreport from main report in Jasper

Tool used: ireport

What is subreport?

Before going into details, let me tell you what is subreport and what is the relation between a main report and subreport. A subreport is a report which is used inside another report.  This functionality helps us during complex designs where the data comes either from different portions of a single document or from different datasources.

In some cases, there are some values which needs to be used both on the main report and the subreport. The values can be either the parameters used to call the report, variables or can be the fields generated from the query. In those cases we need to pass those values as parameters to the subreport which in turn can be used in the subreport. The main things that we need to take care of are the datatype and the naming convention.

How to pass values to subreport?

The following are the steps:

  1. First thing is to create the subreport and link the subreport with the main report. For example: Let the name of the main report be Main.jrxml and the subreport be Subrpt.jrxml.

subrptElement

 

You now have to link the subreport element that you just added to the main report, to your subreport file (the one you created in step 1) In order to do that, select the subreport element in the main report, and modify its parameters as follows:

  • Subreport Expression: “repo:/public/MainReport/Subrpt.jrxml”               à Subreport path
  • Expression Class: java.lang.String
  • Connection Type: Use a connection expression

Connection Expression: $P{REPORT_CONNECTION}

subrpt1

2. Now the link has been established between the main report and the subreport. We will get these parameters by passing them from the main report to the subreport. The rule is,for every field(be it either a variable/a query field/ a parameter) we want to pass to a subreporta similar parameter (with another name) has to exist in the subreportso that we can make the mapping. So we have to create a parameter on the subreport side which will have the same name as the name of the field/variable on the main report and the datatype should be the same also. For example: If the name of main report variable is X with datatype java.lang.Integer then the subreport should contain the same parameter of X with datatype java.lang.Integer. This is how Jasper understands and therefore allows the data to flow into the subreport.

3. Go to the main report and click on the subreport band. In the properties section on the right hand side corner of the report, go to parameters option(2nd last option in ireport). Click on the expression tab and a pop up window named Subreport Parameters will open. See below screenshot for better understanding.

subrpt_parameters1Untitledsubrpt_parameters

4.While passing the parameter to the report, the Name section is the section where the name of the parameter present in the subreport is specified and in the expression section is the value you want to pass from the main report. In the above screenshot, I am just passing the parameter of the main report as my passing parameter to the subreport.

Note: It is advisable to keep the name of the parameter in subreport and the parameter’s name in the main report same. However if you want to change the name of the subreport parameter then we have to first mention over here and then mention the same name in the subreport. But we can’t change the data type of the parameter unless we are changing the type in the connection expression.

Hence, this is how we pass values(parameters/variables/fields) of main report as parameters to the subreport. Now we use that parameter anywhere in the subreport.

Thanks, Nitish

 

 

Dates in Jasper iReport – Expression for default values

This post teach you how to play with default date parameters & calculations on dates in jasper iReport.
In this tutorial you learn how to make use of  “SimpleDateFormat” class and java.util.Calendar class.

I) Let us start with Report Start(Start Date) and Report End(Report End) parameters.

In some reports you need to give default parameters in your start date and end date.
Eg:
Start Date = Previous month 1st day
End Date= Previous month last day
Qn :
 How to write default expression for this ?
Ans:
NOTE: 
$P{cal}  is a parameter used to reduce the expression complexity.
$P{cal}=java.util.Calendar.getInstance()
Start Date: ( java.util.Date)
(
$P{cal}.add(java.util.Calendar.MONTH, -1) ||
$P{cal}.set(java.util.Calendar.DAY_OF_MONTH, 1)
)
? null : $P{cal}.getTime()
 End Date(java.util.Date)
(
$P{cal}.set(java.util.Calendar.DAY_OF_MONTH, $P{cal}.getActualMaximum(java.util.Calendar.DAY_OF_MONTH))
)
? null : $P{cal}.getTime()
Sample output(i.e., default values in the date place holders would be)
Star Date :  2013-10-01
End Date : 2013-10-31

II) To get the individuals of a date using SimpleDateFormat class.

From Report Start  Date($P{ReportStart})
StartYYYY
Integer.parseInt(new SimpleDateFormat(“yyyy”).format($P{ReportStart}))
StartMM
Integer.parseInt(new SimpleDateFormat(“MM”).format($P{ReportStart}))
StartDD
Integer.parseInt(new SimpleDateFormat(“dd”).format($P{ReportStart})) 
Similarly you can extract the parts of the date for ReportEnd parameter($P{ReportEnd} )

III) To get the current parts of the date from Calendar class in iReport

CurrentYear:
$P{cal}.get(Calendar.Year)
CurrentMonth:
$P{cal}.get(Calendar.MONTH)+1
CurrentDayOfMonth:
$P{cal}.get(Calendar.DAY_OF_MONTH)