Monday, May 11, 2009

Rendering BLOB Image(s) in ADF 11g JSP pages

Although there is not wizard based embedded solution about how to render BLOB Image in ADF 11g JSP pages.

I had tried to configure how to show BLOB image to jsp page . and

Here is the solution,

later I will put a project source for this solution. also will post of rendering OrgImage in Jdeveloper11g/ADf11g .


All code as below: Process Steps:

Step1: Create a servlet class






package CPS.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.sql.SQLException;


import java.util.Iterator;
import java.util.Map;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.jbo.ApplicationModule;
import oracle.jbo.JboException;
import oracle.jbo.Row;
import oracle.jbo.ViewObject;
import oracle.jbo.client.Configuration;
import oracle.jbo.domain.BlobDomain;




public class ClsImage extends HttpServlet {

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {


String appModuleName = "CPS.model.CPSAppModule";
String appModuleConfig = "TempCPSAppModule";


String imgno = request.getParameter("imgno");
String tblName = request.getParameter("tblnm");
String clmName = request.getParameter("clnm");
String pkclmName = request.getParameter("pkclnm");
String mType = request.getParameter("mtype");


// if (imgno == null imgno.equals(""))
// return;
// if (tblName == null tblName.equals(""))
// return;
// if (clmName == null clmName.equals(""))
// return;
// if (pkclmName == null pkclmName.equals(""))
// return;
// if (mType == null mType.equals(""))
// mType = "jpg";
//


String voQuery =
"select " + clmName + " from " + tblName + " where " + pkclmName +
" = '" + imgno + "'";


String mimeType = mType;

ApplicationModule am =
Configuration.createRootApplicationModule(appModuleName,
appModuleConfig);

am.clearVOCaches("TempView", true);


ViewObject vo = null;


try {
vo = am.createViewObjectFromQueryStmt("TempView", voQuery);

} catch (Exception e) {
System.out.println("VO Exists...");
vo.remove();
vo = am.createViewObjectFromQueryStmt("TempView", voQuery);
}


// Run the query
try {
vo.executeQuery();
} catch (Exception e) {
System.out.println(e.toString());
}
// Get the result (only the first row is taken into account
Row product = vo.first();


BlobDomain image = null;
// Check if a row has been found
if (product != null) {
// We assume the Blob to be the first a field
image = (BlobDomain)product.getAttribute(0);

// Check if there are more fields returned. If so, the second one
// is considered to hold the mime type

if (product.getAttributeCount() > 1) {
mimeType = (String)product.getAttribute(1);
}
} else {
System.out.println("No row found to get image from !!!");
vo.remove();
return;
}

// Set the content-type. Only images are taken into account
response.setContentType("image/" + mimeType);
OutputStream os = response.getOutputStream();
InputStream is = image.getInputStream();

// copy blob to output
byte[] buffer = new byte[image.getBufferSize()];
int nread;

vo.remove();

while ((nread = is.read(buffer)) != -1) {

os.write(buffer);

}

is.close();
os.close();
os.flush();

}

}



Step 2: You should add servlet definition in web.xml

    <servlet>
<servlet-name>images</servlet-name>
<servlet-class>CPS.model.ClsImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>images</servlet-name>
<url-pattern>/render_images</url-pattern>
</servlet-mapping>




Steps 3: Add temporary Application module definition in BC4J.xcfgadd below code before tag
< / AppModuleConfigBag >


      <AppModuleConfig name="TempCPSAppModule" ApplicationName="CPS.model.CPSAppModule" DeployPlatform="LOCAL" JDBCName="connCPS" jbo.project="CPS.model.cpsModel" java.naming.factory.initial="oracle.jbo.common.JboInitialContextFactory">
<AM-Pooling jbo.ampool.isuseexclusive="false" jbo.ampool.maxpoolsize="1"/>
<Security AppModuleJndiName="CPS.model.CPSAppModule"/>
</AppModuleConfig>
</AppModuleConfigBag>


Step 4 Add ADF Tag in JSP page:

<h:graphicImage url="/render_images?imgno=#{bindings.CustPk.inputValue}&amp;tblnm=MSCUST_INFO&amp;clnm=CUST_IMG&amp;pkclnm=CUST_PK&amp;mtype='jpg'"
height="180" width="250"/>