ccd214d66a74a0378140d01017ae0ac6f3ad0582.svn-base 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. package com.sinosoft.lz.system.right.role.service;
  2. import java.io.UnsupportedEncodingException;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.Date;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import javax.ws.rs.POST;
  15. import javax.ws.rs.Path;
  16. import javax.ws.rs.PathParam;
  17. import javax.ws.rs.ProduceMime;
  18. import org.apache.log4j.Logger;
  19. import com.formaction.Utils;
  20. import com.formaction.vo.Msg;
  21. import com.persistence.DbConnection;
  22. import com.persistence.service.JDBCHelper;
  23. import com.persistence.service.PersistenceFactory;
  24. import com.persistence.service.SysPersistence;
  25. import com.persistence.service.assitant.generator.CodeGenerator;
  26. import com.persistence.service.exception.PersistenceException;
  27. import com.sinosoft.lz.system.function.busi.FunctionBean;
  28. import com.sinosoft.lz.system.function.vo.FunctionButtonVO;
  29. import com.sinosoft.lz.system.function.vo.FunctionVO;
  30. import com.sinosoft.lz.system.right.role.busi.RoleBean;
  31. import com.sinosoft.lz.system.right.role.busi.SysRoleRightRelBean;
  32. import com.sinosoft.lz.system.right.role.dao.RoleDAO;
  33. import com.sinosoft.lz.system.right.role.vo.RoleVO;
  34. import com.sinosoft.lz.system.right.role.vo.SysRoleRightRelVO;
  35. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  36. import com.sysmodel.datamodel.xmlmodel.able.MdpModule;
  37. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  38. import com.sysmodel.datamodel.xmlmodel.impl.MdpModuleImpl;
  39. import net.sf.json.JSONArray;
  40. import net.sf.json.JSONObject;
  41. @Path("/RoleService/")
  42. public class RoleService{
  43. private Logger log = Logger.getLogger(this.getClass());
  44. // 编码解析
  45. protected String decode(String params) {
  46. try {
  47. params = java.net.URLDecoder.decode(params, "UTF-8");
  48. } catch (UnsupportedEncodingException e) {
  49. e.printStackTrace();
  50. }
  51. return params;
  52. }
  53. /**
  54. * 所有按钮列表服务
  55. *
  56. * @param params
  57. * @return
  58. * @throws Exception
  59. */
  60. @SuppressWarnings("unchecked")
  61. @ProduceMime("application/json")
  62. @POST
  63. @Path("/getRoleList")
  64. public String getRoleList() throws Exception {
  65. StringBuffer sb = new StringBuffer();
  66. String sql = "select * from SYS_Role where role_type='0'";
  67. RoleBean roleBean = new RoleBean();
  68. Collection<RoleVO> coll = roleBean.getSysRole(sql);
  69. sb.append("[");
  70. Iterator<RoleVO> it = coll.iterator();
  71. while (it.hasNext()) {
  72. RoleVO vo = it.next();
  73. sb.append("{\"FD_OBJECTID\":\"" + vo.getFdObjectid() + "\",\"ROLE_TYPE\":\""
  74. + vo.getIsDel() + "\",\"ROLE_ID\":\"" + vo.getRoleId() + "\"");
  75. sb.append(",\"ROLE_NAME\":\"" + vo.getRoleName() + "\"},");
  76. }
  77. if (sb.lastIndexOf(",") > -1) {
  78. sb.deleteCharAt(sb.lastIndexOf(","));
  79. }
  80. sb.append("]");
  81. log.info("json:" + sb);
  82. return sb.toString();
  83. }
  84. /**
  85. * 获取角色名称和id,用于下拉框
  86. * 格式如:{"list":[{"value":"1","key":"人身事故"},{"value":"2","key":"电网事故"},{"value":"3","key":"设备事故"},{"value":"4","key":"防风防汛"}]}
  87. * @param params
  88. * @return
  89. * @throws Exception
  90. */
  91. @SuppressWarnings("unchecked")
  92. @ProduceMime("application/json")
  93. @POST
  94. @Path("/getRoleKVs")
  95. public String getRoleKVs() throws Exception {
  96. StringBuffer sb = new StringBuffer();
  97. String sql = "select role_id,role_name from SYS_Role ";
  98. Connection conn = null;
  99. Statement stat = null;
  100. ResultSet rs = null;
  101. DbConnection db = new DbConnection();
  102. try {
  103. conn = db.getConnection();
  104. stat = conn.createStatement();
  105. rs = stat.executeQuery(sql);
  106. sb.append("{\"list\":[");
  107. while (rs.next()) {
  108. String roleId = (String) rs.getString("role_id") == null ? "": rs.getString("role_id");
  109. String roleName = (String) rs.getString("role_name") == null ? "" : rs.getString("role_name");
  110. sb.append("{\"value\":\"" + roleId + "\",\"key\":\"" + roleName + "\"},");
  111. }
  112. if (sb.lastIndexOf(",") > -1) {
  113. sb.deleteCharAt(sb.lastIndexOf(","));
  114. }
  115. sb.append("]}");
  116. } catch (SQLException e) {
  117. this.log.error(e.getMessage(), e);
  118. throw new ClassNotFoundException("取得RoleKVs集合" + sql, e);
  119. } finally {
  120. db.close(rs);
  121. db.close(stat);
  122. db.close(conn);
  123. }
  124. log.info("json:" + sb);
  125. return sb.toString();
  126. }
  127. /**
  128. * 获取所有功能点
  129. *
  130. * @return
  131. * @throws Exception
  132. */
  133. @ProduceMime("application/json")
  134. @POST
  135. @Path("/getRoleButtonList/{roleId}")
  136. public String getFunctionList(@PathParam("roleId") String roleId) throws Exception {
  137. StringBuffer sb = new StringBuffer();
  138. sb.append("[");
  139. sb.append(getTreeJson(roleId));
  140. if (sb.lastIndexOf(",") > -1) {
  141. sb.deleteCharAt(sb.lastIndexOf(","));
  142. }
  143. sb.append("]");
  144. log.info("json:" + sb);
  145. return sb.toString();
  146. }
  147. private StringBuffer getTreeJson(String roleId) throws ClassNotFoundException {
  148. Connection conn = null;
  149. Statement stat = null;
  150. ResultSet rs = null;
  151. DbConnection db = new DbConnection();
  152. StringBuffer sb = new StringBuffer();
  153. SysModel sysmodel = ModelFactory.getSysmodel();
  154. ArrayList<MdpModuleImpl> mdplist = sysmodel.getMdpModules();
  155. String sql = "select module_id from SYS_function group by module_id";
  156. FunctionBean functionBean = new FunctionBean();
  157. try {
  158. conn = db.getConnection();
  159. stat = conn.createStatement();
  160. rs = stat.executeQuery(sql);
  161. while (rs.next()) {
  162. String[] moduleCode = rs.getString(1).split("_");
  163. MdpModule mdp = sysmodel.getMdpModuleByCode(moduleCode[1]);
  164. for (MdpModuleImpl mdps : mdplist) {
  165. if (moduleCode[1].equals(mdps.getCode()) && mdps.getPcode().equals("")) {
  166. sb.append("{\"module_id\":\"" + mdp.getName() + "\",\"id\":\""
  167. + mdp.getCode() + "\",\"state\":\"closed\"},");
  168. } else {
  169. if (moduleCode[1].equals(mdps.getCode())) {
  170. sb.append("{\"module_id\":\"" + mdp.getName() + "\",\"id\":\""
  171. + mdp.getCode() + "\",\"state\":\"closed\"");
  172. // sb.append("\"caozuo\":\"<input name='"+mdp.getName()+"' value='"+mdp.getName()+"' onclick=checkAllBox(this,'"+mdp.getCode()+"') type='checkbox'/>全选\"");
  173. ArrayList<FunctionVO> functionVO = (ArrayList<FunctionVO>) functionBean
  174. .getSysFunction("select * from SYS_function where module_id='"
  175. + rs.getString(1) + "'");
  176. sb.append(",\"children\":[");
  177. for (FunctionVO fvo : functionVO) {
  178. String[] fmoduleCode = fvo.getModuleId().split("_");
  179. MdpModule fmdp = sysmodel.getMdpModuleByCode(fmoduleCode[1]);
  180. sb.append("{\"fd_objectid\":\"" + fvo.getFdObjectid()
  181. + "\",\"id\":\"" + fvo.getId() + "\",\"name\":\""
  182. + fvo.getName() + "\",\"module_id\":\"" + fmdp.getName()
  183. + "\"");
  184. // sb.append(",\"caozuo\":\"<input value='"+fvo.getId()+"' onclick=checkAllBox(this,'selected') type='checkbox'/>全选\"");
  185. ArrayList<FunctionButtonVO> fbvList = (ArrayList<FunctionButtonVO>) functionBean
  186. .getSysFunctionButton("select * from SYS_FUNCTION_BUTTON where fun_id='"
  187. + fvo.getFdObjectid() + "'");
  188. sb.append(",\"button\":\"");
  189. for (FunctionButtonVO fbv : fbvList) {
  190. // sb.append("<input type='checkbox' id='selected' value='"+fbv.getFdObjectid()+"' name='selected' "+this.getChecked(fbv.getFdObjectid(),
  191. // roleId)+" onClick=checkData(this,'selected');>");
  192. sb.append("<input type='checkbox' id='selected' value='"
  193. + fbv.getFdObjectid() + "' name='selected' "
  194. + this.getChecked(fbv.getFdObjectid(), roleId) + ">");
  195. sb.append(fbv.getName() + " </input>");
  196. }
  197. sb.append("" + "\",\"state\":\"open\"");
  198. sb.append("},");
  199. }
  200. if (sb.lastIndexOf(",") > -1) {
  201. sb.deleteCharAt(sb.lastIndexOf(","));
  202. }
  203. sb.append("]");
  204. sb.append("},");
  205. }
  206. }
  207. }
  208. }
  209. } catch (SQLException e) {
  210. log.error(e.getMessage(), e);
  211. throw new ClassNotFoundException("DAO Layou: 取得SysFunctionVO集合" + sql, e);
  212. } finally {
  213. db.close(rs);
  214. db.close(stat);
  215. db.close(conn);
  216. }
  217. return sb;
  218. }
  219. @SuppressWarnings("rawtypes")
  220. private boolean isValueInVector(String value, ArrayList rsVec) {
  221. String[] temp = null;
  222. if (rsVec != null) {
  223. for (int i = 0; i < rsVec.size(); i++) {
  224. temp = (String[]) rsVec.get(i);
  225. temp[0] = temp[0] == null ? "" : temp[0];
  226. if (value.equals(temp[0]))
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. @SuppressWarnings("rawtypes")
  233. public String getChecked(String value, String roleId) {
  234. String sql = "select Right_ID FROM SYS_ROLE_RIGHT_REL WHERE Role_ID='" + roleId + "'";
  235. SysModel sysmodel = ModelFactory.getSysmodel();
  236. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  237. ArrayList rsVec;
  238. try {
  239. rsVec = persistence.getSearchResult(886, sql);
  240. if (isValueInVector(value, rsVec)) {
  241. return "checked";
  242. }
  243. } catch (PersistenceException e) {
  244. // TODO Auto-generated catch block
  245. e.printStackTrace();
  246. }
  247. return "";
  248. }
  249. /**
  250. * 按钮的新增 选择按钮的入库,修改 所选择的按钮的展示
  251. *
  252. * @throws ClassNotFoundException
  253. */
  254. @POST
  255. @Path("/saveRole")
  256. public String saveRole(String params) throws ClassNotFoundException {
  257. String msg = "";
  258. params = decode(params);
  259. log.info(params);
  260. RoleBean roleBean = new RoleBean();
  261. String name = Utils.getParameter("role_name", params) == null ? "" : Utils.getParameter(
  262. "role_name", params);
  263. String role_type = Utils.getParameter("role_type", params) == null ? "" : Utils
  264. .getParameter("role_type", params);
  265. String deptId = Utils.getParameter("deptId", params) == null ? "" : Utils.getParameter(
  266. "deptId", params);
  267. String roleId = CodeGenerator.produceIdentity(881);
  268. RoleVO vo = new RoleVO();
  269. vo.setRoleId(roleId);
  270. vo.setRoleName(name);
  271. // vo.setRoleDescription(name);
  272. // vo.setRoleState(1);
  273. // vo.setRoleType(role_type);
  274. // vo.setCreateUnit(deptId);
  275. boolean flag = roleBean.addSysRole(vo);
  276. if (!flag) {
  277. msg = "{\"success\":" + new Boolean(true).toString() + "}";
  278. } else {
  279. msg = "{\"success\":" + new Boolean(false).toString() + "}";
  280. }
  281. log.info(msg);
  282. return msg;
  283. }
  284. /**
  285. * 按钮的修改 选择按钮的入库,修改 所选择的按钮的展示
  286. *
  287. * @throws ClassNotFoundException
  288. */
  289. @POST
  290. @Path("/updateRole")
  291. public String updateRole(String params) throws ClassNotFoundException {
  292. String msg = "";
  293. log.info(params);
  294. params = decode(params);
  295. String fd_objectid = Utils.getParameter("fd_objectid", params) == null ? "" : Utils
  296. .getParameter("fd_objectid", params);
  297. String name = Utils.getParameter("role_name", params) == null ? "" : Utils.getParameter(
  298. "role_name", params);
  299. RoleBean roleBean = new RoleBean();
  300. RoleVO vo = roleBean.getRoleVO(fd_objectid);
  301. vo.setRoleName(name);
  302. // vo.setRoleDescription(name);
  303. boolean flag = roleBean.modifySysRole(vo);
  304. log.info(flag);
  305. if (flag) {
  306. msg = "{\"success\":" + new Boolean(true).toString() + "}";
  307. } else {
  308. msg = "{\"success\":" + new Boolean(false).toString() + "}";
  309. }
  310. return msg;
  311. }
  312. /**
  313. * 按钮的删除 选择按钮的入库,修改 所选择的按钮的展示
  314. *
  315. * @throws ClassNotFoundException
  316. */
  317. @POST
  318. @Path("/deleteRole")
  319. public String deleteRole(String params) throws ClassNotFoundException {
  320. String msg = "";
  321. params = decode(params);
  322. RoleBean roleBean = new RoleBean();
  323. String roleId = Utils.getParameter("roleId", params) == null ? "" : Utils.getParameter(
  324. "roleId", params);
  325. boolean flag = roleBean.delSysRole(roleId);
  326. log.info(flag);
  327. if (flag) {
  328. msg = "{\"success\":" + new Boolean(true).toString() + "}";
  329. } else {
  330. msg = "{\"success\":" + new Boolean(false).toString() + "}";
  331. }
  332. return msg;
  333. }
  334. @POST
  335. @Path("/saveRoleButton")
  336. public String saveRoleButton(String params) throws ClassNotFoundException {
  337. params = decode(params);
  338. log.info(params);
  339. String msg = "";
  340. String roleId = Utils.getParameter("roleId", params) == null ? "" : Utils.getParameter(
  341. "roleId", params);
  342. String roleType = Utils.getParameter("roleType", params) == null ? "" : Utils.getParameter(
  343. "roleType", params);
  344. String buttonStr = Utils.getParameter("selected", params) == null ? "" : Utils
  345. .getParameter("selected", params);
  346. SysRoleRightRelBean roleBean = new SysRoleRightRelBean();
  347. SysRoleRightRelVO relvo = roleBean.getSysRoleRightRelVO(roleId);
  348. if (relvo != null) {
  349. roleBean.delSysRoleRightRel(roleId);
  350. }
  351. SysRoleRightRelVO vo = null;
  352. ArrayList<SysRoleRightRelVO> voList = new ArrayList<SysRoleRightRelVO>();
  353. String[] str = buttonStr.split(",");
  354. for (int i = 0; i < str.length; i++) {
  355. vo = new SysRoleRightRelVO();
  356. vo.setRoleId(roleId);
  357. vo.setRoleType(roleType);
  358. vo.setRightId(str[i]);
  359. voList.add(vo);
  360. }
  361. int[] num = roleBean.addSysRoleRightRel(voList);
  362. if (num.length > 0) {
  363. msg = "{\"success\":" + new Boolean(true).toString() + "}";
  364. } else {
  365. msg = "{\"success\":" + new Boolean(false).toString() + "}";
  366. }
  367. log.info(msg);
  368. return msg;
  369. }
  370. /**
  371. * 功能:根据角色类型查询所有角色
  372. *
  373. * @param params
  374. * @return
  375. * @throws Exception
  376. */
  377. @SuppressWarnings("rawtypes")
  378. @ProduceMime("application/json")
  379. @POST
  380. @Path("/getRoleInfo")
  381. public String getRoleInfo(String params) {
  382. // 角色类型
  383. String result = null;
  384. RoleDAO dao = new RoleDAO();
  385. List list = dao.loadRoleInfoByCondition();
  386. result = "{\"rows\":" + JSONArray.fromObject(list).toString() + "}";
  387. log.info(result);
  388. return result;
  389. }
  390. /**
  391. * 功能:获取角色菜单多选下拉框
  392. *
  393. * @param params
  394. * @return
  395. * @throws Exception
  396. */
  397. @ProduceMime("application/json")
  398. @POST
  399. @Path("/getMenuTreeRole")
  400. public String getMenuTreeRole(String params){
  401. log.info(params);
  402. // String[] condition = node.split("node=");
  403. String node = Utils.getParameter("id", params)== null ? "0" : Utils.getParameter("id", params);
  404. String role_id = Utils.getParameter("role_id", params)== null ? "1" : Utils.getParameter("role_id", params);
  405. JDBCHelper jdbc = new JDBCHelper();
  406. jdbc.begin();
  407. ArrayList<String[]> list = new ArrayList<String[]>();
  408. StringBuffer buf=new StringBuffer();
  409. buf.append("select distinct m4.menu_id,m4.parent_menu_id,m4.menu_name,m4.fd_objectid,m3.is_parent from \n");
  410. buf.append("( select m1.menu_id menu_id,m1.parent_menu_id,m1.menu_name,m2.fd_objectid \n");
  411. 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");
  412. 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");
  413. log.info(buf.toString());
  414. try {
  415. list = jdbc.getSearchResult(99, buf.toString());
  416. }catch(Exception e){
  417. e.printStackTrace();
  418. jdbc.rollback();
  419. }finally{
  420. if(jdbc!=null){
  421. jdbc.release();
  422. }
  423. }
  424. StringBuffer json = new StringBuffer();
  425. json.append("{treeNodes:[");
  426. String[] temp = null;
  427. String code = "";
  428. String parentcode = "";
  429. String fd_RightName = "";
  430. String flag = "true";
  431. String checked = "";
  432. String isparent="";
  433. if(list.size()>0){
  434. for (int i = 0 ; i< list.size() ; i++){
  435. temp = list.get(i);
  436. code = temp[0];
  437. parentcode = temp[1];
  438. fd_RightName = temp[2];
  439. checked=temp[3];
  440. isparent=temp[4];
  441. if(isparent!=null){
  442. flag="true";
  443. }else{
  444. flag="false";
  445. }
  446. json.append("{");
  447. json.append("id:'"+code+"',parentId:'"+parentcode+"',");
  448. json.append("isParent:'"+flag+"',");
  449. json.append("name:'"+fd_RightName+"',");
  450. json.append("icon:'/nwyj/images/system/button/toc_closed.gif'");
  451. if(checked!=null){
  452. json.append(",checked:'true'");
  453. }
  454. json.append("},");
  455. }
  456. }
  457. if(json.lastIndexOf(",") > -1){
  458. json.deleteCharAt(json.lastIndexOf(","));
  459. }
  460. json.append("]}");
  461. //log.info(JSONObject.fromObject(json.toString()).toString());
  462. System.out.println(JSONObject.fromObject(json.toString()).toString());
  463. return JSONObject.fromObject(json.toString()).toString();
  464. }
  465. /**
  466. * 功能:系统搜索模块单选下拉框
  467. *
  468. * @param params
  469. * @return
  470. * @throws Exception
  471. */
  472. @ProduceMime("application/json")
  473. @POST
  474. @Path("/getSearchTree")
  475. public String getSearchTree(){
  476. JDBCHelper jdbc = new JDBCHelper();
  477. jdbc.begin();
  478. ArrayList<String[]> list = new ArrayList<String[]>();
  479. StringBuffer buf=new StringBuffer();
  480. buf.append(" select * from ( ( select menu_id,menu_name ,parent_menu_id from sys_menu where length(menu_id)=5 ) t1 left join \n");
  481. 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");
  482. log.info(buf.toString());
  483. try {
  484. list = jdbc.getSearchResult(99, buf.toString());
  485. }catch(Exception e){
  486. e.printStackTrace();
  487. jdbc.rollback();
  488. }finally{
  489. if(jdbc!=null){
  490. jdbc.release();
  491. }
  492. }
  493. StringBuffer json = new StringBuffer();
  494. json.append("{treeNodes:[");
  495. String[] temp = null;
  496. String menu_id = "";
  497. String menu_name = "";
  498. String parent_menu_id;
  499. String isparent="";
  500. String flag="false";
  501. if(list.size()>0){
  502. for (int i = 0 ; i< list.size() ; i++){
  503. temp = list.get(i);
  504. menu_id = temp[0];
  505. menu_name = temp[1];
  506. parent_menu_id=temp[2];
  507. isparent = temp[3];
  508. if(isparent!=null){
  509. flag="true";
  510. }else{
  511. flag="false";
  512. }
  513. json.append("{");
  514. json.append("id:'"+menu_id+"',parentId:'"+parent_menu_id+"',");
  515. json.append("isParent:'"+flag+"',");
  516. json.append("name:'"+menu_name+"',");
  517. json.append("icon:'/nwyj/images/system/button/toc_closed.gif'");
  518. json.append("},");
  519. }
  520. }
  521. if(json.lastIndexOf(",") > -1){
  522. json.deleteCharAt(json.lastIndexOf(","));
  523. }
  524. json.append("]}");
  525. log.info(JSONObject.fromObject(json.toString()).toString());
  526. return JSONObject.fromObject(json.toString()).toString();
  527. }
  528. /**
  529. * 保存菜单与角色的关系
  530. * @param params
  531. * @return
  532. */
  533. @ProduceMime("application/json")
  534. @POST
  535. @Path("/saveMenuRoleRel")
  536. public Msg saveMenuRoleRel(String params){
  537. //按钮id
  538. String buttonIds=Utils.getParameter("buttonIds", params)== null ? "" : Utils.getParameter("buttonIds", params);
  539. log.info(buttonIds);
  540. //角色id
  541. String roleId=Utils.getParameter("roleId", params)== null ? "" : Utils.getParameter("roleId", params);
  542. //删除该角色已有的按钮分配信息
  543. String sql = "delete from SYS_ROLE_RIGHT_REL where ROLE_ID='"+roleId+"'";
  544. //返回信息
  545. Msg msg=new Msg();
  546. JDBCHelper jdbc=new JDBCHelper();
  547. jdbc.begin();
  548. msg.setSucsess("true");
  549. ArrayList<String> list=new ArrayList<String>();
  550. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
  551. try {
  552. jdbc.executeUpdateSQL(sql);//执行删除sql
  553. //拆分按钮id
  554. String[] s = buttonIds.split("#");
  555. for(String str:s){
  556. //插入语句
  557. sql="insert into SYS_ROLE_RIGHT_REL values('"+roleId+"','"+roleId+"','"+str+"','0','"+df.format(new Date())+"')";
  558. //将插入语句放入list,批量保存
  559. list.add(sql);
  560. }
  561. //执行批量保存方法
  562. jdbc.executeUpdateSQL(list);
  563. } catch (PersistenceException e) {
  564. // TODO Auto-generated catch block
  565. e.printStackTrace();
  566. //事务回滚
  567. jdbc.rollback();
  568. //输出错误信息
  569. log.error(e.getMessage(),e);
  570. //返回错误信息
  571. msg.setSucsess("error");
  572. }
  573. jdbc.commit();
  574. return msg;
  575. }
  576. @ProduceMime("application/json")
  577. @POST
  578. @Path("/getOrgTree")
  579. public String getOrgTree(String params){
  580. log.info(params);
  581. String node = Utils.getParameter("id", params)== null ? "1" : Utils.getParameter("id", params);
  582. JDBCHelper jdbc = new JDBCHelper();
  583. jdbc.begin();
  584. ArrayList<String[]> list = new ArrayList<String[]>();
  585. String sql = "select dept_id ,parent_id , dept_name from sys_department where parent_id = '"+node+"'";
  586. log.info(sql);
  587. try {
  588. list = jdbc.getSearchResult(99, sql);
  589. }catch(Exception e){
  590. e.printStackTrace();
  591. jdbc.rollback();
  592. }finally{
  593. if(jdbc!=null){
  594. jdbc.release();
  595. }
  596. }
  597. StringBuffer json = new StringBuffer();
  598. json.append("{treeNodes:[");
  599. String[] temp = null;
  600. String code = "";
  601. String parentcode = "";
  602. String fd_RightName = "";
  603. String flag = "true";
  604. if(list.size()>0){
  605. for (int i = 0 ; i< list.size() ; i++){
  606. temp = list.get(i);
  607. code = temp[0];
  608. parentcode = temp[1];
  609. fd_RightName = temp[2];
  610. if(!isOrgLeaf(code)){
  611. flag="false";
  612. }else{
  613. flag="true";
  614. }
  615. json.append("{");
  616. json.append("id:'"+code+"',parentId:'"+parentcode+"',");
  617. json.append("isParent:'"+flag+"',");
  618. json.append("name:'"+fd_RightName+"',");
  619. json.append("icon:'/nwyj/scripts/qui/libs/icons/home.gif'");
  620. json.append("},");
  621. }
  622. }
  623. if(json.lastIndexOf(",") > -1){
  624. json.deleteCharAt(json.lastIndexOf(","));
  625. }
  626. json.append("]}");
  627. log.info(JSONObject.fromObject(json.toString()).toString());
  628. return JSONObject.fromObject(json.toString()).toString();
  629. }
  630. //判定是否有子节点
  631. public boolean isOrgLeaf (String node){
  632. boolean flag = false;
  633. SysModel sysmodel = ModelFactory.getSysmodel();
  634. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  635. ArrayList<String[]> childList = new ArrayList<String[]>();
  636. String sql = "select dept_id from sys_department where parent_id = '" + node + "'";
  637. // log.debug("判断是否有子节点:"+sql);
  638. try{
  639. childList = persistence.getSearchResult(99,sql);
  640. }catch(PersistenceException e){
  641. e.printStackTrace();
  642. }
  643. if(childList.size() > 0){
  644. flag = true;
  645. }
  646. return flag;
  647. }
  648. //删除角色,包括角色表、角色按钮关联表、角色菜单关联表
  649. @ProduceMime("application/json")
  650. @POST
  651. @Path("/deleteRoleID")
  652. public String deleteRoleID(String params){
  653. String flag="false";
  654. try {
  655. params=new String(params.getBytes("ISO8859-1"),"UTF-8");
  656. } catch (UnsupportedEncodingException e1) {
  657. // TODO Auto-generated catch block
  658. e1.printStackTrace();
  659. }
  660. String[] strs=params.split(",");
  661. DbConnection dbConn = new DbConnection();
  662. Connection conn = null;
  663. PreparedStatement pstm1 = null;
  664. PreparedStatement pstm2 = null;
  665. PreparedStatement pstm3 = null;
  666. String SQL1="delete from sys_role where fd_objectid = ?";
  667. String SQL2="delete from SYS_ROLE_BUTTON_REL where ROLE_ID = ?";
  668. String SQL3="delete from SYS_ROLE_RIGHT_REL where ROLE_ID= ?";
  669. try{
  670. conn=dbConn.getConnection();
  671. conn.setAutoCommit(false);
  672. pstm1=conn.prepareStatement(SQL1);
  673. pstm2=conn.prepareStatement(SQL2);
  674. pstm3=conn.prepareStatement(SQL3);
  675. for(int i=0;i<strs.length;i++){
  676. pstm1.setString(1, strs[i]);
  677. pstm2.setString(1, strs[i]);
  678. pstm3.setString(1, strs[i]);
  679. pstm1.executeUpdate();
  680. pstm2.executeUpdate();
  681. pstm3.executeUpdate();
  682. }
  683. conn.commit();
  684. conn.setAutoCommit(true);
  685. //System.out.println(123123123);
  686. flag="true";
  687. }catch(Exception e){
  688. try {
  689. conn.rollback();
  690. conn.setAutoCommit(true);
  691. } catch (SQLException e1) {
  692. e1.printStackTrace();
  693. }
  694. e.printStackTrace();
  695. }finally{
  696. try {
  697. if(pstm1!=null)
  698. pstm1.close();
  699. if(pstm2!=null)
  700. pstm2.close();
  701. if(pstm3!=null)
  702. pstm3.close();
  703. if(conn!=null)
  704. conn.close();
  705. } catch (SQLException e) {
  706. e.printStackTrace();
  707. }
  708. }
  709. return flag;
  710. }
  711. }