Jaspersoft Reports, how to check if a file exists or not in IF-ELSE clause? - jasper-reports

For a Jaspersoft Report I have an HTML file(very simple string content, no HTML marks) which I load into a Text Field, the loading path is dynamic, so it depends on a given parameter. Loading the HTML file works fine, I used this code:
new String(net.sf.jasperreports.repo.RepositoryUtil.getInstance($P{JASPER_REPORTS_CONTEXT}).getBytesFromLocation("repo:/home/res/html/file_" + $P{PARAM_HTML} + ".html"), java.nio.charset.StandardCharsets.UTF_8)
If i try it with a file who doesnt exists, i get the following error message:
net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: new String(net.sf.jasperreports.repo.RepositoryUtil.getInstance($P{JASPER_REPORTS_CONTEXT}).getBytesFromLocation("repo:/home/res/html/file_" + $P{PARAM_HTML} + ".html"), java.nio.charset.StandardCharsets.UTF_8)
at com.jaspersoft.studio.editor.preview.view.control.ReportController.fillReport(ReportController.java:551)
at com.jaspersoft.studio.editor.preview.view.control.ReportController.access$18(ReportController.java:526)
at com.jaspersoft.studio.editor.preview.view.control.ReportController$1.run(ReportController.java:444)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: new String(net.sf.jasperreports.repo.RepositoryUtil.getInstance($P{JASPER_REPORTS_CONTEXT}).getBytesFromLocation("repo:/home/res/html/file_" + $P{PARAM_HTML} + ".html"), java.nio.charset.StandardCharsets.UTF_8)
at net.sf.jasperreports.engine.fill.JREvaluator.handleEvaluationException(JREvaluator.java:294)
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:328)
at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:673)
at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:641)
at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1173)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:555)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:540)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:383)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:548)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:325)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:258)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:110)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:621)
at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFill.run(BaseFillHandle.java:135)
at java.lang.Thread.run(Thread.java:748)
Caused by: net.sf.jasperreports.engine.JRException: Byte data not found at: repo:/home/res/html/file_12.html.
at net.sf.jasperreports.repo.RepositoryUtil.getBytesFromLocation(RepositoryUtil.java:215)
at Blank_A4_1636094714772_424965.evaluate(Blank_A4_1636094714772_424965:96)
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:313)
... 13 more
The Problem is, that it is not clear if the file exists or not, so I use a IF-ELSE clause to check if the HTML file exists or not. If the file exists, the content should be loaded into the Text Field, if not, a default HTML file should be used instead. What I've tried so far are the following conditions in the Text Field expression.
(new File("repo:/home/res/html/file_" + $P{PARAM_HTML} + ".html").isFile()) ? "LOAD FILE" : "LOAD DEFAULT FILE"
(new File("repo:/home/res/html/file_" + $P{PARAM_HTML} + ".html").exists()) ? "LOAD FILE" : "LOAD DEFAULT FILE"
I also tried the same conditions to use in a variable, and use the variable in the Text Field. Always it uses the ELSE condition, even if the file exists or not.
I am not sure where my mistake is, or what I did wrong.

Related

Context Variable from txt file

I am trying to get Context (data connection) variable (Database input) from Txt file. This is the input of my txt file.
host;"xxx"
port;"xxx"
database;"xxx"
username;"xxx"
password;"xY"
database;"xx"
schema;"xY"
This is the query I am using to get table information "SELECT * FROM COMPANY". I have tried with Database. Table name also. But I am getting below error. "Exception in component tDBInput_1 (DataConnection_FromFile)
java.sql.SQLRecoverableException: IO Error: Invalid number format for port number"
Could you please suggest do I need to follow any other steps.
It seems that your port number context variable is typed as 'Integer'. Try to modify this and put it as a String in the 'Context' view.

How to correct use $X in jasper??. Problem with parameters and IN clause in JSS

I have a report made with Jaspersoft Studio and in the dataset query I need to use an IN clause, for that I am using the expression "$X{IN ..."
Question # 1: What is the correct type to use for the parameter?
I'm using the following format:
Question # 2: How do I test in the preview?
Parameters screen:
To help, follow the excerpt of where with the parameter being used:
"...Where (($X{IN, db.empresa, paramIdEmpresa}) OR $ P!{ParamIdEmpresa} IS NULL) and (db_view ... "
Error that appears in the preview with the above parameters:
net.sf.jasperreports.engine.JRException: Error executing SQL statement
for: unit1. at com.jaspersoft.studio.editor.preview.view.control.ReportController.fillReport
(ReportController.java:551) at com.jaspersoft.studio.editor.preview.view.control.ReportController.access
(BaseFillHandle.java:135) at java.lang.Thread.run (Thread.java:748)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at
or near "["   Position: 199 at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse
(QueryExecutorImpl.java:2440) at
org.postgresql.core.v3.QueryExecutorImpl.processResults
the problem was in the query and not in jasper...
the correct query is:
where
(($X{IN,db.idempresa, paramIdEmpresa}) OR db.idempresa IS NULL )
thanks to GP...
The best possible way to solve it is simple:
create a parameter called $P!{paramWhere}. of your application
pass the complete where clause with this parameter, e.g. "id = 200"
In your query inside jasper, you put it like this: "... select * from <anywhere> where $P!{paramWhere} ..." and it will magically work.

