Accessing/importing user defined classes in jrxml - import

Have anyone tried to import user defined classes in jasper report (.jrxml file)?
I want to use some (user defined) Util class inside my jasper report to cook some bean attributes. I am using Javabean datasource
Please let me know if you need further clarification.
syntax to import class is
<import value="java.util.HashMap"/>
I want to use
<import value="mypackage.MyUtil" />
....
....
<field name="myVar" class="java.lang.String">
<fieldDescription><![CDATA[MyUtil.cook(myData)]]>
</fieldDescription>
</field>
The simple definition for MyUtil.java could be
package mypackage;
public class MyUtil
{
public static String cook(String data)
{
return data + "_cooked";
}
}

I think I should have tried sufficiently before asking this.
There is nothing extra needed apart from
There are two sections in jrxml:
1. Defining fields from javabean source
2. Using fields defined in step 1. to populate values in detail band
I was trying to cook the value of javabean members even before they are used to create fields
So, jasper was trying to parse that 'expression' as javabean member.
Following is wrong
<field name="myVar" class="java.lang.String">
<fieldDescription><![CDATA[MyUtil.cook(myData)]]>
</fieldDescription>
</field>
When I used the Util class on field value, it worked.
<textField>
<reportElement x="200" y="0" width="100" height="13"/>
<textElement/>
<textFieldExpression class="java.lang.String">
<![CDATA[MyUtil.cook($F{myVar})]]>
</textFieldExpression>
</textField>
Thanks
Nayn

You have to set the classpath in your iReport. It depends on its version, but is generally under Settings/Classpath

Related

Unable to add extra content into exported word doument from jasper report

I am using TIBCO Jaspersoft Studio-6.2.0.final for generating report . I am able to generate report in word (.docx).
I added some sample text. In export word sample text shown inside a frame. But when I am trying to update or add any extra text into that report in word, its hiding inside the frame. When I expand that frame, then it showing.
How I can generate report in word without any frame like normal word? So that I can add any text, so it will show normally.
Below I added jrxml code.
<queryString>
<![CDATA[SELECT 1 FROM DUal]]>
</queryString>
<field name="1" class="java.math.BigDecimal"/>
<detail>
<band height="20" splitType="Stretch">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="0" y="1" width="510" height="19" uuid="68ec5b9f-416e-45d6-b2aa-4d1f31a08250">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
</reportElement>
<textElement>
<font fontName="Arial" size="11"/>
</textElement>
<textFieldExpression><![CDATA["This is the sample text for testing.To test in word"]]></textFieldExpression>
</textField>
</band>
</detail>
Update:
Is any one having any update on this?
Currently there is no possibility to generate paragraph-oriented docx content. The built-in .docx exporter is a grid-layout exporter, so it will generate a table-based document. This is why enclosing frames/table cells are present in the document. Even if you'd use a single huge textfield for your entire document, a table with a single cell will be generated as docx output.
The only way to get the desired behavior is to implement and use a custom paragraph-oriented docx exporter, based on existing JasperReports APIs.
I hope this helps.
Jasper export .docx as grid/table. In your exported document use Layout -> View Gridlines to show those tables. You can edit them manually if it's a single report what you need. Else, check Aspose library or try to implement manually a custom exporter.
If your only problem is that fields doesn't expand when you write, just add this code to your report properties:
<property name="net.sf.jasperreports.export.docx.flexible.row.height" value="true"/>

How to avoid error when filling print, error evaluating expression?

