<%@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.SV_GetCourse" %>
<%@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("get course 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");
					//out.print(rawtimedata);
					mypck = rtnmsg.convStrToMsgPackage(rawtimedata);
					/* use mypck to save into db */

					SV_GetCourse svg = new SV_GetCourse(mypck);
					svg.run();
					//System.out.print("--- get task return data ---");
					//System.out.print("encrypted --> " + mypck.getPackage());
					
				}
			}else{
				String rawdata = "{\"devno\":\"\",\"passcode\":\"\",\"companyid\":\"\",\"comcode\":\"\",\"serialno\":\"\",\"authtype\":\"\",\"chgpass\":\"\",\"empgroup\":\"\",\"zonegroup\":\"\",\"install\":false,\"logserv1\":\"\",\"logserv2\":\"\",\"datserv\":\"\",\"datserv2\":\"\",\"dev_loc\":\"\",\"dev_long\":\"\",\"dev_lat\":\"\",\"authen\":false,\"fpid\":0,\"empid\":\"\",\"positionid\":\"\",\"fpfilename\":\"\",\"servClock\":\"\",\"tasklist\":[],\"emplist\":[],\"timelist\":[],\"accinfolist\":[],\"empfp\":[],\"empmenu\":[],\"courses\":[],\"dvp\":{\"devno\":\"\",\"password\":\"\",\"companyid\":\"\",\"serialno\":\"\",\"authtype\":\"\",\"empgroup\":\"\",\"zonegroup\":\"\",\"logserv1\":\"\",\"logserv2\":\"\",\"datserv\":\"\",\"datserv2\":\"\",\"dev_loc\":\"\",\"dev_long\":\"\",\"dev_lat\":\"\"},\"lang\":\"\",\"msg\":\"\",\"condition\":\"\",\"status\":\"0\",\"fptp\":null,\"taskcond\":null,\"chksum\":20013,\"chksumfp\":0,\"isValidPackage\":true,\"reqTimeAttdata\":{\"start_date\":\"\",\"end_date\":\"\",\"arrTimeAtt\":[],\"arrReason\":[],\"sendto_id\":\"\",\"sendto_name\":\"\",\"employeeid\":\"\",\"status\":false,\"msg\":\"\"},\"reqOT\":{\"arrOT\":[],\"sendto_id\":\"\",\"sendto_name\":\"\",\"employeeid\":\"\",\"status\":false,\"msg\":\"\"},\"reqLeave\":{\"lvtype\":null,\"lvdesc\":null,\"f_day\":false,\"f_halfday\":false,\"f_fhalf\":false,\"f_shalf\":false,\"f_hour\":false,\"lvreqstatus\":false,\"d_start_date\":null,\"d_end_date\":null,\"fs_start_date\":null,\"h_start_date\":null,\"h_start_time\":null,\"h_end_date\":null,\"h_end_time\":null,\"remark\":null,\"lmsg\":null,\"ltypeprop\":[],\"lsprop\":[],\"sendto_id\":\"\",\"sendto_name\":\"\",\"employeeid\":\"\",\"status\":false,\"msg\":\"\"}}";

				SV_GetCourse svg = new SV_GetCourse(mypck);
				svg.run();

				System.out.print("--- get task return data ---");
				System.out.print("encrypted --> " + mypck.getPackage());
			}
		} catch(Exception e){
			e.printStackTrace();
		}

		out.print(mypck.getPackage());
		//System.out.print("------- success ----" + mypck.getPackage());
%>


