<!-- Author : Jagbir Singh -->
<!-- Debug : Jagbir Singh -->
<!-- Tested by : Amanbir Singh -->

<%@ page import ="com.bhm.ReadExcelFile" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="com.scand.fileupload.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%
/*

	this file handles the uploading of raw file from the system 

*/
String uploadFolder = config.getServletContext().getRealPath("/")+"uploads";

// Check that we have a file upload request
boolean isMultipart = FileUpload.isMultipartContent(request);

if (!isMultipart) {

	out.println ("Use multipart form to upload a file!");

} else {

String fileId = request.getParameter("sessionId").toString().trim();
String patientId = (String)session.getAttribute("sUserName");
// Create a new file upload handler
FileItemFactory factory = new ProgressMonitorFileItemFactory(request, fileId);
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if (item.isFormField()) {
        //processFormField
    } else {
        //processUploadedFile
		String fieldName = item.getFieldName();
		String fileName = item.getName();
		int i2 = fileName.lastIndexOf("\\");
		if(i2>-1) fileName = fileName.substring(i2+1);
		File dirs = new File(uploadFolder);
	
		if(!dirs.exists()){
       		dirs.mkdir();
       	}
		
			File uploadedFile = new File(dirs,fileName);
			item.write(uploadedFile);
			Vector dataHolder = ReadExcelFile.ReadCSV(uploadedFile.getAbsolutePath());
	      	ReadExcelFile.insertDataIntoDB(dataHolder, patientId);			//parsing function called to enter the details in DB dataInfo table
			session.setAttribute("FileUpload.Progress."+fileId,"-1");
						
		
		
    }
 }

}  
%>