Jump to content
We've recently updated our Privacy Statement, available here ×

How do I ask a java object to perform a method


leeyuiwah

Recommended Posts

In iReport, the report parameters are java object.  But how can we ask these objects to perform some object methods that return null?

The only way that I know asking these objects to perform any method is in the "Default Value Expression" of the parameter. But there is a severe limitation.  The java object method must returns itself (the object) -- so as to "initialize" the parameter.

If this sounds too abstract, here is what I want to do.

1. I can make a parameter of the type java.util.Date, let's call this $P{today}

2. To get the "day" value of the Date object, the right way to do is to use java.lang.Calendar, in the following sequences:

Calendar cal = Calendar.getInstance();

cal.setTime($P{today});  // setTime() method returns void

cal.get(Calendar.DATE_OF_MONTH); // get the "day" value

But the problem is how can I fit the above steps into the iReport's model?

a. As I said, the "Default Value Expression" requires that the expression returns a java object to fill up the parameter.

b. If I ask cal to setTime(), it returns a type of "void"

c. Can I define a function in iReport? So that I can group the above multiple statements into one function call, say getDay(), and then have this function called at the "Default Value Expression" of another parameter (e.g. $P{today_Day}).

I am stuck here. Please help.  Happy New Year!

Clement

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

You can do this 2 ways.

1. Develop a scriptlet jar with static methods that do what you want. The defaultValueExpression would then be something like:

 

MyScriptlet.getDayOfMonth()

 

2. "Cascade" parameters, like:

<parameter name="MyCal" class="java.util.Calendar">
    <defaultValueExpression>java.util.Calendar.getInstance()</defaultValueExpression>
</parameter>

 

<parameter name="DayOfMonth" class="java.lang.Integer">
    <defaultValueExpression>$P{MyCal}.get(Calendar.DATE_OF_MONTH)</defaultValueExpression>
</parameter>

 

Sherman

Jaspersoft

Link to comment
Share on other sites

Dear Sherman,

Thanks!  A mystery solved! (see also Post #16497 in
http://tinyurl.com/8nokn6)

The first method works for me but not the second, because I have to set
the date of the Calendar object to a different date than today.

BTW, for the record, in case someone else comes to this.  Please note that
after you created the helper class, compiled the .class and package the
.jar file, you need to make the .jar file available to Jasper.  If you use
iReport, this means that you have to do "Tools->Options->Classpath->'Add
JAR'".  If you use JasperServer, you have to put the .jar under the
correct WEB-INF/lib directory.

Clement
 

Link to comment
Share on other sites

I've been trying to create a jar file to perfom what was in that older thread you posted, but I can't ever get it to work.

I created a SQLUtils.java file in a text editor.

Ran javac to compile it and it created a SQLUtils.class file for me.

Ran jar cf  SQLUtils.jar SQLUtils.class  to make it a jar file and that seemed to work fine.

I then added the path to the jar file in ireport from tools>options and have also put it on the report server in WEB-INF/LIB

Anytime I run the report on the server or in ireport I get these errors:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 1. SQLUtils cannot be resolved value = (java.lang.String)(SQLUtils.enumerate(((java.util.Collection)parameter_Sites.getValue()), true));//$JR_EXPR_ID=0$ <------> 2. SQLUtils cannot be resolved value = (java.lang.String)(SQLUtils.enumerate(((java.util.Collection)parameter_Sites.getValue()), true));//$JR_EXPR_ID=0$ <------> 3. SQLUtils cannot be resolved value = (java.lang.String)(SQLUtils.enumerate(((java.util.Collection)parameter_Sites.getValue()), true));//$JR_EXPR_ID=0$ <------> 3 errors

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:1. SQLUtils cannot be resolved                value = (java.lang.String)(SQLUtils.enumerate(((java.util.Collection)parameter_Sites.getValue()), true));//$JR_EXPR_ID=0$                                           <------>2. SQLUtils cannot be resolved                value = (java.lang.String)(SQLUtils.enumerate(((java.util.Collection)parameter_Sites.getValue()), true));//$JR_EXPR_ID=0$                                           <------>3. SQLUtils cannot be resolved                value = (java.lang.String)(SQLUtils.enumerate(((java.util.Collection)parameter_Sites.getValue()), true));//$JR_EXPR_ID=0$                                           <------>3 errors



Post Edited by Glen Ki at 01/20/09 19:26

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...