How use a parameter as datasource in Jasper list element? - jasper-reports

I have a report template where a parameter is defined as:
<parameter name="phonenumbers" class="java.util.List"/>
Those phonenumbers are objects of type:
se.primenta.data.entity.PhoneNumber
and these objects have two methods that I need present in the report:
String getMaskedNumber();
Long getNumber;
I can't understand how to get these two fields into a subDataset that can be visualized in a List element. How do I write the dataset and the list definitition in the JRXML template?

What about having a Datasource for the list like :
<dataSourceExpression>
<![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{phonenumbers})]]>
</dataSourceExpression>
And then using
$F{maskedNumber} and $F{number}
inside the list ?

Related

How get fields of object in parameter iReport

I'm passing the object "OrdemServico" as parameter to report.
OrdemServico ordemServico = new OrdemServico();
ordemServico.setCodigo("CA123");
mapa.put("ORDEMSERVICO", ordemServico);
And on the .jrxml i added a parameter with same name of the parameter in map. how i can get the fields of the object? Tnks
You need also add import and declare the parameter
Great.
To work here:
<parameter name="RelatorioMaquina" class="br.pro.x87.reports.RelatorioMaquina"/>
and to acess the field:
$P{RelatorioMaquina}.getDescricao()

JasperReport table column sum for page

I need TO add a column sum to column footer of table object for each page.
If I create variable and set sum aggregation function than sum value appear for full report. But I need sum column data for each page, not for full report.
Can anyone help me solve this problem?
This is not a solution for the jr:table component since it uses a subdataset where its not allowed to set resetType="Page", but using a subreport it can be used.
I don't think there is a work around in components (jr:table,jr:list) using a subdataset but would be happy If someone can prove me wrong
Solution using subreport
On your variable set resetType="Page"
es.
<variable name="var" class="java.lang.Double" resetType="Page" calculation="Sum"/>
....
</variable>
For more info on resetType see JRVariable API
On your textField set evaluationTime="Page"
es.
<textField evaluationTime="Page">
....
</textField>
For more info on evaluationTime see JRTextField API
In case of Dataset set resetType="Report"

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>

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"/>

iReport parameter not being informed by field

I am using iReport - my first time using it - and I am trying to make a report that will involve passing a parameter to a subreport. I am not able to get that to work, so I did the following test:
I have a large query that grabs various Fields from my DB - when I preview the report these fields are being pulled and display (over and over, etc., a lot of stuff is being pulled).
I created a new parameter on the main report called "myparam". I made it's default value one of the fields that is being successfully pulled from my DB.
I created a textfield to display this param - the intent being it will echo the field that displays already in my report. If I can get it to do that, I figure I can get it to correctly "plug in" to my subreport.
The issue is - when I preview, I am asked to give the parameter a value - I choose default, which defaults it to the Field I set, which means it should now echo that field (aka I WANT it to echo that field). But it does not echo that field. Instead it just shows null over and over again. Here is a screenshot of what I am talking about
Here is the setup for my report:
Here is proof I've set the default value in my xml:
<parameter name="myparam" class="java.lang.String">
<defaultValueExpression><![CDATA[$F{TCORDERID}]]> </defaultValueExpression>
</parameter>
Here is a snippet of the report which is being generated incorrectly (aka myparam is not getting populated right).
THEY ARE BOTH STRINGS
From my understanding of parameters, they are passed to the report/subreport when it gets created. You have set the default value of the parameter to one of your fields, which has not been populated yet when the report gets generated.
I would suggest adding a simple subreport (which is the reason you want to use parameters anyway), remove the parameter on the main report, and add it to the subreport (with a text field to display it).
Now, add the Parameter to the subreport object in your main report. (This is under the Subreport properties section in iReport.
Name: The name of the Parameter in the subreport.
Expression: The field from your main report that will be passed to the subreport.
So your main report will have the following under the subreport detail:
<subreportParameter name="myparam">
<subreportParameterExpression><![CDATA[$F{TCORDERID}]]></subreportParameterExpression>
</subreportParameter>
And then the Parameter in the subreport:
<parameter name="myparam" class="java.lang.String">
<defaultValueExpression><![CDATA["No result"]]></defaultValueExpression>
</parameter>