JasperReports: report parameter as an argument for message bundle lookup

How to look up a particular internationalized property based on a report parameter?
This works, but is static:
$R{some_literal_string}
This works too, but is not internationalized:
$P{key_to_parameters_map_element}
What I need is:
$R{$P{key_to_parameters_map_element}}
Unfortunately, I get a pile of error messages:
Caused by: net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. Syntax error on token "}", delete this token
value = str("$P{key_to_parameters_map_element")}; //$JR_EXPR_ID=13$
This doesn't change anything:
$R{$P{key_to_parameters_map_element}.toString()}
Is this possible at all?
It's
str($P{key_to_parameters_map_element})
Quite intuitive, isn't it?

Type Mismatch VBScript Error when getting RecordSet

I am working on some legacy code that is written in Classic ASP / VBSCript.
The code handles the data submitted via a HTML form, and breaks on the following line.
' Get all input questions
Set inputQuestions = getListOfInputQuestionsForPage("additional")
The function getListOfInputQuestionsForPage(pageName) is defined as follows:
Function getListOfInputQuestionsForPage(pageName)
' Instantiate Command
Set objCommand = Server.CreateObject("ADODB.Command")
' Inform Command what Connection to use.
Set objCommand.ActiveConnection = myConn
' SQL Query to run
objCommand.CommandText = "SELECT QUESTION_TABLE.PK_QUESTION AS ""QUESTION_ID"", QUESTION_TABLE.QUESTION AS ""QUESTION"", QUESTION_TABLE.INPUT_TYPE AS ""TYPE"", QUESTION_TABLE.IS_FOR_ALL_CUSTOMERS AS ""FOR_ALL_CUSTOMERS"" FROM QUESTION_TABLE WHERE QUESTION_TABLE.DISPLAY_PAGE = '" & pageName & "' ORDER BY PK_QUESTION ASC"
' Execute SQL and return result
Set getListOfInputQuestionsForPage = objCommand.Execute()
End Function
I find it strange that I am getting the following error:
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'getListOfInputQuestionsForPage'
/site/path_to_file/edit_additional.asp, line 110
All I am doing is trying to grab some data from the database. And I know the data exists and a RecordSet is returned.
The error threw me off. The reason why it wasn't working was because the function getListOfInputQuestionsForPage(pageName) did not exist on the same page, and I had to include the file that contained the function definition, which now is very obvious, but because the error is so weird for what I'm trying to do (type mismatch) ... I did not think of that reason at all.
So in short, make sure your classic asp file knows about that function, ie has a link to the file containing the function, or the function is defined in the same file.

Getting an error when using a date parameter

I am using iReport 5.0.1. I created a date parameter and am using it in my query:
When I preview the report, I enter my date parameter:
After I enter all the parameters and run the report, I get this error:
Error filling print... Error executing SQL statement for : Sample
net.sf.jasperreports.engine.JRException: Error executing SQL statement for : Sample
at com.jaspersoft.jrx.query.PlSqlQueryExecuter.createDatasource(PlSqlQueryExecuter.java:143)
at net.sf.jasperreports.engine.fill.JRFillDataset.createQueryDatasource(JRFillDataset.java:1086)
at net.sf.jasperreports.engine.fill.JRFillDataset.initDatasource(JRFillDataset.java:667)
at net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:1258)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:877)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:822)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:61)
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: org.postgresql.util.PSQLException: ERROR: syntax error at or near "Feb"
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1592)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1327)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:192)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
at com.jaspersoft.jrx.query.PlSqlQueryExecuter.createDatasource(PlSqlQueryExecuter.java:136)
... 12 more
Print not filled. Try to use an EmptyDataSource...
What is going on here, and how do I fix this? Thanks!
I think the ! in $P!{...} means to include the parameter's literal value in the report.
Try using $P{start_date} and $P{end_date}, both without the !.
Effectively, your SQL statement is using:
and finalinsdate >= Feb/18/2013
That is not a valid SQL statement, which means PostgreSQL will complain with:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "Feb"
For next time, copy and paste the source code, instead of taking a screen capture.