Statement.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.query = MWF.xApplication.query || {};
  3. MWF.xApplication.query.StatementDesigner = MWF.xApplication.query.StatementDesigner || {};
  4. MWF.APPDSMD = MWF.xApplication.query.StatementDesigner;
  5. MWF.xDesktop.requireApp("query.StatementDesigner", "lp."+MWF.language, null, false);
  6. //MWF.xDesktop.requireApp("query.StatementDesigner", "Property", null, false);
  7. MWF.xDesktop.requireApp("query.TableDesigner", "Property", null, false);
  8. o2.require("o2.widget.JavascriptEditor", null, false);
  9. MWF.xApplication.query.StatementDesigner.Statement = new Class({
  10. Extends: MWF.widget.Common,
  11. Implements: [Options, Events],
  12. options: {
  13. "style": "default",
  14. "isView": false,
  15. "showTab": true,
  16. "propertyPath": "/x_component_query_StatementDesigner/$Statement/statement.html"
  17. },
  18. initialize: function(designer, data, options){
  19. this.setOptions(options);
  20. this.path = "/x_component_query_StatementDesigner/$Statement/";
  21. this.cssPath = "/x_component_query_StatementDesigner/$Statement/"+this.options.style+"/css.wcss";
  22. this._loadCss();
  23. this.designer = designer;
  24. this.data = data;
  25. this.parseData();
  26. this.node = this.designer.designNode;
  27. this.areaNode = new Element("div", {"styles": {"height": "100%", "overflow": "auto"}});
  28. //this.statementRunNode = this.designer.designerStatementArea;
  29. if(this.designer.application) this.data.applicationName = this.designer.application.name;
  30. if(this.designer.application) this.data.application = this.designer.application.id;
  31. this.isNewStatement = (this.data.id) ? false : true;
  32. this.view = this;
  33. this.autoSave();
  34. this.designer.addEvent("queryClose", function(){
  35. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  36. }.bind(this));
  37. },
  38. parseData: function(){
  39. this.json = this.data;
  40. if (!this.json.type) this.json.type = "select";
  41. if (!this.json.format) this.json.format = "jpql";
  42. if (!this.json.entityCategory) this.json.entityCategory = "official";
  43. if (!this.json.entityClassName) this.json.entityClassName = "com.x.processplatform.core.entity.content.Task";
  44. },
  45. autoSave: function(){
  46. this.autoSaveTimerID = window.setInterval(function(){
  47. if (!this.autoSaveCheckNode) this.autoSaveCheckNode = this.designer.contentToolbarNode.getElement("#MWFAutoSaveCheck");
  48. if (this.autoSaveCheckNode){
  49. if (this.autoSaveCheckNode.get("checked")){
  50. this.save();
  51. }
  52. }
  53. }.bind(this), 60000);
  54. },
  55. load : function(){
  56. // this.setAreaNodeSize();
  57. // this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  58. this.areaNode.inject(this.node);
  59. this.designer.statementListAreaNode.getChildren().each(function(node){
  60. var statement = node.retrieve("statement");
  61. if (statement.id==this.data.id){
  62. if (this.designer.currentListStatementItem){
  63. this.designer.currentListStatementItem.setStyles(this.designer.css.listStatementItem);
  64. }
  65. node.setStyles(this.designer.css.listStatementItem_current);
  66. this.designer.currentListStatementItem = node;
  67. this.lisNode = node;
  68. }
  69. }.bind(this));
  70. this.loadStatement();
  71. this.showProperty();
  72. },
  73. showProperty: function(){
  74. if (!this.property){
  75. this.property = new MWF.xApplication.query.TableDesigner.Property(this, this.designer.designerContentArea, this.designer, {
  76. "path": this.options.propertyPath,
  77. "onPostLoad": function(){
  78. this.property.show();
  79. }.bind(this)
  80. });
  81. this.property.load();
  82. }else{
  83. this.property.show();
  84. }
  85. },
  86. loadStatement: function(){
  87. //this.statementDesignerNode = new Element("div", {"styles": this.css.statementDesignerNode}).inject(this.areaNode);
  88. this.loadStatementHtml(function(){
  89. this.designerArea = this.areaNode.getElement(".o2_statement_statementDesignerNode");
  90. this.jpqlArea = this.areaNode.getElement(".o2_statement_statementDesignerJpql");
  91. this.scriptArea = this.areaNode.getElement(".o2_statement_statementDesignerScript");
  92. this.formatTypeArea = this.areaNode.getElement(".o2_statement_statementDesignerFormatContent");
  93. this.entityCategorySelect = this.areaNode.getElement(".o2_statement_statementDesignerCategoryContent").getElement("select");
  94. this.dynamicTableArea = this.areaNode.getElement(".o2_statement_statementDesignerTableArea_dynamic");
  95. this.officialTableArea = this.areaNode.getElement(".o2_statement_statementDesignerTableArea_official");
  96. this.customTableArea = this.areaNode.getElement(".o2_statement_statementDesignerTableArea_custom");
  97. this.dynamicTableSelect = this.areaNode.getElement(".o2_statement_statementDesignerSelectTable");
  98. this.officialTableSelect = this.officialTableArea.getElement("select");
  99. this.dynamicTableContent = this.areaNode.getElement(".o2_statement_statementDesignerTableContent");
  100. this.jpqlTypeSelect = this.areaNode.getElement(".o2_statement_statementDesignerTypeContent").getElement("select");
  101. // this.jpqlSelectEditor = this.areaNode.getElement(".o2_statement_statementDesignerJpql_select");
  102. // this.jpqlUpdateEditor = this.areaNode.getElement(".o2_statement_statementDesignerJpql_update");
  103. // this.jpqlDeleteEditor = this.areaNode.getElement(".o2_statement_statementDesignerJpql_sdelete");
  104. // this.jpqlSelectEditor_selectContent= this.jpqlSelectEditor.getElement(".o2_statement_statementDesignerJpql_jpql_selectContent");
  105. // this.jpqlSelectEditor_fromContent= this.jpqlSelectEditor.getElement(".o2_statement_statementDesignerJpql_jpql_fromContent");
  106. // this.jpqlSelectEditor_whereContent= this.jpqlSelectEditor.getElement(".o2_statement_statementDesignerJpql_jpql_whereContent");
  107. this.jpqlEditorNode = this.areaNode.getElement(".o2_statement_statementDesignerJpqlLine");
  108. this.runArea = this.areaNode.getElement(".o2_statement_statementRunNode");
  109. this.runTitleNode = this.areaNode.getElement(".o2_statement_statementRunTitleNode");
  110. this.runContentNode = this.areaNode.getElement(".o2_statement_statementRunContentNode");
  111. this.runJsonNode = this.runContentNode.getFirst();
  112. this.runActionNode = this.runJsonNode.getNext();
  113. this.runResultNode = this.runContentNode.getLast();
  114. this.setRunnerSize();
  115. this.designer.addEvent("resize", this.setRunnerSize.bind(this));
  116. if (this.json.format=="script"){
  117. this.loadStatementScriptEditor();
  118. }else{
  119. this.loadStatementEditor();
  120. }
  121. this.loadStatementRunner();
  122. this.setEvent();
  123. }.bind(this));
  124. },
  125. loadStatementScriptEditor: function(){
  126. if (! this.scriptEditor){
  127. debugger;
  128. o2.require("o2.widget.ScriptArea", function(){
  129. this.scriptEditor = new o2.widget.ScriptArea(this.scriptArea, {
  130. "isbind": false,
  131. "maxObj": this.designer.designNode,
  132. "title": this.designer.lp.scriptTitle,
  133. "onChange": function(){
  134. this.json.scriptText = this.scriptEditor.toJson().code;
  135. }.bind(this)
  136. });
  137. this.scriptEditor.load({"code": this.json.scriptText})
  138. }.bind(this), false);
  139. }
  140. },
  141. setRunnerSize: function(){
  142. debugger;
  143. var size = this.areaNode.getSize();
  144. var designerSize = this.designerArea.getComputedSize();
  145. var y = size.y-designerSize.totalHeight;
  146. var mTop = this.runArea.getStyle("margin-top").toInt();
  147. var mBottom = this.runArea.getStyle("margin-bottom").toInt();
  148. var pTop = this.runArea.getStyle("padding-top").toInt();
  149. var pBottom = this.runArea.getStyle("padding-bottom").toInt();
  150. y = y-mTop-mBottom-pTop-pBottom-1;
  151. this.runArea.setStyle("height", ""+y+"px");
  152. var titleSize = this.runTitleNode.getComputedSize();
  153. y = y - titleSize.totalHeight;
  154. this.runContentNode.setStyle("height", ""+y+"px");
  155. },
  156. loadStatementEditor: function(){
  157. if (!this.editor){
  158. o2.require("o2.widget.JavascriptEditor", function(){
  159. this.editor = new o2.widget.JavascriptEditor(this.jpqlEditorNode, {"title": "JPQL", "option": {"mode": "sql"}});
  160. this.editor.load(function(){
  161. if (this.json.data){
  162. this.editor.editor.setValue(this.json.data);
  163. }else{
  164. var table = "table";
  165. switch (this.json.type) {
  166. case "update":
  167. this.editor.editor.setValue("UPDATE "+table+" o SET ");
  168. break;
  169. case "delete":
  170. this.editor.editor.setValue("DELETE "+table+" o WHERE ");
  171. break;
  172. default:
  173. this.editor.editor.setValue("SELECT * FROM "+table+" o");
  174. }
  175. }
  176. this.json.data = this.editor.editor.getValue();
  177. this.editor.addEditorEvent("change", function(){
  178. debugger;
  179. this.data.data = this.editor.getValue();
  180. this.checkJpqlType();
  181. }.bind(this));
  182. // this.editor.editor.on("change", function(){
  183. // this.data.data = this.editor.getValue();
  184. // this.checkJpqlType();
  185. // }.bind(this));
  186. }.bind(this));
  187. }.bind(this), false);
  188. }
  189. },
  190. setSatementTable: function(){
  191. if (!this.json.type) this.json.type = "select";
  192. this.changeType(this.json.type, true);
  193. if (this.json.data){
  194. this.editor.editor.setValue(this.json.data);
  195. }else{
  196. var table = (this.json.tableObj) ? this.json.tableObj.name : "table";
  197. switch (this.json.type) {
  198. case "update":
  199. this.editor.editor.setValue("UPDATE "+table+" o SET ");
  200. break;
  201. case "delete":
  202. this.editor.editor.setValue("DELETE "+table+" o WHERE ");
  203. break;
  204. default:
  205. this.editor.editor.setValue("SELECT * FROM "+table+" o");
  206. }
  207. }
  208. },
  209. checkJpqlType: function(){
  210. var str = this.json.data;
  211. this.json.data = str;
  212. var jpql_select = /^select/i;
  213. var jpql_update = /^update/i;
  214. var jpql_delete = /^delete/i;
  215. if (jpql_select.test(str)) return this.changeType("select");
  216. if (jpql_update.test(str)) return this.changeType("update");
  217. if (jpql_delete.test(str)) return this.changeType("delete");
  218. },
  219. changeType: function(type, force){
  220. if (this.json.type!=type) this.json.type=type;
  221. if (type != this.jpqlTypeSelect.options[this.jpqlTypeSelect.selectedIndex].value || force){
  222. for (var i=0; i<this.jpqlTypeSelect.options.length; i++){
  223. if (this.jpqlTypeSelect.options[i].value==type){
  224. this.jpqlTypeSelect.options[i].set("selected", true);
  225. break;
  226. }
  227. }
  228. }
  229. },
  230. loadStatementHtml: function(callback){
  231. this.areaNode.loadAll({
  232. "css": this.path+this.options.style+"/statement.css",
  233. "html": this.path+"statementDesigner.html"
  234. }, {
  235. "bind": {"lp": this.designer.lp, "data": this.data}
  236. },function(){
  237. if (callback) callback();
  238. }.bind(this));
  239. },
  240. loadStatementRunner: function(){
  241. o2.require("o2.widget.JavascriptEditor", function(){
  242. this.jsonEditor = new o2.widget.JavascriptEditor(this.runJsonNode, {"title": "JPQL", "option": {"mode": "json"}});
  243. this.jsonEditor.load(function(){
  244. this.jsonEditor.editor.setValue("{}");
  245. }.bind(this));
  246. }.bind(this), false);
  247. },
  248. setEvent: function(){
  249. this.formatTypeArea.getElements("input").addEvent("click", function(e){
  250. if (e.target.checked){
  251. var v = e.target.get("value");
  252. if (v==="script"){
  253. this.scriptArea.show();
  254. this.jpqlArea.hide();
  255. this.loadStatementScriptEditor();
  256. }else{
  257. this.scriptArea.hide();
  258. this.jpqlArea.show();
  259. this.loadStatementEditor();
  260. }
  261. this.json.format = v;
  262. }
  263. }.bind(this));
  264. this.entityCategorySelect.addEvent("change", function(e){
  265. var entityCategory = e.target.options[e.target.selectedIndex].value;
  266. switch (entityCategory) {
  267. case "dynamic":
  268. this.officialTableArea.hide();
  269. this.dynamicTableArea.show();
  270. this.customTableArea.hide();
  271. break;
  272. case "custom":
  273. this.officialTableArea.hide();
  274. this.dynamicTableArea.hide();
  275. this.customTableArea.show();
  276. break;
  277. default:
  278. this.officialTableArea.show();
  279. this.dynamicTableArea.hide();
  280. this.customTableArea.hide();
  281. break;
  282. }
  283. this.json.entityCategory = entityCategory
  284. }.bind(this));
  285. //@todo change table
  286. this.officialTableSelect.addEvent("change", function(e){
  287. debugger;
  288. var entityClassName = e.target.options[e.target.selectedIndex].value;
  289. this.json.entityClassName = entityClassName;
  290. if (this.json.format=="jpql"){
  291. if (this.editor){
  292. var re = /(.*from\s*)/ig;
  293. if (this.json.type=="update") re = /(.*update\s*)/ig;
  294. //if (this.json.type=="select" && this.editor){
  295. var v = this.json.data;
  296. var re2 = /(\s+)/ig;
  297. var arr = re.exec(v);
  298. if (arr && arr[0]){
  299. var left = arr[0]
  300. v = v.substring(left.length, v.length);
  301. //var ar = re2.exec(v);
  302. var right = v.substring(v.indexOf(" "),v.length);
  303. this.json.data = left+entityClassName+right;
  304. this.editor.editor.setValue(this.json.data);
  305. }
  306. //}
  307. }
  308. }
  309. // var className = e.target.options[e.target.selectedIndex].value;
  310. // if (this.json.type=="select"){
  311. // this.json.data
  312. // /(select)*(where|)/g
  313. // }
  314. // }.bind(this));
  315. // this.jpqlTypeSelect.addEvent("change", function(){
  316. // var type = e.target.options[e.target.selectedIndex].value;
  317. // switch (entityCategory) {
  318. // case "update":
  319. // this.jpqlSelectEditor.hide();
  320. // this.jpqlUpdateEditor.show();
  321. // this.jpqlDeleteEditor.hide();
  322. // this.loadJpqlUpdateEditor();
  323. // break;
  324. // case "delete":
  325. // this.jpqlSelectEditor.hide();
  326. // this.jpqlUpdateEditor.hide();
  327. // this.jpqlDeleteEditor.show();
  328. // break;
  329. // default:
  330. // this.jpqlSelectEditor.show();
  331. // this.jpqlUpdateEditor.hide();
  332. // this.jpqlDeleteEditor.hide();
  333. // break;
  334. // }
  335. }.bind(this));
  336. this.runActionNode.getFirst().addEvent("click", this.runStatement.bind(this));
  337. this.dynamicTableSelect.addEvent("click", this.selectTable.bind(this));
  338. this.jpqlTypeSelect.addEvent("change", function(){
  339. var t = this.jpqlTypeSelect.options[this.jpqlTypeSelect.selectedIndex].value;
  340. if (t!=this.json.type) this.json.type=t;
  341. }.bind(this));
  342. },
  343. selectTable: function(){
  344. new MWF.O2Selector(this.designer.content, {
  345. "type": "queryTable",
  346. "count": 1,
  347. "values": (this.json.table) ? [this.json.table] : [],
  348. "title": this.designer.lp.selectTable,
  349. "onComplete": function(items){
  350. if (items.length){
  351. var id = items[0].data.id;
  352. var name = items[0].data.name;
  353. this.dynamicTableContent.set("text", name);
  354. this.json.table = name;
  355. this.json.tableObj = items[0].data;
  356. }else{
  357. this.dynamicTableContent.set("text", "");
  358. this.json.table = "";
  359. }
  360. }.bind(this)
  361. });
  362. },
  363. runStatement:function(){
  364. debugger;
  365. // if (!this.json.data){
  366. // this.designer.notice(this.designer.lp.inputStatementData, "error");
  367. // return false;
  368. // }
  369. o2.require("o2.widget.Mask", null, false);
  370. this.runMask = new o2.widget.Mask();
  371. this.runMask.loadNode(this.node);
  372. this.saveSilence(function(){
  373. var json = this.jsonEditor.editor.getValue();
  374. var o = JSON.parse(json);
  375. o2.Actions.get("x_query_assemble_designer").executeStatement(this.json.id, 1, 50 , o, function(json){
  376. o2.require("o2.widget.JsonParse", function(){
  377. this.runResultNode.empty();
  378. var jsonResult = new o2.widget.JsonParse(json.data, this.runResultNode);
  379. jsonResult.load();
  380. }.bind(this));
  381. this.runMask.hide();
  382. }.bind(this), function(xhr, text, error){
  383. debugger;
  384. if (this.runMask) this.runMask.hide();
  385. var errorText = error;
  386. if (xhr){
  387. var json = JSON.decode(xhr.responseText);
  388. if (json){
  389. errorText = json.message.trim() || "request json error";
  390. }else{
  391. errorText = "request json error: "+xhr.responseText;
  392. }
  393. }
  394. errorText = errorText.replace(/\</g, "&lt;");
  395. errorText = errorText.replace(/\</g, "&gt;");
  396. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  397. }.bind(this))
  398. }.bind(this));
  399. },
  400. save: function(callback){
  401. debugger;
  402. if (!this.data.name){
  403. this.designer.notice(this.designer.lp.inputStatementName, "error");
  404. return false;
  405. }
  406. //if( !this.data.tableType ){
  407. // this.data.tableType = "dynamic";
  408. //}
  409. if (this.editor) this.data.data = this.editor.editor.getValue();
  410. if (this.scriptEditor) this.data.scriptText = this.scriptEditor.toJson().code;
  411. this.designer.actions.saveStatement(this.data, function(json){
  412. this.designer.notice(this.designer.lp.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  413. this.data.id = json.data.id;
  414. if (this.lisNode) {
  415. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  416. }
  417. if (callback) callback();
  418. }.bind(this));
  419. },
  420. _setEditStyle: function(){},
  421. saveSilence: function(callback){
  422. if (!this.data.name){
  423. this.designer.notice(this.designer.lp.inputStatementName, "error");
  424. return false;
  425. }
  426. if (this.editor) this.data.data = this.editor.editor.getValue();
  427. if (this.scriptEditor) this.data.scriptText = this.scriptEditor.toJson().code;
  428. this.designer.actions.saveStatement(this.data, function(json){
  429. //this.designer.notice(this.designer.lp.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  430. this.data.id = json.data.id;
  431. if (this.lisNode) {
  432. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  433. }
  434. if (callback) callback();
  435. }.bind(this));
  436. }
  437. });