Tuesday, March 27, 2018

Uploading data form .csv file in OAF

Create a Page with a message file upload button
Create a button to process upload in my case a button with Id UploadBtn
Then use this code in the Process Form Request

      if (pageContext.getParameter("UploadBtn")!=null)
      {
      DataObject fileUploadData =pageContext.getNamedDataObject("UploadItem");
      String fileName = null;
      String contentType = null;
      Long fileSize = null;
      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);
      }
      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;
          System.out.println("inside the try1===>");
      while (((lineReader = in.readLine()) !=null) )
      {
      //Split the deliminated data and
      System.out.println("inside while==>");
      if(!lineReader.split(",")[0].equals("Dept"))
      {
      if (lineReader.trim().length()>0)
      {
      linetext = lineReader.split(",");
      System.out.println("LineText"+linetext);
      t++;
      if(!ReqIntVO.isPreparedForExecution())
      {
          ReqIntVO.setMaxFetchSize(-1);   
      }
      Row row=ReqIntVO.createRow();
      ReqIntVO.insertRow(row);
      row.setNewRowState(Row.STATUS_INITIALIZED);
     row.setAttribute("ReqIntId",pageContext.getApplicationModule(webBean)
     .getOADBTransaction().getSequenceValue("XXDPC_REQ_INT_ID_S"));
      row.setAttribute("UserName",pageContext.getUserName());
      if(!linetext[0].trim().equals("ENG"))
      {
      //row.setAttribute("DepartmentCode",linetext[0].trim());
      row.setAttribute("DepartmentCode",orgParams[0]);
      row.setAttribute("LineNum",linetext[1].trim());
      row.setAttribute("HdrDesc",linetext[2].trim());
      row.setAttribute("SourceDocNumber",linetext[3].trim());
      row.setAttribute("SourceDocLineNum",linetext[4].trim());
      row.setAttribute("Quantity",linetext[5].trim());
      row.setAttribute("Attribute1","01"); 
      row.setAttribute("Attribute2",linetext[6].trim());
      row.setAttribute("Attribute3",linetext[7].trim());
      row.setAttribute("Attribute4",linetext[8].trim());
      row.setAttribute("Attribute5",linetext[9].trim());
      row.setAttribute("Attribute6","00");
      row.setAttribute("Attribute7","0000");
      row.setAttribute("ProcessStatus","N");
      }
}
In this case am creating a Row in a VO and setting values to it so when later if we commit record will be created in data base

No comments: