package com.itjob.servlet;

import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.itjob.dao.model.TRight;
import com.itjob.service.*;

public class RightServlet  extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RightManager rightManager;

	public void setRightManager(RightManager rightManager) {
		this.rightManager = rightManager;
	}
	
	public void init(ServletConfig config) throws ServletException {  
		// 从web.xml中获得初始化参数
		super.init(config);
	}
	
	protected void service(HttpServletRequest request, HttpServletResponse response) {
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); 
		rightManager = (RightManager) ctx.getBean("rightManager");
		
		// 首先设置文档类型
		response.setContentType("text/html; charset=UTF-8");
		String methodName = request.getParameter("methodName");
		try {
			request.setCharacterEncoding("utf-8");
			if ("list".equals(methodName)) {
				// 获得权限列表
				list(request, response);
			} else if ("saveRight".equals(methodName)) {
				// 保存权限信息
				saveRight(request, response);
			} else if ("getRight".equals(methodName)) {
				// 获得某个权限的信息
				getRight(request, response);
			} else if ("updateRight".equals(methodName)) {
				// 更新某个权限的信息
				updateRight(request, response);
			} else if ("deleteRight".equals(methodName)) {
				// 删除一到多个权限信息
				deleteRight(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List rightList = rightManager.getRightList();
		request.setAttribute("rightList", rightList);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/rightManage/list.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void saveRight(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 保存权限信息
		TRight right = new TRight();
		right.setRightName(request.getParameter("rightName"));
		right.setRightUrl(request.getParameter("rightUrl"));
		right.setDescription(request.getParameter("description"));
		rightManager.saveRight(right);
		
		request.setAttribute("returnPage", "/servlet/RightServlet?methodName=list");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp");
		dispatcher.forward(request, response);
	}
	
	protected void getRight(HttpServletRequest request, HttpServletResponse response) throws Exception {
		long trId = Long.parseLong(request.getParameter("trId"));
		TRight right = rightManager.getRight(trId);
		
		request.setAttribute("right", right);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/rightManage/update.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void updateRight(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 更新权限信息
		TRight right = new TRight();
		right.setTrId(Long.parseLong(request.getParameter("trId")));
		right.setRightName(request.getParameter("rightName"));
		right.setRightUrl(request.getParameter("rightUrl"));
		right.setDescription(request.getParameter("description"));
		rightManager.updateRight(right);
		// 跳转到成功页面
		request.setAttribute("returnPage", "/servlet/RightServlet?methodName=list");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void deleteRight(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] idArray = request.getParameterValues("trId");
		rightManager.deleteRight(idArray);
		// 跳转到成功页面
		request.setAttribute("returnPage", "/servlet/RightServlet?methodName=list");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp");
		dispatcher.forward(request, response);  
	}
}
																																												