all-validation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //对面板上的流程图进行校验
  2. NodeWrapper = function(node) {
  3. this.node = node;
  4. this.id = node.text;
  5. this.outgoingLinks = [];
  6. Gef.each(node.outgoingConnections, function(connection) {
  7. this.outgoingLinks.push(connection.getTarget().text);
  8. }, this);
  9. this.incomingLinks = [];
  10. Gef.each(node.incomingConnections, function(connection) {
  11. this.incomingLinks.push(connection.getSource().text);
  12. }, this);
  13. this.oldInCount = this.incomingLinks.length;
  14. };
  15. NodeWrapper.prototype = {
  16. isFree: function() {
  17. return this.node.getType() == 'start'
  18. || (this.oldInCount > 0 && this.incomingLinks.length == 0);
  19. },
  20. isEntry: function() {
  21. return this.oldInCount > this.incomingLinks.length;
  22. }
  23. };
  24. Validation = function(editor) {
  25. this.editor = editor;
  26. this.processEditPart = editor.getGraphicalViewer().getContents();
  27. this.processModel = this.processEditPart.getModel();
  28. };
  29. Validation.prototype = {
  30. validate: function() {
  31. return this.verifyModel()
  32. && this.verifySyntax()
  33. && this.verifyAlone()
  34. && this.verifySplit();
  35. },
  36. // ========================================================================
  37. verifyModel: function() {
  38. var result = true;
  39. Gef.each(this.processModel.children, function(node) {
  40. //修改遍历当前的组件的同时 选中当前组件
  41. var viewer = Gef.activeEditor.getGraphicalViewer();
  42. var browserListener = viewer.getBrowserListener();
  43. var selectionManager = browserListener.getSelectionManager();
  44. //selectionManager.selectAll(); 选中所有的
  45. selectionManager.selectIn(node);
  46. //判断是否是function 如 discision
  47. if (typeof node.isValid == 'function') {
  48. result = node.isValid();
  49. if (result === false) {
  50. return false;
  51. }
  52. }
  53. });
  54. return result;
  55. },
  56. // ========================================================================
  57. verifySyntax: function() {
  58. return this.verifyStart()
  59. && this.verifyEnd()
  60. && this.verifyDuplicateNodeName()
  61. && this.verifyDuplicateEdge();
  62. },
  63. verifyStart: function() {
  64. var result = true;
  65. var count = 0;
  66. Gef.each(this.processModel.children, function(node) {
  67. if (node.getType() == 'start') {
  68. if (count > 0) {
  69. alert('cannot have more then one START.');
  70. result = false;
  71. return false;
  72. }
  73. if (node.getIncomingConnections().length != 0) {
  74. alert('START cannot have any incoming connections.');
  75. result = false;
  76. return false;
  77. }
  78. if (node.getOutgoingConnections().length != 1) {
  79. alert('START can have only one outgoing connection.');
  80. result = false;
  81. return false;
  82. }
  83. count++;
  84. }
  85. });
  86. if (count == 0) {
  87. if (Gef.notEmpty(Ext)) {
  88. Ext.Msg.alert('提示', '必须设置开始节点');
  89. } else {
  90. alert('必须设置开始节点');
  91. }
  92. result = false;
  93. }
  94. return result;
  95. },
  96. verifyEnd: function() {
  97. var count = 0;
  98. var result = true;
  99. Gef.each(this.processModel.children, function(node) {
  100. if (node.getType() == 'end') {
  101. count++;
  102. if (node.getOutgoingConnections().length != 0) {
  103. alert('END cannot have any outgoing connections.');
  104. result = false;
  105. return false;
  106. }
  107. }
  108. });
  109. if (result === false) {
  110. return false;
  111. }
  112. if (count == 0) {
  113. if (Gef.notEmpty(Ext)) {
  114. Ext.Msg.alert('提示', '必须设置结束节点');
  115. } else {
  116. alert('必须设置结束节点');
  117. }
  118. return false;
  119. }
  120. result = false;
  121. Gef.each(this.processModel.children, function(node) {
  122. if (node.getType() == 'end') {
  123. var ends = node.dom.getAttribute('ends');
  124. if (Gef.isEmpty(ends) || ends == 'processinstance') {
  125. result = true;
  126. return false;
  127. }
  128. }
  129. });
  130. if (result === false) {
  131. if (Gef.notEmpty(Ext)) {
  132. Ext.Msg.alert('提示', '不能将所有的结束节点的结束方式都设置为execution');
  133. } else {
  134. alert('不能将所有的结束节点的结束方式都设置为execution');
  135. }
  136. }
  137. return result;
  138. },
  139. verifyDuplicateNodeName: function() {
  140. var hasEmptyNodeName = false;
  141. var map = {};
  142. Gef.each(this.processModel.children, function(sourceNode) {
  143. var id = sourceNode.text;
  144. if (Gef.isEmpty(id)) {
  145. hasEmptyNodeName = true;
  146. return false;
  147. }
  148. if (typeof map[id] == 'undefined') {
  149. map[id] = 1;
  150. } else {
  151. map[id]++;
  152. }
  153. });
  154. if (hasEmptyNodeName) {
  155. if (Gef.notEmpty(Ext)) {
  156. Ext.Msg.alert('提示', '节点名称不能为空');
  157. } else {
  158. alert('节点名称不能为空');
  159. }
  160. }
  161. var info = '节点名称不能重复';
  162. for (var id in map) {
  163. var num = map[id];
  164. if (num > 1) {
  165. info += '\n ' + id;
  166. }
  167. }
  168. if (info != '节点名称不能重复') {
  169. if (Gef.notEmpty(Ext)) {
  170. Ext.Msg.alert('提示', info);
  171. } else {
  172. alert(info);
  173. }
  174. return false;
  175. } else {
  176. return true;
  177. }
  178. },
  179. verifyDuplicateEdge: function() {
  180. var hasDuplicatedEdgeName = false;
  181. var duplicatedEdgetName = null;
  182. var duplicatedNodeName = null;
  183. var map = {};
  184. Gef.each(this.processModel.children, function(sourceNode) {
  185. hasDuplicatedEdgeName = false;
  186. var edgeNames = [];
  187. Gef.each(sourceNode.getOutgoingConnections(), function(connection) {
  188. var connectionName = connection.text;
  189. if (Gef.isEmpty(connectionName)) {
  190. connectionName = '';
  191. }
  192. if (edgeNames.indexOf(connectionName) == -1) {
  193. edgeNames.push(connectionName);
  194. } else {
  195. hasDuplicatedEdgeName = true;
  196. duplicatedEdgeName = connectionName;
  197. duplicatedNodeName = sourceNode.text;
  198. return false;
  199. }
  200. var targetNode = connection.getTarget();
  201. var id = sourceNode.text + ' to ' + targetNode.text;
  202. if (typeof map[id] == 'undefined') {
  203. map[id] = 1;
  204. } else {
  205. map[id]++;
  206. }
  207. });
  208. if (hasDuplicatedEdgeName) {
  209. return false;
  210. }
  211. });
  212. if (hasDuplicatedEdgeName) {
  213. if (Gef.notEmpty(Ext)) {
  214. Ext.Msg.alert('提示', duplicatedNodeName + '存在重名的外向连线[' + duplicatedEdgeName + ']');
  215. } else {
  216. alert(duplicatedNodeName + '存在重名的外向连线[' + duplicatedEdgeName + ']');
  217. }
  218. return false;
  219. }
  220. var info = '连线不能重复';
  221. for (var id in map) {
  222. var num = map[id];
  223. if (num > 1) {
  224. info += '\n ' + id;
  225. }
  226. }
  227. if (info != '连线不能重复') {
  228. if (Gef.notEmpty(Ext)) {
  229. Ext.Msg.alert('提示', info);
  230. } else {
  231. alert(info);
  232. }
  233. return false;
  234. } else {
  235. return true;
  236. }
  237. },
  238. // ========================================================================
  239. verifyAlone: function() {
  240. var result = true;
  241. var elementsToCheck = {};
  242. Gef.each(this.processModel.children, function(node) {
  243. if (node.getType() != 'start'
  244. && node.getOutgoingConnections().length == 0
  245. && node.getIncomingConnections().length == 0) {
  246. result = false;
  247. return false;
  248. }
  249. elementsToCheck[node.text] = new NodeWrapper(node);
  250. });
  251. if (result === false) {
  252. alert('不能包括空组件!');
  253. return false;
  254. }
  255. var count = this.processModel.children;
  256. while (count > 0) {
  257. var freeElement = null;
  258. for (var id in elementsToCheck) {
  259. var elem = elementsToCheck[id];
  260. if (elem.isFree()) {
  261. freeElement = elem;
  262. break;
  263. }
  264. }
  265. if (freeElement == null) {
  266. var entry = null;
  267. for (var id in elementsToCheck) {
  268. var elem = elementsToCheck[id];
  269. if (elem.isEntry()) {
  270. entry = elem;
  271. }
  272. }
  273. if (entry == null) {
  274. alert('there is alone node');
  275. return false;
  276. }
  277. // reverse
  278. for (var i = 0; i < entry.incomingLinks.length; i++) {
  279. var sourceId = entry.id;
  280. var targetId = entry.incomingLinks[i];
  281. var target = elementsToCheck[targetId];
  282. var index = target.outgoingLinks.indexOf(sourceId);
  283. target.outgoingLinks.splice(index, 1);
  284. target.incomingLinks.push(sourceId);
  285. }
  286. entry.incomingLinks = [];
  287. }
  288. for (var i = 0; i < freeElement.outgoingLinks.length; i++) {
  289. var targetId = freeElement.outgoingLinks[i];
  290. var target = elementsToCheck[targetId];
  291. var index = target.incomingLinks.indexOf(freeElement.id);
  292. target.incomingLinks.splice(index, 1);
  293. }
  294. delete elementsToCheck[id];
  295. count--;
  296. }
  297. return true;
  298. },
  299. // ========================================================================
  300. verifySplit: function() {
  301. // TODO
  302. return true;
  303. }
  304. };