Currently, I am working in JasperReport Server and iReport. The version of JasperReport 5.5.0 and iReport are 5.5.0. And the database is MYSQL.
I declared the variable Dr like the following where am_primeamt is coming from SQL query.
<variable name="Dr" class="java.lang.Double">
<variableExpression><![CDATA[($F{am_primeamt} > 0 ? $F{am_primeamt} : 0 )]]></variableExpression>
</variable>
And tried to show like following:
<textField pattern="#,##0.00">
<reportElement x="427" y="0" width="82" height="20" uuid="332ceda3-5237-40b5-a0ef-3aad009a7911">
<printWhenExpression><![CDATA[$V{Dr} != 0]]></printWhenExpression>
</reportElement>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{Dr}]]></textFieldExpression>
When I am trying to see the preview it's showing the following error:
Error filling print... Error evaluating expression :      Source text : $V{Dr} != 0
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      Source text : $V{Dr} != 0      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:244)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:591)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:559)      at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1016)      at net.sf.jasperreports.engine.fill.JRFillElement.evaluatePrintWhenExpression(JRFillElement.java:795)      at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:482)      at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)      at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:456)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2057)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:778)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:288)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:151)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:932)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)      at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:87)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:891)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  Caused by: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double      at gl_transaction_1454989470091_439976.evaluate(gl_transaction_1454989470091_439976:363)      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:231)      ... 20 more 
Print not filled. Try to use an EmptyDataSource...
Edited:
Language is set java from the beginning and that showed the error like this. After making it groovy, it solved my problem.
The accepted answer does not address the problem and since its upvoted with deleted users (ask your-self why) even if question does not have complete mcve I have decided to answer to help future viewer to understand the issue. The accepted answer has been deleted by moderators, but question remains and has indicated the solution of setting language groovy
The issues is:
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to
java.lang.Double
This is most probably caused by that the field $F{am_primeamt} is defined as java.math.BigDecimal
<field name="am_primeamt" class="java.math.BigDecimal">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
The correct solution would be to change the current variable declaration to same class or return declared class
Same class
<variable name="Dr" class="java.math.BigDecimal">
<variableExpression><![CDATA[($F{am_primeamt}.doubleValue() > 0 ? $F{am_primeamt} : new java.math.BigDecimal(0) )]]></variableExpression>
</variable>
Note: printWhenExpression not to be changed to example $V{Dr}.doubleValue() != 0
Declared class
<variable name="Dr" class="java.lang.Double">
<variableExpression><![CDATA[($F{am_primeamt}.doubleValue() > 0 ? $F{am_primeamt}.doubleValue() : 0d )]]></variableExpression>
</variable>
Why does groovy works?, it does automatic conversion using java.lang.Number and use operator overloading to allow direct mathematical operations on the BigDecimal object see http://www.groovy-lang.org/, but before you choose it instead of java its better to understand why.......

jasper report custom properties are not accessible in customexporter

I am writing my own custom xml exporter for jasper reports. I would like to define some properties in my .jrxml file, which i would like to access them later in my custom xml exporter.
I have added two custom properties(last two). I can see the other two properties in my custom xml exporter but not my custom properties. Is there anything i am missing?
<detail>
<band height="13">
<textField isBlankWhenNull="true">
<reportElement key="auctionCode" x="256" y="0" width="48" height="13" uuid="03408ccf-c8ba-4e4f-8da6-694f02b70e4b">
<property name="net.sf.jasperreports.export.csv.column.name" value="ShipCountry"/>
<property name="net.sf.jasperreports.export.csv.repeat.value" value="true"/>
<property name="com.xxxx.cdt.export.xml.test.value" value="abcdefghij"/>
<property name="com.xxxx.cdt.xml.test1.value1" value="ijklmnopqr"/>
You'll need to set a global property to instruct JR to transfer your properties from the JRXML to the generated report.
Include something like the following in your jasperreports.properties:
net.sf.jasperreports.print.transfer.xxxx=com.xxxx.cdt

jasper decimal separator being ignored

In a jasper report I use a JSON datasource.
This datasource contains numbers (e.g. 159.994).
There is a field of class java.lang.double (let's call it "doubleField") and a text field with this field and a pattern ("#,##0.00 ¤ (brutto)").
The pattern works fine (german currency format) but if I print out just the value of the doubleField the expected output would be "195,99" or "159.994".
But in fact it is "159.994,00" or "159994.0".
There was a solution changing the language of the report from "groovy" to "JavaScript" but this won't help and is also not applicable.
There is a "quick&dirty" solution (without being quick) to just take the JSON value as a String and then cast it to a double in the text field. This works fine but it's dirty and not really quick.
Any ideas?
I have a Jasper report definition. I'm just including a few things from that report definition that I think might be relevant to your problem.
First, I have several field definitions. Here are two of them:
<field name="FundFixedBeginningBalance" class="java.lang.Double">
<fieldDescription>
<![CDATA[FundFixedBeginningBalance/FundFixedBeginningBalanceValue]]>
</fieldDescription>
</field>
<field name="FundNonFixedBeginningBalance" class="java.lang.Double">
<fieldDescription>
<![CDATA[FundNonFixedBeginningBalance/FundNonFixedBeginningBalanceValue]]>
</fieldDescription>
</field>
Note how both are defined with class="java.lang.Double". I'm using XPath expressions because my data is in XML, but that is not fundamentally different from JSON. Later, in the report layout section I have:
<textField pattern="#,##0.00 ;(#,##0.00)" isBlankWhenNull="true">
<reportElement positionType="Float" x="368" y="52" width="100" height="14" uuid="deac984d-39b8-49f4-b4d2-28e66681c098">
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
</reportElement>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{FundFixedBeginningBalance} + $F{FundNonFixedBeginningBalance}]]></textFieldExpression>
</textField>
Notice there is no casting within the textFieldExpression element; just substituting both fields in with $F and adding them together (and this prints correctly). Are you doing something different?

