Oracle BI EE 11g - Action Framework - Java, EJB's and PDF Watermarks
As mentioned in the blog entry yesterday, Action Framework of BI EE 11g opens up direct integration with Java. So, any process that can be called via Java can be directly called from BI EE as well. In 10g, the only way for calling Java Processes was by using Delivers. I have blogged about it in detail here. In 11g, the old method is still supported but is not recommended. The recommended approach is to embed the Java Methods in a EJB session bean and then call it from BI EE. Lets look at how the integration works in this blog entry. To make it interesting, lets look at a method of adding Watermarks to the standard PDF export available in BI EE.
For this example, we will be needing JDeveloper 1113. We start of with creating a simple Generic Application called WaterMark as shown below in JDeveloper
Also in our example, we are planning to watermark a PDF generated by BI EE. There are different ways of doing this. One way is to use the BI Publisher watermarking feature that we put into use before here. To keep it simple, i will be using the iText package available for download here. Ensure that the project that we created above has both the actionframework-common.jar and the iText-xxx.jar in the Classpath.
Open up SessionEJB.java and enter the code below. This declares our method that we will be calling from BI EE.
package watermark; import java.io.FileNotFoundException; import java.io.IOException; import javax.activation.ActivationDataFlavor; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.activation.URLDataSource; import javax.ejb.Remote; import oracle.bi.action.annotation.OBIActionParameter; @Remote public interface SessionEJB { String WatermarkReport( @OBIActionParameter (name = "Enter Filename", prompt = "Enter filename location:") String filename, @OBIActionParameter (name = "Analysis", prompt = "Report to Export:") DataHandler document) throws FileNotFoundException, IOException; }
package watermark; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; @Stateless(name = "SessionEJB", mappedName = "ejb/WaterMark-watermark-SessionEJB") @TransactionManagement(TransactionManagementType.BEAN) @Remote @Local public class SessionEJBBean implements SessionEJB, SessionEJBLocal { public SessionEJBBean() { } public String WatermarkReport (String filename, javax.activation.DataHandler report) throws FileNotFoundException, IOException { File f = new File(filename); FileOutputStream outputStream = new FileOutputStream(f); report.writeTo(outputStream); outputStream.close(); try { PdfReader pdfreadobject = new PdfReader(filename); int no = pdfreadobject.getNumberOfPages(); int cnt = 0; PdfStamper pdfstamp = new PdfStamper(pdfreadobject, new FileOutputStream("C:\\PDFExport\\RMWatermark.pdf")); PdfContentByte pdfcontent; Image image = Image.getInstance("C:\\PDFExport\\RMLogo.png"); image.setAbsolutePosition(300, 400); while (cnt < no) { cnt++; pdfcontent = pdfstamp.getUnderContent(cnt); pdfcontent.addImage(image); } pdfstamp.close(); } catch (Exception ex) { ex.printStackTrace(); } return "Report Exported"; } }
And finally in the SessionEJBLocal.java enter the code given below
package watermark; import java.io.FileNotFoundException; import java.io.IOException; import javax.activation.ActivationDataFlavor; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.activation.URLDataSource; import javax.ejb.Local; @Local public interface SessionEJBLocal { String WatermarkReport(String filename, DataHandler document) throws FileNotFoundException, IOException; }
Now compile this entire project. Create a deployment profile for this project using EJB-JAR option and ensure that the libraries that we have chosen are included properly in the profile.
<registry> <id>reg05</id> <name>WaterMark EJBs</name> <content-type>java</content-type> <provider-class>oracle.bi.action.registry.java.EJBRegistry</provider-class> <description>WaterMark BIEE</description> <location> <path/> </location> <custom-config> <ejb-targets> <appserver> <context-factory>weblogic.jndi.WLInitialContextFactory</context-factory> <jndi-url>t3://localhost:9704</jndi-url> <server-name>localhost</server-name> <account>WLSJNDI</account> <ejb-exclude>mgmt</ejb-exclude> <ejb-exclude>PopulationServiceBean</ejb-exclude> </appserver> <ejb-app> <server>localhost</server> <app-context>watermark</app-context> </ejb-app> </ejb-targets> </custom-config> </registry>