Translate

Friday, 6 December 2013

How to add user defined function in iReport

In iReport it is very important to have a knowledge of integrating the Java code with the Jasper Report as the things do easier and provide lot of versatility to the report. Usage of Java Code in iReport is achieved by usage of scriptlets.

Scriptlets acts as bridge to get the Java Code in the iReport, scriptlet is a Java class which extends the JRDefaultScriptlet, and in this class we could write our user defined functions which later could be easily accessed in the report.


There are few simple steps which could be followed in order to run the User Defined Functions.

1. Tools(in the iReport Menu bar ) ----->  Options -----> Click on Claspath Tab ------> ADD Jar -----> choose the Jar file ( This JAR is made on building the project containing the scriptlet )
2. Then in the iReport there is a node called Scriptlet in the Report Inspector ( By Default on left ).
3. Go to the properties of the scriptlet , there is a property called 'Scriptlet Class'
4. Key in the the location of the scriptlet class where the user defined function is present  for instance:
com.jasper.scriptlet.TestScriptlet -- In this scenario TestScriptlet is my class where the user defined function is written.
(Remember the the class containing the user defined function needs to be extended by the JRDefaultScriptlet )
5. Now you are set to use it in the report, to use in the iReport you need to use the reference of a parameter called $P{REPORT_SCRIPTLET} , this is a predefined parameter which could come handy.
 6. In order to use the user defined in the iReport, in the the expression would be given as 
 $P{REPORT_SCRIPTLET} .userDefinedFun() -  This is a user defined function which returns values from the scriptlet.
$P{REPORT_SCRIPTLET} .userDefinedFun($F{field})  - This is also a user defined function which returns values from scriptlet, but has an input parameter from the field in the report. So this gives fluidity of the Java from Jasper to Java code.

Here is a running sample-

Prerequesites-
1. JRXML File
2. Jar File
3. PDF Export 

Steps to set up this Sample Report.
1. Tools(in the iReport Menu bar )-----> Options ----- > Click on Claspath Tab ------> ADD Jar -----> choose the Jar file
2.  Then navigate to Scriptlet node in the Report inspector, and then modify the property Scriptlet Class to 'pkg1.ConvertToWord'
3. Then you must choose Sample Database in the datasource
4. Then next you need to activate the the sample database from Help(in the iReport Menu bar)---->Samples---->Run Sample Database
5. Then Run the  Report
There is a PDF export also which would show exactly how the report looks.

Please put in you queries/suggestions, I will be more than happy to know.
Cheers .. Happy Coding!!

