--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+import { Component, OnInit, ViewContainerRef, ViewChild, AfterContentInit, OnDestroy } from '@angular/core';\r
+import { HttpClient } from '@angular/common/http';\r
+import { routerTransition } from '../../router.animations';\r
+import { ListService } from '../../shared/services/list.service';\r
+import { Router } from '@angular/router';\r
+import { TestDefinitionService } from '../../shared/services/test-definition.service';\r
+import { TestInstanceService } from '../../shared/services/test-instance.service';\r
+import { MatTableDataSource } from '@angular/material/table';\r
+import { MatPaginator, MatDialog, MatSnackBar } from '@angular/material';\r
+import { AlertModalComponent } from '../../shared/modules/alert-modal/alert-modal.component';\r
+import { CreateTestComponent } from '../onboarding/create-test/create-test.component';\r
+import { TestDefinitionModalComponent } from 'app/shared/modules/test-definition-modal/test-definition-modal.component';\r
+import { ViewWorkflowModalComponent } from 'app/shared/modules/view-workflow-modal/view-workflow-modal.component';\r
+import { AlertSnackbarComponent } from 'app/shared/modules/alert-snackbar/alert-snackbar.component';\r
+import { TestInstanceModalComponent } from '../../shared/modules/test-instance-modal/test-instance-modal.component';\r
+import { UserService } from 'app/shared/services/user.service';\r
+import { CookieService } from "ngx-cookie-service";\r
+import { GroupService } from 'app/shared/services/group.service';\r
+import { appInitializerFactory } from '@angular/platform-browser/src/browser/server-transition';\r
+import { element } from '@angular/core/src/render3/instructions';\r
+import { GridOptionsWrapper, RowNode, initialiseAgGridWithAngular1 } from 'ag-grid-community';\r
+import { every } from 'rxjs/operators';\r
+import { Subscription } from 'rxjs';\r
+\r
+@Component({\r
+ selector: 'app-tests',\r
+ templateUrl: './tests.component.pug',\r
+ styleUrls: ['./tests.component.scss'],\r
+ animations: [routerTransition()]\r
+})\r
+export class TestsComponent implements OnInit, OnDestroy {\r
+\r
+ private toDestroy: Array<Subscription> = [];\r
+\r
+ public dataSource;\r
+ public displayedColumns: string[] = ['lock', 'name', 'description', 'id', 'processDefinitionKey', 'options'];\r
+ public resultsLength;\r
+ public loading = false;\r
+\r
+\r
+ public columns = [\r
+ \r
+ {headerName: 'Name', field: 'testName', sortable: true, filter: true, resizable: true, checkboxSelection:true, headerCheckboxSelection: true, headerCheckboxSelectionFilteredOnly: true, width: 300},\r
+ {headerName: 'Description', field: 'testDescription', sortable: true, filter: true, resizable: true},\r
+ {headerName: 'Id', field: '_id', sortable: true, filter: true, resizable: true, editable: true},\r
+ {headerName: 'Process Definition key', field: 'processDefinitionKey', sortable: true, filter: true, resizable: true},\r
+ {headerName: '', field: 'disabled', cellRenderer: this.disabledIndicator, hide: false, width: 80}\r
+ \r
+ ];\r
+ public rowData;\r
+ \r
+ /*\r
+ public rowData = [\r
+ { _id: '5cfe7e5d6f4e5d0040a3b235', testDescription: 'For testing', testName: "testflow", processDefinitionKey: "demo"},\r
+ { make: 'Ford', model: 'Mondeo', price: 32000 },\r
+ { make: 'Porsche', model: 'Boxter', price: 72000 }\r
+]; */\r
+\r
+ public hasSelectedRows = false;\r
+ public selectedSingleRow = false;\r
+ public selectedUnlockedRows = true;\r
+ public selectedLockedRows = false; \r
+\r
+ private gridApi;\r
+ private gridColumnApi;\r
+ private selectedRows = {};\r
+\r
+ @ViewChild(MatPaginator) paginator: MatPaginator;\r
+\r
+ constructor(private http: HttpClient,\r
+ private router: Router,\r
+ private viewRef: ViewContainerRef,\r
+ private testDefinition: TestDefinitionService,\r
+ private modal: MatDialog,\r
+ private snack: MatSnackBar,\r
+ private user: UserService,\r
+ private testInstanceService: TestInstanceService,\r
+ private cookie: CookieService,\r
+ private _groups: GroupService\r
+ ) { }\r
+ \r
+ ngOnInit() {\r
+\r
+ this.setComponentData(this._groups.getGroup());\r
+ this.toDestroy.push(this._groups.groupChange().subscribe(group => {\r
+ this.setComponentData(group);\r
+ }));\r
+\r
+\r
+ }\r
+\r
+ ngOnDestroy() {\r
+ this.toDestroy.forEach(elem => elem.unsubscribe());\r
+ }\r
+\r
+ setComponentData(group) {\r
+ \r
+ if(!group){\r
+ return;\r
+ }\r
+\r
+ this.loading = true;\r
+\r
+ this.dataSource = new MatTableDataSource();\r
+ this.dataSource.paginator = this.paginator;\r
+\r
+\r
+ \r
+ this.testDefinition.find({\r
+ $limit: -1,\r
+ groupId: group['_id'],\r
+ $sort: {\r
+ createdAt: -1\r
+ },\r
+ $select: ['testName', 'testDescription', 'processDefinitionKey', 'bpmnInstances.isDeployed', 'disabled', 'groupId']\r
+ }).subscribe((list) => {\r
+ this.dataSource.data = list;\r
+ this.resultsLength = this.dataSource.data.length;\r
+ this.loading = false;\r
+ // Getting row data filled with list\r
+ this.rowData = list;\r
+\r
+\r
+\r
+ //console.log("This is the rowdata: "+ JSON.stringify(this.rowData[1]))\r
+ //this.rowData = [].concat.apply([], list);\r
+ })\r
+ \r
+ \r
+ }\r
+\r
+ applyFilter(filterValue: string) {\r
+ this.dataSource.filter = filterValue.trim().toLowerCase();\r
+ }\r
+//createInstance(element)\r
+ createInstance() {\r
+ \r
+\r
+ this.selectedRows = this.gridApi.getSelectedRows().map(({ _id, testName }) => ({_id, testName}));\r
+ \r
+ const create = this.modal.open(TestInstanceModalComponent, {\r
+ width: '90%',\r
+ data: {\r
+ td: this.selectedRows[0]._id//element._id\r
+ },\r
+ disableClose: true\r
+ });\r
+ }\r
+\r
+ create() {\r
+ let create = this.modal.open(TestDefinitionModalComponent, {\r
+ disableClose: true\r
+ });\r
+\r
+ create.afterClosed().subscribe(res => {\r
+ this.ngOnInit();\r
+ })\r
+ }\r
+\r
+\r
+ // view(td){\r
+ // this.modal.open(ViewWorkflowModalComponent, {\r
+ // width: '90%',\r
+ // height: '70%',\r
+ // maxWidth: '100%',\r
+ // data: {\r
+ // id: td._id\r
+ // }\r
+ // });\r
+ // }\r
+\r
+\r
+\r
+\r
+\r
+ deleteMultiple(){\r
+ for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
+ this.delete(i);\r
+ }\r
+ }\r
+\r
+ delete(td) {\r
+\r
+ this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testName }) => ({_id, testName}));\r
+ const deleter = this.modal.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: 'confirmation',\r
+ 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
+ }\r
+ });\r
+\r
+ deleter.afterClosed().subscribe(result => {\r
+ if (result) {\r
+ this.testDefinition.delete(this.selectedRows[td]._id).subscribe(response => {\r
+ this.snack.openFromComponent(AlertSnackbarComponent, {\r
+ duration: 1500,\r
+ data: {\r
+ message: 'Test definition was deleted'\r
+ }\r
+ })\r
+ //this.ngOnInit();\r
+ this.setComponentData(this._groups.getGroup());\r
+ });\r
+ }\r
+ });\r
+ }\r
+\r
+ edit() {\r
+ this.selectedRows = this.gridApi.getSelectedRows().map(({_id }) => ({_id}));\r
+ var editor = this.modal.open(TestDefinitionModalComponent, {\r
+ disableClose: true,\r
+ data: {\r
+ testDefinitionId: this.selectedRows[0]._id\r
+ }\r
+ });\r
+ }\r
+\r
+ lockMultiple(){\r
+ \r
+ for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
+ this.lock(i);\r
+ }\r
+\r
+ }\r
+\r
+\r
+ lock(td) {\r
+ this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testName, groupId, }) => ({_id, testName, groupId}));\r
+\r
+ let user = JSON.parse(this.cookie.get('currentUser'));\r
+ let isAdmin = false;\r
+ for (let i = 0; i < user.groups.length; i++) {\r
+ if (this.selectedRows[td].groupId === user.groups[i].groupId) {\r
+ if (user.groups[i].permissions.includes("admin")) {\r
+ isAdmin = true;\r
+ }\r
+ }\r
+ }\r
+ user = '';\r
+ if (!isAdmin) {\r
+ this.modal.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: 'alert',\r
+ message: 'You do not have the correct permissions to lock/unlock test definitions.'\r
+ }\r
+ })\r
+ return;\r
+ }\r
+ this.modal.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: 'confirmation',\r
+ 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
+ }\r
+ }).afterClosed().subscribe((result) => {\r
+ if (result) {\r
+ let testDef = {\r
+ '_id': this.selectedRows[td]._id,\r
+ 'disabled': true\r
+ }\r
+ this.testDefinition.patch(testDef).subscribe((res) => {\r
+ this.selectedRows[td].disabled = true;\r
+ this.testInstanceService.find({ $limit: -1, testDefinitionId: this.selectedRows[td]._id }).subscribe((result) => {\r
+ \r
+ \r
+\r
+ if (result['length']) {\r
+ for (let i = 0; i < result['length']; i++) {\r
+ let ti = {\r
+ '_id': null,\r
+ 'disabled': true\r
+ }\r
+ ti._id = result[i]._id;\r
+ ti.disabled = true;\r
+ let temp = ti;\r
+ \r
+ this.testInstanceService.patch(ti).subscribe((results) => {\r
+ \r
+ this.snack.openFromComponent(AlertSnackbarComponent, {\r
+ duration: 1500,\r
+ data: {\r
+ message: 'Test Instance ' + results['testInstanceName'] + ' was locked'\r
+ }\r
+ })\r
+ });\r
+ }\r
+ } else {\r
+ let ti = {\r
+ '_id': null,\r
+ 'disabled': true\r
+ }\r
+ ti._id = result['_id'];\r
+ this.testInstanceService.patch(ti).subscribe((results) => {\r
+ this.snack.openFromComponent(AlertSnackbarComponent, {\r
+ duration: 1500,\r
+ data: {\r
+ message: 'Test Instance ' + results['testInstanceName'] + ' was locked'\r
+ }\r
+ })\r
+ });;\r
+ }\r
+ });\r
+ this.setComponentData(this._groups.getGroup());\r
+ }, (error) => {\r
+ this.modal.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: "alert",\r
+ message: 'Test Definition could not be locked.'\r
+ }\r
+ })\r
+ });\r
+\r
+ }\r
+ })\r
+ }\r
+\r
+\r
+ updateData(){\r
+ \r
+ this.setComponentData(this._groups.getGroup());\r
+ }\r
+\r
+ unlockMultiple() {\r
+ for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
+ this.unlock(i);\r
+ }\r
+ }\r
+//unlock multiple and loop through single unlock\r
+ unlock(td) {\r
+ this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testName, groupId, }) => ({_id, testName, groupId}));\r
+ let user = JSON.parse(this.cookie.get('currentUser'));\r
+ let isAdmin = false;\r
+ for (let i = 0; i < user.groups.length; i++) {\r
+ if (this.selectedRows[td].groupId === user.groups[i].groupId) {\r
+ if (user.groups[i].permissions.includes("admin")) {\r
+ isAdmin = true;\r
+ }\r
+ }\r
+ }\r
+ user = '';\r
+ if (!isAdmin) {\r
+ this.modal.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: 'alert',\r
+ message: 'You do not have the correct permissions to lock/unlock test definitions.'\r
+ }\r
+ })\r
+ return;\r
+ }\r
+\r
+ this.modal.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: 'confirmation',\r
+ message: 'Are you sure you want to unlock ' + td.testName + '? All test instances using this test definition will be unlocked as well.'\r
+ }\r
+ }).afterClosed().subscribe((result) => {\r
+ if (result) {\r
+ let testDef = {\r
+ '_id': this.selectedRows[td]._id,\r
+ 'disabled': false\r
+ }\r
+ this.testDefinition.patch(testDef).subscribe((res) => {\r
+ this.selectedRows[td].disabled = false;\r
+ this.testInstanceService.find({ $limit: -1, testDefinitionId: this.selectedRows[td]._id }).subscribe((result) => {\r
+ \r
+ // console.log(result);\r
+ if (result['length']) {\r
+ for (let i = 0; i < result['length']; i++) {\r
+ let ti = {\r
+ '_id': null,\r
+ 'disabled': false\r
+ }\r
+ ti._id = result[i]._id;\r
+ ti.disabled = false;\r
+ this.testInstanceService.patch(ti).subscribe((results) => {\r
+ this.snack.openFromComponent(AlertSnackbarComponent, {\r
+ duration: 1500,\r
+ data: {\r
+ message: 'Test Instance ' + results['testInstanceName'] + ' was unlocked'\r
+ }\r
+ })\r
+ });\r
+ }\r
+ } else {\r
+ let ti = {\r
+ '_id': null,\r
+ 'disabled': false\r
+ }\r
+ ti._id = result['_id'];\r
+ \r
+ this.testInstanceService.patch(ti).subscribe((results) => {\r
+ this.snack.openFromComponent(AlertSnackbarComponent, {\r
+ duration: 1500,\r
+ data: {\r
+ message: 'Test Instance ' + results['testInstanceName'] + ' was unlocked'\r
+ }\r
+ })\r
+ });;\r
+ }\r
+ });\r
+ this.setComponentData(this._groups.getGroup());\r
+ }, (error) => {\r
+ this.modal.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: "alert",\r
+ message: 'Test Definition could not be locked.'\r
+ }\r
+ })\r
+ });\r
+\r
+ }\r
+ })\r
+ }\r
+\r
+\r
+ isDeployed(element) {\r
+ let deployed = false;\r
+ if (element.bpmnInstances) {\r
+ element.bpmnInstances.forEach(elem => {\r
+ if (elem.isDeployed) {\r
+ deployed = true;\r
+ }\r
+ });\r
+ }\r
+ return deployed;\r
+ }\r
+\r
+\r
+\r
+ onRowSelected(event){\r
+\r
+ this.selectedRows = this.gridApi.getSelectedRows().map(({ _id, disabled }) => ({ _id, disabled}));\r
+\r
+ if(event.api.getSelectedNodes().length > 0){\r
+ this.hasSelectedRows = true;\r
+\r
+ //Checks for all Unlocked rows\r
+ for (let i = 0; i < event.api.getSelectedNodes().length; i++ )\r
+ {\r
+\r
+ if(!this.selectedRows[i].disabled)\r
+ {\r
+ this.selectedUnlockedRows = true;\r
+ }\r
+ else{\r
+ this.selectedUnlockedRows = false;\r
+ break;\r
+ }\r
+ }\r
+\r
+ //Checks for all Locked rows\r
+ for (let i = 0; i < event.api.getSelectedNodes().length; i++ )\r
+ {\r
+\r
+ if(this.selectedRows[i].disabled)\r
+ {\r
+ this.selectedLockedRows = true;\r
+ }\r
+ else{\r
+ this.selectedLockedRows = false;\r
+ break;\r
+ }\r
+ }\r
+\r
+\r
+\r
+\r
+\r
+ }\r
+ else{\r
+ this.hasSelectedRows = false;\r
+ this.selectedLockedRows = false;\r
+ this.selectedUnlockedRows = true; \r
+\r
+ }\r
+ //One Row was selected\r
+ if((event.api.getSelectedNodes().length == 1)){\r
+ this.selectedSingleRow = true;\r
+ \r
+ }else{\r
+ this.selectedSingleRow = false;\r
+ }\r
+\r
+ }\r
+\r
+ onGridReady(params){\r
+ this.gridApi = params.api;\r
+ \r
+ this.gridColumnApi = params.columnApi;\r
+\r
+ //auto size the column widths\r
+ this.gridColumnApi.autoSizeColumns(['name']);\r
+ }\r
+\r
+ disabledIndicator(params){\r
+ if (params.value){\r
+ return `<mat-icon class="mat-icon mat-icon-no-color" role="img" >\r
+ locked</mat-icon>`; \r
+ }\r
+ }\r
+\r
+\r
+\r
+\r
+ navToDefinition(event){\r
+ this.router.navigate(['/test-definitions', event.data._id]);\r
+ }\r
+\r
+ testDefinitionModeler(){\r
+ this.router.navigate(['/modeler'], {queryParams: {testDefinitionId: this.selectedRows[0]._id}});\r
+ }\r
+\r
+}\r