Wednesday, July 25, 2012

Calling Java Scripts and Java Classes


Oracle BI EE 10g/11g – Calling Java Scripts and Java Classes from iBots

One of the very good features of BI EE is its ability to call custom Java Scripts and Java Classes after the execution of iBots. Lets look at some examples today. In the first example we shall see how to save the reports scheduled via ibots to a local directory using a simple Java Script. It is assumed that you have your scheduler up and running. The first step in this process is to create a simple Java Script like the one below. I have taken this directly from the docs for demonstration purposes. You can customize this to your needs.
var FSO = new ActiveXObject(“Scripting.FileSystemObject”);
var fileName = “D:\\” + Parameter(1);
var fooFile = FSO.CopyFile(Parameter(0), fileName, true);
As you see above what this Java Script basically does is that it accepts one parameter (Parameter(1)) which is the file name and saves this file to a desired location that is in D drive. You need to save this file under {OracleBI}\server\Scripts\Common folder.
      
      
Once this is done, the next step is to call these scripts in your ibots. For example, lets choose a dashboard for content, PDF for attachment and schedule it to run immediately. In the Advanced of this ibot, choose the Java Script that you had created earlier. Remember you need to pass the file name (in my case it is sample.pdf) for Parameter(1) as a parameter.
      
      
      
      
      
The above was pretty straight forward. All we needed to do was to put the custom Java Script in a designated folder and call that in the Advanced tab of an ibot. Now lets look at calling a Java Class from an Ibot. There are certain things that we need to understand before we proceed further. Oracle Delivers uses the Java Host Service to make calls to a Java Procedure. So, if you go to {OracleBI}\web\javahost you would find a directory called Scheduler. Under this directory you would find a jar file called schedulerrpccalls.jar. This is the scheduler jar file that we can use to directly obtain the scheduler objects like the iBot attachments, the instance, job ids etc. So, our aim is to call a Java Program after the execution of an iBot is to write the details of the iBot schedule to a text file. In this example, i shall be using JDeveloper. Lets look at the steps one by one.
1.   Open the Config.xml file under {OracleBI}\web\javahost\config in a text editor. Search for the Scheduler tag and change it as shown below.
<Scheduler>
 <Enabled>True</Enabled> <DefaultUserJarFilePath>D:\Oracle\OracleBI\web\javahost\lib</DefaultUserJarFilePath>
</Scheduler>
The reason why we are changing the above tags is to make Java Host aware of the Java Classes that scheduler would be calling. And the DefaultUserJarFilePath tag points to the directory where we would be placing our Jar file( custom jar file using our custom Java Class). After changing this restart the Java Host Service.
2.   Open JDeveloper and create a simple Application, Project with a simple Java Class. In my case i have created a simple Application called SAWSched which in turn has an application called sawsched. The final class file that i created under this project is called as sawsched.java.
3.   In this custom java file lets include the following code. Ensure that you are including the schedulerrpccalls.jar in your Project Properties.

package sawsched;import java.io.*;
import java.lang.Thread;
import com.siebel.analytics.scheduler.javahostrpccalls.SchedulerJavaExtension;
import com.siebel.analytics.scheduler.javahostrpccalls.SchedulerJobException;
import com.siebel.analytics.scheduler.javahostrpccalls.SchedulerJobInfo;
public class sawsched implements SchedulerJavaExtension{
public void run(SchedulerJobInfo jobInfo) throws SchedulerJobException
{
System.out.println(“JobID is:” + jobInfo.jobID());
System.out.println(“Instance ID is:” + jobInfo.instanceID());
System.out.println(“JobInfo to string is:” + jobInfo.toString());
try
{
File outputFile = new File(“D:\\JavaJob.txt”);
File attachFile = jobInfo.getResultSetFile();
attachFile.createNewFile();
FileWriter out = new FileWriter(outputFile);
out.write(“User ID:\t\t” + jobInfo.userID() + “\r\n”);
out.write(“Job ID:\t\t” + jobInfo.jobID() + “\r\n”);
out.write(“Instance ID:\t\t” + jobInfo.instanceID() + “\r\n”);
out.write(“Parameter Count:\t\t” + jobInfo.parameterCount() + “\r\n”);
out.write(“File Path: ” + attachFile.getAbsolutePath());
for(int i = 0; i < jobInfo.parameterCount(); ++i)
{
out.write(“\tParameter “);
out.write(new Integer(i).toString());
out.write(“:\t” + jobInfo.parameter(i) + “\r\n”);
}
out.close();
}
catch(Exception ex)
{
throw new SchedulerJobException(1, 1, ex.getMessage());
}
}
public void cancel()
{
}
}
      
What this class basically does is it creates a text file which would have details about the iBot job. The above example is available in the bookshelf.
4.   Once this is done compile this code to ensure that you have no errors. The next step is to create a JAR file out of this class. So, right click on your Project and create a Jar File. In your JAR ensure that you are also including your schedulerrpccalls.jar and the above compiled class.
      
      
      
I have named my Jar file as BISched.
      
      
Once this is done right click on your resources ( you would find your Jar there) and then click on Deploy.
      
Copy your Deployed Jar file to {OracleBI}\web\javahost\lib (The one that we included in the DefaultUserJarFilePath tag).
      
5.   Now lets go back to delivers and create a simple iBot and choose Java Program in the Advanced Options.
      
In the Java Properties enter the class name, in our case it is sawsched.sawsched and then in the CLASSPATH enter the jar file that we had bundled. Save the ibot and you would notice that a text would have been created under D drive.
      


Thanks,
Satya Ranki Reddy

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. very useful really good information thanks for posting such a good information it will hepls the people a lot keep it up , Regards, obiee training in hyderabad

    ReplyDelete
  3. I am in serious need of help due Monday can u help me. My requirement is to have dashboards/reports exported to a shared folder... Please help me. Alontech1@gmail.com

    ReplyDelete