JasperReports not converting postgresql timestamp to simple date - jasper-reports

I saw this question which is similar to my question, but however the workaround still gave me an error.
I have an sql query string like this in my .jrxml file
<queryString>
<![CDATA[select sl_no,cast(action_on as date) from action_history]]>
</queryString>
I am using the Eclipse plugin JasperStudio for designing the template and am using PostgreSql for my database. Now this query is fetching data properly as expected.
My action_on is a java.sql.Timestamp type.
<field name="action_on" class="java.sql.Timestamp">
I only want the date like 22/08/15 or 22/08/2015 to be printed and don't want the time to be printed, so when I try casting or using action_on::date , I get 22/08/15 12:00 AM printed with the time always 12:00 AM. What perplexes me more is that when I try this command on my terminal , it doesn't give me the time and only the unformatted date!
Is there a workaround for this?
EDIT
I changed <field name="action_on" class="java.sql.Timestamp"> to <field name="action_on" class="java.lang.String"> and I get the date 2015-08-22 !
Is there a way to now format this?

I got it resolved myself , so I am posting my solution to my problem . A better solution is obviously welcome!
I converted the field type to String i.e did the EDIT to the question. To format the date you can now use
<![CDATA[select sl_no,to_char(action_on,'dd-MM-yyyy') as my_date from action_history]]>
Don't forget to replace every following instance of declaring or using action_on in your field declarations by my_date !

The other solution is to not format in query (but to format when you output) using pattern attribute on the textField tag
<textField pattern="dd-MM-yyyy">
<reportElement x="0" y="o" width="100" height="20"uuid="b8baea82-84c4-42fa-bccd-62abc96eeded"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{action_on}]]></textFieldExpression>
</textField>
The main advantage is that if you export to for example excel the column will be correctly filled with a date object (hence a user of excel can filter/sort/format on the date object).

Related

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 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?

Grouping by parameter value in jasper

Well, I don't know, maybe I'm missing something, but I've been trying to group data in a jasper report, but so far the grouping appears only if the group expression is a field. Is it possible to group data by parameter value instead of field value? i.e., something like
<groupExpression><[!CDATA[$P{some_param}]]></groupExpression>
instead of
<groupExpression><[!CDATA[$F{some_field}]]></groupExpression>
in the .jrxml file?
Is it possible to group data by parameter value instead of field value?
Yes, it is, just like you mentioned. You would have no syntax errors and the report would be generated.
The problem is that it may not bring what you're expecting. The group bands are usually printed every time the "groupExpression" changes. So basically this parameter needs to be associated with something that will change during the report generation (for example, filling a parameter of a subreport with a field in way that this subreport uses this paramater as a group expression). And of course, it needs to be associated with something that makes sense and bring you the desirable behavior.
You can have something like this in your subreport:
...
<parameter name="START" class="java.util.Date"/>
<parameter name="END" class="java.util.Date"/>
...
And something like this in a detail band of your "super" report:
<subreport>
<reportElement x="0" y="10" width="555" height="200" uuid="ac2c99da-f595-4498-a518-2bfb1f31b73c"/>
<subreportParameter name="START">
<subreportParameterExpression><![CDATA[$F{start}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="END">
<subreportParameterExpression><![CDATA[$F{end}]]></subreportParameterExpression>
</subreportParameter>
...
</subreport>
Just notice I'm assuming the fields $F{end} and $F{start} are also "java.util.Date" objects.

How to get the username of user who logged into Jasper Server?

Please tell me if there is any method through which I can get the user name of the user who logged into the Jasper Server, and that name could be displayed in the report.
Is there any parameter which can be referred or any other work around to obtain the logged in user name or any other way to deal with the issue.
Thanks in Advance!!
Try declaring a parameter with the name "LoggedInUsername" (this exact name, since it is reserverd by jasper to hold the username of the logged user).
<parameter name="LoggedInUsername" class="java.lang.String" isForPrompting="false"/>
and use it like $P{LoggedInUsername}.
Note: this will only work on the server, not in iReport.
Open report in iReport and create a parameter with name "LoggedInUser" and class "com.jaspersoft.jasperserver.api.metadata.user.domain.User", otherwise you can copy the below XML line in XML of the report where all parameters are listed.
<parameter name="LoggedInUser"
class="com.jaspersoft.jasperserver.api.metadata.user.domain.User"/>
Then add a text field and then you can refer to this parameter, like:
<textFieldExpression class="java.lang.String">
<![CDATA[$P{LoggedInUser}.getFullName()]]></textFieldExpression>
or
<textFieldExpression class="java.lang.String">
<![CDATA[$P{LoggedInUser}.getUsername()]]></textFieldExpression>

How to pass date as a field in jrxml of JasperReports?

I want to pass date as a field to the jrxml.
Following is the code for it.
<xyLineChart>
<chart evaluationTime="Band">
<reportElement x="0" y="0" width="555" height="500"/>
</chart>
<xyDataset>
<dataset incrementType="None"/>
<xySeries>
<seriesExpression><![CDATA["CpuUsageGraph"]]></seriesExpression>
<xValueExpression><![CDATA[new java.util.Date($F{time}.getTime())]]></xValueExpression>
<yValueExpression><![CDATA[$F{cpuUsage}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
</linePlot>
</xyLineChart>
But it is not working.
Its giving error as can not cast from date to number.
Then how to convert it?
You are using the time field in the constructor of Date. No need for that.
Instead of this:
java.util.Date($F{time}.getTime())
use this:
$F{time}
If you want the long value of it: then use this
$F{time}.getTime()
UPDATE
I didn't notice you are using a chart, here is a new answer:
In charts, X and Y value expressions should be any Number object, check subclasses in Number Class JavaDoc, in your case you are getting the long value of your Time field, which cannot be cast to Number, you will need to define a new object, for example:
new Long($F{time}.getTime())
Side Note: in this case the report will compile and work, BUT, you are getting the number of milliseconds and using it in your chart. I don't think that is what you want exactly. So i would suggest extracting a specific field from your Date field like Day. Month, Year ... etc
I cannot see the attached JRXML. However, open your JRXML file in a text editor, and check that the field is defined something like this:
<field name="MyDate" class="java.util.Date"/>