package com.sinosoft.lz.system.right.role.service; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.ProduceMime; import org.apache.log4j.Logger; import com.formaction.Utils; import com.formaction.vo.Msg; import com.persistence.DbConnection; import com.persistence.service.JDBCHelper; import com.persistence.service.PersistenceFactory; import com.persistence.service.SysPersistence; import com.persistence.service.assitant.generator.CodeGenerator; import com.persistence.service.exception.PersistenceException; import com.sinosoft.lz.system.function.busi.FunctionBean; import com.sinosoft.lz.system.function.vo.FunctionButtonVO; import com.sinosoft.lz.system.function.vo.FunctionVO; import com.sinosoft.lz.system.right.role.busi.RoleBean; import com.sinosoft.lz.system.right.role.busi.SysRoleRightRelBean; import com.sinosoft.lz.system.right.role.dao.RoleDAO; import com.sinosoft.lz.system.right.role.vo.RoleVO; import com.sinosoft.lz.system.right.role.vo.SysRoleRightRelVO; import com.sysmodel.datamodel.xmlmodel.ModelFactory; import com.sysmodel.datamodel.xmlmodel.able.MdpModule; import com.sysmodel.datamodel.xmlmodel.able.SysModel; import com.sysmodel.datamodel.xmlmodel.impl.MdpModuleImpl; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @Path("/RoleService/") public class RoleService{ private Logger log = Logger.getLogger(this.getClass()); // 编码解析 protected String decode(String params) { try { params = java.net.URLDecoder.decode(params, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return params; } /** * 所有按钮列表服务 * * @param params * @return * @throws Exception */ @SuppressWarnings("unchecked") @ProduceMime("application/json") @POST @Path("/getRoleList") public String getRoleList() throws Exception { StringBuffer sb = new StringBuffer(); String sql = "select * from SYS_Role where role_type='0'"; RoleBean roleBean = new RoleBean(); Collection coll = roleBean.getSysRole(sql); sb.append("["); Iterator it = coll.iterator(); while (it.hasNext()) { RoleVO vo = it.next(); sb.append("{\"FD_OBJECTID\":\"" + vo.getFdObjectid() + "\",\"ROLE_TYPE\":\"" + vo.getIsDel() + "\",\"ROLE_ID\":\"" + vo.getRoleId() + "\""); sb.append(",\"ROLE_NAME\":\"" + vo.getRoleName() + "\"},"); } if (sb.lastIndexOf(",") > -1) { sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("]"); log.info("json:" + sb); return sb.toString(); } /** * 获取角色名称和id,用于下拉框 * 格式如:{"list":[{"value":"1","key":"人身事故"},{"value":"2","key":"电网事故"},{"value":"3","key":"设备事故"},{"value":"4","key":"防风防汛"}]} * @param params * @return * @throws Exception */ @SuppressWarnings("unchecked") @ProduceMime("application/json") @POST @Path("/getRoleKVs") public String getRoleKVs() throws Exception { StringBuffer sb = new StringBuffer(); String sql = "select role_id,role_name from SYS_Role "; Connection conn = null; Statement stat = null; ResultSet rs = null; DbConnection db = new DbConnection(); try { conn = db.getConnection(); stat = conn.createStatement(); rs = stat.executeQuery(sql); sb.append("{\"list\":["); while (rs.next()) { String roleId = (String) rs.getString("role_id") == null ? "": rs.getString("role_id"); String roleName = (String) rs.getString("role_name") == null ? "" : rs.getString("role_name"); sb.append("{\"value\":\"" + roleId + "\",\"key\":\"" + roleName + "\"},"); } if (sb.lastIndexOf(",") > -1) { sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("]}"); } catch (SQLException e) { this.log.error(e.getMessage(), e); throw new ClassNotFoundException("取得RoleKVs集合" + sql, e); } finally { db.close(rs); db.close(stat); db.close(conn); } log.info("json:" + sb); return sb.toString(); } /** * 获取所有功能点 * * @return * @throws Exception */ @ProduceMime("application/json") @POST @Path("/getRoleButtonList/{roleId}") public String getFunctionList(@PathParam("roleId") String roleId) throws Exception { StringBuffer sb = new StringBuffer(); sb.append("["); sb.append(getTreeJson(roleId)); if (sb.lastIndexOf(",") > -1) { sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("]"); log.info("json:" + sb); return sb.toString(); } private StringBuffer getTreeJson(String roleId) throws ClassNotFoundException { Connection conn = null; Statement stat = null; ResultSet rs = null; DbConnection db = new DbConnection(); StringBuffer sb = new StringBuffer(); SysModel sysmodel = ModelFactory.getSysmodel(); ArrayList mdplist = sysmodel.getMdpModules(); String sql = "select module_id from SYS_function group by module_id"; FunctionBean functionBean = new FunctionBean(); try { conn = db.getConnection(); stat = conn.createStatement(); rs = stat.executeQuery(sql); while (rs.next()) { String[] moduleCode = rs.getString(1).split("_"); MdpModule mdp = sysmodel.getMdpModuleByCode(moduleCode[1]); for (MdpModuleImpl mdps : mdplist) { if (moduleCode[1].equals(mdps.getCode()) && mdps.getPcode().equals("")) { sb.append("{\"module_id\":\"" + mdp.getName() + "\",\"id\":\"" + mdp.getCode() + "\",\"state\":\"closed\"},"); } else { if (moduleCode[1].equals(mdps.getCode())) { sb.append("{\"module_id\":\"" + mdp.getName() + "\",\"id\":\"" + mdp.getCode() + "\",\"state\":\"closed\""); // sb.append("\"caozuo\":\"全选\""); ArrayList functionVO = (ArrayList) functionBean .getSysFunction("select * from SYS_function where module_id='" + rs.getString(1) + "'"); sb.append(",\"children\":["); for (FunctionVO fvo : functionVO) { String[] fmoduleCode = fvo.getModuleId().split("_"); MdpModule fmdp = sysmodel.getMdpModuleByCode(fmoduleCode[1]); sb.append("{\"fd_objectid\":\"" + fvo.getFdObjectid() + "\",\"id\":\"" + fvo.getId() + "\",\"name\":\"" + fvo.getName() + "\",\"module_id\":\"" + fmdp.getName() + "\""); // sb.append(",\"caozuo\":\"全选\""); ArrayList fbvList = (ArrayList) functionBean .getSysFunctionButton("select * from SYS_FUNCTION_BUTTON where fun_id='" + fvo.getFdObjectid() + "'"); sb.append(",\"button\":\""); for (FunctionButtonVO fbv : fbvList) { // sb.append(""); sb.append(""); sb.append(fbv.getName() + " "); } sb.append("" + "\",\"state\":\"open\""); sb.append("},"); } if (sb.lastIndexOf(",") > -1) { sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("]"); sb.append("},"); } } } } } catch (SQLException e) { log.error(e.getMessage(), e); throw new ClassNotFoundException("DAO Layou: 取得SysFunctionVO集合" + sql, e); } finally { db.close(rs); db.close(stat); db.close(conn); } return sb; } @SuppressWarnings("rawtypes") private boolean isValueInVector(String value, ArrayList rsVec) { String[] temp = null; if (rsVec != null) { for (int i = 0; i < rsVec.size(); i++) { temp = (String[]) rsVec.get(i); temp[0] = temp[0] == null ? "" : temp[0]; if (value.equals(temp[0])) return true; } } return false; } @SuppressWarnings("rawtypes") public String getChecked(String value, String roleId) { String sql = "select Right_ID FROM SYS_ROLE_RIGHT_REL WHERE Role_ID='" + roleId + "'"; SysModel sysmodel = ModelFactory.getSysmodel(); SysPersistence persistence = PersistenceFactory.getInstance(sysmodel); ArrayList rsVec; try { rsVec = persistence.getSearchResult(886, sql); if (isValueInVector(value, rsVec)) { return "checked"; } } catch (PersistenceException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } /** * 按钮的新增 选择按钮的入库,修改 所选择的按钮的展示 * * @throws ClassNotFoundException */ @POST @Path("/saveRole") public String saveRole(String params) throws ClassNotFoundException { String msg = ""; params = decode(params); log.info(params); RoleBean roleBean = new RoleBean(); String name = Utils.getParameter("role_name", params) == null ? "" : Utils.getParameter( "role_name", params); String role_type = Utils.getParameter("role_type", params) == null ? "" : Utils .getParameter("role_type", params); String deptId = Utils.getParameter("deptId", params) == null ? "" : Utils.getParameter( "deptId", params); String roleId = CodeGenerator.produceIdentity(881); RoleVO vo = new RoleVO(); vo.setRoleId(roleId); vo.setRoleName(name); // vo.setRoleDescription(name); // vo.setRoleState(1); // vo.setRoleType(role_type); // vo.setCreateUnit(deptId); boolean flag = roleBean.addSysRole(vo); if (!flag) { msg = "{\"success\":" + new Boolean(true).toString() + "}"; } else { msg = "{\"success\":" + new Boolean(false).toString() + "}"; } log.info(msg); return msg; } /** * 按钮的修改 选择按钮的入库,修改 所选择的按钮的展示 * * @throws ClassNotFoundException */ @POST @Path("/updateRole") public String updateRole(String params) throws ClassNotFoundException { String msg = ""; log.info(params); params = decode(params); String fd_objectid = Utils.getParameter("fd_objectid", params) == null ? "" : Utils .getParameter("fd_objectid", params); String name = Utils.getParameter("role_name", params) == null ? "" : Utils.getParameter( "role_name", params); RoleBean roleBean = new RoleBean(); RoleVO vo = roleBean.getRoleVO(fd_objectid); vo.setRoleName(name); // vo.setRoleDescription(name); boolean flag = roleBean.modifySysRole(vo); log.info(flag); if (flag) { msg = "{\"success\":" + new Boolean(true).toString() + "}"; } else { msg = "{\"success\":" + new Boolean(false).toString() + "}"; } return msg; } /** * 按钮的删除 选择按钮的入库,修改 所选择的按钮的展示 * * @throws ClassNotFoundException */ @POST @Path("/deleteRole") public String deleteRole(String params) throws ClassNotFoundException { String msg = ""; params = decode(params); RoleBean roleBean = new RoleBean(); String roleId = Utils.getParameter("roleId", params) == null ? "" : Utils.getParameter( "roleId", params); boolean flag = roleBean.delSysRole(roleId); log.info(flag); if (flag) { msg = "{\"success\":" + new Boolean(true).toString() + "}"; } else { msg = "{\"success\":" + new Boolean(false).toString() + "}"; } return msg; } @POST @Path("/saveRoleButton") public String saveRoleButton(String params) throws ClassNotFoundException { params = decode(params); log.info(params); String msg = ""; String roleId = Utils.getParameter("roleId", params) == null ? "" : Utils.getParameter( "roleId", params); String roleType = Utils.getParameter("roleType", params) == null ? "" : Utils.getParameter( "roleType", params); String buttonStr = Utils.getParameter("selected", params) == null ? "" : Utils .getParameter("selected", params); SysRoleRightRelBean roleBean = new SysRoleRightRelBean(); SysRoleRightRelVO relvo = roleBean.getSysRoleRightRelVO(roleId); if (relvo != null) { roleBean.delSysRoleRightRel(roleId); } SysRoleRightRelVO vo = null; ArrayList voList = new ArrayList(); String[] str = buttonStr.split(","); for (int i = 0; i < str.length; i++) { vo = new SysRoleRightRelVO(); vo.setRoleId(roleId); vo.setRoleType(roleType); vo.setRightId(str[i]); voList.add(vo); } int[] num = roleBean.addSysRoleRightRel(voList); if (num.length > 0) { msg = "{\"success\":" + new Boolean(true).toString() + "}"; } else { msg = "{\"success\":" + new Boolean(false).toString() + "}"; } log.info(msg); return msg; } /** * 功能:根据角色类型查询所有角色 * * @param params * @return * @throws Exception */ @SuppressWarnings("rawtypes") @ProduceMime("application/json") @POST @Path("/getRoleInfo") public String getRoleInfo(String params) { // 角色类型 String result = null; RoleDAO dao = new RoleDAO(); List list = dao.loadRoleInfoByCondition(); result = "{\"rows\":" + JSONArray.fromObject(list).toString() + "}"; log.info(result); return result; } /** * 功能:获取角色菜单多选下拉框 * * @param params * @return * @throws Exception */ @ProduceMime("application/json") @POST @Path("/getMenuTreeRole") public String getMenuTreeRole(String params){ log.info(params); // String[] condition = node.split("node="); String node = Utils.getParameter("id", params)== null ? "0" : Utils.getParameter("id", params); String role_id = Utils.getParameter("role_id", params)== null ? "1" : Utils.getParameter("role_id", params); JDBCHelper jdbc = new JDBCHelper(); jdbc.begin(); ArrayList list = new ArrayList(); StringBuffer buf=new StringBuffer(); buf.append("select distinct m4.menu_id,m4.parent_menu_id,m4.menu_name,m4.fd_objectid,m3.is_parent from \n"); buf.append("( select m1.menu_id menu_id,m1.parent_menu_id,m1.menu_name,m2.fd_objectid \n"); buf.append("from SYS_MENU m1 left join SYS_ROLE_RIGHT_REL m2 on m1.menu_id=m2.menu_id and m2.is_del = '0' and m2.role_id='"+role_id+"' \n"); buf.append(") m4 left join ( select parent_menu_id is_parent from sys_menu where is_del='0' group by parent_menu_id ) m3 on m4.menu_id=m3.is_parent \n"); log.info(buf.toString()); try { list = jdbc.getSearchResult(99, buf.toString()); }catch(Exception e){ e.printStackTrace(); jdbc.rollback(); }finally{ if(jdbc!=null){ jdbc.release(); } } StringBuffer json = new StringBuffer(); json.append("{treeNodes:["); String[] temp = null; String code = ""; String parentcode = ""; String fd_RightName = ""; String flag = "true"; String checked = ""; String isparent=""; if(list.size()>0){ for (int i = 0 ; i< list.size() ; i++){ temp = list.get(i); code = temp[0]; parentcode = temp[1]; fd_RightName = temp[2]; checked=temp[3]; isparent=temp[4]; if(isparent!=null){ flag="true"; }else{ flag="false"; } json.append("{"); json.append("id:'"+code+"',parentId:'"+parentcode+"',"); json.append("isParent:'"+flag+"',"); json.append("name:'"+fd_RightName+"',"); json.append("icon:'/nwyj/images/system/button/toc_closed.gif'"); if(checked!=null){ json.append(",checked:'true'"); } json.append("},"); } } if(json.lastIndexOf(",") > -1){ json.deleteCharAt(json.lastIndexOf(",")); } json.append("]}"); //log.info(JSONObject.fromObject(json.toString()).toString()); System.out.println(JSONObject.fromObject(json.toString()).toString()); return JSONObject.fromObject(json.toString()).toString(); } /** * 功能:系统搜索模块单选下拉框 * * @param params * @return * @throws Exception */ @ProduceMime("application/json") @POST @Path("/getSearchTree") public String getSearchTree(){ JDBCHelper jdbc = new JDBCHelper(); jdbc.begin(); ArrayList list = new ArrayList(); StringBuffer buf=new StringBuffer(); buf.append(" select * from ( ( select menu_id,menu_name ,parent_menu_id from sys_menu where length(menu_id)=5 ) t1 left join \n"); buf.append(" (select parent_menu_id is_parent from sys_menu group by parent_menu_id ) t2 on t1.menu_id=t2.is_parent ) \n"); log.info(buf.toString()); try { list = jdbc.getSearchResult(99, buf.toString()); }catch(Exception e){ e.printStackTrace(); jdbc.rollback(); }finally{ if(jdbc!=null){ jdbc.release(); } } StringBuffer json = new StringBuffer(); json.append("{treeNodes:["); String[] temp = null; String menu_id = ""; String menu_name = ""; String parent_menu_id; String isparent=""; String flag="false"; if(list.size()>0){ for (int i = 0 ; i< list.size() ; i++){ temp = list.get(i); menu_id = temp[0]; menu_name = temp[1]; parent_menu_id=temp[2]; isparent = temp[3]; if(isparent!=null){ flag="true"; }else{ flag="false"; } json.append("{"); json.append("id:'"+menu_id+"',parentId:'"+parent_menu_id+"',"); json.append("isParent:'"+flag+"',"); json.append("name:'"+menu_name+"',"); json.append("icon:'/nwyj/images/system/button/toc_closed.gif'"); json.append("},"); } } if(json.lastIndexOf(",") > -1){ json.deleteCharAt(json.lastIndexOf(",")); } json.append("]}"); log.info(JSONObject.fromObject(json.toString()).toString()); return JSONObject.fromObject(json.toString()).toString(); } /** * 保存菜单与角色的关系 * @param params * @return */ @ProduceMime("application/json") @POST @Path("/saveMenuRoleRel") public Msg saveMenuRoleRel(String params){ //按钮id String buttonIds=Utils.getParameter("buttonIds", params)== null ? "" : Utils.getParameter("buttonIds", params); log.info(buttonIds); //角色id String roleId=Utils.getParameter("roleId", params)== null ? "" : Utils.getParameter("roleId", params); //删除该角色已有的按钮分配信息 String sql = "delete from SYS_ROLE_RIGHT_REL where ROLE_ID='"+roleId+"'"; //返回信息 Msg msg=new Msg(); JDBCHelper jdbc=new JDBCHelper(); jdbc.begin(); msg.setSucsess("true"); ArrayList list=new ArrayList(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 try { jdbc.executeUpdateSQL(sql);//执行删除sql //拆分按钮id String[] s = buttonIds.split("#"); for(String str:s){ //插入语句 sql="insert into SYS_ROLE_RIGHT_REL values('"+roleId+"','"+roleId+"','"+str+"','0','"+df.format(new Date())+"')"; //将插入语句放入list,批量保存 list.add(sql); } //执行批量保存方法 jdbc.executeUpdateSQL(list); } catch (PersistenceException e) { // TODO Auto-generated catch block e.printStackTrace(); //事务回滚 jdbc.rollback(); //输出错误信息 log.error(e.getMessage(),e); //返回错误信息 msg.setSucsess("error"); } jdbc.commit(); return msg; } @ProduceMime("application/json") @POST @Path("/getOrgTree") public String getOrgTree(String params){ log.info(params); String node = Utils.getParameter("id", params)== null ? "1" : Utils.getParameter("id", params); JDBCHelper jdbc = new JDBCHelper(); jdbc.begin(); ArrayList list = new ArrayList(); String sql = "select dept_id ,parent_id , dept_name from sys_department where parent_id = '"+node+"'"; log.info(sql); try { list = jdbc.getSearchResult(99, sql); }catch(Exception e){ e.printStackTrace(); jdbc.rollback(); }finally{ if(jdbc!=null){ jdbc.release(); } } StringBuffer json = new StringBuffer(); json.append("{treeNodes:["); String[] temp = null; String code = ""; String parentcode = ""; String fd_RightName = ""; String flag = "true"; if(list.size()>0){ for (int i = 0 ; i< list.size() ; i++){ temp = list.get(i); code = temp[0]; parentcode = temp[1]; fd_RightName = temp[2]; if(!isOrgLeaf(code)){ flag="false"; }else{ flag="true"; } json.append("{"); json.append("id:'"+code+"',parentId:'"+parentcode+"',"); json.append("isParent:'"+flag+"',"); json.append("name:'"+fd_RightName+"',"); json.append("icon:'/nwyj/scripts/qui/libs/icons/home.gif'"); json.append("},"); } } if(json.lastIndexOf(",") > -1){ json.deleteCharAt(json.lastIndexOf(",")); } json.append("]}"); log.info(JSONObject.fromObject(json.toString()).toString()); return JSONObject.fromObject(json.toString()).toString(); } //判定是否有子节点 public boolean isOrgLeaf (String node){ boolean flag = false; SysModel sysmodel = ModelFactory.getSysmodel(); SysPersistence persistence = PersistenceFactory.getInstance(sysmodel); ArrayList childList = new ArrayList(); String sql = "select dept_id from sys_department where parent_id = '" + node + "'"; // log.debug("判断是否有子节点:"+sql); try{ childList = persistence.getSearchResult(99,sql); }catch(PersistenceException e){ e.printStackTrace(); } if(childList.size() > 0){ flag = true; } return flag; } //删除角色,包括角色表、角色按钮关联表、角色菜单关联表 @ProduceMime("application/json") @POST @Path("/deleteRoleID") public String deleteRoleID(String params){ String flag="false"; try { params=new String(params.getBytes("ISO8859-1"),"UTF-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String[] strs=params.split(","); DbConnection dbConn = new DbConnection(); Connection conn = null; PreparedStatement pstm1 = null; PreparedStatement pstm2 = null; PreparedStatement pstm3 = null; String SQL1="delete from sys_role where fd_objectid = ?"; String SQL2="delete from SYS_ROLE_BUTTON_REL where ROLE_ID = ?"; String SQL3="delete from SYS_ROLE_RIGHT_REL where ROLE_ID= ?"; try{ conn=dbConn.getConnection(); conn.setAutoCommit(false); pstm1=conn.prepareStatement(SQL1); pstm2=conn.prepareStatement(SQL2); pstm3=conn.prepareStatement(SQL3); for(int i=0;i