Wednesday 26 February 2014

How to Read CSV and insert Errors in Table dynamically:

if (pageContext.getParameter("uploadItm") != null) {
DataObject fileUploadData =
(DataObject)pageContext.getNamedDataObject("uploadFileItm2");
String fileName = null;
String contentType = null;
Long fileSize = null;
Integer fileType = new Integer(6);
BlobDomain uploadedByteStream = null;
BufferedReader in = null;
long count = 0;
long errorCount = 0;
String fileId=am.getOADBTransaction().getSequenceValue("gConItem_fileID_SEQ").toString();
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;
int z = 1;
String removeRow="N";
String[] linetext;

while (((lineReader = in.readLine()) != null)) {

//Split the deliminated data and
if (lineReader.trim().length() > 0) {

linetext = lineReader.split(",");
t++;
count=t;
if (t > 1) {

//Print the current line being
int a = linetext.length;
// System.out.println("line text length is : "+a);
Row arrow = arvo.createRow();
// need to enable after altering table
// arrow.setAttribute("FileId",fileId);
removeRow="N";
for (int k = 0; k < a; k++)
{
try {
arrow.setAttribute(k,linetext[k].toString());
}
catch (Exception e)
{ //inserting errors in error table dynamically
Row errorrow = errorVO.createRow();
errorrow.setAttribute(0,fileName);
errorrow.setAttribute(1,t);
errorrow.setAttribute(2,e.getMessage().toString());
removeRow="Y";
break; }
//System.out.println("line number"+t+"and cloumn"+k);
// System.out.println(linetext[k]);
// arrow.setAttribute(k, linetext[k]);
}
if (removeRow.equalsIgnoreCase("N"))
{ arvo.insertRow(arrow);
}else{ arrow.remove(); errorCount++;
} z++;
if (z % 10000 == 0) am.getOADBTransaction().commit();
}
// System.out.println("record"+t);
}
// need to save records here
}
System.out.println("end time");
}
catch (IOException e)
{
throw new OAException(e.getMessage(), OAException.ERROR);
}
configId=pageContext.getParameter("configHeaderIdFV").toString();
String convItm=pageContext.getParameter("convertionItemFV").toString();
String ou=pageContext.getParameter("ouIdFv").toString();
String invOrg=null;
if(pageContext.getParameter("invOrgIdFV")!=null)
{ invOrg= pageContext.getParameter("invOrgIdFV").toString();}
System.out.println(pageContext.getParameter("configHeaderIdFV").toString()+"--"+pageContext.getParameter ("convertionItemFV").toString());
count=count-errorCount-1;
AM.addFilesDetails(configId,convItm,count,errorCount,fileName,ou,invOrg,fileId); }// end upload functionality


No comments:

Post a Comment