18 comments:

  1. Thank you Ankur for your response. I have couple of questions on the way scriptlets work.

    1. There are some methods beforeDetailAvail, BeforeReportInit(), AfterReportInit() and so on. I have defined system.out.println(). But when I run the report, the output is not being deisplayed in the report by default. My understanding is that, these default methods will be called whenever we define a scriptlet and assign to the report.

    2. After using scriptlet function, we run the report first time. Assuming that the values appear correctly. When we referesh the report to fetch latest data, does scriptlet uses previously saved values?

    Please share your thoughts on this. Thanks a lot!

    Also please provide java file to understand the functions.

    Best Regards
    Gowtham

    ReplyDelete
    Replies
    1. Hi,

      First of all let me start with your first doubt, the methods beforeDetailAvail, BeforeReportInit(), AfterReportInit() etc. are bascically the function which could be included in any scriptlet.These functions are fired when the Jasper Print object is created, and it is created when a JRXML runs.

      You can use the System.out.print inside any of these functions or user defined function but remember that the System.out.print prints the value in the console, so if you do use System.out.print in the scriptlet then it would definitely print the values in the REPORT OUTPUT window.

      For the second query if you refresh the report and you assuming that you have changes in the database, and the data passed to the scriptlet changes then the values are refreshed as well. For the override functions of the scriptlet, as I have stated above that it fires only when the JRXML runs, so refreshing the report causes this scenario to occur and hence the scriptlet value do refresh when report is refreshed.

      Hope this explains your doubts.

      Delete
  2. I tried to follow same instructions. But the report is throwing an error as "Unable to resolve class name pkg1.ConvertToWord.

    Any suggestions please?

    ReplyDelete
    Replies
    1. I have described the whole process. I will record a video and place the video link using the files I have included in the sample maybe it helps.

      Thanks,
      Ankur

      Delete
  3. how to include multiple scriptlet classes in a single report?

    ReplyDelete
    Replies
    1. You can simply add several scriptlets under the Scriptlet node.
      Each of te scriptlet could point to a different class all together.
      Mind that you will have to place as many jar dependencies, rather it is advisable that a single jar is used wherein you can have several classes which are to be assigned to different scriptlet.

      Essentially in this manner you would be able to use several scriptlets in a single report

      Cheers!!

      Delete
  4. Replies
    1. Hi Harley,

      The link above contains site of the company infovisionssolutions
      do you require report development on Jasper ?

      Let me know

      Thanks
      Ankur Gupta

      Delete
  5. Hi,

    I successfully write scriptlet in java and integrate it with ireport but when i deployed it to jasperserver it doesn't get loaded. it shows loading message forever.

    Please help

    ReplyDelete
    Replies
    1. Hi,

      Have you placed that Jar file as of the resources of the report on Jasper Server, if not then please do so.

      Hope that this helps
      Thanks
      Ankur Gupta

      Delete
    2. Hi ankur,

      Thanks for your rply!

      Yes i have added JAR file on jasperserver as well. but still it is not getting loaded. that is i think because of my scriptlet class. i have created scriptlet class that opens popup using JOptionPane and it shows in ireport sucessfully but it doesn't displayed on jasperserver.

      Here is my scriptlet class:

      package Demo;
      import java.awt.Frame;
      import java.util.Iterator;
      import javax.swing.JOptionPane;
      import net.sf.jasperreports.engine.JRDefaultScriptlet;
      import net.sf.jasperreports.engine.JRScriptletException;
      public class AlertScriptlet extends JRDefaultScriptlet
      {
      public void message() throws JRScriptletException
      {
      Iterator a=fieldsMap.keySet().iterator();
      String name=a.next();
      String res=(String)getFieldValue(name);
      Frame frame=new Frame();
      JOptionPane.showMessageDialog(frame, res);
      }
      public static void main(String[] args)
      {
      // TODO Auto-generated method stub
      }
      }

      Delete
    3. Hi Ankur!

      No Idea?? OK..

      Thanks for help

      Delete
    4. Hey,

      Sorry I forgot to post a reply,
      Actually I wanted to confirm you that, have you placed your JAR file in the lib folder of Jasper Server in the tomcat folder.

      If not then put it there and restart the server, and hope that the scriptlet runs.

      Hope that this helps.

      Do let me know if you still face the issue.

      Delete
    5. Hi,

      yes i have put the JAR file in following directory. still it doesn't working.
      C:\Jaspersoft\jasperreports-server-cp-5.5.0a\apache-tomcat\webapps\jasperserver\WEB-INF\lib

      but yes i tried with removing the JOptionPane and returning simple string. then its working fine. but i want to show popup message. that i have implementes using this JOptionPane and it is working perfectly. but on jasperserver it doesn't getting loaded.

      Delete
    6. Hi Ankur!

      No Idea?? OK..

      Thanks for help

      Delete
  6. Hi ankur,

    Thanks for your rply!

    Yes i have added JAR file on jasperserver as well. but still it is not getting loaded. that is i think because of my scriptlet class. i have created scriptlet class that opens popup using JOptionPane and it shows in ireport sucessfully but it doesn't displayed on jasperserver.

    Here is my scriptlet class:

    package Demo;
    import java.awt.Frame;
    import java.util.Iterator;
    import javax.swing.JOptionPane;
    import net.sf.jasperreports.engine.JRDefaultScriptlet;
    import net.sf.jasperreports.engine.JRScriptletException;
    public class AlertScriptlet extends JRDefaultScriptlet
    {
    public void message() throws JRScriptletException
    {
    Iterator a=fieldsMap.keySet().iterator();
    String name=a.next();
    String res=(String)getFieldValue(name);
    Frame frame=new Frame();
    JOptionPane.showMessageDialog(frame, res);
    }
    public static void main(String[] args)
    {
    // TODO Auto-generated method stub
    }
    }

    ReplyDelete
  7. Hi Sir,

    i am software developer from nagpur, m using jasper report tolls last one month i want one issue regarding the jasper report,

    1) i want to get paramater as query and the on the jasper report dyanamically print the report

    2) how to set the coloumn and rows in the report print.
    3) hoiw to pass query in paramater..

    i hope u gor this mail and help me out from that problm......

    thanks sir,

    Rohit...........Nagpur

    ReplyDelete
  8. Hi Ankur ,

    I am not able to use value of sciptlet method in printwhenexpression .

    ReplyDelete

Please post your queries or suggestions here!!