package jp.ac.kcska.questionsystem.exammanager.horizon;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.ac.kcska.questionsystem.ExcuteDatabase;
import jp.ac.kcska.questionsystem.questionmanager.horizon.GetQuestionList;
import jp.ac.kcska.questionsystem.questionmanager.horizon.Mst_questionVo;

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

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; UTF-8");
		
		GetQuestionList getQuestionList = new GetQuestionList();
		ArrayList<Mst_questionVo> questionList = getQuestionList.getList();
		int id = Integer.parseInt(req.getParameter("id"));
		ArrayList<ExamquestionVo> examList = this.getList(id);
		HttpSession session = req.getSession();
		session.setAttribute("questionList", questionList);
		session.setAttribute("examList", examList);
		RequestDispatcher dispatcher = req.getRequestDispatcher("/editexam.jsp");
		dispatcher.forward(req, resp);
	}
	
	private ArrayList<ExamquestionVo> getList(int id){
		ArrayList<ExamquestionVo> list = new ArrayList<ExamquestionVo>();
		ExcuteDatabase excuteDatabase = new ExcuteDatabase();
		ResultSet resultSet = null;
		try {
			resultSet = excuteDatabase.excuteSelect("SELECT * FROM EXAMQUESTION WHERE EXAMID =" + id);
			while(resultSet.next()){
				ExamquestionVo examquestionVo = new ExamquestionVo();
				int questionid = resultSet.getInt("id");
				String categoryname = resultSet.getString("categoryname");
				String fieldname = resultSet.getString("fieldname");
				String sentence = resultSet.getString("sentence");
				int examid = resultSet.getInt("examid");
				String examname = resultSet.getString("examname");
				
				examquestionVo.setId(questionid);
				examquestionVo.setCategoryname(categoryname);
				examquestionVo.setFieldname(fieldname);
				examquestionVo.setSentence(sentence);
				examquestionVo.setExamid(examid);
				examquestionVo.setExamname(examname);
				
				list.add(examquestionVo);
			}
		} catch (SQLException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		return list;

	}
	
	
}
