Statement.js 20 KB

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