package com.itjob.servlet;

import java.util.*;

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.dao.model.Role;
import com.itjob.service.*;

public class RoleServlet  extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RoleManager roleManager;
	private 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");
		roleManager = (RoleManager) ctx.getBean("roleManager");
		
		// 首先设置文档类型
		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 ("saveRole".equals(methodName)) {
				// 保存角色信息
				saveRole(request, response);
			} else if ("getRole".equals(methodName)) {
				// 获得某个角色的信息
				getRole(request, response);
			} else if ("updateRole".equals(methodName)) {
				// 更新某个角色的信息
				updateRole(request, response);
			} else if ("deleteRole".equals(methodName)) {
				// 删除一到多个角色信息
				deleteRole(request, response);
			} else if ("initUpdateRoleRight".equals(methodName)) {
				// 获得更新角色的权限信息页所需要的信息
				initUpdateRoleRight(request, response);
			} else if ("updateRoleRight".equals(methodName)) {
				// 更新角色的权限信息
				updateRoleRight(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void list(HttpServletRequest request, HttpServletResponse response) 
									throws Exception {
		List roleList = roleManager.getRoleList();
		request.setAttribute("roleList", roleList);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/roleManage/list.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void saveRole(HttpServletRequest request, HttpServletResponse response) 
									throws Exception {
		// 保存角色信息
		Role role = new Role();
		role.setRoleName(request.getParameter("roleName"));
		role.setDescription(request.getParameter("description"));
		roleManager.saveRole(role);
		
		request.setAttribute("returnPage", "/servlet/RoleServlet?methodName=list");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp");
		dispatcher.forward(request, response);
	}
	
	protected void getRole(HttpServletRequest request, HttpServletResponse response) 
									throws Exception {
		long trId = Long.parseLong(request.getParameter("trId"));
		Role role = roleManager.getRole(trId);
		request.setAttribute("role", role);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/roleManage/update.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void updateRole(HttpServletRequest request, HttpServletResponse response) 
									throws Exception {
		// 更新角色信息
		Role role = new Role();
		role.setTrId(Long.parseLong(request.getParameter("trId")));
		role.setRoleName(request.getParameter("roleName"));
		role.setDescription(request.getParameter("description"));
		roleManager.updateRole(role);
		// 跳转到成功页面
		request.setAttribute("returnPage", "/servlet/RoleServlet?methodName=list");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void deleteRole(HttpServletRequest request, HttpServletResponse response) 
									throws Exception {
		String[] idArray = request.getParameterValues("trId");
		roleManager.deleteRole(idArray);
		// 跳转到成功页面
		request.setAttribute("returnPage", "/servlet/RoleServlet?methodName=list");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void initUpdateRoleRight(HttpServletRequest request, HttpServletResponse response) 
									throws Exception {
		List<TRight> rightList = rightManager.getRightList(); // 全部权限列表
		List<TRight> selectRightList = new ArrayList<TRight>();		  // 该用户已选择的权限列表
		List<TRight> notSelRightList = new ArrayList<TRight>();		  // 该用户未选择的权限列表
		
		Role role = roleManager.getRole(Long.parseLong(request.getParameter("trId")));
		Set selectRoleSet = role.getRights();
		Iterator ite = selectRoleSet.iterator();
		while (ite.hasNext()) {
			selectRightList.add((TRight) ite.next());
		}
		
		boolean flag = false;
		if (!rightList.isEmpty()) {
			ite = rightList.iterator();
			while (ite.hasNext()) {
				flag = false;
				Map<String, String> map = (Map<String, String>) ite.next();
				TRight right = new TRight();
				right.setTrId(Long.parseLong(map.get("trId")));
				right.setRightName(map.get("rightName"));
				right.setRightUrl(map.get("rightUrl"));
				right.setDescription(map.get("description"));
				
				for (int i = 0; i < selectRightList.size(); i++) {
					TRight right2 = selectRightList.get(i);
					if (right2.getTrId() == right.getTrId()) {
						flag = true;
						break;
					}
				}
				
				if (!flag) {
					notSelRightList.add(right);
				}
			}
		}
		
		request.setAttribute("trId", request.getParameter("trId"));
		request.setAttribute("selectList", selectRightList);
		request.setAttribute("notSelectList", notSelRightList);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/roleManage/updateRoleRight.jsp");
		dispatcher.forward(request, response);  
	}
	
	protected void updateRoleRight(HttpServletRequest request, HttpServletResponse response) 
									throws Exception {
		long trId = Long.parseLong(request.getParameter("trId"));
		String rightIds = request.getParameter("rightIds");
		roleManager.updateRoleRight(trId, rightIds);
		
		request.setAttribute("returnPage", "/servlet/RoleServlet?methodName=list");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp");
		dispatcher.forward(request, response);  
	}
}
																																												