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.Student_QQ287307421DAO;
import com.mobileserver.domain.Student_QQ287307421;

import org.json.JSONStringer;

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

	/*构造学生信息业务层对象*/
	private Student_QQ287307421DAO student_QQ287307421DAO = new Student_QQ287307421DAO();

	/*默认构造函数*/
	public Student_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 studentNumber = request.getParameter("studentNumber");
			studentNumber = studentNumber == null ? "" : new String(request.getParameter(
					"studentNumber").getBytes("iso-8859-1"), "UTF-8");
			String studentName = request.getParameter("studentName");
			studentName = studentName == null ? "" : new String(request.getParameter(
					"studentName").getBytes("iso-8859-1"), "UTF-8");
			String studentClassNumber = "";
			if (request.getParameter("studentClassNumber") != null)
				studentClassNumber = request.getParameter("studentClassNumber");
			Timestamp studentBirthday = null;
			if (request.getParameter("studentBirthday") != null)
				studentBirthday = Timestamp.valueOf(request.getParameter("studentBirthday"));

			/*调用业务逻辑层执行学生信息查询*/
			List<Student_QQ287307421> student_QQ287307421List = student_QQ287307421DAO.QueryStudent_QQ287307421(studentNumber,studentName,studentClassNumber,studentBirthday);

			/*将查询的结果集通过xml格式传输给客户端*/
			StringBuffer sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\r\n")
			.append("<Student_QQ287307421s>").append("\r\n");
			for (int i = 0; i < student_QQ287307421List.size(); i++) {
				sb.append("	<Student_QQ287307421>").append("\r\n")
				.append("		<studentNumber>")
				.append(student_QQ287307421List.get(i).getStudentNumber())
				.append("</studentNumber>").append("\r\n")
				.append("		<studentName>")
				.append(student_QQ287307421List.get(i).getStudentName())
				.append("</studentName>").append("\r\n")
				.append("		<studentPassword>")
				.append(student_QQ287307421List.get(i).getStudentPassword())
				.append("</studentPassword>").append("\r\n")
				.append("		<studentSex>")
				.append(student_QQ287307421List.get(i).getStudentSex())
				.append("</studentSex>").append("\r\n")
				.append("		<studentClassNumber>")
				.append(student_QQ287307421List.get(i).getStudentClassNumber())
				.append("</studentClassNumber>").append("\r\n")
				.append("		<studentBirthday>")
				.append(student_QQ287307421List.get(i).getStudentBirthday())
				.append("</studentBirthday>").append("\r\n")
				.append("		<studentState>")
				.append(student_QQ287307421List.get(i).getStudentState())
				.append("</studentState>").append("\r\n")
				.append("		<studentPhoto>")
				.append(student_QQ287307421List.get(i).getStudentPhoto())
				.append("</studentPhoto>").append("\r\n")
				.append("		<studentTelephone>")
				.append(student_QQ287307421List.get(i).getStudentTelephone())
				.append("</studentTelephone>").append("\r\n")
				.append("		<studentEmail>")
				.append(student_QQ287307421List.get(i).getStudentEmail())
				.append("</studentEmail>").append("\r\n")
				.append("		<studentQQ>")
				.append(student_QQ287307421List.get(i).getStudentQQ())
				.append("</studentQQ>").append("\r\n")
				.append("		<studentAddress>")
				.append(student_QQ287307421List.get(i).getStudentAddress())
				.append("</studentAddress>").append("\r\n")
				.append("		<studentMemo>")
				.append(student_QQ287307421List.get(i).getStudentMemo())
				.append("</studentMemo>").append("\r\n")
				.append("	</Student_QQ287307421>").append("\r\n");
			}
			sb.append("</Student_QQ287307421s>").append("\r\n");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(sb.toString());
		} else if (action.equals("add")) {
			/* 添加学生信息：获取学生信息参数，参数保存到新建的学生信息对象 */ 
			Student_QQ287307421 student_QQ287307421 = new Student_QQ287307421();
			String studentNumber = new String(request.getParameter("studentNumber").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentNumber(studentNumber);
			String studentName = new String(request.getParameter("studentName").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentName(studentName);
			String studentPassword = new String(request.getParameter("studentPassword").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentPassword(studentPassword);
			String studentSex = new String(request.getParameter("studentSex").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentSex(studentSex);
			String studentClassNumber = new String(request.getParameter("studentClassNumber").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentClassNumber(studentClassNumber);
			Timestamp studentBirthday = Timestamp.valueOf(request.getParameter("studentBirthday"));
			student_QQ287307421.setStudentBirthday(studentBirthday);
			String studentState = new String(request.getParameter("studentState").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentState(studentState);
			String studentPhoto = new String(request.getParameter("studentPhoto").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentPhoto(studentPhoto);
			String studentTelephone = new String(request.getParameter("studentTelephone").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentTelephone(studentTelephone);
			String studentEmail = new String(request.getParameter("studentEmail").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentEmail(studentEmail);
			String studentQQ = new String(request.getParameter("studentQQ").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentQQ(studentQQ);
			String studentAddress = new String(request.getParameter("studentAddress").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentAddress(studentAddress);
			String studentMemo = new String(request.getParameter("studentMemo").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentMemo(studentMemo);

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

			// 客户端查询的学生信息对象，返回json数据格式, 将List<Book>组织成JSON字符串
			JSONStringer stringer = new JSONStringer(); 
			try{
			  stringer.array();
			  stringer.object();
			  stringer.key("studentNumber").value(student_QQ287307421.getStudentNumber());
			  stringer.key("studentName").value(student_QQ287307421.getStudentName());
			  stringer.key("studentPassword").value(student_QQ287307421.getStudentPassword());
			  stringer.key("studentSex").value(student_QQ287307421.getStudentSex());
			  stringer.key("studentClassNumber").value(student_QQ287307421.getStudentClassNumber());
			  stringer.key("studentBirthday").value(student_QQ287307421.getStudentBirthday());
			  stringer.key("studentState").value(student_QQ287307421.getStudentState());
			  stringer.key("studentPhoto").value(student_QQ287307421.getStudentPhoto());
			  stringer.key("studentTelephone").value(student_QQ287307421.getStudentTelephone());
			  stringer.key("studentEmail").value(student_QQ287307421.getStudentEmail());
			  stringer.key("studentQQ").value(student_QQ287307421.getStudentQQ());
			  stringer.key("studentAddress").value(student_QQ287307421.getStudentAddress());
			  stringer.key("studentMemo").value(student_QQ287307421.getStudentMemo());
			  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")) {
			/* 更新学生信息：获取学生信息参数，参数保存到新建的学生信息对象 */ 
			Student_QQ287307421 student_QQ287307421 = new Student_QQ287307421();
			String studentNumber = new String(request.getParameter("studentNumber").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentNumber(studentNumber);
			String studentName = new String(request.getParameter("studentName").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentName(studentName);
			String studentPassword = new String(request.getParameter("studentPassword").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentPassword(studentPassword);
			String studentSex = new String(request.getParameter("studentSex").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentSex(studentSex);
			String studentClassNumber = new String(request.getParameter("studentClassNumber").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentClassNumber(studentClassNumber);
			Timestamp studentBirthday = Timestamp.valueOf(request.getParameter("studentBirthday"));
			student_QQ287307421.setStudentBirthday(studentBirthday);
			String studentState = new String(request.getParameter("studentState").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentState(studentState);
			String studentPhoto = new String(request.getParameter("studentPhoto").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentPhoto(studentPhoto);
			String studentTelephone = new String(request.getParameter("studentTelephone").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentTelephone(studentTelephone);
			String studentEmail = new String(request.getParameter("studentEmail").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentEmail(studentEmail);
			String studentQQ = new String(request.getParameter("studentQQ").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentQQ(studentQQ);
			String studentAddress = new String(request.getParameter("studentAddress").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentAddress(studentAddress);
			String studentMemo = new String(request.getParameter("studentMemo").getBytes("iso-8859-1"), "UTF-8");
			student_QQ287307421.setStudentMemo(studentMemo);

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