Tuesday 28 January 2014

Uploading a CSV file into Data Base Table using OAF

Hi Friends,

Today i am going to share how to upload a CSV file into Data Base table in OAF.

1)Create an item type: MessageFileUpload and change the itemid.It will give us a browse option for file.

2)Create a button for uploading event.Here i am using normal button and updated event as Go.

3)In controller Process form request we need to handle Go event.

Before that please import below classes:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.cabo.ui.data.DataObject;
import oracle.jbo.domain.BlobDomain;
------------------

in Process for request :
------------------


    OAApplicationModule am =
      (OAApplicationModule)pageContext.getApplicationModule(webBean);
   
    if ("Go".equals(pageContext.getParameter(EVENT_PARAM)))
    {
      DataObject fileUploadData =
        (DataObject)pageContext.getNamedDataObject("FileUploadItem");
      String fileName = null;
      String contentType = null;
      Long fileSize = null;
      Integer fileType = new Integer(6);
      BlobDomain uploadedByteStream = null;
      BufferedReader in = null;

      try
      {
        fileName =
            (String)fileUploadData.selectValue(null, "UPLOAD_FILE_NAME");
        contentType =
            (String)fileUploadData.selectValue(null, "UPLOAD_FILE_MIME_TYPE");
        uploadedByteStream =
            (BlobDomain)fileUploadData.selectValue(null, fileName);
        in =
            new BufferedReader(new InputStreamReader(uploadedByteStream.getBinaryStream()));

        fileSize = new Long(uploadedByteStream.getLength());
        System.out.println("fileSize  : " + fileSize);
        System.out.println("fileName :"+fileName);
        System.out.println("contentType :"+contentType);
       
      } catch (NullPointerException ex)
      {
        throw new OAException("Please Select a File to Upload",
                              OAException.ERROR);
      }

      try
      {
        //Open the CSV file for reading
        String lineReader = "";
        long t = 0;
        String[] linetext;
        while (((lineReader = in.readLine()) != null))
        {
          //Split the deliminated data and
          if (lineReader.trim().length() > 0)
          {
            System.out.println("lineReader" + lineReader.length());
            linetext = lineReader.split(",");
            t++;
           int a= linetext.length;
           System.out.println("line text length is : "+a);
           for(int k=0;k<a;k++){
          
           System.out.println(linetext[k]);
           }
}
// Now Create a new row in respective View Object and set the attributes and Save the transaction.


1 comment: