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

Convert Groovy expression into Java expression in Studio 6.0.0


wtirlic33
Go to solution Solved by lucianc,

Recommended Posts

I have a JasperReports Server Custom Report Template, which I want to use when creating Reports from AdHoc views. Due to our requirements I've created a couple of Variables, to calculate previous Friday Date on that Template. It is written in Groovy. However, it fails when there are measures on the columns. After digging the Jasper Forum, I've found out that currently Crosstab AdHoc doesn't support Groovy expressions, and the only way is to convert my Groovy expression into Java, and that should sort the problem. Can anyone help me with the converting this expression into the Java expression ? Here is the Groovy one:

($V{Calendar}.set($V{Today}.getYear()+1900,
    $V{Today}.getMonth(),
        $V{Today}.getDate()) || $V{Calendar}.add(Calendar.DAY_OF_MONTH,
            IF ($V{Calendar}.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY, -7,
                IF($V{Calendar}.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY, -1, -7 + (Calendar.FRIDAY - $V{Calendar}.get(Calendar.DAY_OF_WEEK))))))? null : $V{Calendar}.getTime()

 As you can see I have created 3 variables, the Calendar, Today and a PreviousFridayDate. The last one is what I'm interesting in.

The Calendar is set to java.util.Calendar  and with an expression Calendar.getInstance()

The Today variable is set to java.util.Date with an expression new Date()

Can anyone help me with converting it into Java expression or maybe there are some more option on how to get it work ? Any help more than welcome. Thanks

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

  • Solution

I don't think directly converting the expression to Java is possible.  For one, Groovy allows calling void methods in expressions (evaluating to null) but in Java there's no way to call a void method (such as Calendar.set or Calendar.add) in an expression.

One alternative is to write a method in a utility class (or create a custom function in Jaspersoft Studio), and use it in the expression.  Cramming too much logic in an expression is not generally a good idea, having it implemented externally would allow you to do more things, reuse it, etc.

Regards,

Lucian

Link to comment
Share on other sites

Thanks for your reply Lucian. Yes you are right, it throwing an error when I rty to compile Report within the studio, exactly with Calendar.add

Could you please direct me a little bit more on how to write this method, as I'm not so confident yet with all these methods, expressions etc.

Some usefull links maybe, or if you can modify existing expression above ?

Thanks.

Link to comment
Share on other sites

I was following this tutorial ( http://jasper-bi-suite.blogspot.co.uk/2016/01/user-defined-scriptlet-example-in.html  ) on how to manually create a custom scriplet. I've created a Jar file within Eclipse, extracted and imported it into the Studio. However, after all steps I'm getting an error when trying to compile:

java.lang.ClassFormatError: Invalid length 5525 in LocalVariableTable inclass file org/eclipse/jdt/internal/compiler/problem/ProblemReporter    at java.lang.ClassLoader.defineClass1(Native Method)    at java.lang.ClassLoader.defineClass(Unknown Source)    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.defineClass(DefaultClassLoader.java:188)    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClassHoldingLock(ClasspathManager.java:632)    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClass(ClasspathManager.java:607)    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findClassImpl(ClasspathManager.java:568)    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClassImpl(ClasspathManager.java:492)    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:465)    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216)    at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:395)    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:464)    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)    at java.lang.ClassLoader.loadClass(Unknown Source)    at org.eclipse.jdt.internal.compiler.Compiler.<init>(Compiler.java:281)    at org.eclipse.jdt.internal.compiler.Compiler.<init>(Compiler.java:109)    at net.sf.jasperreports.eclipse.builder.jdt.JRJdtCompiler.compileUnits(JRJdtCompiler.java:91)    at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:201)    at net.sf.jasperreports.eclipse.builder.JasperReportCompiler.compileReport(JasperReportCompiler.java:169)    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.compileJasperDesign(ReportControler.java:395)    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$14(ReportControler.java:383)    at com.jaspersoft.studio.editor.preview.view.control.ReportControler$4.run(ReportControler.java:314)    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)[/code]

I don't really undestand what is that error means and how to solve it ?! 2nd day I'm trying to sort this thing out and no luck .. Anyone, any help ?

Thanks

 

Link to comment
Share on other sites

Sorted. I've created a custom java class. I compiled it using Eclipse and packaged to a JAR file.
Then, I added that JAR to Jaspersoft Studio Project Build Path, specified Scriptlet class and used my custom method in the report: $P{REPORT_SCRIPTLET}.myMethod()

Hopefully it will help someone. Thanks.

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