model-dsource.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. if($ == undefined){
  2. $ = jQuery;
  3. }
  4. function initdsourcetable(){
  5. if($("#dsourcetable").size() > 0){
  6. $("#dsourcetable").datagrid("load", {t:Math.random()});
  7. return;
  8. }
  9. var ctx = "<table id=\"dsourcetable\" title=\"数据源管理\" ><thead><tr><th data-options=\"field:'ck',checkbox:true\"></th><th data-options=\"field:'dsname',width:120\">名称</th><th data-options=\"field:'use',width:120,align:'center'\">类型</th><th data-options=\"field:'linkType',width:150,align:'center'\">数据库</th><th data-options=\"field:'linkUrl',width:300\">链接字符串</th><th data-options=\"field:'linkName',width:120,align:'center'\">用户名</th></tr></thead></table>";
  10. $("#optarea").html(ctx);
  11. $("#dsourcetable").datagrid({
  12. singleSelect:true,
  13. collapsible:false,
  14. pagination:false,
  15. border:false,
  16. fit:true,
  17. url:'listDataSource.action',
  18. toolbar:[{
  19. text:'新增',
  20. iconCls:'icon-add',
  21. handler:function(){
  22. newdsource(false);
  23. }
  24. },{
  25. text:'修改',
  26. iconCls:'icon-edit',
  27. handler:function(){
  28. var row = $("#dsourcetable").datagrid("getChecked");
  29. if(row == null || row.length == 0){
  30. $.messager.alert("出错了。","您还未勾选数据。", "error");
  31. return;
  32. }
  33. newdsource(true, row[0].dsid);
  34. }
  35. },{
  36. text:'删除',
  37. iconCls:'icon-cancel',
  38. handler:function(){
  39. var row = $("#dsourcetable").datagrid("getChecked");
  40. if(row == null || row.length == 0){
  41. $.messager.alert("出错了。","您还未勾选数据。", "error");
  42. return;
  43. }
  44. delDsource(row[0].dsid);
  45. }
  46. }]
  47. });
  48. }
  49. function delDsource(dsid){
  50. if(confirm("是否确认删除?")){
  51. $.ajax({
  52. url:'deleteDataSource.action',
  53. data: {dsid:dsid},
  54. type:'POST',
  55. dataType:'JSON',
  56. success:function(){
  57. $("#dsourcetable").datagrid("reload", {t:Math.random});
  58. },
  59. error:function(){
  60. msginfo("系统出错,请查看后台日志。");
  61. }
  62. });
  63. }
  64. }
  65. function newdsource(isupdate, dsid){
  66. var ds;
  67. if(isupdate){
  68. $.ajax({
  69. type:'GET',
  70. url:'getDataSource.action',
  71. dataType:'JSON',
  72. data:{dsid:dsid},
  73. async:false,
  74. success: function(resp){
  75. ds = resp;
  76. }
  77. });
  78. }
  79. var ctx = "<div id=\"dsource_tab\" style=\"height:auto; width:auto;\"><div title=\"JDBC\"><form id=\"datasourceform\" name=\"datasourceform\"><input type=\"hidden\" name=\"connstate\" id=\"connstate\"><div class=\"textpanel\"><span class=\"inputtext\">数据源名称:</span><input type=\"text\" id=\"dsname\" name=\"dsname\" class=\"inputform\" style=\"width:400px;\" value=\""+(ds&&ds.use=='jdbc'?ds.dsname:"")+"\"><br/><span class=\"inputtext\">数据源类型:</span><select id=\"linkType\" name=\"linkType\" style=\"width:400px;\" class=\"inputform\"><option value=\"mysql\" "+(ds&&ds.use=='jdbc'&&ds.linkType=='mysql'?"selected":"")+">mysql</option><option value=\"oracle\" "+(ds&&ds.use=='jdbc'&&ds.linkType=='oracle'?"selected":"")+">oracle</option><option value=\"sqlser\" "+(ds&&ds.use=='jdbc'&&ds.linkType=='sqlser'?"selected":"")+">SQL Server</option><option value=\"db2\" "+(ds&&ds.use=='jdbc'&&ds.linkType=='db2'?"selected":"")+">DB2</option><option value=\"postgresql\" "+(ds&&ds.use=='jdbc'&&ds.linkType=='postgresql'?"selected":"")+">postgresql</option><option value=\"hive\" "+(ds&&ds.use=='jdbc'&&ds.linkType=='hive'?"selected":"")+">hive</option><option value=\"kylin\" "+(ds&&ds.use=='jdbc'&&ds.linkType=='kylin'?"selected":"")+">kylin</option></select><br/><span class=\"inputtext\">连接字符串:</span><input type=\"text\" id=\"linkUrl\" name=\"linkUrl\" class=\"inputform\" style=\"width:400px;\" value=\""+(ds&&ds.use=='jdbc'?ds.linkUrl:"jdbc:mysql://ip/database?useUnicode=true&characterEncoding=UTF8")+"\"><br/><span class=\"inputtext\">连接用户名:</span><input type=\"text\" id=\"linkName\" name=\"linkName\" class=\"inputform\" style=\"width:400px;\" value=\""+(ds&&ds.use=='jdbc'?ds.linkName:"")+"\"> <br/><span class=\"inputtext\">连接密码:</span><input type=\"password\" name=\"linkPwd\" id=\"linkPwd\" style=\"width:400px;\" value=\""+(ds&&ds.use=='jdbc'?ds.linkPwd:"")+"\" class=\"inputform\"></div></form></div><div data-options=\""+(ds&&ds.use=='jndi'?"selected:true":"")+"\" title=\"JNDI\"><div class=\"textpanel\"><span class=\"inputtext\">JNDI名称:</span><input type=\"text\" value=\""+(ds&&ds.use=='jndi'?ds.jndiName:"")+"\" style=\"width:400px;\" name=\"jndiName\" id=\"jndiName\" class=\"inputform\"><br/><span class=\"inputtext\">数据源类型:</span><select id=\"jndilinktype\" name=\"jndilinktype\" style=\"width:400px;\" class=\"inputform\"><option value=\"mysql\" "+(ds&&ds.use=='jndi'&&ds.linkType=='mysql'?"selected":"")+">mysql</option><option value=\"oracle\" "+(ds&&ds.use=='jndi'&&ds.linkType=='oracle'?"selected":"")+">oracle</option><option value=\"sqlser\" "+(ds&&ds.use=='jndi'&&ds.linkType=='sqlser'?"selected":"")+">SQL Server</option><option value=\"db2\" "+(ds&&ds.use=='jndi'&&ds.linkType=='db2'?"selected":"")+">DB2</option><option value=\"postgresql\" "+(ds&&ds.use=='jndi'&&ds.linkType=='postgresql'?"selected":"")+">postgresql</option><option value=\"hive\" "+(ds&&ds.use=='jndi'&&ds.linkType=='hive'?"selected":"")+">hive</option></select></div></div></div>";
  80. $('#pdailog').dialog({
  81. title: isupdate ? "编辑数据源" : '创建数据源',
  82. width: 540,
  83. height: 300,
  84. closed: false,
  85. cache: false,
  86. modal: true,
  87. toolbar:null,
  88. content:ctx,
  89. buttons:[{
  90. text:"测试连接",
  91. handler:function(){
  92. var tab = $('#dsource_tab').tabs('getSelected');
  93. var index = $('#dsource_tab').tabs('getTabIndex',tab);
  94. if(index == 0){
  95. var param = $("#datasourceform").serialize();
  96. $.ajax({
  97. type: "POST",
  98. url: "testDataSource.action",
  99. dataType:"json",
  100. data: param,
  101. success: function(resp){
  102. if(resp.result == 1){
  103. msginfo("测试成功!", "suc");
  104. $("#datasourceform #connstate").val("y");
  105. }else{
  106. msginfo("测试失败!<br/>"+resp.msg);
  107. }
  108. },
  109. error:function(){
  110. msginfo("测试失败!");
  111. }
  112. });
  113. }else if(index == 1){
  114. var param = {"jndiName":$("#dsource_tab #jndiName").val()};
  115. $.ajax({
  116. type: "POST",
  117. url: "testJndi.action",
  118. dataType:"json",
  119. data: param,
  120. success: function(resp){
  121. if(resp.result == 1){
  122. msginfo("测试成功!", "suc");
  123. $("#datasourceform #connstate").val("y");
  124. }else{
  125. msginfo("测试失败!<br/>"+resp.msg);
  126. }
  127. },
  128. error:function(){
  129. msginfo("测试失败!");
  130. }
  131. });
  132. }
  133. }
  134. },{
  135. text:'确定',
  136. iconCls:"icon-ok",
  137. handler:function(){
  138. var tab = $('#dsource_tab').tabs('getSelected');
  139. var index = $('#dsource_tab').tabs('getTabIndex',tab);
  140. if(index == 0){
  141. if($("#datasourceform #dsname").val() == ''){
  142. msginfo("请输入数据源名称!");
  143. $("#datasourceform #dsname").focus();
  144. return;
  145. }
  146. if($("#datasourceform #connstate").val() != "y"){
  147. msginfo("请先测试连接正常再确定!");
  148. return;
  149. }
  150. if(isupdate == false){
  151. var ds = {"linkType":$("#linkType").val(), "linkName":$("#linkName").val(), "linkPwd":$("#linkPwd").val(), "linkUrl":$("#linkUrl").val(),"dsname":$("#datasourceform #dsname").val(),"dsid":newGuid(),"use":"jdbc"};
  152. $.ajax({
  153. url:'saveDataSource.action',
  154. data: $.param(ds),
  155. type:'POST',
  156. dataType:'JSON',
  157. success:function(){
  158. $("#dsourcetable").datagrid("reload", {t:Math.random});
  159. },
  160. error:function(){
  161. msginfo("系统出错,请查看后台日志。");
  162. }
  163. });
  164. }else{
  165. var nds = {"linkType":$("#linkType").val(), "linkName":$("#linkName").val(), "linkPwd":$("#linkPwd").val(), "linkUrl":$("#linkUrl").val(),"dsname":$("#datasourceform #dsname").val(),"dsid":dsid,"use":"jdbc"};
  166. $.ajax({
  167. url:'updateDataSource.action',
  168. data:nds,
  169. type:'POST',
  170. dataType:'JSON',
  171. success:function(){
  172. $("#dsourcetable").datagrid("reload", {t:Math.random});
  173. },
  174. error:function(){
  175. msginfo("系统出错,请查看后台日志。");
  176. }
  177. });
  178. }
  179. }else if(index == 1){
  180. if($("#dsource_tab #jndiName").val() == ''){
  181. msginfo("请输入JNDI名称!");
  182. $("#dsource_tab #jndiName").focus();
  183. return;
  184. }
  185. if($("#datasourceform #connstate").val() != "y"){
  186. msginfo("请先测试连接正常再确定!");
  187. return;
  188. }
  189. if(isupdate == false){
  190. var ds = {"dsname":$("#dsource_tab #jndiName").val(),"linkType":$("#dsource_tab #jndilinktype").val(),"dsid":newGuid(),"use":"jndi"};
  191. $.ajax({
  192. url:'saveDataSource.action',
  193. data: ds,
  194. type:'POST',
  195. dataType:'html',
  196. success:function(){
  197. $("#dsourcetable").datagrid("reload", {t:Math.random});
  198. },
  199. error:function(){
  200. msginfo("系统出错,请查看后台日志。");
  201. }
  202. });
  203. }else{
  204. var ds = {"dsname":$("#dsource_tab #jndiname").val(),"linkType":$("#dsource_tab #jndilinktype").val(),"dsid":dsid,"use":"jndi"};
  205. $.ajax({
  206. url:'updateDataSource.action',
  207. data:ds,
  208. type:'POST',
  209. dataType:'html',
  210. success:function(){
  211. $("#dsourcetable").datagrid("reload", {t:Math.random});
  212. },
  213. error:function(){
  214. msginfo("系统出错,请查看后台日志。");
  215. }
  216. });
  217. }
  218. }
  219. $('#pdailog').dialog('close');
  220. }
  221. },{
  222. text:'取消',
  223. iconCls:"icon-cancel",
  224. handler:function(){
  225. $('#pdailog').dialog('close');
  226. }
  227. }]
  228. });
  229. $("#pdailog #dsource_tab").tabs({
  230. fit:true,border:false
  231. });
  232. $("#pdailog #linkType").change(function(){
  233. var val = $(this).val();
  234. if(val == "mysql"){
  235. $("#pdailog #linkUrl").val("jdbc:mysql://ip/database?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8");
  236. }else if(val == "oracle"){
  237. $("#pdailog #linkUrl").val("jdbc:oracle:thin:@ip:1521/sid");
  238. }else if(val == "sqlser"){
  239. $("#pdailog #linkUrl").val("jdbc:jtds:sqlserver://ip:1433/database");
  240. }else if(val == "db2"){
  241. $("#pdailog #linkUrl").val("jdbc:db2://ip:50000/database");
  242. }else if(val == "postgresql"){
  243. $("#pdailog #linkUrl").val("jdbc:postgresql://ip:5432/database");
  244. }else if(val == "hive"){
  245. $("#pdailog #linkUrl").val("jdbc:hive2://ip:10000/default");
  246. }else if(val == "kylin"){
  247. $("#pdailog #linkUrl").val("jdbc:kylin://ip:7070/kylin_project_name");
  248. }
  249. });
  250. }