Wednesday, 11 May 2016

How to Read external property file in ADF Application.

Some time we have requirement without changing the ear file need to get the some values dynamically. Examples like some URL, or some values.

For this kind of requirement we need to use external property file concept,  so dynamically the application fetch the values from file how ever the values are updated in the file.

Here I am posting the steps to achieve this requirement.

First create application, after that set the some properties in view project, like below.





Next set the below code snippet in faces-config.xml file.

<resource-bundle>
      <base-name>nhs.esr.view.MyResourceBundle</base-name>
      <var>viewcontrollerBundle</var>
    </resource-bundle>

Next we need to access the external file which stored in wls server home. And access the file in our application.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.ListResourceBundle;
import java.util.Properties;


public class MyResourceBundle extends ListResourceBundle {
    private static final Object[][] contents = { };

    //    public Object[][] getContents() {
    //        return contents;
    //    }

    public Object[][] getContents() {
        //Object[][] data = null;
        try {
            //Property file path in system
            Properties prop = new Properties();
            String WL_HOME = System.getenv("WL_HOME");
            prop.load(new FileInputStream(WL_HOME +
                                          "/MyResourceBundle.properties"));
            //                String nodeProFile =
            //                "C:\\Users\\Eshwar\\Desktop\\MyResourceBundle.properties";
            //                System.out.println("nodeProFile : " + nodeProFile);
            //                load a properties file
            //                prop.load(new FileInputStream(WL_HOME+"/MyResourceBundle.properties"));
            Enumeration propKeys = prop.keys();
            System.out.println("Property size : " + prop.size());
            //System.out.println("the value is :" + prop.getProperty("KEY_NAME"));
            int i = 0;
            while (propKeys.hasMoreElements()) {
                String key = (String)propKeys.nextElement();
                String value = prop.getProperty(key);
                System.out.println("Key  " + key + " -> : " + value);
                contents[i][0] = key;
                contents[i][1] = value;
                i++;
            }
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return contents;
    }
}


Happy learning.. :)

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...