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

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>

Related

jasper report is empty when input controller is null

I am trying to generate a jasper report using jasper server. I have uploaded the created report to server and created a parameter and an input control named roomId
When I am calling report with a value, it works. But when the roomId is null, report is empty. Means it does not even have table headers.
<parameter name="roomId" class="java.lang.Integer">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<parameter name="roomIdTemp" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{roomId} == null ? "" : " AND T.ROOM_ID_01 = "+ $P{roomId} + " "]]></defaultValueExpression>
</parameter>
Calling url http://localhost:8081/jasperserver/flow.html?...... to generate report.
I am new to jasper reports. Can you please tell me whether this is illegal or not. Or am I missing anything.
I got the answer from somewhere else
I am using $P{parameterName}
It won't work if I am passing an empty string or null.

JasperReports not converting postgresql timestamp to simple date

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).

Jasper Reports print report path on page

I am using iReport 5.6 to design reports.
Is there any way to print report path on a bottom of the page.
I have tried with buit-in Parameters. But I actually don't know how to do this.
I want to show examples;
BaseDir: http://localhost:8080/MyReports
ReportFile: http://localhost:8080/MyReports/myreport.jasper
Thank You,
Supun
passing a parameter to the report should serve the purpose here.
String reportFileName = "your jasper path";
...
Map jasperParameters = new HashMap();
parameters.put("reportFilePath",reportFileName);
In jrxml, you will have do the following.
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
...>
<parameter name="reportFilePath" class="java.lang.String"/>
...
<textFieldExpression class="java.lang.String">
<![CDATA[$P{reportFilePath}]]>
</textFieldExpression>
You may refer to this example for using the parameters.

JasperReport: How to get date from the "oracle.sql.TIMESTAMP' field?

I am using iReport 4.5.1 and i need to get the date from the oracle.sql.TIMESTAMP.
Inside the expression i tried with these two:
$F{DELIVERY_DATE}.dateValue()
$F{DELIVERY_DATE}.timestampValue()
but i am getting the same runtime error
JasperReport Error:
Error filling print... Error evaluating expression : 
    Source text : $F{DELIVERY_DATE}.dateValue() net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : 
    Source text : $F{DELIVERY_DATE}.dateValue()      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:203) 
    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:876) 
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:421) 
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:406) 
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257) 
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:473) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillBandNoOverflow(JRVerticalFiller.java:434) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillPageHeader(JRVerticalFiller.java:398) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:259) 
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128) 
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:836) 
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:746) 
    at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58) 
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417) 
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247) 
    at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:879) 
    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: oracle.sql.TIMESTAMP cannot be cast to oracle.sql.TIMESTAMP 
    at Liste_colisage_par_rack_1439547941026_908820.evaluate(Liste_colisage_par_rack_1439547941026_908820:438) 
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:190) 
    ... 19 more
Thanks in advance
Anand
Try this
In iReport, JasperReport File's XML file :
<queryString>
<![CDATA[*your query*]]>
</queryString>
<field name="DELIVERY_DATE" class="oracle.sql.TIMESTAMP"/>
<field name="NAME" class="java.lang.String"/>
Try changing it too
<queryString>
<![CDATA[*your query*]]>
</queryString>
<field name="DELIVERY_DATE" class="java.util.Date"/>
<field name="NAME" class="java.lang.String"/>
You might be using the DataType oracle.sql.TIMESTAMP from Oracle Database to JasperReport directly when you are modifying Report Query in JasperReport.
i.e. Try changing oracle.sql.TIMESTAMP to java.util.Date in your jasperReport file and use the DELIVERY_DATE Field in Jasper Report directly
NOTE:
You may have to repeatedly modify the dataType of DELIVERY_DATE to java.util.Date in XML of JasperReport if you modify the query in the Report Query section of JasperReport
This problem has been addressed here http://community.jaspersoft.com/questions/514363/oraclesqltimestamp-javasqtimestamp
for those that were led to this page searching for "oracle.sql.TIMESTAMP cannot be cast to oracle.sql.TIMESTAMP" search query - this might be occuring in other cases besides Jasper - for instance, for me, I was using my project on tomcat with ojdbc6.jar both loaded in my webapp, and in Tomcat /lib folder. The solution was to specify ojdbc6 library with "provided" scope in Maven dependencies of my webapp.
the root of the problem is given here

How to populate list with values without java? - JRXML or REST

<parameter name="Bla" class="java.util.List" isForPrompting="false">
<defaultValueExpression><![CDATA[new ArrayList<String>() {{ add("A");}}]]></defaultValueExpression>
</parameter>
I want to see how I can populate list parameter without using java? Is that possible to do?
Error:
java.lang.NoClassDefFoundError: report_1405012032235_427203 (wrong name: report_1405012032235_427203$3)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClass(JRClassLoader.java:339)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassFromBytes(JRClassLoader.java:263)
at net.sf.jasperreports.engine.design.JRAbstractJavaCompiler.loadEvaluator(JRAbstractJavaCompiler.java:102)
at net.sf.jasperreports.engine.design.JRAbstractCompiler.loadEvaluator(JRAbstractCompiler.java:340)
at net.sf.jasperreports.engine.JasperCompileManager.getEvaluator(JasperCompileManager.java:378)
at net.sf.jasperreports.engine.fill.JRFillDataset.createCalculator(JRFillDataset.java:462)
at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:405)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:89)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:104)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:62)
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:179)
at net.sf.jasperreports.engine.fill.BaseFillHandle.<init>(BaseFillHandle.java:75)
at net.sf.jasperreports.engine.fill.AsynchronousFillHandle.<init>(AsynchronousFillHandle.java:98)
at net.sf.jasperreports.engine.fill.AsynchronousFillHandle.<init>(AsynchronousFillHandle.java:87)
at net.sf.jasperreports.engine.fill.AsynchronousFillHandle.createHandle(AsynchronousFillHandle.java:189)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler$4.run(ReportControler.java:330)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
'report' is report name.
It only works when I don't specify inline values like:
new ArrayList<String>()
I am on Jasper Server so effectively I want to understand how do I populate the values for this kind of parameters.
I will be happy even with defaultValueExpression which creates and add values inside. If you can go beyond and show how to do rest call for report unit for such jrxml, I will award extra rep.
Try following code:
<parameter name="Bla" class="java.util.List" isForPrompting="false">
<defaultValueExpression><![CDATA[new ArrayList<String>().add(new String("A"))]]></defaultValueExpression>
</parameter>
You can initialize a list parameter using java with default expression:
<parameter name="paramName" class="java.util.List" isForPrompting="false">
<defaultValueExpression><![CDATA[Arrays.asList(new String[]{"value0","value1","value2"})]]></defaultValueExpression>
</parameter>
Note that you must add enough values as you use in the report, if not you would get an out of index exception.