OAF : Upload Excel File to Database


I want to allow user to upload excel file in database from OAF page.

Suppose that excel file contains below columns
EmpNo
EmpName
Job

Suppose also that I have Entity Object named XxxEmpEO and I created View Object XxxEmpVO based on previous entity object which has below attributes
EmpNo
EmpName
Job

Scenario
I added new item in page of type messageFileUpload ["uploadExcelFile" ] and Button ["uploadButton"].
If user click a button, I will upload excel file that is entered in messageFileUpload item to Entity Object and then commit changes to database.
Idea of Implementation
I will read file as byte stream and then loop for every line in file and insert it in view object.
 
Now I should handle action of upload button, I will write my code in ProcessFromRequest method in Controller Class

   public void processFromRequest(OAPageContext oapagecontext,   
                   OAWebBean oawebbean) {  
     super.processFormRequest(oapagecontext, oawebbean);  
     OAApplicationModule am = oapagecontext.getApplicationModule(oawebbean);  
     OAViewObjectImpl empVO =   
       (OAViewObjectImpl)am.findViewObject("XxxEmpVO");  
     if (oapagecontext.getParameter("uploadButton") != null) {  
       //Get Data of uploaded Excel file  
       DataObject excelUploadData =   
         oapagecontext.getNamedDataObject("uploadExcelFile");  
       //Declare Variable that will be used in reading uploaded file  
       String fileName = null;  
       String contentType = null;  
       Long fileCapacity = null;  
       BlobDomain uploadStream = null;  
       BufferedReader inReader = null;  
       try {  
         fileName =   
             (String)excelUploadData.selectValue(null, "UPLOAD_FILE_NAME");  
         contentType =   
             (String)excelUploadData.selectValue(null, "UPLOAD_FILE_MIME_TYPE");  
         uploadStream =   
             (BlobDomain)excelUploadData.selectValue(null, fileName);  
         inReader =   
             new BufferedReader(new InputStreamReader(uploadStream.getBinaryStream()));  
         fileCapacity = new Long(uploadStream.getLength());  
       } catch (NullPointerException ex) {  
         throw new OAException("Please Select an Excel File to Upload it to Database!!!",   
                    OAException.ERROR);  
       }  
       try {  
         String wholeLine = "";  
         long counter = 0;  
         String[] seperatedCells;  
         while (((wholeLine = inReader.readLine()) != null)) {  
           //Split the deliminated data and  
           if (wholeLine.trim().length() > 0) {  
             //split whole line to cells  
             seperatedCells = wholeLine.split(",");  
             counter++;  
             //Print the current line in Console  
             System.out.println(" line #" + counter + " - " +   
                       seperatedCells[0] + " - " +   
                       seperatedCells[1] + " - " +   
                       seperatedCells[2]);  
             Row row = empVO.createRow();  
             row.setAttribute("EmpNo", seperatedCells[0]);  
             row.setAttribute("EmpName", seperatedCells[1]);  
             row.setAttribute("Job ", seperatedCells[2]);  
           }  
         }  
       } catch (IOException e) {  
         throw new OAException(e.getMessage(), OAException.ERROR);  
       }  
       am.getTransaction().commit();  
       throw new OAException("Excel File Uploaded SuccessFully!!!",   
                  OAException.CONFIRMATION);  
     }  
   }  


Thanks

Popular posts from this blog

ADF : Get Current Logged User Name

OAF : Get Current Row in Table

ADF : Working with ViewCriteria