How to compare dates in JasperReports?
I want to use the "image Expression". I tried but thats not working
$P{current_date} <= $P{image_date} ? '1.jpg' : '2.jpg'
$P{current_date}.after($P{image_date}) ? '1.jpg' : '2.jpg'
new Long($P{current_date}.getTime()) <= new Long($P{image_date}.getTime()) ? '1.jpg' : '2.jpg'
If you have these parameters class="java.util.Date"
<parameter name="current_date" class="java.util.Date" isForPrompting="false">
<defaultValueExpression><![CDATA[new java.util.Date()]]></defaultValueExpression>
</parameter>
<parameter name="image_date" class="java.util.Date">
<defaultValueExpression><![CDATA[new java.util.Date()]]></defaultValueExpression>
</parameter>
This image expression will work
<image>
<reportElement x="139" y="13" width="223" height="93" uuid="fdb46643-77b6-4d23-a88c-7108b97c091f"/>
<imageExpression><![CDATA[$P{current_date}.after($P{image_date}) ? "Full absolute path\\1.jpg" :"Full absolute path\\2.jpg"]]></imageExpression>
</image>
I think that your problem is merly that you are giving relative path of image to jasper report... you need to give absolute path to jasper report es. "C:\\Users\\Friberg\\Pictures\\1.jpg"
I also see some single ' in your code you need to use the " to return the String value of your path.
Development note: the 2 \\ in the path is because \ is escape char in java
Related
I am trying to execute the whole content when the parameter is null and conditional executional execution if the parameter is given a value. The whole content is executing but the conditional execution is not working.
The code which I tried is
<parameter name="Application" class="java.lang.String">
<parameterDescription><![CDATA[Application Name]]></parameterDescription>
</parameter>
<parameter name="whereClause" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{Application} == null || $P{Application}.isEmpty() ?
"1=1" : "((dt>= TO_TIMESTAMP($P{FromDtQry}, 'yyyy-MM-dd HH24:MI:ss') AND dt<=
TO_TIMESTAMP($P{ToDtQry}, 'yyyy-MM-dd HH24:MI:ss')) AND (rv202=" + $P{Application} + ")"
]]>
</defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT id,dt,estz,evt,voc,rv201,rv202,rv203,rv204,rv205,rv40
FROM idmrpt_cefevents_v
WHERE
$P!{whereClause}]]>
</queryString>
Could anyone help me out with this?
I want to put validations on these dates like:
1) END_DATE should not be less than START_DATE
2) START_DATE should not be greater than END_DATE
You could use variables for this case. I created a similar name and reproduced your requirements.
<variable name="START_DATE_corr" class="java.util.Date">
<variableExpression><![CDATA[($F{START_DATE}.getTime() > $F{END_DATE}.getTime()) ? $F{END_DATE} : $F{START_DATE}]]></variableExpression>
</variable>
<variable name="END_DATE_corr" class="java.util.Date">
<variableExpression><![CDATA[$F{END_DATE}.getTime() < $F{START_DATE} ? $F{START_DATE} : $F{END_DATE}]]></variableExpression>
</variable>
Afterwards you can use the variables
$V{START_DATE_corr}
$V{END_DATE_corr}
in your report.
I am very new with ireport-5.6.0.
I have one text field(Sanctioned_intake) and one i declare variable(ROPORT_COUNT).Sanctioned_intake is nothing but total number of Student per Department and REPORT_COUNT is return total no of rows which is enter into the report
For Example:
Sanctioned Intake:140 (Total Student per Department)
Actual Admitted: 10 (Actual Admission Taking)
So Difference Should Be:
Vacancy : 130(Remaining Vacancy )
now my question is how to make difference between this two
I declare One Variable
and set the following properties
variable class=java.math.BigDecimal
Calculation Sum
Reset Type Report
Variable Expression: $F{sanctioned_intake}.substract($V{REPORT_COUNT})
But i got this error
Error filling print... Error evaluating expression : Source text : $F{sanctioned_intake}.substract$V{REPORT_COUNT}
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :
Source text : $F{sanctioned_intake}.substract$V{REPORT_COUNT}
at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:327)
Caused by: groovy.lang.MissingMethodException:
No signature of method: java.lang.String.substract() is applicable for argument types: (java.lang.Integer) values: [1]
Possible solutions: substring(int), substring(int, int)
Data Type of $V{REPORT_COUNT} is integer.
Check data types of each field and variable.
Or if datatype of $F{sanctioned_intake} == BigDecimal then change variable expression to below
$F{sanctioned_intake}.substract(new BigDecimal($V{REPORT_COUNT}))
The error is related to that $F{sanctioned_intake} is declared as java.lang.String.
<field name="sanctioned_intake" class="java.lang.String"/>
However I do not think you will achieve your excepted result changing it to java.math.BigDecimal since jasper report has something called EvalutationTime, the $V{REPORT_COUNT} will start with 0 and reach the total report count only when all records have been displayed.
To the display the difference of 2 fields, declare them as Integer (if you can not, you need to parse them before calculating difference) and then use a text field.
example consider that $F's are java.lang.Integer
<textField>
<reportElement x="163" y="16" width="100" height="20" uuid="4196bd23-306b-44f6-8e3e-4d637facacf6"/>
<textFieldExpression><![CDATA[$F{sanctioned_intake}.intValue()-$F{actual_admitted.intValue()}]]></textFieldExpression>
</textField>
If one needs to be calculated use a variable, calculating (count) and resetting it as needs and then set correct evaluation time on your textField (so that the variables have time to be calculated)
example
<textField evaluationTime="Report">
<reportElement x="163" y="16" width="100" height="20" uuid="4196bd23-306b-44f6-8e3e-4d637facacf6"/>
<textFieldExpression><![CDATA[$F{sanctioned_intake}.intValue()-$V{actual_admitted}.intValue()]]></textFieldExpression>
</textField>
Note: you are using language="groovy" maybe you like to use language="java" on your jasperReport tag.
In JasperStudio, I need to display the text only on (n-1)th page. unfortunately I am unable to do that.
I have tried doing it with variables below
<variable name="nthpage" class="java.lang.Integer" resetType="None">
<variableExpression><![CDATA[$V{PAGE_NUMBER}]]></variableExpression>
</variable>
and
<variable name="n-1thpage" class="java.lang.Integer" resetType="None">
<variableExpression><![CDATA[($V{PAGE_NUMBER}-1)]]></variableExpression>
</variable>
and for the report text field expression used as
<textField evaluationTime="Report">
<reportElement x="383" y="124" width="136" height="16" uuid="3193a49a-7354-44c2-a5e7-62336419a190"/>
<textFieldExpression><![CDATA[$V{n-1thpage}==($V{nthpage})?"Hi":"Hello"]]></textFieldExpression>
</textField>
I have tried with evaluationTime="Auto". But it's not working for me.
How can I achieve this custom behavior?
You need to use the Print When Expression e.g. to print a report element on the 10th page only fill the Print When Expression with the value $V{PAGE_NUMBER} == 10
Let me depict a situation: I have a report A and subreports B and C. A uses B and B uses C. Default value of SUBREPORT_DIR in A is $P{CUSTOM_SUBREPORT_DIR}. In A.jrxml I see:
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{CUSTOM_SUBREPORT_DIR}]]></defaultValueExpression>
</parameter>
<parameter name="CUSTOM_SUBREPORT_DIR" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
I also need a proper value of SUBREPORT_DIR in B so I passed it using iReport (Subreport properties/Parameters) so in A.jrxml I have:
<subreportParameter name="SUBREPORT_DIR">
<subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
</subreportParameter>
And when I'm trying to generate report A I get net.sf.jasperreports.engine.JRException: Incompatible java.lang.Integer value assigned to parameter SUBREPORT_DIR in the B dataset.
I'm sure that $P{SUBREPORT_DIR} isn't an integer and type of SUBREPORT_DIR parameter in B subreport is String. All reports I made using iReport 4.0.2.
You can try the following (I do it in this way and it works):
Define the parameters in report A:
<parameter name="SUBREPORT_B_DIR" class="java.lang.String" isForPrompting="false" />
<parameter name="SUBREPORT_C_DIR" class="java.lang.String" isForPrompting="false" />
Define subreport B in report A. Here I use DataSource for report B from parameter. Also I send a parameter to subreport B with the subreport C directory and another parameter with DataSource for report C.
<subreport>
<reportElement x="0" y="16" width="583" height="10" />
<subreportParameter name="SUBREPORT_C_DIR">
<subreportParameterExpression><![CDATA[$P{SUBREPORT_C_DIR}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="SubReportCDataSource">
<subreportParameterExpression><![CDATA[$P{SubReportCDataSource}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{SubReportBDataSource}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_B_DIR} + "B.jasper"]]></subreportExpression>
</subreport>
Finally, in subreport B, define subreport C:
<subreport>
<reportElement x="0" y="16" width="583" height="10" />
<dataSourceExpression><![CDATA[$P{SubReportCDataSource}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_C_DIR} + "C.jasper"]]></subreportExpression>
</subreport>