package com.mobileserver.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Timestamp;
import java.util.List;

import com.mobileserver.dao.Teacher_QQ287307421DAO;
import com.mobileserver.domain.Teacher_QQ287307421;

import org.json.JSONStringer;

public class Teacher_QQ287307421Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/*构造教师信息业务层对象*/
	private Teacher_QQ287307421DAO teacher_QQ287307421DAO = new Teacher_QQ287307421DAO();

	/*默认构造函数*/
	public Teacher_QQ287307421Servlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		/*获取action参数，根据action的值执行不同的业务处理*/
		String action = request.getParameter("action");
		if (action.equals("query")) {
			/*获取查询教师信息的参数信息*/
			String teacherNumber = request.getParameter("teacherNumber");
			teacherNumber = teacherNumber == null ? "" : new String(request.getParameter(
					"teacherNumber").getBytes("iso-8859-1"), "UTF-8");
			String teacherName = request.getParameter("teacherName");
			teacherName = teacherName == null ? "" : new String(request.getParameter(
					"teacherName").getBytes("iso-8859-1"), "UTF-8");
			Timestamp teacherBirthday = null;
			if (request.getParameter("teacherBirthday") != null)
				teacherBirthday = Timestamp.valueOf(request.getParameter("teacherBirthday"));
			Timestamp teacherArriveDate = null;
			if (request.getParameter("teacherArriveDate") != null)
				teacherArriveDate = Timestamp.valueOf(request.getParameter("teacherArriveDate"));

			/*调用业务逻辑层执行教师信息查询*/
			List<Teacher_QQ287307421> teacher_QQ287307421List = teacher_QQ287307421DAO.QueryTeacher_QQ287307421(teacherNumber,teacherName,teacherBirthday,teacherArriveDate);

			/*将查询的结果集通过xml格式传输给客户端*/
			StringBuffer sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\r\n")
			.append("<Teacher_QQ287307421s>").append("\r\n");
			for (int i = 0; i < teacher_QQ287307421List.size(); i++) {
				sb.append("	<Teacher_QQ287307421>").append("\r\n")
				.append("		<teacherNumber>")
				.append(teacher_QQ287307421List.get(i).getTeacherNumber())
				.append("</teacherNumber>").append("\r\n")
				.append("		<teacherName>")
				.append(teacher_QQ287307421List.get(i).getTeacherName())
				.append("</teacherName>").append("\r\n")
				.append("		<teacherPassword>")
				.append(teacher_QQ287307421List.get(i).getTeacherPassword())
				.append("</teacherPassword>").append("\r\n")
				.append("		<teacherSex>")
				.append(teacher_QQ287307421List.get(i).getTeacherSex())
				.append("</teacherSex>").append("\r\n")
				.append("		<teacherBirthday>")
				.append(teacher_QQ287307421List.get(i).getTeacherBirthday())
				.append("</teacherBirthday>").append("\r\n")
				.append("		<teacherArriveDate>")
				.append(teacher_QQ287307421List.get(i).getTeacherArriveDate())
				.append("</teacherArriveDate>").append("\r\n")
				.append("		<teacherCardNumber>")
				.append(teacher_QQ287307421List.get(i).getTeacherCardNumber())
				.append("</teacherCardNumber>").append("\r\n")
				.append("		<teacherPhone>")
				.append(teacher_QQ287307421List.get(i).getTeacherPhone())
				.append("</teacherPhone>").append("\r\n")
				.append("		<teacherPhoto>")
				.append(teacher_QQ287307421List.get(i).getTeacherPhoto())
				.append("</teacherPhoto>").append("\r\n")
				.append("		<teacherAddress>")
				.append(teacher_QQ287307421List.get(i).getTeacherAddress())
				.append("</teacherAddress>").append("\r\n")
				.append("		<teacherMemo>")
				.append(teacher_QQ287307421List.get(i).getTeacherMemo())
				.append("</teacherMemo>").append("\r\n")
				.append("	</Teacher_QQ287307421>").append("\r\n");
			}
			sb.append("</Teacher_QQ287307421s>").append("\r\n");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(sb.toString());
		} else if (action.equals("add")) {
			/* 添加教师信息：获取教师信息参数，参数保存到新建的教师信息对象 */ 
			Teacher_QQ287307421 teacher_QQ287307421 = new Teacher_QQ287307421();
			String teacherNumber = new String(request.getParameter("teacherNumber").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherNumber(teacherNumber);
			String teacherName = new String(request.getParameter("teacherName").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherName(teacherName);
			String teacherPassword = new String(request.getParameter("teacherPassword").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherPassword(teacherPassword);
			String teacherSex = new String(request.getParameter("teacherSex").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherSex(teacherSex);
			Timestamp teacherBirthday = Timestamp.valueOf(request.getParameter("teacherBirthday"));
			teacher_QQ287307421.setTeacherBirthday(teacherBirthday);
			Timestamp teacherArriveDate = Timestamp.valueOf(request.getParameter("teacherArriveDate"));
			teacher_QQ287307421.setTeacherArriveDate(teacherArriveDate);
			String teacherCardNumber = new String(request.getParameter("teacherCardNumber").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherCardNumber(teacherCardNumber);
			String teacherPhone = new String(request.getParameter("teacherPhone").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherPhone(teacherPhone);
			String teacherPhoto = new String(request.getParameter("teacherPhoto").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherPhoto(teacherPhoto);
			String teacherAddress = new String(request.getParameter("teacherAddress").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherAddress(teacherAddress);
			String teacherMemo = new String(request.getParameter("teacherMemo").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherMemo(teacherMemo);

			/* 调用业务层执行添加操作 */
			String result = teacher_QQ287307421DAO.AddTeacher_QQ287307421(teacher_QQ287307421);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(result);
		} else if (action.equals("delete")) {
			/*删除教师信息：获取教师信息的教师编号*/
			String teacherNumber = new String(request.getParameter("teacherNumber").getBytes("iso-8859-1"), "UTF-8");
			/*调用业务逻辑层执行删除操作*/
			String result = teacher_QQ287307421DAO.DeleteTeacher_QQ287307421(teacherNumber);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			/*将删除是否成功信息返回给客户端*/
			out.print(result);
		} else if (action.equals("updateQuery")) {
			/*更新教师信息之前先根据teacherNumber查询某个教师信息*/
			String teacherNumber = new String(request.getParameter("teacherNumber").getBytes("iso-8859-1"), "UTF-8");
			Teacher_QQ287307421 teacher_QQ287307421 = teacher_QQ287307421DAO.GetTeacher_QQ287307421(teacherNumber);

			// 客户端查询的教师信息对象，返回json数据格式, 将List<Book>组织成JSON字符串
			JSONStringer stringer = new JSONStringer(); 
			try{
			  stringer.array();
			  stringer.object();
			  stringer.key("teacherNumber").value(teacher_QQ287307421.getTeacherNumber());
			  stringer.key("teacherName").value(teacher_QQ287307421.getTeacherName());
			  stringer.key("teacherPassword").value(teacher_QQ287307421.getTeacherPassword());
			  stringer.key("teacherSex").value(teacher_QQ287307421.getTeacherSex());
			  stringer.key("teacherBirthday").value(teacher_QQ287307421.getTeacherBirthday());
			  stringer.key("teacherArriveDate").value(teacher_QQ287307421.getTeacherArriveDate());
			  stringer.key("teacherCardNumber").value(teacher_QQ287307421.getTeacherCardNumber());
			  stringer.key("teacherPhone").value(teacher_QQ287307421.getTeacherPhone());
			  stringer.key("teacherPhoto").value(teacher_QQ287307421.getTeacherPhoto());
			  stringer.key("teacherAddress").value(teacher_QQ287307421.getTeacherAddress());
			  stringer.key("teacherMemo").value(teacher_QQ287307421.getTeacherMemo());
			  stringer.endObject();
			  stringer.endArray();
			}
			catch(Exception e){}
			response.setContentType("text/json; charset=UTF-8");  //JSON的类型为text/json
			response.getOutputStream().write(stringer.toString().getBytes("UTF-8"));
		} else if(action.equals("update")) {
			/* 更新教师信息：获取教师信息参数，参数保存到新建的教师信息对象 */ 
			Teacher_QQ287307421 teacher_QQ287307421 = new Teacher_QQ287307421();
			String teacherNumber = new String(request.getParameter("teacherNumber").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherNumber(teacherNumber);
			String teacherName = new String(request.getParameter("teacherName").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherName(teacherName);
			String teacherPassword = new String(request.getParameter("teacherPassword").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherPassword(teacherPassword);
			String teacherSex = new String(request.getParameter("teacherSex").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherSex(teacherSex);
			Timestamp teacherBirthday = Timestamp.valueOf(request.getParameter("teacherBirthday"));
			teacher_QQ287307421.setTeacherBirthday(teacherBirthday);
			Timestamp teacherArriveDate = Timestamp.valueOf(request.getParameter("teacherArriveDate"));
			teacher_QQ287307421.setTeacherArriveDate(teacherArriveDate);
			String teacherCardNumber = new String(request.getParameter("teacherCardNumber").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherCardNumber(teacherCardNumber);
			String teacherPhone = new String(request.getParameter("teacherPhone").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherPhone(teacherPhone);
			String teacherPhoto = new String(request.getParameter("teacherPhoto").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherPhoto(teacherPhoto);
			String teacherAddress = new String(request.getParameter("teacherAddress").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherAddress(teacherAddress);
			String teacherMemo = new String(request.getParameter("teacherMemo").getBytes("iso-8859-1"), "UTF-8");
			teacher_QQ287307421.setTeacherMemo(teacherMemo);

			/* 调用业务层执行更新操作 */
			String result = teacher_QQ287307421DAO.UpdateTeacher_QQ287307421(teacher_QQ287307421);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(result);
		}
	}
}
