/*************************************************** * Copyright 2014-7-23, -sinosoft.com.cn All rights reserved. *【创建日期】 : 2014-7-23 *【创建人】 : 徐杨 *【类名】:SysCustomMenuService.java *【功能】:本类是菜单角色自定义服务实现类 *【修改日志】 * -------------------------------------------------- * VERSION DATE 修改人 修改内容 * -------------------------------------------------- * 1.0 2014-7-23 * -------------------------------------------------- **************************************************/ package com.sinosoft.lz.system.menu.service; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.ProduceMime; import net.sf.json.JSONArray; import org.apache.log4j.Logger; import com.formaction.Utils; import com.formaction.dao.OperateAble; import com.formaction.dao.OperateImpl; import com.formaction.vo.Msg; import com.google.gson.Gson; import com.persistence.DbConnection; import com.persistence.service.assitant.DataObject; import com.persistence.service.assitant.Field; import com.persistence.service.assitant.generator.IdentityGenerator; import com.persistence.service.exception.PersistenceException; import com.persistence.service.exception.TransformTypeException; import com.sinosoft.common.excel.JsonPluginsUtil; import com.sinosoft.lz.system.menu.dao.ISysCustomMenuDao; import com.sinosoft.lz.system.menu.dao.SysCustomMenuDaoImpl; import com.sinosoft.lz.system.menu.vo.CustomTreeNodeVO; import com.sinosoft.lz.system.menu.vo.CustomTreeVO; import com.sinosoft.lz.system.menu.vo.IndexMenu; import com.sinosoft.lz.system.menu.vo.SysMenu; import com.sinosoft.lz.system.positiontab.service.PositionTabService; import com.sinosoft.lz.system.util.JsonUtil; import com.sinosoft.lz.system.util.tree.TreeNode; @Path("/SysCustomMenuService/") public class SysCustomMenuService { private final static Logger log = Logger.getLogger(SysCustomMenuService.class); private List showMenuList = new ArrayList(); @ProduceMime("application/json") @POST @Path("/getShowMenuInfoByRoleIds/") public String getShowMenuInfoByRoleIds(String params){ String roleids = Utils.getParameter("roleids", params) == null ? "" : Utils.getParameter("roleids", params); log.info("角色IDS-----------:"+roleids); String[] rids = null; if(roleids!=null&&!"".equals(roleids)){ rids = roleids.split(","); String ins = ""; for(int i=0;i menuList = new ArrayList(); try{ conn = dbConn.getConnection(); pstm = conn.prepareStatement(sql.toString()); for(int i=0;i> mList = new ArrayList>(); Map rcMap = new HashMap(); rcMap.put("display","日常管理"); List rcList = mDao.getIndexMenu("3", roleids); rcMap.put("value",rcList); mList.add(rcMap); Map czMap = new HashMap(); czMap.put("display","应急处置"); List czList = mDao.getIndexMenu("4", roleids); czMap.put("value",czList); mList.add(czMap); Map bgdMap = new HashMap(); bgdMap.put("display","保供电"); List bgdList = mDao.getIndexMenu("5", roleids); bgdMap.put("value",bgdList); mList.add(bgdMap); Map sysMap = new HashMap(); sysMap.put("display","系统管理"); List sysList = mDao.getIndexMenu("9", roleids); sysMap.put("value",sysList); mList.add(sysMap); String menuStr = JsonPluginsUtil.listToJson(mList); log.info(menuStr); return menuStr; } return JsonUtil.string2json(""); } @ProduceMime("application/json") @POST @Path("/queryIndexMenu/") public String queryIndexMenu(String params){ String roleids = Utils.getParameter("roleids", params) == null ? "" : Utils.getParameter("roleids", params); String menuName = Utils.getParameter("menuName", params) == null ? "" : Utils.getParameter("menuName", params); log.info("角色IDS-----------:"+roleids); if(roleids!=null&&!"".equals(roleids)){ SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl(); List> mList = new ArrayList>(); List spList = mDao.queryIndexMenu(menuName, roleids); List sbList = mDao.queryIndexSubMenu(menuName, roleids); for(int i=0;i rcMap = new HashMap(); rcMap.put("display",spMenu.getMenu_name()); List rcList = new ArrayList(); for(int j=0;j list = mDao.queryIndexMenu(user_id); result = JsonPluginsUtil.listToJson(list); }else{ String menu_name = Utils.getParameter("menu_name", params) == null ? "" : Utils.getParameter("menu_name", params); String menu_url = Utils.getParameter("menu_url", params) == null ? "" : Utils.getParameter("menu_url", params); String menu_img_url = Utils.getParameter("menu_img_url", params) == null ? "" : Utils.getParameter("menu_img_url", params); //String state = Utils.getParameter("state", params) == null ? "" : Utils.getParameter("state", params); String sort = Utils.getParameter("sort", params) == null ? "" : Utils.getParameter("sort", params); //封装一条数据 IndexMenu indexMenu = new IndexMenu(); indexMenu.setFD_OBJECTID(IdentityGenerator.getIdentityGenerator().gerenalIdentity(394)); indexMenu.setBUSINESS_URL(menu_url); indexMenu.setIMAGE_URL(menu_img_url); indexMenu.setIS_DEL("0"); indexMenu.setSTATE("0"); indexMenu.setCLICK_NUM(0); indexMenu.setTITLE_NAME(menu_name); indexMenu.setSORT(sort); indexMenu.setUSER_ID(user_id); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); indexMenu.setUPDATEDAE(sdf.format(date)); indexMenu.setMENU_ID(menu_id); result = mDao.addIndexMenu(indexMenu); if("ok".equals(result)){ List list = mDao.queryIndexMenu(user_id); result = JsonPluginsUtil.listToJson(list); } } return result; } /** * 用户工作台菜单使用频率计数 * @param params * @return */ @POST @ProduceMime("html/text") @Path("/setUserClickNum/") public String setUserClickNum(String params){ String result = "error"; SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl(); String menu_id = Utils.getParameter("menu_id", params) == null ? "" : Utils.getParameter("menu_id", params); String user_id = Utils.getParameter("user_id", params) == null ? "" : Utils.getParameter("user_id", params); result = mDao.setUserClickNum(menu_id,user_id); log.info(user_id+"===改变=====菜单:"+menu_id+"==========="+result); return result; } /** * 删除工作台菜单 * state=0为使用 state=1为移除 * @param params * @return */ @POST @ProduceMime("html/text") @Path("/deleteIndexMenu/") public String deleteIndexMenu(String params){ String result = "error"; String fd_id = Utils.getParameter("fd_id", params) == null ? "" : Utils.getParameter("fd_id", params); SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl(); result = mDao.deleteAddMenu(fd_id); return result; } /** * 检查工作台菜单表中是否已存在该菜单,若存在则将其状态state改成1 * 即使用状态 * @param menu_id * @param user_id * @return */ private boolean checkAddMenu(String user_id, String menu_id) { boolean flag = false; int result = 0; SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl(); result = mDao.checkAddMenu(user_id,menu_id); if(result==1){ flag = true; } return flag; } /** * 查询个人偏好菜单 * @param params * @return */ @POST @ProduceMime("application/json") @Path("/queryIndexMenuByroleId/") public String queryIndexMenuByroleId(String params){ String user_id = Utils.getParameter("user_id", params) == null ? "" : Utils.getParameter("user_id", params); String moduleStr = "{\"state\":\"ok\",\"length\":0,\"resultData\":\"[]\"}"; if(user_id!=null&&!"".equals(user_id)){ SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl(); List result = mDao.queryIndexMenu(user_id); int len = result.size(); String resultStr = JsonPluginsUtil.listToJson(result); moduleStr = "{\"state\":\"ok\",\"length\":"+len+",\"resultData\":"+resultStr+"}"; } log.info(user_id+"==工作台菜单查询==:"+moduleStr); return moduleStr; } /** * 功能:根据角色查询出功能菜单树的信息 * @param roleid 角色编号 */ @SuppressWarnings("unchecked") @POST @ProduceMime("application/json") @Path("/getMenuTreeByRoleId/") public String getMenuTreeByRoleId(String params){ //获取角色id String roleid = Utils.getParameter("roleid", params) == null ? "" : Utils.getParameter("roleid", params); String isAdmin = Utils.getParameter("isAdmin", params) == null ? "" : Utils.getParameter("isAdmin", params); ISysCustomMenuDao dao = new SysCustomMenuDaoImpl(); //如果角色值为ADMIN_ROLEID常量的值,则表明是管理员角色,查出所有权限 List> list = null; //查询左边树数据 if("1".equals(isAdmin)){ list = dao.loadMenuInfo(); log.info("-- "+JSONArray.fromObject(list).toString()); return JSONArray.fromObject(list).toString().toLowerCase(); }else{ list = dao.loadMenuInfoByRoleId(roleid); //处理数据 Set set = processData(list); log.info("--- "+JSONArray.fromObject(set).toString()); return JSONArray.fromObject(set).toString().toLowerCase(); } } /** * 功能:根据角色查询出自定义菜单显示树的信息 * @param roleid 角色编号 */ @SuppressWarnings("unchecked") @POST @ProduceMime("application/json") @Path("/getCustomTreeByRoleId/") public String getCustomTreeByRoleId(String params){ //获取角色id String roleid = Utils.getParameter("roleid", params) == null ? "" : Utils.getParameter("roleid", params); log.info("----- "+roleid); ISysCustomMenuDao dao = new SysCustomMenuDaoImpl(); List> list = null; //查询右边树数据 list = dao.loadCustomMenuInfoByRoleId(roleid); log.info(JSONArray.fromObject(list).toString()); return JSONArray.fromObject(list).toString().toLowerCase(); } /** * 功能:根据角色复制菜单权限 * @param params */ @POST @ProduceMime("application/json") @Path("/grantMenu") public Msg grantMenu(String params){ Gson gson = new Gson(); ISysCustomMenuDao dao = new SysCustomMenuDaoImpl(); OperateAble persistence = new OperateImpl(); DataObject dataObject = null; Msg msg = null; log.info("params--- "+params); try { params = java.net.URLDecoder.decode(params, "utf-8"); CustomTreeVO customTree = gson.fromJson(params, CustomTreeVO.class); //保存数据之前,先删除数据 dao.clearCustomMenuByRoleId(customTree.getRoleid()); //模型方式保存数据 for(CustomTreeNodeVO ct : customTree.getNodes()){ dataObject = new DataObject(); /* dataObject.setClassid(892); //为保证SYS_CUSTOM_MENU表中的id和pid的值不与实际功能菜单中的id和pid的值冲突,故要做规则处理 dataObject.addAttribute(new Field("id", ct.getId())); dataObject.addAttribute(new Field("pid", "null".equals(ct.getPid()) ? "10" : ct.getPid())); dataObject.addAttribute(new Field("roleid", customTree.getRoleid())); dataObject.addAttribute(new Field("name", "null".equals(ct.getName()) ? "" : ct.getName())); dataObject.addAttribute(new Field("sort", "null".equals(ct.getSort()) ? "" : ct.getSort())); dataObject.addAttribute(new Field("type", "null".equals(ct.getType()) ? "" : ct.getType())); dataObject.addAttribute(new Field("handler", "null".equals(ct.getHandler()) ? "" : ct.getHandler())); //如果handler和type值都为”1“, 则表明该菜单项是可显示的功能菜单,所有要关联实际功能菜单的id值 if("1".equals(ct.getHandler()) && "1".equals(ct.getType())){ dataObject.addAttribute(new Field("menuid", ct.getId())); }*/ dataObject.setClassid(886); //为保证SYS_CUSTOM_MENU表中的id和pid的值不与实际功能菜单中的id和pid的值冲突,故要做规则处理 // dataObject.addAttribute(new Field("FD_OBJECTID", customTree.getRoleid()+ct.getId())); dataObject.addAttribute(new Field("ROLE_ID", customTree.getRoleid())); dataObject.addAttribute(new Field("MENU_ID", ct.getId())); dataObject.addAttribute(new Field("IS_DEL", "0")); log.info("-------------"); msg = persistence.createDataObject(dataObject); } } catch (UnsupportedEncodingException e) { log.error(e); } catch (TransformTypeException e) { log.error(e); } catch (PersistenceException e) { log.error(e); } return msg; } /** * 功能:根据角色获取显示菜单数据 * @return */ @POST @ProduceMime("application/json") @Path("/getShowMenuInfoByRoleId") public String getShowMenuInfoByRoleId(String params){ String roleid = Utils.getParameter("roleid", params) == null ? "" : Utils.getParameter("roleid", params); String nodeId = Utils.getParameter("nodeid", params) == null ? "10" : Utils.getParameter("nodeid", params); ISysCustomMenuDao dao = new SysCustomMenuDaoImpl(); List retList = new ArrayList(); //把菜单数据全部读到内存中 showMenuList = dao.loadShowMenuInfoByRoleId(roleid); retList = findChildren(nodeId); log.info(JSONArray.fromObject(retList).toString().toLowerCase()); return JSONArray.fromObject(retList).toString(); }; /** * 功能:根据权限过滤功能菜单 * @param list 源数据集 * @return Set 结果数据集 */ private Set processData(List> list) { Set> set = new HashSet>(); if(list != null){ for(int i = 0, len = list.size(); i < len; i++){ Map entry = list.get(i); String value = entry.get("menuid"); if(value == null) continue; if("".equals(value)) continue; if("null".equals(value)) continue; set.add(entry); this.findParent(set, list, entry); } } return set; } /** * 功能:递归查询父节点 * @param list * @param srcList * @param value */ private void findParent(Set> list, List> srcList, Map value){ for(int i = 0, len = srcList.size(); i < len; i++){ Map entry = srcList.get(i); if(value.get("pid").equals(entry.get("id"))){ list.add(entry); this.findParent(list, srcList, entry); } } } /*** * 递归查询显示菜单的树形结构数据 * 算法思路:先把所有的数据查询出来并放到内存中,再进行递归查询 * @param nodeId 当前节点的id * @return */ private List findChildren(String nodeId){ List list = new ArrayList(); if(showMenuList.size() > 0){ for(int i = 0, len = showMenuList.size(); i < len; i++){ Map entry = (Map)showMenuList.get(i); //获取pid String pid = (String)entry.get("PID"); //获取id String id = (String)entry.get("ID"); //如果该条数据等于pid,则加入集合当中 if(nodeId.equals(pid)){ entry.put("leaf", (this.isLeaf(id))); entry.put("children", findChildren(id)); list.add(entry); } } } return list; } private boolean isLeaf(String nodeId){ boolean flag = true; int n = 0; for(int i = 0, len = showMenuList.size(); i < len; i++){ Map entry = (Map)showMenuList.get(i); //获取pid String pid = (String)entry.get("PID"); //如果该条数据等于pid,则加入集合当中 if(pid.equals(nodeId)){ n++; } } if (n > 0) { flag = false; } return flag; } }