Thursday, September 20, 2012

Logging data in ADF using ADF logger

import oracle.adf.share.logging.ADFLogger;

public class sampleBean{

    private static ADFLogger log= ADFLogger.createADFLogger(sampleBean.class);

    public sampleBean() {   
        intialize();
    }

  public void customMethod(String input1) {

     try {

          log.info("Given Input:" + input1);
       } catch (Exception ex) {

         log.severe("Exception message: " + ex.getMessage() );
       }
    }
}

Handling fileDownloadActionListener in ADF

If you got to work with Download Action Listener in ADF, you will find an issue that, the listener will prompt you to download file even though file is not available. To handle this bug in ADF, we can use Java Script in our JSF page or managed bean. It's little bit tricky, but easy to do it.

Create two buttons in JSF page, one to search file & another one(visible=false) to handle download file:

<af:commandButton text="SearchFile"
                            id="cl1"
                            actionListener="#pageFlowScope.managedBean.searchFile}"                              
                            partialSubmit="true"
                           clientComponent="true"/>
                              
<af:commandButton text="Downloadfile"
                                  id="cb1"
                                  visible="false"
                                  partialTriggers="cl1"
                                  partialSubmit="false"
                                  binding="#{pageFlowScope.managedBean.fileDLButton}"
                                  clientComponent="true">
        <af:fileDownloadActionListener
                     filename="SampleFileName.pdf"
                     contentType="application/pdf; charset=utf-8"
                     method="#{pageFlowScope.managedBean.downloadFile}"/>      
 </af:commandButton>  


Bind button in JSF using accessor method in managed bean:

    public void setfileDLButton(RichCommandButton fileDLButton) {
        this.fileDLButton= fileDLButton;
    }

    public RichCommandButton getfileDLButton() {
        return fileDLButton;
    }

Create two methods in Managed bean to Search & Download file, and in the Search file method use Java Script to trigger second button defined in JSF:

public void searchFile() {
          ............................................................................
     FacesContext fctx = FacesContext.getCurrentInstance();

     String buttonId = getfileDLButton().getClientId(fctx); 

     StringBuilder script = new StringBuilder();

     script.append("var dlButton = AdfPage.PAGE.findComponentByAbsoluteId('" + buttonId + "');");
     script.append("dlEvent = new AdfActionEvent(dlButton);");
     script.append("dlButton.queueEvent(dlEvent, true);");
     ExtendedRenderKitService erks = Service.getService(fctx.getRenderKit(), ExtendedRenderKitService.class);
     erks.addScript(fctx,script.toString());
 }

public void downloadFile(FacesContext context,OutputStream outputStream) {
            ..............................................................................
}


Get File from UCM using RIDC client

import oracle.stellent.ridc.convenience.adf.connection.AdfConnectionFacade;
import oracle.stellent.ridc.IdcClient;
import oracle.stellent.ridc.IdcClientException;
import oracle.stellent.ridc.IdcClientManager;
import oracle.stellent.ridc.IdcContext;
import oracle.stellent.ridc.model.DataBinder;
import oracle.stellent.ridc.protocol.ServiceResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import oracle.adf.share.logging.ADFLogger;

private static ADFLogger log= ADFLogger.createADFLogger(getFile.class);

public void getFile(FacesContext context,OutputStream outputStream) {

   try {
        IdcClientManager manager = new IdcClientManager();                    
        AdfConnectionFacade facade = new AdfConnectionFacade("UcmConnection");
        IdcClient idcClient = facade.getIdcClient();
                           
        IdcContext userContext;
                            
        if(adfCtx.getSessionScope().get("IdcContext")==null) {
           userContext = new IdcContext(facade.getUserCredentials("user"));
           adfCtx.getSessionScope().put("IdcContext",userContext);
          }
        else {
           userContext=(IdcContext)adfCtx.getSessionScope().get("IdcContext");
        }
        DataBinder dataBinder = idcClient.createBinder();   
        dataBinder.putLocal("IdcService", "GET_FILE");        
        dataBinder.putLocal("dID",docID);        
            
        responseString =idcClient.sendRequest(userContext, dataBinder);             
        InputStream  inputStream = responseString.getResponseStream();  

        byte[] buf = new byte[1024 * 256];
        long i = 0;
        int len;

        log.severe("Downloading File from UCM Server"); 

        while (true) {
                i++;
                len = inputStream.read(buf);
                if (len == -1) {
                    break;
               }
             outputStream.write(buf, 0, len);
            }
             outputStream.flush(); 
             inputStream.close();
             outputStream.close();
             context.responseComplete();
        } catch (IdcClientException ice) {
                 log.severe("IDC Client Exception occured. Exception message: " + ice.getMessage());
        } catch (IOException ioe) {
                 log.severe("IO Exception occurred. Unable to retrieve file. Message: " + ioe.getMessage()); 
        } catch (Exception ex) {
                 log.severe("Exception message: " + ex.getMessage() );
        }               
    }
}

