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

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

<%
		MsgPackage rtnmsg = new MsgPackage();
		MsgPackage mypck = new MsgPackage();

		try {

			System.out.print("/**************************************/");
			System.out.print("check leave request page has been called from client");

			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 !!");
					}
				}

				/* send receive msg to servr */
				if(dataitem.containsKey("data")){

					String rawtimedata = (String) dataitem.get("data");
					mypck = rtnmsg.convStrToMsgPackage(rawtimedata);
					/* use mypck to save into db */

					SV_ChkLeaveReq chkreq = new SV_ChkLeaveReq(mypck);
					chkreq.run();
					System.out.print("---chk leave return data ---");
					System.out.print("encrypted --> " + mypck.getPackage());
					
				}
			}else{
					String rawtimedata = "{\"devno\":\"002\",\"passcode\":\"\",\"companyid\":\"100\",\"comcode\":\"\",\"serialno\":\"\",\"authtype\":\"\",\"install\":false,\"logserv1\":\"\",\"logserv2\":\"\",\"datserv\":\"\",\"dev_loc\":\"\",\"dev_long\":\"\",\"dev_lat\":\"\",\"authen\":false,\"fpid\":0,\"empid\":\"0078\",\"positionid\":\"\",\"fpfilename\":\"\",\"servClock\":\"\",\"tasklist\":[],\"emplist\":[],\"timelist\":[],\"accinfolist\":[],\"empfp\":[],\"lang\":\"en\",\"msg\":\"\",\"condition\":\"\",\"status\":\"0\",\"fptp\":null,\"taskcond\":null,\"chksum\":15953,\"chksumfp\":0,\"isValidPackage\":true,\"reqTimeAttdata\":{\"start_date\":\"\",\"end_date\":\"\",\"arrTimeAtt\":[],\"arrReason\":[],\"sendto_id\":\"\",\"sendto_name\":\"\",\"employeeid\":\"0078\",\"status\":false,\"msg\":\"\"},\"reqOT\":{\"arrOT\":[],\"sendto_id\":\"1020\",\"sendto_name\":\"นายปรัชญา ครอบครัวเดียวกัน\",\"employeeid\":\"\",\"status\":false,\"msg\":\"\"},\"reqLeave\":{\"lvtype\":\"A03\",\"f_day\":true,\"f_halfday\":false,\"f_fhalf\":false,\"f_shalf\":false,\"f_hour\":false,\"d_start_date\":\"2014-07-22\",\"d_end_date\":\"2014-07-22\",\"fs_start_date\":null,\"h_start_date\":null,\"h_start_time\":null,\"h_end_date\":null,\"h_end_time\":null,\"remark\":\"sick leave test\",\"ltypeprop\":[{\"lvid\":\"A01\",\"desc\":\"ลาพักผ่อนประจำปี\"},{\"lvid\":\"A02\",\"desc\":\"ลากิจ\"},{\"lvid\":\"A03\",\"desc\":\"ลาป่วย\"},{\"lvid\":\"A04\",\"desc\":\"ลาป่วย (ในงาน)\"},{\"lvid\":\"A05\",\"desc\":\"ลาคลอด\"},{\"lvid\":\"A06\",\"desc\":\"ลาเพื่อรับราชการทหารฯ\"},{\"lvid\":\"A07\",\"desc\":\"ลาอุปสมบท\"},{\"lvid\":\"A08\",\"desc\":\"ลาทำหมัน\"},{\"lvid\":\"A09\",\"desc\":\"ลาฝึกอบรม\"},{\"lvid\":\"OTL\",\"desc\":\"ลาหยุดพิเศษ (หักจากยอดชม. โอทีสะสม)\"}],\"lsprop\":[{\"lvtypeid\":\"A01\",\"lvtypedesc\":\"ลาพักผ่อนประจำปี\",\"lvcarry_forward\":\"00:00:00\",\"lvlimit\":\"15:00:00\",\"lvused\":\"03:00:00\",\"lvremain\":\"12:00:00\"},{\"lvtypeid\":\"A02\",\"lvtypedesc\":\"ลากิจ\",\"lvcarry_forward\":\"\",\"lvlimit\":\"06:00:00\",\"lvused\":\"00:05:00\",\"lvremain\":\"05:03:00\"},{\"lvtypeid\":\"A03\",\"lvtypedesc\":\"ลาป่วย\",\"lvcarry_forward\":\"\",\"lvlimit\":\"30:00:00\",\"lvused\":\"01:00:00\",\"lvremain\":\"29:00:00\"},{\"lvtypeid\":\"A04\",\"lvtypedesc\":\"ลาป่วย (ในงาน)\",\"lvcarry_forward\":\"\",\"lvlimit\":\"45:00:00\",\"lvused\":\"00:00:00\",\"lvremain\":\"45:00:00\"},{\"lvtypeid\":\"A05\",\"lvtypedesc\":\"ลาคลอด\",\"lvcarry_forward\":\"\",\"lvlimit\":\"90:00:00\",\"lvused\":\"00:00:00\",\"lvremain\":\"90:00:00\"},{\"lvtypeid\":\"A06\",\"lvtypedesc\":\"ลาเพื่อรับราชการทหารฯ\",\"lvcarry_forward\":\"\",\"lvlimit\":\"99:00:00\",\"lvused\":\"00:00:00\",\"lvremain\":\"99:00:00\"},{\"lvtypeid\":\"A07\",\"lvtypedesc\":\"ลาอุปสมบท\",\"lvcarry_forward\":\"\",\"lvlimit\":\"30:00:00\",\"lvused\":\"00:00:00\",\"lvremain\":\"30:00:00\"},{\"lvtypeid\":\"A08\",\"lvtypedesc\":\"ลาทำหมัน\",\"lvcarry_forward\":\"\",\"lvlimit\":\"99:00:00\",\"lvused\":\"00:00:00\",\"lvremain\":\"99:00:00\"},{\"lvtypeid\":\"A09\",\"lvtypedesc\":\"ลาฝึกอบรม\",\"lvcarry_forward\":\"\",\"lvlimit\":\"99:00:00\",\"lvused\":\"00:00:00\",\"lvremain\":\"99:00:00\"},{\"lvtypeid\":\"OTL\",\"lvtypedesc\":\"ลาหยุดพิเศษ (หักจากยอดชม. โอทีสะสม)\",\"lvcarry_forward\":\"\",\"lvlimit\":\"99:00:00\",\"lvused\":\"03:00:00\",\"lvremain\":\"96:00:00\"}],\"sendto_id\":\"\",\"sendto_name\":\"\",\"employeeid\":\"\",\"status\":false,\"msg\":\"\"}}";
					mypck = rtnmsg.convStrToMsgPackage(rawtimedata);
					/* use mypck to save into db */

					SV_ChkLeaveReq chkreq = new SV_ChkLeaveReq(mypck);
					chkreq.run();
			}
		} catch(Exception e){
			e.printStackTrace();
		}

		out.print(mypck.getPackage());
%>


