Monday, 2 May 2016

Read Blob Object as new window in PDF format in ADF (Contextual help Doc)

Today I'll explain how to read a blob object as in new window in adf.

To achieves first need to create a servlet in our application (register in web.xml if not).


Inside servlet code will tell you later. After that create one BTF and make sure the URL-INVOKED should be ALLOWED.
And drag and drop the URl activity in that task flow and URL should be you servlet pattern.


Inside servlet code is :

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException,
                                                           IOException {
        String filename =
            (String)request.getSession().getAttribute("DocTitle");
        response.reset();
        response.setContentType("application/pdf");
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        Connection conn = null;
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("XXX_DATA_SOURCE_NAME");
            conn = ds.getConnection();
            PreparedStatement statement =
                conn.prepareStatement("SELECT STMNT WHERE file_name=?");
            statement.setString(1, filename);
            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                Blob blob = rs.getBlob("FILE_BLOB");
                byte[] doc = readConetnBlob(blob);
                ByteArrayInputStream bais = new ByteArrayInputStream(doc);
                response.setHeader("Content-Length",
                                   String.valueOf(doc.length));
                response.setHeader("Content-Disposition",
                                   "filename=\"" + filename + "\"");

                input = new BufferedInputStream(bais, doc.length);
                output =
                        new BufferedOutputStream(response.getOutputStream(), doc.length);

                byte[] buffer = new byte[doc.length];
                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            output.close();
            input.close();
        }


    }

    private byte[] readConetnBlob(Blob file) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int n = 0;
        try {
            InputStream in = file.getBinaryStream();
            while ((n = in.read(buf)) >= 0) {
                baos.write(buf, 0, n);
            }
            in.close();
            return baos.toByteArray();
        } catch (IOException e) {
        } catch (SQLException sqle) {
            sqle.printStackTrace();

        }
        return null;
    }

Next in you jspx and jsff means where you need want call create hyper link as below and following code.

 <af:commandLink text="Display help text" id="cb1"
                            action="#{viewScope.mybean.navigateToHome}"/>

And managed bean code which helps you to call you BTF which has url activity.

        final String taskflowId="ShowPdfFlow";
        String taskflowDocument="/WEB-INF/ShowPdfFlow.xml";
        FacesContext fctx = FacesContext.getCurrentInstance();
        Map<String, Object> params = new HashMap<String, Object>();
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession)ctx.getExternalContext().getSession(false);
         session.setAttribute("DocTitle", "QAS-PRO-Web-Product-Guide.pdf");
        String taskflowURL = ControllerContext.getInstance().getTaskFlowURL(false,new TaskFlowId(taskflowDocument,taskflowId), params);
        System.out.println(taskflowURL);
        ExtendedRenderKitService erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class);
        StringBuilder script = new StringBuilder();
          script.append("window.open(\""+taskflowURL+"\");");
        System.out.println(script.toString());
        erks.addScript(FacesContext.getCurrentInstance(), script.toString());   
        return null;

When you trigger the above ling it will execute the code and invoke the your BTF which has url activity, the URL activity invokes the servlet code. This is the flow.



Happy learning...

No comments:

Post a Comment