<%@page import="com.csc.library.utilities.*,com.csc.library.database.DataSet,java.util.*,com.csc.library.utilities.ThaiUtilities" %>
<%@page contentType="text/html;  charset=UTF-8;" pageEncoding="UTF-8"  %>
<%@page import="com.csc.library.database.CscConnection" %>
<%@page import="com.sun.org.apache.xerces.internal.impl.dv.util.Base64" %>
<%@page import="java.awt.image.BufferedImage"  %>
<%@page import="java.io.ByteArrayInputStream" %>
<%@page import="java.io.ByteArrayOutputStream" %>
<%@page import="java.io.File" %>
<%@page import="java.io.IOException" %>
<%@page import="java.io.InputStream" %>
<%@page import="javax.imageio.ImageIO" %>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@page import="org.apache.commons.fileupload.FileItem" %>
<%@page import="com.csc.library.utility.MsgPackage" %>
<%@page import="com.csc.library.session.*" %>

<%
		System.out.print("***!! Save finger print page has been called from client !!***");

		UProfile up = new UProfile(); 
		up.setDbName("MSTECH");
		up.setSchemaName("dbo");
		up.setUserName("MYHRADMIN");
		up.set("companyid", "100");
		up.set("employeeid", "1262");
		up.set("lang", "eng");
		up.set("appname", "TA");

		boolean isMultipart = ServletFileUpload.isMultipartContent(request);

		if(isMultipart){
			System.out.print("!! Multipart data detected !!");
			// Create a factory for disk-based file items
			FileItemFactory factory = new DiskFileItemFactory();

			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);

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

			// Process the uploaded items
			Iterator iter = items.iterator();
			HashMap dataitem = new HashMap();
			long curtime = System.currentTimeMillis();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();

				if (item.isFormField()) {
					String name = item.getFieldName();
					String value = item.getString();
					dataitem.put(name, value);
					System.out.print("!! Field name : "+ name + " value " + value);
				} else {
					System.out.print("!! File upload detected !!");

					String fieldName = item.getFieldName();
					String fileName = item.getName();
					String contentType = item.getContentType();
					boolean isInMemory = item.isInMemory();
					long sizeInBytes = item.getSize();
					String path = "D:/EnterprisePT/myHRAppServer/webapps/CSC/hrAppWeb.war/UPLOAD/REGISFP/PICTURES/" ;
					//File uploadedFile = new File(path + ((String) dataitem.get("empid")) + ".dat");
					File uploadedFile = new File(path + curtime + ".dat");
					dataitem.put("filepath", uploadedFile.getAbsolutePath());
					System.out.print("!! Absolute path -> " + uploadedFile.getAbsolutePath());
					item.write(uploadedFile);
				}
			}

			if(!dataitem.isEmpty()){
				System.out.print((String) dataitem.get("data"));
				MsgPackage msgpack = new MsgPackage();
				MsgPackage recdata = msgpack.convStrToMsgPackage((String) dataitem.get("data"));

				System.out.print("!! employeeid -> " + recdata.empid);
				System.out.print("!! fpid -> " + recdata.fpid);
				try {
					DbRecord db = new InitialRecord(up).getDbRecord("Mempl_fp");
					db.setColumn("*");
					db.set("employeeid", recdata.empid);
					db.set("companyid", "100");
					db.set("fpid", recdata.fpid);
					db.set("status", "1");
					db.set("filename", curtime + ".dat");

					db.save();
				}catch(Exception e){
					e.printStackTrace();
				}

				
			}
		}
%>

<%="{\"data\":{\"msg\":\"receive_fpdata_successful\"},\"header\":{\"passcode\":\"Xmsd2569SXn\",\"comcode\":1,\"device_no\":\"mcc0001\"}}"%>
