<%@page contentType="text/html;  charset=UTF-8;" pageEncoding="UTF-8"  %>

<%@page import="com.csc.library.utilities.*,com.csc.library.database.DataSet,java.util.*,com.csc.library.utilities.ThaiUtilities" %>
<%@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.fputility.MsgPackage" %>
<%@page import="com.csc.library.session.*" %>
<%@page import="com.csc.library.fpprocess.*" %>
<%@page import="com.google.gson.Gson" %>
<%@page import="com.google.gson.GsonBuilder" %>

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

		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		MsgPackage recdata = new MsgPackage();
		Gson gson = new Gson();
		Gson gs = new GsonBuilder().serializeNulls().create();
		long curtime = System.currentTimeMillis();

		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();
			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 {
					try {
						System.out.print("!! File upload detected !!");
						dataitem.put("attchfile", item);

						/*
						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/" + recdata.getCompany() + "/" ;
						File uploadedFile = new File(path + curtime + ".dat");

						System.out.print("!! Absolute path -> " + uploadedFile.getAbsolutePath());
						item.write(uploadedFile);*/
					}catch(Exception e){
						System.out.print(e.getMessage());
					}
				}
			}

			/* save detail */
			if(dataitem.containsKey("data")){
				try {
					System.out.print("show --> " + (String) dataitem.get("data"));
					MsgPackage msgpack = new MsgPackage();
					recdata = msgpack.convStrToMsgPackage((String) dataitem.get("data"));
					recdata.setFpfilename(curtime + ".dat");

					/*set default (test used)*/
					recdata.setDevNo("0000001");
					recdata.setPassCode("ZBCDFGHJKIUYT");
					recdata.setCompanyid("100");
					recdata.setSerialNo("1236547890");
					/*******/

					SV_RegisFingerprint regf = new SV_RegisFingerprint(recdata);
					regf.run();
				}catch(Exception e){
					System.out.print(e.getMessage());
				}

				/* */
				if(dataitem.containsKey("attchfile")){
					try {
						FileItem item = (FileItem) dataitem.get("attchfile");

						String fieldName = item.getFieldName();
						String fileName = item.getName();
						String contentType = item.getContentType();
						
						boolean isInMemory = item.isInMemory();
						long sizeInBytes = item.getSize();

						String path = "D:/EnterprisePT/Programmer/myHRAppServer/webapps/SYS/hrAppWeb.war/UPLOAD/REGISFP/PICTURES/" + recdata.getCompany() + "/" ;

						System.out.print(" !! Save fp file -- " + path + curtime + ".dat");
						/* create path if not exist */
						File file = new File(path);
						if (!file.exists()) {
							if (file.mkdir()) {
								System.out.println("Directory is created!");

								File uploadedFile = new File(path + curtime + ".dat");
								item.write(uploadedFile);
							} else {
								System.out.println("Failed to create directory!");
							}
						}else{
							File uploadedFile = new File(path + curtime + ".dat");
							item.write(uploadedFile);
						}
					}catch(Exception e){
						System.out.print(e.getMessage());
					}
				}
			}
		}else{
			System.out.print("!! package received is not in multipart format !!");
		}
%>

<%=recdata.getPackage()%>






