added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / tests / tests.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, OnInit, ViewContainerRef, ViewChild, AfterContentInit, OnDestroy } from '@angular/core';\r
18 import { HttpClient } from '@angular/common/http';\r
19 import { routerTransition } from '../../router.animations';\r
20 import { ListService } from '../../shared/services/list.service';\r
21 import { Router } from '@angular/router';\r
22 import { TestDefinitionService } from '../../shared/services/test-definition.service';\r
23 import { TestInstanceService } from '../../shared/services/test-instance.service';\r
24 import { MatTableDataSource } from '@angular/material/table';\r
25 import { MatPaginator, MatDialog, MatSnackBar } from '@angular/material';\r
26 import { AlertModalComponent } from '../../shared/modules/alert-modal/alert-modal.component';\r
27 import { CreateTestComponent } from '../onboarding/create-test/create-test.component';\r
28 import { TestDefinitionModalComponent } from 'app/shared/modules/test-definition-modal/test-definition-modal.component';\r
29 import { ViewWorkflowModalComponent } from 'app/shared/modules/view-workflow-modal/view-workflow-modal.component';\r
30 import { AlertSnackbarComponent } from 'app/shared/modules/alert-snackbar/alert-snackbar.component';\r
31 import { TestInstanceModalComponent } from '../../shared/modules/test-instance-modal/test-instance-modal.component';\r
32 import { UserService } from 'app/shared/services/user.service';\r
33 import { CookieService } from "ngx-cookie-service";\r
34 import { GroupService } from 'app/shared/services/group.service';\r
35 import { appInitializerFactory } from '@angular/platform-browser/src/browser/server-transition';\r
36 import { element } from '@angular/core/src/render3/instructions';\r
37 import { GridOptionsWrapper, RowNode, initialiseAgGridWithAngular1 } from 'ag-grid-community';\r
38 import { every } from 'rxjs/operators';\r
39 import { Subscription } from 'rxjs';\r
40 \r
41 @Component({\r
42   selector: 'app-tests',\r
43   templateUrl: './tests.component.pug',\r
44   styleUrls: ['./tests.component.scss'],\r
45   animations: [routerTransition()]\r
46 })\r
47 export class TestsComponent implements OnInit, OnDestroy {\r
48 \r
49   private toDestroy: Array<Subscription> = [];\r
50 \r
51   public dataSource;\r
52   public displayedColumns: string[] = ['lock', 'name', 'description', 'id', 'processDefinitionKey', 'options'];\r
53   public resultsLength;\r
54   public loading = false;\r
55 \r
56 \r
57   public columns = [\r
58     \r
59     {headerName: 'Name', field: 'testName', sortable: true, filter: true, resizable: true, checkboxSelection:true, headerCheckboxSelection: true, headerCheckboxSelectionFilteredOnly: true, width: 300},\r
60     {headerName: 'Description', field: 'testDescription', sortable: true, filter: true, resizable: true},\r
61     {headerName: 'Id', field: '_id', sortable: true, filter: true, resizable: true, editable: true},\r
62     {headerName: 'Process Definition key', field: 'processDefinitionKey', sortable: true, filter: true, resizable: true},\r
63     {headerName: '', field: 'disabled', cellRenderer: this.disabledIndicator, hide: false, width: 80}\r
64     \r
65   ];\r
66   public rowData;\r
67   \r
68   /*\r
69   public rowData =  [\r
70     { _id: '5cfe7e5d6f4e5d0040a3b235', testDescription: 'For testing', testName: "testflow", processDefinitionKey: "demo"},\r
71     { make: 'Ford', model: 'Mondeo', price: 32000 },\r
72     { make: 'Porsche', model: 'Boxter', price: 72000 }\r
73 ]; */\r
74 \r
75   public hasSelectedRows = false;\r
76   public selectedSingleRow = false;\r
77   public selectedUnlockedRows = true;\r
78   public selectedLockedRows = false; \r
79 \r
80   private gridApi;\r
81   private gridColumnApi;\r
82   private selectedRows = {};\r
83 \r
84   @ViewChild(MatPaginator) paginator: MatPaginator;\r
85 \r
86   constructor(private http: HttpClient,\r
87     private router: Router,\r
88     private viewRef: ViewContainerRef,\r
89     private testDefinition: TestDefinitionService,\r
90     private modal: MatDialog,\r
91     private snack: MatSnackBar,\r
92     private user: UserService,\r
93     private testInstanceService: TestInstanceService,\r
94     private cookie: CookieService,\r
95     private _groups: GroupService\r
96   ) { }\r
97   \r
98   ngOnInit() {\r
99 \r
100     this.setComponentData(this._groups.getGroup());\r
101     this.toDestroy.push(this._groups.groupChange().subscribe(group => {\r
102       this.setComponentData(group);\r
103     }));\r
104 \r
105 \r
106   }\r
107 \r
108   ngOnDestroy() {\r
109     this.toDestroy.forEach(elem => elem.unsubscribe());\r
110   }\r
111 \r
112   setComponentData(group) {\r
113     \r
114     if(!group){\r
115       return;\r
116     }\r
117 \r
118     this.loading = true;\r
119 \r
120     this.dataSource = new MatTableDataSource();\r
121     this.dataSource.paginator = this.paginator;\r
122 \r
123 \r
124   \r
125     this.testDefinition.find({\r
126       $limit: -1,\r
127       groupId: group['_id'],\r
128       $sort: {\r
129         createdAt: -1\r
130       },\r
131       $select: ['testName', 'testDescription', 'processDefinitionKey', 'bpmnInstances.isDeployed', 'disabled', 'groupId']\r
132     }).subscribe((list) => {\r
133       this.dataSource.data = list;\r
134       this.resultsLength = this.dataSource.data.length;\r
135       this.loading = false;\r
136       // Getting row data filled with list\r
137       this.rowData = list;\r
138 \r
139 \r
140 \r
141       //console.log("This is the rowdata: "+ JSON.stringify(this.rowData[1]))\r
142       //this.rowData = [].concat.apply([], list);\r
143     })\r
144   \r
145    \r
146   }\r
147 \r
148   applyFilter(filterValue: string) {\r
149     this.dataSource.filter = filterValue.trim().toLowerCase();\r
150   }\r
151 //createInstance(element)\r
152   createInstance() {\r
153     \r
154 \r
155     this.selectedRows = this.gridApi.getSelectedRows().map(({ _id, testName }) => ({_id, testName}));\r
156     \r
157     const create = this.modal.open(TestInstanceModalComponent, {\r
158       width: '90%',\r
159       data: {\r
160         td: this.selectedRows[0]._id//element._id\r
161       },\r
162       disableClose: true\r
163     });\r
164   }\r
165 \r
166   create() {\r
167     let create = this.modal.open(TestDefinitionModalComponent, {\r
168       disableClose: true\r
169     });\r
170 \r
171     create.afterClosed().subscribe(res => {\r
172       this.ngOnInit();\r
173     })\r
174   }\r
175 \r
176 \r
177   // view(td){\r
178   //   this.modal.open(ViewWorkflowModalComponent, {\r
179   //     width: '90%',\r
180   //     height: '70%',\r
181   //     maxWidth: '100%',\r
182   //     data: {\r
183   //       id: td._id\r
184   //     }\r
185   //   });\r
186   // }\r
187 \r
188 \r
189 \r
190 \r
191 \r
192   deleteMultiple(){\r
193     for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
194       this.delete(i);\r
195     }\r
196   }\r
197 \r
198   delete(td) {\r
199 \r
200     this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testName }) => ({_id, testName}));\r
201     const deleter = this.modal.open(AlertModalComponent, {\r
202       width: '250px',\r
203       data: {\r
204         type: 'confirmation',\r
205         message: 'Are you sure you want to delete ' + this.selectedRows[td].testName + '? Any test instances or executions using this test definition will no longer work.'\r
206       }\r
207     });\r
208 \r
209     deleter.afterClosed().subscribe(result => {\r
210       if (result) {\r
211         this.testDefinition.delete(this.selectedRows[td]._id).subscribe(response => {\r
212           this.snack.openFromComponent(AlertSnackbarComponent, {\r
213             duration: 1500,\r
214             data: {\r
215               message: 'Test definition was deleted'\r
216             }\r
217           })\r
218           //this.ngOnInit();\r
219           this.setComponentData(this._groups.getGroup());\r
220         });\r
221       }\r
222     });\r
223   }\r
224 \r
225   edit() {\r
226     this.selectedRows = this.gridApi.getSelectedRows().map(({_id }) => ({_id}));\r
227     var editor = this.modal.open(TestDefinitionModalComponent, {\r
228       disableClose: true,\r
229       data: {\r
230         testDefinitionId: this.selectedRows[0]._id\r
231       }\r
232     });\r
233   }\r
234 \r
235   lockMultiple(){\r
236    \r
237     for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
238       this.lock(i);\r
239     }\r
240 \r
241   }\r
242 \r
243 \r
244   lock(td) {\r
245     this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testName, groupId,  }) => ({_id, testName, groupId}));\r
246 \r
247     let user = JSON.parse(this.cookie.get('currentUser'));\r
248     let isAdmin = false;\r
249     for (let i = 0; i < user.groups.length; i++) {\r
250       if (this.selectedRows[td].groupId === user.groups[i].groupId) {\r
251         if (user.groups[i].permissions.includes("admin")) {\r
252           isAdmin = true;\r
253         }\r
254       }\r
255     }\r
256     user = '';\r
257     if (!isAdmin) {\r
258       this.modal.open(AlertModalComponent, {\r
259         width: '250px',\r
260         data: {\r
261           type: 'alert',\r
262           message: 'You do not have the correct permissions to lock/unlock test definitions.'\r
263         }\r
264       })\r
265       return;\r
266     }\r
267     this.modal.open(AlertModalComponent, {\r
268       width: '250px',\r
269       data: {\r
270         type: 'confirmation',\r
271         message: 'Are you sure you want to lock ' + this.selectedRows[td].testName + '? All test instances using this test definition will be locked and no more instances can be created until unlocked.'\r
272       }\r
273     }).afterClosed().subscribe((result) => {\r
274       if (result) {\r
275         let testDef = {\r
276           '_id': this.selectedRows[td]._id,\r
277           'disabled': true\r
278         }\r
279         this.testDefinition.patch(testDef).subscribe((res) => {\r
280           this.selectedRows[td].disabled = true;\r
281           this.testInstanceService.find({ $limit: -1, testDefinitionId: this.selectedRows[td]._id }).subscribe((result) => {\r
282             \r
283             \r
284 \r
285             if (result['length']) {\r
286               for (let i = 0; i < result['length']; i++) {\r
287                 let ti = {\r
288                   '_id': null,\r
289                   'disabled': true\r
290                 }\r
291                 ti._id = result[i]._id;\r
292                 ti.disabled = true;\r
293                 let temp = ti;\r
294               \r
295                 this.testInstanceService.patch(ti).subscribe((results) => {\r
296                  \r
297                   this.snack.openFromComponent(AlertSnackbarComponent, {\r
298                     duration: 1500,\r
299                     data: {\r
300                       message: 'Test Instance ' + results['testInstanceName'] + ' was locked'\r
301                     }\r
302                   })\r
303                 });\r
304               }\r
305             } else {\r
306               let ti = {\r
307                 '_id': null,\r
308                 'disabled': true\r
309               }\r
310               ti._id = result['_id'];\r
311               this.testInstanceService.patch(ti).subscribe((results) => {\r
312                 this.snack.openFromComponent(AlertSnackbarComponent, {\r
313                   duration: 1500,\r
314                   data: {\r
315                     message: 'Test Instance ' + results['testInstanceName'] + ' was locked'\r
316                   }\r
317                 })\r
318               });;\r
319             }\r
320           });\r
321           this.setComponentData(this._groups.getGroup());\r
322         }, (error) => {\r
323           this.modal.open(AlertModalComponent, {\r
324             width: '250px',\r
325             data: {\r
326               type: "alert",\r
327               message: 'Test Definition could not be locked.'\r
328             }\r
329           })\r
330         });\r
331 \r
332       }\r
333     })\r
334   }\r
335 \r
336 \r
337   updateData(){\r
338     \r
339     this.setComponentData(this._groups.getGroup());\r
340   }\r
341 \r
342   unlockMultiple() {\r
343     for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
344       this.unlock(i);\r
345     }\r
346   }\r
347 //unlock multiple and loop through single unlock\r
348   unlock(td) {\r
349     this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testName, groupId,  }) => ({_id, testName, groupId}));\r
350     let user = JSON.parse(this.cookie.get('currentUser'));\r
351     let isAdmin = false;\r
352     for (let i = 0; i < user.groups.length; i++) {\r
353       if (this.selectedRows[td].groupId === user.groups[i].groupId) {\r
354         if (user.groups[i].permissions.includes("admin")) {\r
355           isAdmin = true;\r
356         }\r
357       }\r
358     }\r
359     user = '';\r
360     if (!isAdmin) {\r
361       this.modal.open(AlertModalComponent, {\r
362         width: '250px',\r
363         data: {\r
364           type: 'alert',\r
365           message: 'You do not have the correct permissions to lock/unlock test definitions.'\r
366         }\r
367       })\r
368       return;\r
369     }\r
370 \r
371     this.modal.open(AlertModalComponent, {\r
372       width: '250px',\r
373       data: {\r
374         type: 'confirmation',\r
375         message: 'Are you sure you want to unlock ' + td.testName + '? All test instances using this test definition will be unlocked as well.'\r
376       }\r
377     }).afterClosed().subscribe((result) => {\r
378       if (result) {\r
379         let testDef = {\r
380           '_id': this.selectedRows[td]._id,\r
381           'disabled': false\r
382         }\r
383         this.testDefinition.patch(testDef).subscribe((res) => {\r
384           this.selectedRows[td].disabled = false;\r
385           this.testInstanceService.find({ $limit: -1, testDefinitionId: this.selectedRows[td]._id }).subscribe((result) => {\r
386            \r
387             // console.log(result);\r
388             if (result['length']) {\r
389               for (let i = 0; i < result['length']; i++) {\r
390                 let ti = {\r
391                   '_id': null,\r
392                   'disabled': false\r
393                 }\r
394                 ti._id = result[i]._id;\r
395                 ti.disabled = false;\r
396                 this.testInstanceService.patch(ti).subscribe((results) => {\r
397                   this.snack.openFromComponent(AlertSnackbarComponent, {\r
398                     duration: 1500,\r
399                     data: {\r
400                       message: 'Test Instance ' + results['testInstanceName'] + ' was unlocked'\r
401                     }\r
402                   })\r
403                 });\r
404               }\r
405             } else {\r
406               let ti = {\r
407                 '_id': null,\r
408                 'disabled': false\r
409               }\r
410               ti._id = result['_id'];\r
411               \r
412               this.testInstanceService.patch(ti).subscribe((results) => {\r
413                 this.snack.openFromComponent(AlertSnackbarComponent, {\r
414                   duration: 1500,\r
415                   data: {\r
416                     message: 'Test Instance ' + results['testInstanceName'] + ' was unlocked'\r
417                   }\r
418                 })\r
419               });;\r
420             }\r
421           });\r
422           this.setComponentData(this._groups.getGroup());\r
423         }, (error) => {\r
424           this.modal.open(AlertModalComponent, {\r
425             width: '250px',\r
426             data: {\r
427               type: "alert",\r
428               message: 'Test Definition could not be locked.'\r
429             }\r
430           })\r
431         });\r
432 \r
433       }\r
434     })\r
435   }\r
436 \r
437 \r
438   isDeployed(element) {\r
439     let deployed = false;\r
440     if (element.bpmnInstances) {\r
441       element.bpmnInstances.forEach(elem => {\r
442         if (elem.isDeployed) {\r
443           deployed = true;\r
444         }\r
445       });\r
446     }\r
447     return deployed;\r
448   }\r
449 \r
450 \r
451 \r
452   onRowSelected(event){\r
453 \r
454     this.selectedRows = this.gridApi.getSelectedRows().map(({ _id, disabled  }) => ({ _id, disabled}));\r
455 \r
456     if(event.api.getSelectedNodes().length > 0){\r
457       this.hasSelectedRows = true;\r
458 \r
459       //Checks for all Unlocked rows\r
460       for (let i = 0; i < event.api.getSelectedNodes().length; i++ )\r
461       {\r
462 \r
463         if(!this.selectedRows[i].disabled)\r
464         {\r
465           this.selectedUnlockedRows = true;\r
466         }\r
467         else{\r
468           this.selectedUnlockedRows = false;\r
469           break;\r
470         }\r
471       }\r
472 \r
473       //Checks for all Locked rows\r
474       for (let i = 0; i < event.api.getSelectedNodes().length; i++ )\r
475       {\r
476 \r
477         if(this.selectedRows[i].disabled)\r
478         {\r
479           this.selectedLockedRows = true;\r
480         }\r
481         else{\r
482           this.selectedLockedRows = false;\r
483           break;\r
484         }\r
485       }\r
486 \r
487 \r
488 \r
489 \r
490 \r
491     }\r
492     else{\r
493       this.hasSelectedRows = false;\r
494       this.selectedLockedRows = false;\r
495       this.selectedUnlockedRows = true; \r
496 \r
497     }\r
498     //One Row was selected\r
499     if((event.api.getSelectedNodes().length == 1)){\r
500       this.selectedSingleRow = true;\r
501      \r
502     }else{\r
503       this.selectedSingleRow = false;\r
504     }\r
505 \r
506   }\r
507 \r
508   onGridReady(params){\r
509     this.gridApi = params.api;\r
510     \r
511     this.gridColumnApi = params.columnApi;\r
512 \r
513     //auto size the column widths\r
514     this.gridColumnApi.autoSizeColumns(['name']);\r
515   }\r
516 \r
517   disabledIndicator(params){\r
518     if (params.value){\r
519       return `<mat-icon class="mat-icon mat-icon-no-color" role="img" >\r
520        locked</mat-icon>`;   \r
521     }\r
522   }\r
523 \r
524 \r
525 \r
526 \r
527   navToDefinition(event){\r
528     this.router.navigate(['/test-definitions', event.data._id]);\r
529   }\r
530 \r
531   testDefinitionModeler(){\r
532     this.router.navigate(['/modeler'], {queryParams: {testDefinitionId: this.selectedRows[0]._id}});\r
533   }\r
534 \r
535 }\r