123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package com.sinosoft.lz.system.menu.service;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.ProduceMime;
- import org.apache.log4j.Logger;
- import com.formaction.Utils;
- import com.persistence.service.JDBCHelper;
- import com.persistence.service.exception.PersistenceException;
- import com.sinosoft.lz.system.menu.vo.Button;
- import com.sinosoft.lz.system.menu.vo.Menu;
- @Path("/RoleMenuService1/")
- public class RoleMenuService1{
- private Logger log = Logger.getLogger(this.getClass());
- // private Map<String, String> map = new HashMap<String, String>();
- // private SysModel sysmodel = ModelFactory.getSysmodel();
- // private SysPersistence persistence =
- // PersistenceFactory.getInstance(sysmodel);
- // 编码解析
- protected String decode(String params) {
- try {
- params = java.net.URLDecoder.decode(params, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- log.error(e.getMessage(), e);
- }
- return params;
- }
- @ProduceMime("application/json")
- @POST
- @Path("/getMenutTree1/")
- public String getMenutTree1(String params) {
- params = decode(params);
- // String node = Utils.getParameter("node", params) == null ? "" :
- // Utils.getParameter("node", params);
- String role_id = Utils.getParameter("role_id", params) == null ? "" : Utils.getParameter(
- "role_id", params);
- String json = "";
- Menu m = new Menu();
- m.setFd_code("10");
- m.setFd_name("系统功能菜单配置");
- m.setFd_parentcode("");
- m.setCheck(false);
- JDBCHelper jdbc = new JDBCHelper();
- jdbc.begin();
- try {
- json = "[" + getreturnJson(jdbc, m, role_id) + "]";
- } catch (PersistenceException e) {
- // TODO Auto-generated catch block
- log.error(e.getMessage(), e);
- jdbc.rollback();
- } finally {
- if (jdbc != null)
- jdbc.release();
- }
- json = json.replace("},]", "}]");
- json = json.substring(json.indexOf("children:") + 9, json.lastIndexOf("}]"));
- log.info("lastJSON:" + json);
- return json;
- }
- private String getreturnJson(JDBCHelper jdbc, Menu menuItem, String role_id)
- throws PersistenceException {
- List<Menu> list = new ArrayList<Menu>();
- StringBuffer sb = new StringBuffer();
- boolean check = false;
- String isHidden = ",hidden : false ";
- list = getMenuList(jdbc, menuItem.getFd_code(), role_id);
- // log.info("节点 " + menuItem.getFd_name() + " 下子菜单的个数:" +
- // list.size());
- if (list.size() == 0) { // 子节点,然后查询该子节点下的按钮
- List<Button> buttonList = getButtonList(jdbc, menuItem.getFd_code(), role_id); // 查询子菜单下的按钮
- if (buttonList.size() == 0) {// 拼子菜单串
- sb.append("{text:'" + menuItem.getFd_name()
- + "',icon:'../images/road/plan/toc_closed.gif',id:'"
- + menuItem.getFd_code() + "'" + isHidden + ",parentid:'"
- + menuItem.getFd_parentcode()
- + "',leaf:true,expanded : true,type : '1',checked:" + menuItem.isCheck()
- + "},");
- } else {
- sb.append("{text:'" + menuItem.getFd_name()
- + "',icon:'../images/road/plan/toc_closed.gif',id:'"
- + menuItem.getFd_code() + "'" + isHidden + ",parentid:'"
- + menuItem.getFd_parentcode()
- + "',leaf:false,expanded : true,type : '1',checked:" + menuItem.isCheck()
- + ",children:[");
- for (Button butt : buttonList) {
- if (butt.isCheck()) {
- check = true;
- } else {
- check = false;
- }
- sb.append("{text:'" + butt.getFd_rightname() + "',icon:'',id:'"
- + butt.getButton_id() + "'" + isHidden + ",parentid:'"
- + butt.getFd_parentcode()
- + "',leaf:true,expanded : true,type : '2',checked:" + check + "},");
- }
- if (sb.lastIndexOf(",") > -1) {
- sb.deleteCharAt(sb.lastIndexOf(","));
- }
- sb.append("]},");
- }
- } else { // 不是子节点
- sb.append("{text:'" + menuItem.getFd_name()
- + "',icon:'../images/road/plan/toc_closed.gif',id:'" + menuItem.getFd_code()
- + "'" + isHidden + ",parentid:'" + menuItem.getFd_parentcode()
- + "',leaf:false,expanded : true,type : '1',checked:" + menuItem.isCheck()
- + ",children:[");
- for (Menu menu : list) {
- sb.append(getreturnJson(jdbc, menu, role_id));
- }
- sb.append("]},");
- }
- // log.debug(menuItem.getFd_name() + "json:" +sb.toString());
- return sb.toString();
- }
- private List<Button> getButtonList(JDBCHelper jdbc, String nodeid, String role_id)
- throws PersistenceException {
- ArrayList<String[]> list1 = new ArrayList<String[]>();
- ArrayList<String[]> list2 = new ArrayList<String[]>();
- ArrayList<Button> list3 = new ArrayList<Button>();
- Map<String, Button> map = new HashMap<String, Button>();
- // 查询菜单下按钮sql sqlserver中substr改成substring length改成len
- String sql1 = "select m.Button_ID,b.fd_code,b.fd_parentcode,b.fd_rightname "
- + "from SYS_MENU_ASSIGN m,bm$_objecttree b "
- + "where m.Menu_ID ='"
- + nodeid
- + "' and b.fd_parentcode<>'1' and substring(m.Button_ID,len(Menu_ID) + 1,len(m.Button_ID)-len(Menu_ID))=b.fd_code";
- // 查询给角色分配的按钮
- String sql2 = "select right_id from SYS_ROLE_RIGHT_REL where Role_ID='" + role_id
- + "' and Right_Type='2' and Right_ID like '" + nodeid + "%'";
- // log.info("查询菜单下的按钮:" + sql1);
- // log.info("查询给角色分配的按钮:" + sql2);
- list1 = jdbc.getSearchResult(99, sql1);
- list2 = jdbc.getSearchResult(99, sql2);
- Button btn = null;
- for (String[] obj : list1) {
- btn = new Button();
- btn.setButton_id(obj[0]);
- btn.setFd_code(obj[1]);
- btn.setFd_parentcode(obj[2]);
- btn.setFd_rightname(obj[3] + "(id:" + obj[0] + ")");
- map.put(btn.getButton_id(), btn);
- }
- for (String[] str : list2) {
- Button vo = map.get(str[0]);
- if (vo != null) {
- vo.setCheck(true);
- list3.add(vo);
- map.remove(str[0]);
- }
- }
- for (Button bttn : map.values()) {
- bttn.setCheck(false);
- list3.add(bttn);
- }
- return list3;
- }
- private List<Menu> getMenuList(JDBCHelper jdbc, String nodeid, String role_id)
- throws PersistenceException {
- ArrayList<String[]> list = new ArrayList<String[]>();
- ArrayList<String[]> list1 = new ArrayList<String[]>();
- ArrayList<Menu> list2 = new ArrayList<Menu>();
- Map<String, String> map = new HashMap<String, String>();
- // String sql =
- // "select fd_code,fd_parentcode,fd_RightName from BM$_Menu where fd_parentcode = '"
- // + nodeid + "' order by Convert(numeric,fd_sort) desc";
- String sql = "select fd_code,fd_parentcode,fd_RightName from BM$_Menu where fd_parentcode = '"
- + nodeid + "' order by fd_sort";
- // log.debug("节点下子菜单-----sql:" + sql);
- String sql1 = "select right_id from SYS_ROLE_RIGHT_REL where Role_ID='" + role_id
- + "' and right_type='1'";
- // log.debug("判断分配的菜单sql:"+sql1);
- list = jdbc.getSearchResult(99, sql);
- list1 = jdbc.getSearchResult(99, sql1);
- for (int j = 0; j < list1.size(); j++) {
- String[] rs2 = (String[]) list1.get(j);
- String right_id = rs2[0] == null ? "" : rs2[0];
- map.put(right_id, right_id);
- }
- for (int i = 0; i < list.size(); i++) {
- String[] str = list.get(i);
- Menu menu = new Menu();
- menu.setFd_code(str[0]);
- menu.setFd_parentcode(str[1]);
- menu.setFd_name(str[2]);
- if (map.containsKey(menu.getFd_code())) {
- menu.setCheck(true);
- } else {
- menu.setCheck(false);
- }
- list2.add(menu);
- }
- return list2;
- }
- }
|