How do I print a list of strings contained within another list in iReport?

I am creating a simple reporting program using java and iReport (from jasper), which is supposed to create a report in pdf showing PCs with their IP address, their location, whether it's idle or not at the moment (handled by another system), and a list of projects it is currently attached to (managed elsewhere, too).
I'm using iReport for this matter, and have created a dummy collection generating class as follows:
public class PCReports {
public static java.util.Collection PC_collection;
public static java.util.Collection generateCollection() {
PC_collection = new ArrayList<PCLineDTO>();
PCLineDTO line = new PCLineDTO();
line.setIP("192.168.1.1");
line.setLab("location");
line.setActive(true);
line.addProjectName("project1");
line.addProjectName("project2");
line.addProjectName("project3");
PC_collection.add(line);
line = new PCLineDTO();
line.setIp("192.168.1.2");
line.setLab("location2");
line.setActive(false);
line.addProjectName("project1");
line.addProjectName("project2");
PC_collection.add(line);
return PC_collection;
}
}
The entity class in this case being:
public class PCLineDTO {
private String ip;
private String lab;
private Boolean active;
private ArrayList<String> projects;
}
After some searching around the Internet, I found a way to do something similar, using subreports.
The thing is, I don't know how to print a collection of strings passed as a dataSource to this subreport.
In the examples I found on the Internet, for each item in the master collection, the subreports were passed a collection of objects -with their own getter methods for each attribute- instead of a collection of strings as is the case here. In those cases, they accessed the values they needed to use via the iReport syntax, which I was not able to use, for example:
$F{project}
Since iReport looks for a getProject method contained within the objects it receives, but in this case it's a simple String object (without a getProject method, as it were).
Use a subreport or a subdataset.
Pass the subreport a collection datasource
JRBeanCollectionDataSource($F{Projects})
Then in the new subreport create a new field called "_THIS" exactly, this means the bean in the collection passed is the same as the value i want
For more info, check the source code of the class here: JRAbstractBeanDataSource
Note: this is available in JasperReport 3.0.0 im not sure if it exists in previous builds.
Hope this helps
Update: just checked the SVN, seems like this feature is implemented in JasperReports 2.0.0
Interesting. I think you'd better use the List, and then define getName() on the Project class. Then in the subreport define a variable "name". It will work this way, and it will allow you to add easily additional information, like project duration, team-lead, etc.
As Bozho says, in case proyects was an array of complex object you should reference it as a field of type java.util.Collection an then pass it to the inner subreport the same way medopal indicates. And don´t put the _THIS field.
To elaborate on this without using _THIS: let's say a java bean has a list of subBeans and this subBean has a complex format and we want to print each subBean in a custom way. I quote an example where the subDataset element is on the report level and the componentElement is in the detail band:
<subDataset name="ListOfSubBeans" uuid="66c86e41-c565-4f18-bccf-1a1b2a567585">
<field name="subBeanField_1" class="java.lang.String">
<fieldDescription><![CDATA[subBeanField_1]]></fieldDescription>
</field>
</subDataset>
...
<componentElement>
<reportElement x="780" y="0" width="100" height="30" uuid="f73864b9-46dd-4adb-8dad-a6bd8dfae64e">
<property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
</reportElement>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="ListOfSubBeans" uuid="a8dd1c2b-3ac0-4ffa-b9d0-08e4890e199a">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listOfSubBeans})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="30" width="100">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="61700c18-6bb9-45da-a235-b76b9f76a2ea"/>
<textFieldExpression><![CDATA[$F{subBeanField_1}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
...
So, the master dataset has declared that the master bean has a member variable that is a list: listOfSubBeans. This java.util.List is used to feed the datasource of the jr:list, while the fields of the jr:list are declared using a subDataset called ListOfSubBeans (pay attention to case sensitivity).