package org.uestc.uestctestmanager.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.uestc.uestctestmanager.model.CurrentRubricInfo;
import org.uestc.uestctestmanager.model.Rubric;
import org.uestc.uestctestmanager.service.impl.BadgeServiceImpl;
import org.uestc.uestctestmanager.service.impl.ClassInfoServiceImpl;
import org.uestc.uestctestmanager.service.impl.CourseServiceImpl;
import org.uestc.uestctestmanager.service.impl.RubricServiceImpl;
public class RubricServlet extends HttpServlet {
private RubricServiceImpl rubricServiceImpl = new RubricServiceImpl();
/**
* Constructor of the object.
*/
public RubricServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
if (null != method) {
if (method.equalsIgnoreCase("add")) {
add(request, response);
return;
} else if (method.equalsIgnoreCase("del")) {
delete(request, response);
return;
} else if (method.equalsIgnoreCase("list")) {
list(request, response);
return;
} else if (method.equalsIgnoreCase("update")) {
update(request, response);
return;
} else if (method.equalsIgnoreCase("get")) {
get(request, response);
return;
} else if(method.equalsIgnoreCase("out")){
out(request, response);
return;
}else if(method.equalsIgnoreCase("info")){
doOutInfo(request, response);
return;
}else if(method.equalsIgnoreCase("outsubmit")) {
doOutSubmit(request, response);
return;
}
} else {
request.getRequestDispatcher("webpage/rubric.jsp").forward(request,
response);
}
}
public void doOutSubmit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//有可能会出问题,就是在Application对象中没有获得相关的Attribute键名时会是一个null值
//null值转换成CurrentRubricInfo会抛出异常
CurrentRubricInfo currentRubricInfo = (CurrentRubricInfo) this.getServletContext().getAttribute("CURRENTRUBRICINFO");
String [] rubricIds = request.getParameterValues("headerCbx");
if (null != rubricIds) {
for (String idString : rubricIds) {
//这目前有问题,只能用于测试,添加进去的是一个空的题目对象
//得到的题目只是一个题目的唯一的ID号,要添加需要根据这个ID号去查询获得
Rubric rubric = (Rubric) rubricServiceImpl.get(idString);
currentRubricInfo.getMapRubric().put(idString, rubric);
currentRubricInfo.getMapRubric().size();
}
request.setAttribute("RUBRICCOUNT", currentRubricInfo.getMapRubric().size());
request.getRequestDispatcher("webpage/outRubricSuccess.jsp").forward(request, response);
return;
}
request.getRequestDispatcher("empty").forward(request, response);
}
public void doOutInfo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.获得用户在客户端的输入数据,保存到当前的作用域范围中
//2.按用户的输入去读取数据库中的数据,指的是用户选择了哪些课程
//3.把相关的数据返回至客户端供用户选择出题
CurrentRubricInfo currentRubricInfo = new CurrentRubricInfo();
String []courses = request.getParameterValues("course");
for (String s:courses) {
currentRubricInfo.getCourseNameList().add(s);
}
String []classNames = request.getParameterValues("classInfo");
for(String c:classNames){
currentRubricInfo.getCourseNameList().add(c);
}
//这个做法有问题(bug),有可能用户忘了选,
String timeString = request.getParameter("time");
String rubricCountString = request.getParameter("rubricCount");
Integer timeInteger=0;
Integer rubricCount = 0;
if (null != timeString) {
timeInteger = Integer.parseInt(timeString);
}
if (null != rubricCountString) {
rubricCount = Integer.parseInt(rubricCountString);
}
currentRubricInfo.setRubricCount(rubricCount);
currentRubricInfo.setTimeInteger(timeInteger);
//如何在Servlet中获得Application对象
//保存题目的信息
this.getServletContext().setAttribute("CURRENTRUBRICINFO", currentRubricInfo);
//还有很多的问题
//1.对象该保存在什么样的作用域范围中
//2。对象中的数据还需要些什么?
//3.对象是否还该具有些什么行为
List<Rubric> rubricList = rubricServiceImpl.getRubricByCourseList(courses);
request.setAttribute("RUBRICLIST", rubricList);
request.getRequestDispatcher("webpage/showoutrubric.jsp").forward(request, response);
}
public void out(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.获得班级数据
ClassInfoServiceImpl classInfoServiceImpl = new ClassInfoServiceImpl();
ArrayList<Object> classList = classInfoServiceImpl.finAll();
request.setAttribute("CLASSLIST", classList);
//2.获得考试科目的部分
CourseServiceImpl courseServiceImpl = new CourseServiceImpl();
ArrayList<Object> courseList = courseServiceImpl.finAll();
request.setAttribute("COURSELIST", courseList);
request.getRequestDispatcher("webpage/outRubric.jsp").forward(request, response);
}
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String rubricCurrent = request.getParameter("rubricCurrent");
String rubricTitle = request.getParameter("rubricTitle");
String rubricResult = request.getParameter("rubricResult");
String rubricResultCount = request.getParameter("rubricResultCount");
String badgeId = request.getParameter("badgeId");
String courseId = request.getParameter("courseId");
String rubricId = java.util.UUID.randomUUID().toString();
System.out.println(rubricCurrent + "\t" + rubricId + "\t"
+ rubricResult + "\t" + rubricResultCount + "\t" + rubricTitle
+ "\t" + badgeId + "\t" + courseId);
// 省略了输入数据的校验
Rubric rubric = new Rubric();
rubric.setBadgeId(badgeId);
rubric.setCourseId(courseId);
rubric.setRubricCurrent(rubricCurrent);
rubric.setRubricId(rubricId);
rubric.setRubricResult(rubricResult);
rubric.setRubricResultCount(Integer.parseInt(rubricResultCount));
rubric.setRubricTitle(rubricTitle);
int resultInt = rubricServiceImpl.save(rubric);
String msgInfo = "添加题目错误!";
if (resultInt > 0)