2004 IR Help Posted August 24, 2006 Share Posted August 24, 2006 By: veena - shanmugaveena To send argument to the scriptlet dynamically 2005-03-02 22:43 Hai All, I am new user of Jasper report. I want to export JfreeCharts to Pdf and Html format using JasperReports. Since My chart will be changing according to users requirements I want to convert the JFreechart of that requirement to pdf. I following the same methodology in jasperReports-->demo-->sample-->Jfreechart. Here instead of static pie chart i need to send my own jfreechart as argument to drawablerenderer class.I acheived it by having a constructor with string argument for the scriptlet and create an object in my own java class. Then i diretly call the compile,fill and convert to pdf methods of jasperreports. While doing so it is compiling good and creates a jasper file.But i cannt get the jrprint file and it throws a instantiated exception in class. Since we are sending scriptlet as a variable in xml .How xml will take it while creating object to the scriptlet. Thanks in advance. k.s.veena By: W Leong - wk_leong RE: To send argument to the scriptlet dynamically 2005-11-13 19:39 Hi I am also looking at the same issue. Essentially, I have to pass parameter from Servlet -to-> JasperReport -to-> JFreeChart I know Servlet-to-JasperReport can be achieved via the parameters map. How do I continue passing it down to JFreeChart scriptlet? Thank you for your time. Best -WK- Link to comment Share on other sites More sharing options...
dbastian Posted September 28, 2006 Share Posted September 28, 2006 I'm facing a similar challenge: I am querying a double from a datasource, storing it into a field.I have written a scriptlet that accepts the float such as 123.45 andreturns "One Hundred Twenty Three and 45/100". In other words, supplying the Double argument and returning the String for each record returnedfrom the datasource. I'm getting the following error:"Type mismatch: cannot convert from Double to int" at the pointwhere the .getValue() method is invoked on the double field. First, is passing this field value to a scriptlet even supported?Can't find any indication in the docs that it is. Second, where is the convert to int being attempted and can it be castsomehow in the call to circumvent the exception? Link to comment Share on other sites More sharing options...
Teodor Danciu Posted September 28, 2006 Share Posted September 28, 2006 Hi, Why do you say you face a similar challenge?Your problem has nothing to do with passing values to the scriptlet.The exception has to do only with the code you wrote in that scriptlet to convert the numeric value to its text representation.It is not clear to me what code you have there as there is no getValue() method in the java.lang.Double class. Seeing that code might help understand where the exception comes from. Thank you,Teodor Link to comment Share on other sites More sharing options...
dbastian Posted October 3, 2006 Share Posted October 3, 2006 Thanks for your reply. I just tried to send you a long, detailed response but thesession timed out and I lost all the text -- so, for now, I'll just restate my first question: Is passing an argumentto a Scriptlet supported? I'm willing to send you my code but I'm quite sure that thegetValue() method is called from the <serialization>.javafile generated from the .jrxml and derived from the JRAbstractScriptlet class. Just not sure how. Nevertheless, my code never actually gets called, as far as Ican determine. If "forwarding" a datasource-derived field value through the engine and back out to a scriptlet isn't supported, I'll justimplement it as a java stored procedure out of postgresqland stop trying. Just wanted to know if it is doable and, if so, how. Thanks --David Link to comment Share on other sites More sharing options...
lucianc Posted October 3, 2006 Share Posted October 3, 2006 JasperReports expressions are Java expressions (given that you use a Java-based report compiler) that can contain placeholders for parameter, field and variable values. Therefore, anything you could write in a Java expression you can use in a JR expression, including calling methods on objects and passing arguments for the methods. I'm not sure what exactly you are trying to do or what doesn't work. Maybe if you would post relevant code fragments (JRXML expressions, Java methods, field definitions, etc), we could figure out what goes wrong. Regards,Lucian Link to comment Share on other sites More sharing options...
dbastian Posted October 4, 2006 Share Posted October 4, 2006 // To give you a sense for how I've been approaching the jrxml part of this... <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"><jasperReport name="dollars2string" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842" columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages" scriptletClass="dollars2stringScriptlet" isTitleNewPage="false" isSummaryNewPage="false"> ... /** Then this is the syntax I've been using to invoke the scriptlet, where amt2pay is a Double successfully queried in from the datasource.* I tried wrapping the argument in parens but that was disallowed at report compile time)*/ <textFieldExpression class="java.lang.String"><![CDATA[!CDATA[$P{REPORT_SCRIPTLET} [$F{amt2pay}]]]]></textFieldExpression> ...// The complaint at compile time is this...Type mismatch: cannot convert from Double to int value = (java.lang.String)(!CDATA[((dollars2stringScriptlet)parameter_REPORT_SCRIPTLET.getValue()) [((java.lang.Double)field_amt2pay.getValue())]]); /** I did a little hunting for where the getValue() method is invoked and it appears to be in JRAbstractScriptlet which is extended by * my Scriptlet, via JRDefaultScriptlet,*/ as follows... // My code:public class dollars2stringScriptlet extends net.sf.jasperreports.engine.JRDefaultScriptlet { public void beforeReportInit() throws JRScriptletException { ... } ... } //Relevant (?) net.sf.jasperreports.engine.JRAbstractScriptlet code public Object getFieldValue(String fieldName) throws JRScriptletException { JRFillField field = (JRFillField)this.fieldsMap.get(fieldName); if (field == null) { throw new JRScriptletException("Field not found : " + fieldName); } return field.getValue(); } /** My guess is that the exception is thrown at the time amt2pay (the Double value successfully fetched from the database and determined to * be of type Double) is then used as the argument to the getFieldValue method. That the JRScriptletException isn't thrown suggests things* go pretty well to that point but, somehow, fail on the field.getValue() call which must, either by explicit declaration or default, expect a * type int. This is where I lose the thread. Too many inconsistencies. Thus my posting as to supportability and, if supported, syntax.*/ Link to comment Share on other sites More sharing options...
lucianc Posted October 4, 2006 Share Posted October 4, 2006 Correct me if I'm wrong, but your expression is Code:!CDATA[$P{REPORT_SCRIPTLET} [$F{amt2pay}]]. Ignoring the !CDATA[...] parts which have no reason to be here, the expression is $P{REPORT_SCRIPTLET} [$F{amt2pay}] which would only amount to a valid Java expression if $P{REPORT_SCRIPTLET} would be an array and $F{amt2pay} would resolve to a primitive int value. But since $P{REPORT_SCRIPTLET} is not an array but a regular object instance and $F{amt2pay} is a java.lang.Double object, this expression is far from being valid. What exactly are you trying to achieve with this expression? By looking at it, it doesn't make any sense.. Regards,Lucian Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now