How to access input control parameters from Jasper server? - jasper-reports

I want to create a jasper report which changes the query and add/removes additional conditons in its where clause based on the input provided by the user in the jasper server.
One option is to sql inject the querystring in the jrxml file, but that looks messy as we may have additional conditions in the where clause which may be added.
The other approach mentioned in this post Dynamic querystring in JRXML seems to be a good one.
I would like to know how can I access the control parameters in the java code passed from jasper server?
Or can I give some kind of conditional logic within the jrxml file? which checks if some of the input controls are empty then assign one query in the queryString variable and another query if other conditions are valid?
Thanks.

Buld a "fake" parameter named P_DYNAMIC_WHERE_CLAUSE, based on your real input parameters. For instance:
<parameter name="PARAM1" class="java.lang.Long">
<parameterDescription><![CDATA[Real query criteria on column1]]></parameterDescription>
</parameter>
...
<parameter name="P_WHERE_CLAUSE_PARAM1" class="java.lang.String" isForPrompting="false">
<parameterDescription><![CDATA[Computed parameter, containing the optional SQL where clause corresponding to parameter PARAM1]]></parameterDescription>
<defaultValueExpression><![CDATA[$P{PARAM1} != null ? " $X{EQUAL, COLUMN1, PARAM1} " : " "]]></defaultValueExpression>
</parameter>
and use the $P!{P_DYNAMIC_WHERE_CLAUSE} syntax in the queryString.
For instance:
<queryString>
<![CDATA[SELECT /* my_jasper_report */ column1
FROM my_table
WHERE (1=1)
$P!{P_DYNAMIC_WHERE_CLAUSE}
]]>
</queryString>

Related

Get a default value of a single select query parameter Jasperserver

I am trying to select a default value in a Jasperserver 8.0.0 dropdown list, the following code gets the dropdown list, the problem is that it changes the default value but it brings me only that default value and the others disappear
SELECT '', pa.attrname
FROM jiprofileattribute pa
WHERE pa.principalobjectclass = 'com.jaspersoft.jasperserver.api.metadata.user.domain.impl.hibernate.RepoUser'
UNION
SELECT pa.attrvalue, pa.attrname
FROM jiprofileattribute pa
WHERE pa.principalobjectclass = 'com.jaspersoft.jasperserver.api.metadata.user.domain.impl.hibernate.RepoUser'
the solution was that in Jasperstudio the attributes should be passed as default parameter, that way it is chosen by default.
<parameter name="LoggedInUserAttributeValues" class="java.util.Collection" isForPrompting="false"/>
<parameter name="AMB" class="java.util.Collection">
<defaultValueExpression><![CDATA[$P{LoggedInUserAttributeValues}]]></defaultValueExpression>
</parameter>

Using ternary expression inside a queryString

I have a JasperReports 6.3 report query which uses a "start date" parameter as part of its WHERE clause. The parameter may be null, which essentially means "earliest date".
The logic is very simple:
WHERE MyDateField > ($P{StartDate} == null ? DateTime(0L) : $P{StartDate})
But this produces an exception when JasperReports tries to run the query against the database:
SQLServerException: The value is not set for the parameter number 3
As a side note, it would be useful to figure out how to get Jaspersoft Studio to log the generated SQL, to assist in debugging.
It is impossible to use expressions at queryString. You should create another one parameter contains the expression you need.
In your case the code will be like this:
<parameter name="StartDate" class="org.joda.time.DateTime"/>
...
<parameter name="dateFilter" class="org.joda.time.DateTime">
<defaultValueExpression><![CDATA[$P{StartDate} == null ? DateTime(0L) : $P{StartDate}]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT * FROM sometable WHERE MyDateField > $P{dateFilter}]]>
</queryString>
You can find more information about using parameters here:
JasperReports: Passing parameters to query

Bind report parameter to a sql output jasper report

I have an existing report which needs to modified a little. Suppose my report query is like
select name,currency,productcode from where name=?
Now this '?' value will come from a report parameter say countryName. That can be done using parameterized query not a problem.
Now what I need is this parameter countryName to get the data from another query like below
select name from countries
In short I want to bind the value of report parameter countryName to the output of the above query and also I want to put this query in report it self.
Using birt its very easy but I want to know is it possible with jasper?
P.S I'm novice in jasper report.
I have tried to give an small scenario to represent my issue. The actual report is much more that this and is very complex.
Any help would be highly appreciated!!
I give you an answer how this can be achieved without passing anything through the parameter map (even if I think the parameter map should be the preferred way, modify your standard module to support parameter map)
You say you will have combobox value with the country name, we need to make it static. (or have a static way to access it, "there can be only one")
Example class of your interface:
package com.your.package;
public class SelectCountryInterface {
private static JComboBox<String> selectCountry;
public static synchronized String getSelectedCountry(){
Object value = selectCountry.getSelectedItem();
if (value instanceof String){
return (String)value;
}
return "";
}
//...Here goes you code to instance and populate the combobox
}
In jasper report (.jrxml)
Define a parameter and set the defaultValueExpression to point at your static method in class, this way it will be initialized with the value that you are providing from your interface class.
<parameter name="country" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA[com.your.package.SelectCountryInterface.getSelectedCountry()]]> </defaultValueExpression>
</parameter>
Set the queryString to use your parameter
<queryString>
<![CDATA[select name,currency,productcode from where name=$P{country}]]>
</queryString>

Bypassing parameters in ireport 5.6.0

I created a simple report in i-report and i added a parameter on a field salary. Now every time i click on preview i get the parameter pop-up to filter. And if the value if not correct i get a blank page. Now that's exactly what i was trying to do. However i am wondering if there's a way to enter a certain value in the parameter box to display all records. Any idea if this possible and if yes how? Thank you.
WHERE EMPLOYEES."SALARY" = ${P1}
You need to change your query (lets imagine that SALARY is numeric, Double). to
WHERE EMPLOYEES."SALARY" = $P{parameter1} OR 0=$P{parameter1}
and define your parameter with a defaultValueExpression and set attribute isForPrompting="false"
<parameter name="parameter1" class="java.lang.Double" isForPrompting="false">
<defaultValueExpression><![CDATA[new java.lang.Double(0)]]></defaultValueExpression>
</parameter>
You will see no more prompt and display all data, if SALARY is of other class naturally you need to adjust the example accordingly.

How to provide field type in XML datasource of ireport designer?

I am newbie to the ireport designer.This question may simple for you.I have tried to XML file as data source and it worked.The problem is All the fields are coming as java.lang.string. How to provide the field type in XML datasource.Is it possible to provide field type in XML file itself. Consider
<customer>
<name>obuli</name>
<age><22></age>
<subscriber>
<name>sundar</name>
<no_of_transactions>100</no_of_transactions>
</subscriber>
</customer>
Here the customer and subscriber are classes so i need to provide filed type com.test.Customer,com.test.Subscriber. I can achieve this by java bean datasource. But still i need the XML datasource way.
Is it possible to set the field type in XML datasource way ?
I do not think so. I also do not think you need that feature as reports only require scalar values. So just map the scalar values in your datasource to your scalar valued report fields.
You have to go on the XML code of your document and then search your field tag name, for example:
<field name="xxxx" class="java.lang.String"/>
change to Date type:
<field name="xxxx" class="java.util.Date"/>