3c62d8c42a4a33b2bd1653a38d0c8aaf3795649a.svn-base 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.sinosoft.lz.system.menu.service;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.ws.rs.POST;
  8. import javax.ws.rs.Path;
  9. import javax.ws.rs.ProduceMime;
  10. import org.apache.log4j.Logger;
  11. import com.formaction.Utils;
  12. import com.persistence.service.JDBCHelper;
  13. import com.persistence.service.exception.PersistenceException;
  14. import com.sinosoft.lz.system.menu.vo.Button;
  15. import com.sinosoft.lz.system.menu.vo.Menu;
  16. @Path("/RoleMenuService1/")
  17. public class RoleMenuService1{
  18. private Logger log = Logger.getLogger(this.getClass());
  19. // private Map<String, String> map = new HashMap<String, String>();
  20. // private SysModel sysmodel = ModelFactory.getSysmodel();
  21. // private SysPersistence persistence =
  22. // PersistenceFactory.getInstance(sysmodel);
  23. // 编码解析
  24. protected String decode(String params) {
  25. try {
  26. params = java.net.URLDecoder.decode(params, "UTF-8");
  27. } catch (UnsupportedEncodingException e) {
  28. log.error(e.getMessage(), e);
  29. }
  30. return params;
  31. }
  32. @ProduceMime("application/json")
  33. @POST
  34. @Path("/getMenutTree1/")
  35. public String getMenutTree1(String params) {
  36. params = decode(params);
  37. // String node = Utils.getParameter("node", params) == null ? "" :
  38. // Utils.getParameter("node", params);
  39. String role_id = Utils.getParameter("role_id", params) == null ? "" : Utils.getParameter(
  40. "role_id", params);
  41. String json = "";
  42. Menu m = new Menu();
  43. m.setFd_code("10");
  44. m.setFd_name("系统功能菜单配置");
  45. m.setFd_parentcode("");
  46. m.setCheck(false);
  47. JDBCHelper jdbc = new JDBCHelper();
  48. jdbc.begin();
  49. try {
  50. json = "[" + getreturnJson(jdbc, m, role_id) + "]";
  51. } catch (PersistenceException e) {
  52. // TODO Auto-generated catch block
  53. log.error(e.getMessage(), e);
  54. jdbc.rollback();
  55. } finally {
  56. if (jdbc != null)
  57. jdbc.release();
  58. }
  59. json = json.replace("},]", "}]");
  60. json = json.substring(json.indexOf("children:") + 9, json.lastIndexOf("}]"));
  61. log.info("lastJSON:" + json);
  62. return json;
  63. }
  64. private String getreturnJson(JDBCHelper jdbc, Menu menuItem, String role_id)
  65. throws PersistenceException {
  66. List<Menu> list = new ArrayList<Menu>();
  67. StringBuffer sb = new StringBuffer();
  68. boolean check = false;
  69. String isHidden = ",hidden : false ";
  70. list = getMenuList(jdbc, menuItem.getFd_code(), role_id);
  71. // log.info("节点 " + menuItem.getFd_name() + " 下子菜单的个数:" +
  72. // list.size());
  73. if (list.size() == 0) { // 子节点,然后查询该子节点下的按钮
  74. List<Button> buttonList = getButtonList(jdbc, menuItem.getFd_code(), role_id); // 查询子菜单下的按钮
  75. if (buttonList.size() == 0) {// 拼子菜单串
  76. sb.append("{text:'" + menuItem.getFd_name()
  77. + "',icon:'../images/road/plan/toc_closed.gif',id:'"
  78. + menuItem.getFd_code() + "'" + isHidden + ",parentid:'"
  79. + menuItem.getFd_parentcode()
  80. + "',leaf:true,expanded : true,type : '1',checked:" + menuItem.isCheck()
  81. + "},");
  82. } else {
  83. sb.append("{text:'" + menuItem.getFd_name()
  84. + "',icon:'../images/road/plan/toc_closed.gif',id:'"
  85. + menuItem.getFd_code() + "'" + isHidden + ",parentid:'"
  86. + menuItem.getFd_parentcode()
  87. + "',leaf:false,expanded : true,type : '1',checked:" + menuItem.isCheck()
  88. + ",children:[");
  89. for (Button butt : buttonList) {
  90. if (butt.isCheck()) {
  91. check = true;
  92. } else {
  93. check = false;
  94. }
  95. sb.append("{text:'" + butt.getFd_rightname() + "',icon:'',id:'"
  96. + butt.getButton_id() + "'" + isHidden + ",parentid:'"
  97. + butt.getFd_parentcode()
  98. + "',leaf:true,expanded : true,type : '2',checked:" + check + "},");
  99. }
  100. if (sb.lastIndexOf(",") > -1) {
  101. sb.deleteCharAt(sb.lastIndexOf(","));
  102. }
  103. sb.append("]},");
  104. }
  105. } else { // 不是子节点
  106. sb.append("{text:'" + menuItem.getFd_name()
  107. + "',icon:'../images/road/plan/toc_closed.gif',id:'" + menuItem.getFd_code()
  108. + "'" + isHidden + ",parentid:'" + menuItem.getFd_parentcode()
  109. + "',leaf:false,expanded : true,type : '1',checked:" + menuItem.isCheck()
  110. + ",children:[");
  111. for (Menu menu : list) {
  112. sb.append(getreturnJson(jdbc, menu, role_id));
  113. }
  114. sb.append("]},");
  115. }
  116. // log.debug(menuItem.getFd_name() + "json:" +sb.toString());
  117. return sb.toString();
  118. }
  119. private List<Button> getButtonList(JDBCHelper jdbc, String nodeid, String role_id)
  120. throws PersistenceException {
  121. ArrayList<String[]> list1 = new ArrayList<String[]>();
  122. ArrayList<String[]> list2 = new ArrayList<String[]>();
  123. ArrayList<Button> list3 = new ArrayList<Button>();
  124. Map<String, Button> map = new HashMap<String, Button>();
  125. // 查询菜单下按钮sql sqlserver中substr改成substring length改成len
  126. String sql1 = "select m.Button_ID,b.fd_code,b.fd_parentcode,b.fd_rightname "
  127. + "from SYS_MENU_ASSIGN m,bm$_objecttree b "
  128. + "where m.Menu_ID ='"
  129. + nodeid
  130. + "' and b.fd_parentcode<>'1' and substring(m.Button_ID,len(Menu_ID) + 1,len(m.Button_ID)-len(Menu_ID))=b.fd_code";
  131. // 查询给角色分配的按钮
  132. String sql2 = "select right_id from SYS_ROLE_RIGHT_REL where Role_ID='" + role_id
  133. + "' and Right_Type='2' and Right_ID like '" + nodeid + "%'";
  134. // log.info("查询菜单下的按钮:" + sql1);
  135. // log.info("查询给角色分配的按钮:" + sql2);
  136. list1 = jdbc.getSearchResult(99, sql1);
  137. list2 = jdbc.getSearchResult(99, sql2);
  138. Button btn = null;
  139. for (String[] obj : list1) {
  140. btn = new Button();
  141. btn.setButton_id(obj[0]);
  142. btn.setFd_code(obj[1]);
  143. btn.setFd_parentcode(obj[2]);
  144. btn.setFd_rightname(obj[3] + "(id:" + obj[0] + ")");
  145. map.put(btn.getButton_id(), btn);
  146. }
  147. for (String[] str : list2) {
  148. Button vo = map.get(str[0]);
  149. if (vo != null) {
  150. vo.setCheck(true);
  151. list3.add(vo);
  152. map.remove(str[0]);
  153. }
  154. }
  155. for (Button bttn : map.values()) {
  156. bttn.setCheck(false);
  157. list3.add(bttn);
  158. }
  159. return list3;
  160. }
  161. private List<Menu> getMenuList(JDBCHelper jdbc, String nodeid, String role_id)
  162. throws PersistenceException {
  163. ArrayList<String[]> list = new ArrayList<String[]>();
  164. ArrayList<String[]> list1 = new ArrayList<String[]>();
  165. ArrayList<Menu> list2 = new ArrayList<Menu>();
  166. Map<String, String> map = new HashMap<String, String>();
  167. // String sql =
  168. // "select fd_code,fd_parentcode,fd_RightName from BM$_Menu where fd_parentcode = '"
  169. // + nodeid + "' order by Convert(numeric,fd_sort) desc";
  170. String sql = "select fd_code,fd_parentcode,fd_RightName from BM$_Menu where fd_parentcode = '"
  171. + nodeid + "' order by fd_sort";
  172. // log.debug("节点下子菜单-----sql:" + sql);
  173. String sql1 = "select right_id from SYS_ROLE_RIGHT_REL where Role_ID='" + role_id
  174. + "' and right_type='1'";
  175. // log.debug("判断分配的菜单sql:"+sql1);
  176. list = jdbc.getSearchResult(99, sql);
  177. list1 = jdbc.getSearchResult(99, sql1);
  178. for (int j = 0; j < list1.size(); j++) {
  179. String[] rs2 = (String[]) list1.get(j);
  180. String right_id = rs2[0] == null ? "" : rs2[0];
  181. map.put(right_id, right_id);
  182. }
  183. for (int i = 0; i < list.size(); i++) {
  184. String[] str = list.get(i);
  185. Menu menu = new Menu();
  186. menu.setFd_code(str[0]);
  187. menu.setFd_parentcode(str[1]);
  188. menu.setFd_name(str[2]);
  189. if (map.containsKey(menu.getFd_code())) {
  190. menu.setCheck(true);
  191. } else {
  192. menu.setCheck(false);
  193. }
  194. list2.add(menu);
  195. }
  196. return list2;
  197. }
  198. }