<%@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("prepare time attendance 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_SaveTimeAttReq savereq = new SV_SaveTimeAttReq(mypck);
					savereq.run();
					System.out.print("---save time att req time att. return data ---");
					System.out.print("encrypted --> " + mypck.getPackage());
					
				}
			}else{
				String rawtimedata = "{\"devno\":\"\",\"passcode\":\"\",\"companyid\":\"100\",\"comcode\":\"\",\"serialno\":\"\",\"authtype\":\"\",\"install\":false,\"logserv1\":\"\",\"logserv2\":\"\",\"datserv\":\"\",\"dev_loc\":\"\",\"dev_long\":\"\",\"dev_lat\":\"\",\"authen\":false,\"fpid\":0,\"empid\":\"0078\",\"fpfilename\":\"\",\"servClock\":\"\",\"tasklist\":[],\"emplist\":[],\"timelist\":[],\"accinfolist\":[],\"empfp\":[],\"lang\":\"\",\"msg\":\"\",\"condition\":\"\",\"status\":\"0\",\"fptp\":null,\"taskcond\":null,\"chksum\":7832,\"chksumfp\":0,\"isValidPackage\":true,\"reqTimeAttdata\":{\"start_date\":\"\",\"end_date\":\"\",\"arrTimeAtt\":[{\"swipedate\":\"06-03-2014\",\"swipetime\":\"17.00\",\"swipetype\":\"1\",\"reasonid\":\"\",\"reason_desc\":\"\"},{\"swipedate\":\"14-03-2014\",\"swipetime\":\"17.00\",\"swipetype\":\"1\",\"reasonid\":\"\",\"reason_desc\":\"\"},{\"swipedate\":\"15-03-2014\",\"swipetime\":\"08.00\",\"swipetype\":\"0\",\"reasonid\":\"\",\"reason_desc\":\"\"},{\"swipedate\":\"15-03-2014\",\"swipetime\":\"16.00\",\"swipetype\":\"1\",\"reasonid\":\"\",\"reason_desc\":\"\"}],\"arrReason\":[{\"reasonid\":\"001\",\"reason_desc\":\"ลืมบันทึกเวลา\"},{\"reasonid\":\"002\",\"reason_desc\":\"ลืมบัตรพนักงาน\"},{\"reasonid\":\"003\",\"reason_desc\":\"กิจกรรมบริษัท\"},{\"reasonid\":\"004\",\"reason_desc\":\"เข้างานใหม่(ยังไม่มีบัตร)\"},{\"reasonid\":\"005\",\"reason_desc\":\"อบรมนอกสถานที่\"},{\"reasonid\":\"006\",\"reason_desc\":\"ปฏิบัติงานนอกสถานที่\"},{\"reasonid\":\"007\",\"reason_desc\":\"บัตรหาย/ชำรุด\"},{\"reasonid\":\"008\",\"reason_desc\":\"ไม่มีเครื่องบันทึกเวลา (เฉพาะพนักงานบางซื่อ)\"},{\"reasonid\":\"009\",\"reason_desc\":\"รถบริษัทมาสาย\"}],\"sendto_id\":\"1020\",\"sendto_name\":\"Mr. PRACHYA Brother Bands\",\"employeeid\":\"0078\",\"status\":false,\"msg\":\"\"}}";
				mypck = rtnmsg.convStrToMsgPackage(rawtimedata);
				/* use mypck to save into db */

				SV_SaveTimeAttReq savereq = new SV_SaveTimeAttReq(mypck);
				savereq.run();
			}
		} catch(Exception e){
			e.printStackTrace();
		}

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


