RestActions.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. MWF.xDesktop.requireApp("process.ProcessManager", "package", null, false);
  2. MWF.xApplication.process.ProcessManager.RestActions = new Class({
  3. initialize: function(actions){
  4. this.actions = actions;
  5. this.designAddress = "";
  6. },
  7. listApplicationAddress: function(success, failure){
  8. var url = this.actions.listAddress;
  9. url = this.actions.slotHost+url;
  10. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  11. MWF.getJSON(url, callback);
  12. },
  13. getDesignAddress: function(success, failure){
  14. // var name = "x_processplatform_core_designer";
  15. // var url = this.actions.getAddress.replace(/{id}/g, name);
  16. // url = this.actions.slotHost+url;
  17. // var callback = new MWF.process.RestActions.Callback(success, failure, function(data){
  18. // this.designAddress = data.data.url;
  19. // }.bind(this));
  20. // MWF.getJSON(url, callback);
  21. this.designAddress = "http://xa01.zoneland.net:9080/x_processplatform_core_designer";
  22. //this.designAddress = "http://localhost:9080/x_processplatform_front_designer";
  23. if (success) success.apply();
  24. },
  25. request: function(success, failure, type, arg){
  26. if (this.designAddress){
  27. this["_"+type](success, failure, arg);
  28. }else{
  29. this.getDesignAddress(function(data){
  30. this["_"+type](success, failure, arg);
  31. }.bind(this), failure);
  32. }
  33. },
  34. _getId: function(success, failure, count){
  35. var url = this.designAddress+this.actions.getId;
  36. url = url.replace(/{count}/g, count);
  37. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  38. MWF.getJSON(url, callback);
  39. },
  40. getId: function(count, success, failure){
  41. this.request(success, failure, "getId", count);
  42. },
  43. getUUID: function(){
  44. if (!this.designAddress) this.getDesignAddress();
  45. var url = this.designAddress+this.actions.getId;
  46. url = url.replace(/{count}/g, "1");
  47. var id = "";
  48. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(function(ids){
  49. id = ids.data[0].id;
  50. }, null);
  51. MWF.getJSON(url, callback, false);
  52. return id;
  53. },
  54. _listFormCategory: function(success, failure, sign){
  55. var url = this.designAddress+this.actions.listFormCategory;
  56. url = url.replace(/{id}/g, sign.lastId ? sign.lastId : "(0)");
  57. url = url.replace(/{count}/g, sign.count ? sign.count : "20");
  58. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  59. MWF.getJSON(url, callback);
  60. },
  61. listFormCategory: function(lastId, count, success, failure){
  62. this.request(success, failure, "listFormCategory", {"lastId": lastId, "count": count});
  63. },
  64. _listProcessCategory: function(success, failure, sign){
  65. var url = this.designAddress+this.actions.listProcessCategory;
  66. url = url.replace(/{id}/g, sign.lastId ? sign.lastId : "(0)");
  67. url = url.replace(/{count}/g, sign.count ? sign.count : "20");
  68. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  69. MWF.getJSON(url, callback);
  70. },
  71. listProcessCategory: function(lastId, count, success, failure){
  72. this.request(success, failure, "listProcessCategory", {"lastId": lastId, "count": count});
  73. },
  74. _listForm: function(success, failure, arg){
  75. var url = this.designAddress+this.actions.listForm;
  76. url = url.replace(/{cid}/g, arg.category);
  77. url = url.replace(/{id}/g, arg.last ? arg.last : "(0)");
  78. url = url.replace(/{count}/g, arg.count ? arg.count : "20");
  79. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  80. MWF.getJSON(url, callback);
  81. },
  82. listForm: function(category, last, count, success, failure){
  83. this.request(success, failure, "listForm", {"category": category, "last": last, "count": count});
  84. },
  85. _listProcess: function(success, failure, arg){
  86. var url = this.designAddress+this.actions.listProcess;
  87. url = url.replace(/{cid}/g, arg.category);
  88. url = url.replace(/{id}/g, arg.last ? arg.last : "(0)");
  89. url = url.replace(/{count}/g, arg.count ? arg.count : "20");
  90. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  91. MWF.getJSON(url, callback);
  92. },
  93. listProcess: function(category, last, count, success, failure){
  94. this.request(success, failure, "listProcess", {"category": category, "last": last, "count": count});
  95. },
  96. _getForm: function(success, failure, form){
  97. var url = this.designAddress+this.actions.getForm;
  98. url = url.replace(/{id}/g, form);
  99. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  100. MWF.getJSON(url, callback);
  101. },
  102. getForm: function(form, success, failure){
  103. this.request(success, failure, "getForm", form);
  104. },
  105. _getProcess: function(success, failure, process){
  106. var url = this.designAddress+this.actions.getProcess;
  107. url = url.replace(/{id}/g, process);
  108. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  109. MWF.getJSON(url, callback);
  110. },
  111. getProcess: function(process, success, failure){
  112. this.request(success, failure, "getProcess", process);
  113. },
  114. _getProcessCategory: function(success, failure, id){
  115. var url = this.designAddress+this.actions.getProcessCategory;
  116. url = url.replace(/{id}/g, id);
  117. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  118. MWF.getJSON(url, callback);
  119. },
  120. getProcessCategory: function(id, success, failure){
  121. this.request(success, failure, "getProcessCategory", id);
  122. },
  123. _getFormCategory: function(success, failure, id){
  124. var url = this.designAddress+this.actions.getFormCategory;
  125. url = url.replace(/{id}/g, id);
  126. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  127. MWF.getJSON(url, callback);
  128. },
  129. getFormCategory: function(id, success, failure){
  130. this.request(success, failure, "getFormCategory", id);
  131. },
  132. saveProcessCategory: function(categoryData, success, failure){
  133. if (this.designAddress){
  134. if (categoryData.id){
  135. this.updateProcessCategory(categoryData, success, failure);
  136. }else{
  137. this.addProcessCategory(categoryData, success, failure);
  138. }
  139. }else{
  140. this.getDesignAddress(function(data){
  141. if (categoryData.id){
  142. this.updateProcessCategory(categoryData, success, failure);
  143. }else{
  144. this.addProcessCategory(categoryData, success, failure);
  145. }
  146. }.bind(this), failure);
  147. }
  148. },
  149. updateProcessCategory: function(categoryData, success, failure){
  150. var address = this.designAddress+this.actions.updataProcessCategory;
  151. address = address.replace(/{id}/g, categoryData.id);
  152. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  153. MWF.restful("PUT", address, JSON.encode(categoryData), callback);
  154. },
  155. addProcessCategory: function(categoryData, success, failure){
  156. this.getId(1, function(json){
  157. categoryData.id = json.data[0].id;
  158. var address = this.designAddress+this.actions.addProcessCategory;
  159. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  160. MWF.restful("POST", address, JSON.encode(categoryData), callback);
  161. }.bind(this));
  162. },
  163. saveProcess: function(processData, success, failure){
  164. if (this.designAddress){
  165. if (!processData.isNewProcess){
  166. this.updateProcess(processData, success, failure);
  167. }else{
  168. this.addProcess(processData, success, failure);
  169. }
  170. }else{
  171. this.getDesignAddress(function(data){
  172. if (!processData.isNewProcess){
  173. this.updateProcess(processData, success, failure);
  174. }else{
  175. this.addProcess(processData, success, failure);
  176. }
  177. }.bind(this), failure);
  178. }
  179. },
  180. addProcess: function(processData, success, failure){
  181. var address = this.designAddress+this.actions.addProcess;
  182. address = address.replace(/{id}/g, processData.categoryId);
  183. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  184. MWF.restful("POST", address, JSON.encode(processData), callback);
  185. },
  186. updateProcess: function(processData, success, failure){
  187. var address = this.designAddress+this.actions.updataProcess;
  188. address = address.replace(/{id}/g, processData.id);
  189. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  190. MWF.restful("PUT", address, JSON.encode(processData), callback);
  191. },
  192. _deleteProcess: function(success, failure, id){
  193. var url = this.designAddress+this.actions.removeProcess;
  194. url = url.replace(/{id}/g, id);
  195. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  196. MWF.restful("DELETE", url, "", callback);
  197. },
  198. deleteProcess: function(id, success, failure){
  199. this.request(success, failure, "deleteProcess", id);
  200. },
  201. saveFormCategory: function(categoryData, success, failure){
  202. if (this.designAddress){
  203. if (categoryData.id){
  204. this.updateFormCategory(categoryData, success, failure);
  205. }else{
  206. this.addFormCategory(categoryData, success, failure);
  207. }
  208. }else{
  209. this.getDesignAddress(function(data){
  210. if (categoryData.id){
  211. this.updateFormCategory(categoryData, success, failure);
  212. }else{
  213. this.addFormCategory(categoryData, success, failure);
  214. }
  215. }.bind(this), failure);
  216. }
  217. },
  218. updateFormCategory: function(categoryData, success, failure){
  219. var address = this.designAddress+this.actions.updataFormCategory;
  220. address = address.replace(/{id}/g, categoryData.id);
  221. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  222. MWF.restful("PUT", address, JSON.encode(categoryData), callback);
  223. },
  224. addFormCategory: function(categoryData, success, failure){
  225. this.getId(1, function(json){
  226. categoryData.id = json.data[0].id;
  227. var address = this.designAddress+this.actions.addFormCategory;
  228. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  229. MWF.restful("POST", address, JSON.encode(categoryData), callback);
  230. }.bind(this));
  231. },
  232. saveForm: function(formData, success, failure){
  233. if (this.designAddress){
  234. if (!formData.isNewForm){
  235. this.updateForm(formData, success, failure);
  236. }else{
  237. this.addForm(formData, success, failure);
  238. }
  239. }else{
  240. this.getDesignAddress(function(data){
  241. if (!formData.isNewForm){
  242. this.updateForm(formData, success, failure);
  243. }else{
  244. this.addForm(formData, success, failure);
  245. }
  246. }.bind(this), failure);
  247. }
  248. },
  249. updateForm: function(formData, success, failure){
  250. var address = this.designAddress+this.actions.updataForm;
  251. address = address.replace(/{id}/g, formData.json.id);
  252. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  253. var data = MWF.encodeJsonString(JSON.encode(formData));
  254. var json = "{\"id\":\""+formData.json.id+"\", \"data\":\""+data+"\", \"name\":\""+formData.json.name+"\", \"alias\":\""+formData.json.name+"\", \"description\":\""+formData.json.description+"\", \"formCategory\": \""+formData.json.formCategory+"\"}";
  255. MWF.restful("PUT", address, json, callback);
  256. },
  257. addForm: function(formData, success, failure){
  258. var address = this.designAddress+this.actions.addForm;
  259. address = address.replace(/{id}/g, formData.json.categoryId);
  260. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  261. var data = MWF.encodeJsonString(JSON.encode(formData));
  262. alert(data)
  263. var json = "{\"id\":\""+formData.json.id+"\", \"data\": \""+data+"\", \"name\":\""+formData.json.name+"\", \"alias\":\""+formData.json.name+"\", \"description\":\""+formData.json.description+"\", \"formCategory\": \""+formData.json.formCategory+"\"}";
  264. MWF.restful("POST", address, json, callback);
  265. },
  266. _deleteForm: function(success, failure, id){
  267. var url = this.designAddress+this.actions.removeForm;
  268. url = url.replace(/{id}/g, id);
  269. var callback = new MWF.xApplication.process.ProcessManager.RestActions.Callback(success, failure);
  270. MWF.restful("DELETE", url, "", callback);
  271. },
  272. deleteForm: function(id, success, failure){
  273. this.request(success, failure, "deleteForm", id);
  274. }
  275. });
  276. MWF.xApplication.process.ProcessManager.RestActions.Callback = new Class({
  277. initialize: function(success, failure, appendSuccess, appendFailure){
  278. this.success = success;
  279. this.failure = failure;
  280. this.appendSuccess = appendSuccess;
  281. this.appendFailure = appendFailure;
  282. },
  283. onSuccess: function(responseJSON, responseText){
  284. switch(responseJSON.type) {
  285. case "success":
  286. if (this.appendSuccess) this.appendSuccess(responseJSON);
  287. if (this.success) this.success(responseJSON);
  288. break;
  289. case "warn":
  290. MWF.xDesktop.notice("info", {x: "right", y:"top"}, responseJSON.errorMessage.join("\n"));
  291. if (this.appendSuccess) this.appendSuccess(responseJSON);
  292. if (this.success) this.success(responseJSON);
  293. break;
  294. case "error":
  295. this.doError(null, responseText, responseJSON.message);
  296. break;
  297. }
  298. },
  299. onRequestFailure: function(xhr){
  300. this.doError(xhr, "", "");
  301. },
  302. onFailure: function(xhr){
  303. this.doError(xhr, "", "");
  304. },
  305. onError: function(text, error){
  306. this.doError(null, text, error);
  307. },
  308. doError: function(xhr, text, error){
  309. if (this.appendFailure) this.appendFailure(xhr, text, error);
  310. if (this.failure) this.failure(xhr, text, error);
  311. if (!this.failure && !this.appendFailure){
  312. var errorText = error;
  313. if (xhr) errorText = xhr.responseText;
  314. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  315. // throw "request error: "+errorText;
  316. }
  317. }
  318. });