Search File in UCM using RIDC client

public void searchFile(String docTitle) {
   try {
        IdcClientManager manager = new IdcClientManager();                    
        AdfConnectionFacade facade = new AdfConnectionFacade("UcmConnection");
        IdcClient idcClient = facade.getIdcClient();
                           
        IdcContext userContext;
                            
        if(adfCtx.getSessionScope().get("IdcContext")==null) {
           userContext = new IdcContext(facade.getUserCredentials("user"));
           adfCtx.getSessionScope().put("IdcContext",userContext);
          }
        else {
           userContext=(IdcContext)adfCtx.getSessionScope().get("IdcContext");
        }
         
        DataBinder dataBinder = idcClient.createBinder();
        dataBinder.putLocal("IdcService", "GET_SEARCH_RESULTS");
        dataBinder.putLocal("QueryText", "dDocTitle <matches> `" + docTitle + "`");
        dataBinder.putLocal("ResultCount", "1");          
  
        ServiceResponse response =idcClient.sendRequest(userContext, dataBinder);
        DataBinder responseData = response.getResponseAsBinder();
        String count = responseData.getLocal("TotalRows");
        DataResultSet resultSet = responseData.getResultSet("SearchResults");
       
       if (count.equals("0")) {
                 log.severe("Document Not Found in UCM Server");
            } else {           
                    for (DataObject dataObject : resultSet.getRows()) {
                         docID = dataObject.get("dID");
                         log.info("UCM Doc ID: " + docID); 
                      }
             }                       
         } catch (IdcClientException ice) {
                log.severe("IDC Client Exception occured. Exception message: " + ice.getMessage());
         } catch (Exception ex) {
                log.severe("Exception message: " + ex.getMessage() );
    }

Calling Database Package/Procedure in ADF

Define execute procedure Operation in Application Module Impl:   

import java.sql.Types;
import oracle.jdbc.OracleCallableStatement;

public String executeProc(String value1,String value2,String value3) {
       
       String output=null;
       Connection conn;
             
        try {
            conn = getDBTransaction().createStatement(1).getConnection();
                
            String executeProcStatement=
                "begin SAMPLE_PKG.SAMPLE_PROC(:inputParam1," +
                ":inputParam2,"+
                ":inputParam3," +
                ":outputParam1," +
                ":outputParam2);
                end;";              
         
           CallableStatement st = null;
            st =
            getDBTransaction().createCallableStatement(executeProcStatement, this.getDBTransaction().DEFAULT);
            try{
                st =
                (OracleCallableStatement)getDBTransaction().createCallableStatement(executeProcStatement,0);
               
                st.setObject("inputParam1", value1);
                st.setObject("inputParam2", value2);
                st.setObject("inputParam3", value3);

                st.registerOutParameter("outputParam1", Types.VARCHAR);
                st.registerOutParameter("outputParam2", Types.VARCHAR);
               
               st.execute();
                output=(String)st.getObject("outputParam1");
                System.out.println(st.getObject("outputParam1"));
            }catch (SQLException e) {
                     .....................................................
            } finally {
                if (st != null) {
                    try {
                        st.close();
                    } catch (SQLException e) {
                     .....................................................
                    }
                }
            }
        }
        catch (SQLException e) {
                    System.out.println(e);
                }
       
        return output;
    }
   
Define Method in backing or managed bean and specify it in ADF faces components action listener property:

    public void executeProcListener(ActionEvent actionEvent) {
   
    OperationBinding executeProc = ADFUtils.findOperation("executeProc");
    Map params=executeProc.getParamsMap();
        params.put("inputParam1", value1);
        params.put("inputParam2", value2);
        params.put("inputParam3", value3);
        String output=(String)executeProc.execute();
       
        FacesContext fc=FacesContext.getCurrentInstance();
       
        if(output!=null) {
                    .................................................
        }
        else
        {
                    .................................................
        }
        log.severe("Output "+output);
    }