all-validation.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. //hey 连线的name不能重复 类型不能为空 task节点的连线进出至少要有一条
  221. var doubleName = false;//重复名称
  222. var badNode = false;//task节点的连线进出至少要有一条
  223. var nodeName = '';//问题节点
  224. var nullType = false;//空类型
  225. var lineName = '';//问题线名称
  226. allConnection = new Array();
  227. allNode = new Array();
  228. Gef.each(this.processModel.children, function(sourceNode) {
  229. if(sourceNode.getTagName()=='task'&&sourceNode.flowtype=='task'){
  230. if(sourceNode.incomingConnections.length<1||sourceNode.outgoingConnections.length<1){
  231. badNode = true;
  232. nodeName = sourceNode.text;
  233. }
  234. }
  235. if(sourceNode.flowtype!="idea"&&sourceNode.flowtype!="derive"&&sourceNode.flowtype!="mission"){//不校验派生节点
  236. allNode.push({
  237. name : sourceNode.text
  238. });//所有的节点
  239. }
  240. Gef.each(sourceNode.getOutgoingConnections(), function(connection) {
  241. if(connection.dom.getAttribute('linetype').length<1){
  242. nullType = true;
  243. lineName = connection.text;
  244. }
  245. allConnection.push({
  246. name : connection.text,
  247. id: connection.id,
  248. source:connection.source.text,
  249. tag:connection.target.text
  250. });//所有的线段
  251. });
  252. });
  253. if(badNode){
  254. if (Gef.notEmpty(Ext)) {
  255. Ext.Msg.alert('提示', 'task节点至少有一条出线和入线 : '+nodeName);
  256. } else {
  257. alert(info);
  258. }
  259. return false;
  260. }
  261. if(nullType){
  262. if (Gef.notEmpty(Ext)) {
  263. Ext.Msg.alert('提示', '不能有空类型的的线 : '+lineName);
  264. } else {
  265. alert(info);
  266. }
  267. return false;
  268. }
  269. //比较是否有重名的线
  270. Gef.each(allConnection, function(connection1) {
  271. Gef.each(allConnection, function(connection2) {
  272. if(connection1.id!=connection2.id){
  273. if(connection1.name==connection2.name){
  274. doubleName = true;
  275. lineName = connection1.name;
  276. }
  277. }
  278. });
  279. });
  280. if(doubleName){
  281. if (Gef.notEmpty(Ext)) {
  282. Ext.Msg.alert('提示', '不能有同名的线 : '+lineName);
  283. } else {
  284. alert(info);
  285. }
  286. return false;
  287. }
  288. var info = '连线不能重复';
  289. for (var id in map) {
  290. var num = map[id];
  291. if (num > 1) {
  292. info += '\n ' + id;
  293. }
  294. }
  295. if (info != '连线不能重复') {
  296. if (Gef.notEmpty(Ext)) {
  297. Ext.Msg.alert('提示', info);
  298. } else {
  299. alert(info);
  300. }
  301. return false;
  302. } else {
  303. return true;
  304. }
  305. },
  306. // ========================================================================
  307. verifyAlone: function() {
  308. var result = true;
  309. var elementsToCheck = {};
  310. Gef.each(this.processModel.children, function(node) {
  311. if (node.getType() != 'start'
  312. && node.getOutgoingConnections().length == 0
  313. && node.getIncomingConnections().length == 0) {
  314. result = false;
  315. return false;
  316. }
  317. elementsToCheck[node.text] = new NodeWrapper(node);
  318. });
  319. if (result === false) {
  320. alert('不能包括空组件!');
  321. return false;
  322. }
  323. var count = this.processModel.children;
  324. while (count > 0) {
  325. var freeElement = null;
  326. for (var id in elementsToCheck) {
  327. var elem = elementsToCheck[id];
  328. if (elem.isFree()) {
  329. freeElement = elem;
  330. break;
  331. }
  332. }
  333. if (freeElement == null) {
  334. var entry = null;
  335. for (var id in elementsToCheck) {
  336. var elem = elementsToCheck[id];
  337. if (elem.isEntry()) {
  338. entry = elem;
  339. }
  340. }
  341. if (entry == null) {
  342. alert('there is alone node');
  343. return false;
  344. }
  345. // reverse
  346. for (var i = 0; i < entry.incomingLinks.length; i++) {
  347. var sourceId = entry.id;
  348. var targetId = entry.incomingLinks[i];
  349. var target = elementsToCheck[targetId];
  350. var index = target.outgoingLinks.indexOf(sourceId);
  351. target.outgoingLinks.splice(index, 1);
  352. target.incomingLinks.push(sourceId);
  353. }
  354. entry.incomingLinks = [];
  355. }
  356. for (var i = 0; i < freeElement.outgoingLinks.length; i++) {
  357. var targetId = freeElement.outgoingLinks[i];
  358. var target = elementsToCheck[targetId];
  359. var index = target.incomingLinks.indexOf(freeElement.id);
  360. target.incomingLinks.splice(index, 1);
  361. }
  362. delete elementsToCheck[id];
  363. count--;
  364. }
  365. return true;
  366. },
  367. // ========================================================================
  368. verifySplit: function() {
  369. // TODO
  370. return true;
  371. }
  372. };