Statement.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. },
  40. autoSave: function(){
  41. this.autoSaveTimerID = window.setInterval(function(){
  42. if (!this.autoSaveCheckNode) this.autoSaveCheckNode = this.designer.contentToolbarNode.getElement("#MWFAutoSaveCheck");
  43. if (this.autoSaveCheckNode){
  44. if (this.autoSaveCheckNode.get("checked")){
  45. this.save();
  46. }
  47. }
  48. }.bind(this), 60000);
  49. },
  50. load : function(){
  51. // this.setAreaNodeSize();
  52. // this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  53. this.areaNode.inject(this.node);
  54. this.designer.statementListAreaNode.getChildren().each(function(node){
  55. var statement = node.retrieve("statement");
  56. if (statement.id==this.data.id){
  57. if (this.designer.currentListStatementItem){
  58. this.designer.currentListStatementItem.setStyles(this.designer.css.listStatementItem);
  59. }
  60. node.setStyles(this.designer.css.listStatementItem_current);
  61. this.designer.currentListStatementItem = node;
  62. this.lisNode = node;
  63. }
  64. }.bind(this));
  65. this.loadStatement();
  66. this.showProperty();
  67. },
  68. showProperty: function(){
  69. if (!this.property){
  70. this.property = new MWF.xApplication.query.TableDesigner.Property(this, this.designer.designerContentArea, this.designer, {
  71. "path": this.options.propertyPath,
  72. "onPostLoad": function(){
  73. this.property.show();
  74. }.bind(this)
  75. });
  76. this.property.load();
  77. }else{
  78. this.property.show();
  79. }
  80. },
  81. loadStatement: function(){
  82. //this.statementDesignerNode = new Element("div", {"styles": this.css.statementDesignerNode}).inject(this.areaNode);
  83. this.loadStatementHtml(function(){
  84. this.designerArea = this.areaNode.getElement(".o2_statement_statementDesignerNode");
  85. this.jpqlTypeSelect = this.areaNode.getElement("select");
  86. this.tableSelect = this.areaNode.getElement(".o2_statement_statementDesignerSelectTable");
  87. this.tableArea = this.areaNode.getElement(".o2_statement_statementDesignerTableContent");
  88. this.jpqlEditorNode = this.areaNode.getElement(".o2_statement_statementDesignerJpqlLine");
  89. this.runArea = this.areaNode.getElement(".o2_statement_statementRunNode");
  90. this.runTitleNode = this.areaNode.getElement(".o2_statement_statementRunTitleNode");
  91. this.runContentNode = this.areaNode.getElement(".o2_statement_statementRunContentNode");
  92. this.runJsonNode = this.runContentNode.getFirst();
  93. this.runActionNode = this.runJsonNode.getNext();
  94. this.runResultNode = this.runContentNode.getLast();
  95. this.setRunnerSize();
  96. this.designer.addEvent("resize", this.setRunnerSize.bind(this));
  97. this.loadStatementEditor();
  98. this.loadStatementRunner();
  99. this.setEvent();
  100. }.bind(this));
  101. },
  102. setRunnerSize: function(){
  103. debugger;
  104. var size = this.areaNode.getSize();
  105. var designerSize = this.designerArea.getComputedSize();
  106. var y = size.y-designerSize.totalHeight;
  107. var mTop = this.runArea.getStyle("margin-top").toInt();
  108. var mBottom = this.runArea.getStyle("margin-bottom").toInt();
  109. var pTop = this.runArea.getStyle("padding-top").toInt();
  110. var pBottom = this.runArea.getStyle("padding-bottom").toInt();
  111. y = y-mTop-mBottom-pTop-pBottom-1;
  112. this.runArea.setStyle("height", ""+y+"px");
  113. var titleSize = this.runTitleNode.getComputedSize();
  114. y = y - titleSize.totalHeight;
  115. this.runContentNode.setStyle("height", ""+y+"px");
  116. },
  117. loadStatementEditor: function(){
  118. o2.require("o2.widget.JavascriptEditor", function(){
  119. this.editor = new o2.widget.JavascriptEditor(this.jpqlEditorNode, {"title": "JPQL", "option": {"mode": "sql"}});
  120. this.editor.load(function(){
  121. if (this.json.table){
  122. o2.Actions.get("x_query_assemble_designer").getTable(this.json.table, function(json){
  123. this.json.tableObj = json.data;
  124. this.tableArea.set("text", json.data.name);
  125. this.setSatementTable();
  126. }.bind(this))
  127. }else{
  128. this.setSatementTable();
  129. }
  130. this.editor.editor.on("change", function(){
  131. this.checkJpqlType();
  132. }.bind(this));
  133. }.bind(this));
  134. }.bind(this), false);
  135. },
  136. setSatementTable: function(){
  137. if (!this.json.type) this.json.type = "select";
  138. this.changeType(this.json.type, true);
  139. if (this.json.data){
  140. this.editor.editor.setValue(this.json.data);
  141. }else{
  142. var table = (this.json.tableObj) ? this.json.tableObj.name : "table";
  143. switch (this.json.type) {
  144. case "update":
  145. this.editor.editor.setValue("update "+table+" o set ");
  146. break;
  147. case "delete":
  148. this.editor.editor.setValue("select "+table+" o where ");
  149. break;
  150. default:
  151. this.editor.editor.setValue("select o from "+table+" o");
  152. }
  153. }
  154. },
  155. checkJpqlType: function(){
  156. var str = this.editor.editor.getValue();
  157. this.json.data = str;
  158. var jpql_select = /^select/i;
  159. var jpql_update = /^update/i;
  160. var jpql_delete = /^delete/i;
  161. if (jpql_select.test(str)) return this.changeType("select");
  162. if (jpql_update.test(str)) return this.changeType("update");
  163. if (jpql_delete.test(str)) return this.changeType("delete");
  164. },
  165. changeType: function(type, force){
  166. if (this.json.type!=type) this.json.type=type;
  167. if (type != this.jpqlTypeSelect.options[this.jpqlTypeSelect.selectedIndex].value || force){
  168. for (var i=0; i<this.jpqlTypeSelect.options.length; i++){
  169. if (this.jpqlTypeSelect.options[i].value==type){
  170. this.jpqlTypeSelect.options[i].set("selected", true);
  171. break;
  172. }
  173. }
  174. }
  175. },
  176. loadStatementHtml: function(callback){
  177. this.areaNode.loadAll({
  178. "css": this.path+this.options.style+"/statement.css",
  179. "html": this.path+"statementDesigner.html"
  180. }, {
  181. "bind": {"lp": this.designer.lp}
  182. },function(){
  183. if (callback) callback();
  184. }.bind(this));
  185. },
  186. loadStatementRunner: function(){
  187. o2.require("o2.widget.JavascriptEditor", function(){
  188. this.jsonEditor = new o2.widget.JavascriptEditor(this.runJsonNode, {"title": "JPQL", "option": {"mode": "json"}});
  189. this.jsonEditor.load(function(){
  190. this.jsonEditor.editor.setValue("{}");
  191. }.bind(this));
  192. }.bind(this), false);
  193. },
  194. setEvent: function(){
  195. this.runActionNode.getFirst().addEvent("click", this.runStatement.bind(this));
  196. this.tableSelect.addEvent("click", this.selectTable.bind(this));
  197. this.jpqlTypeSelect.addEvent("change", function(){
  198. var t = this.jpqlTypeSelect.options[this.jpqlTypeSelect.selectedIndex].value;
  199. if (t!=this.json.type) this.json.type=t;
  200. }.bind(this));
  201. },
  202. selectTable: function(){
  203. new MWF.O2Selector(this.designer.content, {
  204. "type": "queryTable",
  205. "count": 1,
  206. "values": (this.json.table) ? [this.json.table] : [],
  207. "title": this.designer.lp.selectTable,
  208. "onComplete": function(items){
  209. if (items.length){
  210. var id = items[0].data.id;
  211. var name = items[0].data.name;
  212. this.tableArea.set("text", name);
  213. this.json.table = name;
  214. this.json.tableObj = items[0].data;
  215. }else{
  216. this.tableArea.set("text", "");
  217. this.json.table = "";
  218. }
  219. }.bind(this)
  220. });
  221. },
  222. runStatement:function(){
  223. debugger;
  224. if (!this.json.data){
  225. this.designer.notice(this.designer.lp.inputStatementData, "error");
  226. return false;
  227. }
  228. o2.require("o2.widget.Mask", null, false);
  229. this.runMask = new o2.widget.Mask();
  230. this.runMask.loadNode(this.node);
  231. this.saveSilence(function(){
  232. var json = this.jsonEditor.editor.getValue();
  233. var o = JSON.parse(json);
  234. o2.Actions.get("x_query_assemble_designer").executeStatement(this.json.id, 1, 10 , o, function(json){
  235. o2.require("o2.widget.JsonParse", function(){
  236. this.runResultNode.empty();
  237. var jsonResult = new o2.widget.JsonParse(json.data, this.runResultNode);
  238. jsonResult.load();
  239. }.bind(this));
  240. this.runMask.hide();
  241. }.bind(this) )
  242. }.bind(this));
  243. },
  244. save: function(callback){
  245. if (!this.data.name){
  246. this.designer.notice(this.designer.lp.inputStatementName, "error");
  247. return false;
  248. }
  249. //if( !this.data.tableType ){
  250. // this.data.tableType = "dynamic";
  251. //}
  252. this.designer.actions.saveStatement(this.data, function(json){
  253. this.designer.notice(this.designer.lp.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  254. this.data.id = json.data.id;
  255. if (this.lisNode) {
  256. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  257. }
  258. if (callback) callback();
  259. }.bind(this));
  260. },
  261. _setEditStyle: function(){},
  262. saveSilence: function(callback){
  263. if (!this.data.name){
  264. this.designer.notice(this.designer.lp.inputStatementName, "error");
  265. return false;
  266. }
  267. this.designer.actions.saveStatement(this.data, function(json){
  268. //this.designer.notice(this.designer.lp.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  269. this.data.id = json.data.id;
  270. if (this.lisNode) {
  271. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  272. }
  273. if (callback) callback();
  274. }.bind(this));
  275. }
  276. });