package com.lgx8.right.action;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.lgx8.common.PageArgument;
import com.lgx8.common.PageList;
import com.lgx8.common.servlet.BaseController;
import com.lgx8.common.util.Constants;
import com.lgx8.common.util.tag.authority.AuthorityControl;
import com.lgx8.right.dao.IModuleDao;
import com.lgx8.right.dao.IRoleDao;
import com.lgx8.right.entities.Module;
import com.lgx8.right.entities.Role;
import com.lgx8.right.entities.RunTimeUser;
import com.lgx8.right.service.IRoleService;
@Controller
public class RoleController extends BaseController {
@Resource(name="roleService")
IRoleService roleService;
@Resource(name="moduleDao")
IModuleDao moduleDao;
@Resource(name="roleDao")
IRoleDao roleDao;
@RequestMapping(value="/pageQuery.do")
public ModelAndView queryRolesByPage(HttpServletRequest request,HttpServletResponse response){
String roleName = request.getParameter("roleName");
RunTimeUser curUser = RunTimeUser.getRunTimeUser(request);
PageArgument pageArgment = getPageInfo(request);
String hql = "";
Object[] o = null;
if(null==roleName||"".equals(roleName.trim())){
hql = "from Role r where r.uid = ?";
o = new Object[]{curUser.getId()};
}else{
hql = "from Role r where r.name like ? and r.uid = ?";
o = new Object[]{"%"+roleName+"%",curUser.getId()};
}
PageList pageList = roleService.queryRolesByPage(pageArgment, hql, o);
printJSON(response, pageList.toJSON());
return null;
}
/**
* 角色编辑菜单树
* @param request
* @param response
*/
@RequestMapping(value="/authzTree.do")
public void authzTree(HttpServletRequest request,HttpServletResponse response){
List<Module> modules = Constants.getMenuList();
//获取当前登录用户权限
RunTimeUser user = RunTimeUser.getRunTimeUser(request);
String mids = (String) request.getSession().getAttribute("checkedModule");
if(mids==null)mids = "";
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().write(getXML(modules,","+mids+"," ,user));
response.getWriter().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping(value="/modifyRole.do")
public ModelAndView modifyRole(HttpServletRequest request, HttpServletResponse response){
ModelAndView view = new ModelAndView("/management/systemRole/editrole");
request.getSession().removeAttribute("rid");
String id = request.getParameter("id");
request.getSession().setAttribute("rid", id);
Role role = roleDao.findRole("id", Integer.parseInt(id));
request.setAttribute("role", role);
return view;
}
public String getXML(List<Module> modules,String mids,RunTimeUser user) throws UnsupportedEncodingException{
StringBuffer xml = new StringBuffer(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<tree id=\"0\">");
getXML(xml,modules, mids,user);
xml.append("</tree>");
return xml.toString();
}
private void getXML(StringBuffer xml,List<Module> modules,String mids,RunTimeUser user) throws UnsupportedEncodingException{
for(Module m : modules) {
//过滤授权用户不能管理的权限
if (!AuthorityControl.isAuthorized(user, m.getId())) {
continue;
}
xml.append("<item text=\""
+ m.getName()
+ "\" id=\""
+ m.getId());
xml.append("\" im0=\"tombs.gif\" im1=\"tombs.gif\" im2=\"iconSafe.gif\"");
if (mids.indexOf(("," + m.getId() + ",")) >= 0) {
if(isAllSubMenuChecked(m.getModules(), mids)){
xml.append(" checked=\"1\"");
}
}
xml.append(">");
xml.append("<userdata name=\"level\">" + m.getLevel()
+ "</userdata>");
xml.append("<userdata name=\"url\"><![CDATA[" + m.getUrl()
+ "]]></userdata>");
getXML(xml, m.getModules(),mids,user);
xml.append("</item>");
}
}
/**
* @method isAllSubMenuChecked: 判断菜单下的子菜单是否都被选中
* @Date 2011/2011-11-4
* @param list 菜单集合
* @param checked 被选中的菜单id串
* @return true全部被选中,false有没被选中的
*/
private boolean isAllSubMenuChecked(List<Module> list, String checked) {
boolean isChecked = true;
for (Module menu : list) {
if (checked.indexOf(("," + menu.getId() + ",")) >= 0) {
isChecked = isAllSubMenuChecked(menu.getModules(), checked);
if (!isChecked) {
break;
}
} else {
isChecked = false;
break;
}
}
return isChecked;
}
@RequestMapping(value="/roleEdit.do")
public ModelAndView roleEdit(HttpServletRequest request, HttpServletResponse response) {
RunTimeUser user = RunTimeUser.getRunTimeUser(request);
String roleName = request.getParameter("name");
String modules = request.getParameter("module");
String id = request.getParameter("roleId");
Role role = new Role();
role.setId(Integer.parseInt(id));
role.setMids(modules);
role.setName(roleName);
role.setUid(user.getId());
role.setCreateTime(new Date());
roleDao.updateRole(role);
return new ModelAndView(new RedirectView("page/management/systemRole/rolelist.jsp"));
}
@RequestMapping(value="/forAdd.do")
public ModelAndView forAdd(HttpServletRequest request, HttpServletResponse response) {
request.getSession().removeAttribute("checkedModule");
return new ModelAndView("/management/systemRole/addrole");
}
@RequestMapping(value="/roleSave.do")
public ModelAndView roleSave(HttpServletRequest request, HttpServletResponse response) {
RunTimeUser user = RunTimeUser.getRunTimeUser(request);
String roleName = request.getParameter("name");
String modules = request.getParameter("module");
Role role = new Role();
role.setMids(modules);
role.setName(roleName);
role.setUid(user.getId());
role.setCreateTime(new Date());
roleDao.createRole(role);
return new ModelAndView(new RedirectView("page/management/systemRole/rolelist.jsp"));
}
@RequestMapping(value="/roleDel.do")
public ModelAndView roleDel(HttpServletRequest request,HttpServletResponse response) {
//String keyid= request.getParameter("keyid");
//准备删除的角色列表
String[] delRoleIds = this.getKeyStoreArray(request);
if (delRoleIds.length > 0) {
for (int i = 0 ; i < delRoleIds.length ; i++) {
roleService.ForceDelRole(Integer.parseInt(delRoleIds[i]));
}
}
this.clearKeyStore(request);
return new ModelAndView(new RedirectView("page/management/systemRole/rolelist.jsp"));
}
}