added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / modules / create-test-form / create-test-form.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, EventEmitter, Input, OnInit, Output, ViewChild, ElementRef, OnDestroy } from '@angular/core';\r
18 import { MatDialog, MatSnackBar } from '@angular/material';\r
19 import { SelectTestHeadModalComponent } from '../select-test-head-modal/select-test-head-modal.component';\r
20 import { GroupService } from '../../services/group.service';\r
21 import { TestDefinitionService } from '../../services/test-definition.service';\r
22 import { AlertModalComponent } from '../alert-modal/alert-modal.component';\r
23 import { Alert } from 'selenium-webdriver';\r
24 import { ListService } from '../../services/list.service';\r
25 import { AlertSnackbarComponent } from '../alert-snackbar/alert-snackbar.component';\r
26 import { TestHeadService } from 'app/shared/services/test-head.service';\r
27 import { FileUploader, FileItem, ParsedResponseHeaders } from 'ng2-file-upload';\r
28 import { AppGlobals } from 'app/app.global';\r
29 import { HttpHeaders } from '@angular/common/http';\r
30 import { CookieService } from 'ngx-cookie-service';\r
31 import { stringify } from '@angular/core/src/render3/util';\r
32 import Modeler from 'bpmn-js';\r
33 import { FileService } from 'app/shared/services/file.service';\r
34 import { FileTransferService } from 'app/shared/services/file-transfer.service';\r
35 import { TestDefinition } from './test-definition.class';\r
36 import { connectableObservableDescriptor } from 'rxjs/internal/observable/ConnectableObservable';\r
37 import { Buffer } from 'buffer';\r
38 import { ViewWorkflowModalComponent } from '../view-workflow-modal/view-workflow-modal.component';\r
39 import { BpmnFactoryService } from 'app/shared/factories/bpmn-factory.service';\r
40 import { Bpmn } from 'app/shared/models/bpmn.model';\r
41 \r
42 \r
43 @Component({\r
44   selector: 'app-create-test-form',\r
45   templateUrl: './create-test-form.component.pug',\r
46   styleUrls: ['./create-test-form.component.scss']\r
47 })\r
48 export class CreateTestFormComponent implements OnInit, OnDestroy {\r
49 \r
50 \r
51   public codeConfig = {\r
52     mode: 'yaml',\r
53     theme: 'eclipse',\r
54     lineNumbers: true\r
55   };\r
56 \r
57   public trackByFn;\r
58 \r
59   public selectedTestHead;\r
60   public groups;\r
61   public isUploading;\r
62   public successUpload = false;\r
63   public processDefinitionKey = false;\r
64   public validateResponse;\r
65   public pStatus;\r
66   public file: File;\r
67   public hasBeenSaved = false;\r
68   public saved = false;\r
69   public isClicked;\r
70   public isZip = true;\r
71   public viewer: Bpmn;\r
72   public scriptFiles = [];\r
73   public existingTd;\r
74 \r
75   @ViewChild('testDefinitionForm') form: any;\r
76   @ViewChild('canvas') canvas;\r
77   @ViewChild('scripts') scripts: ElementRef;\r
78   @ViewChild('file') bpmnFileInput: ElementRef;\r
79 \r
80   @Input() public listKey;\r
81 \r
82   @Input() public formData;\r
83 \r
84   @Output() public childEvent = new EventEmitter();\r
85 \r
86   public uploader: FileUploader;\r
87   public bpmnUploader: FileUploader;\r
88 \r
89   public inProgress = false;\r
90 \r
91   // New variables\r
92   public ptd: TestDefinition;\r
93   public isNew = true;\r
94 \r
95   constructor(\r
96     public dialog: MatDialog,\r
97     private list: ListService,\r
98     private testHead: TestHeadService,\r
99     private group: GroupService,\r
100     private testDefinition: TestDefinitionService,\r
101     private snack: MatSnackBar,\r
102     private cookie: CookieService,\r
103     private fileTransfer: FileTransferService,\r
104     private fileService: FileService,\r
105     private bpmnFactory: BpmnFactoryService\r
106     ) { }\r
107 \r
108   print(){\r
109     console.log(this.ptd);\r
110   }\r
111 \r
112   async ngOnInit() {\r
113     //this.setNew();\r
114 \r
115     this.viewer = await this.bpmnFactory.setup({\r
116       mode: 'viewer',\r
117       options: {\r
118         container: this.canvas.nativeElement\r
119       }\r
120     })\r
121     \r
122     this.ptd = new TestDefinition();\r
123     this.ptd.reset();\r
124     this.ptd.switchVersion();\r
125 \r
126     let uploadOptions = {\r
127       url: AppGlobals.baseAPIUrl + 'file-transfer',\r
128       authTokenHeader: 'Authorization',\r
129       authToken: 'Bearer ' + JSON.parse(this.cookie.get('access_token'))\r
130     };\r
131 \r
132     //File Uploaders\r
133     this.uploader = new FileUploader(uploadOptions);\r
134     this.bpmnUploader = new FileUploader(uploadOptions);\r
135 \r
136     if (this.formData && this.formData !== 'new') {\r
137       this.hasBeenSaved = true;\r
138       this.successUpload = true;\r
139       this.isNew = false;\r
140       this.setTestDefinition();\r
141     }\r
142 \r
143     this.group.find({$limit: -1}).subscribe((x) => {\r
144       this.groups = x;\r
145     });\r
146 \r
147   }\r
148 \r
149   ngOnDestroy(){\r
150     \r
151   }\r
152 \r
153   waitSave(){\r
154     return new Promise((resolve, reject) => {\r
155       console.log('waitsave')\r
156       //upload bpmn file\r
157       this.saveBpmnFile().then(bpmnFile => {\r
158         console.log(bpmnFile)\r
159         console.log('pass save bpmnfile')\r
160         this.checkTestDataTemplate();\r
161 \r
162         let data = this.gatherTestDefinition(bpmnFile);\r
163         console.log(data)\r
164         //If this is not a new version\r
165         if(!this.existingTd){\r
166           delete data._id;\r
167           \r
168           this.create(data).then(\r
169             result => {\r
170               resolve(result);\r
171               this.setTestDefinition(result);\r
172               this.showHasBeenSaved();\r
173             }\r
174           ).catch(err => {\r
175             reject(err);\r
176             this.showHasNotBeenSaved();\r
177           });\r
178         }else{\r
179           //create version by updating definition\r
180           this.saveVersion(data).then(\r
181             result => {\r
182               resolve(result);\r
183               this.setTestDefinition(result);\r
184               this.showHasBeenSaved();\r
185             }\r
186           ).catch(err => {\r
187             reject(err);\r
188             this.showHasNotBeenSaved();\r
189           });\r
190         }\r
191       });\r
192     })\r
193   }\r
194   \r
195   // Saves Test Definition - Triggered by "Save" button\r
196   save() {\r
197     //set in progress\r
198     this.inProgress = true;\r
199     return this.waitSave();\r
200 \r
201   }\r
202 \r
203   // Updates Test Definition - Triggered by "Update" button\r
204   update() {\r
205     this.inProgress = true;\r
206     return this.saveBpmnFile().then(bpmnFile => {\r
207       this.checkTestDataTemplate();\r
208 \r
209       var data = this.gatherTestDefinition(bpmnFile);\r
210 \r
211       return this.testDefinition.patch(data)\r
212         .subscribe(\r
213           result => {\r
214             this.uploadResources(result).then(\r
215               res => {\r
216                 this.setTestDefinition(res);\r
217                 this.showHasBeenUpdated();\r
218               }\r
219             );\r
220             return result;\r
221           },\r
222           error => {\r
223             this.showHasNotBeenUpdated(error);\r
224           }\r
225         );\r
226     });\r
227   }\r
228 \r
229   deleteVersion(){\r
230     let deleteDialog = this.dialog.open(AlertModalComponent, {\r
231       width: '250px',\r
232       data: { type: 'confirmation', message: 'Are you sure you want to delete version ' + this.ptd.currentVersionName }\r
233     });\r
234 \r
235     deleteDialog.afterClosed().subscribe(\r
236       result => {\r
237         if(result){\r
238           this.inProgress = true;\r
239           if(this.ptd.bpmnInstances.length == 1){\r
240             this.testDefinition.delete(this.ptd._id).subscribe(\r
241               result => {\r
242                 this.childEvent.emit();\r
243               }\r
244             )\r
245           }else{\r
246             this.ptd.removeBpmnInstance(this.ptd.currentVersionName);\r
247             this.update().then(\r
248               res => {\r
249                 this.inProgress = false;\r
250               }\r
251             );\r
252           }\r
253         }\r
254       }\r
255     )\r
256   }\r
257 \r
258   // Deploys Test Definition version - Triggerd by "Deploy" button\r
259   deploy(versionName?) {\r
260     this.inProgress = true;\r
261     //console.log(this.ptd)\r
262     this.testDefinition.deploy(this.ptd, versionName)\r
263       .subscribe(result => {\r
264         \r
265         this.handleResponse(result);\r
266         this.inProgress = false;\r
267         if (result['statusCode'] == 200) {\r
268           this.snack.openFromComponent(AlertSnackbarComponent, {\r
269             duration: 1500,\r
270             data: {\r
271               message: 'Test Definition Deployed!'\r
272             }\r
273           });\r
274           this.ptd.currentInstance.isDeployed = true;\r
275         } else {\r
276           this.dialog.open(AlertModalComponent, {\r
277             width: '250px',\r
278             data: {\r
279               type: 'Alert',\r
280               message: JSON.stringify(result)\r
281             }\r
282           });\r
283         }\r
284       },\r
285         err => {\r
286           this.inProgress = false;\r
287         }\r
288 \r
289       );\r
290   }\r
291 \r
292   create(data){\r
293     return new Promise((resolve, reject) => {\r
294       this.testDefinition.create(data)\r
295         .subscribe(\r
296           result => {\r
297             this.uploadResources(result).then(\r
298               res => {\r
299                 resolve(res);\r
300               }\r
301             );\r
302           },\r
303           error => {\r
304             this.dialog.open(AlertModalComponent, {\r
305               width: '250px',\r
306               data: {\r
307                 type: 'Alert',\r
308                 message: JSON.stringify(error)\r
309               }\r
310             });\r
311             reject(error);\r
312           }\r
313         );\r
314     });\r
315   }\r
316 \r
317   newVersion(processDefinitionKey){\r
318     this.hasBeenSaved = false;\r
319     this.isNew = true;\r
320     this.ptd.reset();\r
321     this.ptd.switchVersion();\r
322     this.ptd.setProcessDefinitionKey(processDefinitionKey);\r
323   }\r
324 \r
325   checkProcessDefinitionKey() {\r
326     this.pStatus = 'loading';\r
327     this.testDefinition.check(this.ptd.getProcessDefinitionKey()).subscribe(result => {\r
328       console.log(result);\r
329       if (result['statusCode'] == 200) {\r
330         this.pStatus = 'unique';\r
331       } else {\r
332         this.pStatus = 'notUnique';\r
333       }\r
334 \r
335       this.ptd.bpmnInstances = this.ptd.bpmnInstances.filter((e, i) => {\r
336         return i == 0;\r
337       })\r
338       \r
339       // this.ptd.bpmnInstances.forEach((elem, val) => {\r
340       //   if(val > 0){\r
341       //     this.ptd.bpmnInstances.splice(val, 1);\r
342       //   }\r
343       // })\r
344 \r
345       //New Code\r
346       if(result['body'] && result['body'][0]){\r
347         //when changing bpmn dont\r
348         //if(this.ptd.currentInstance.isDeployed){\r
349           let res = result['body'][0];\r
350           this.existingTd = true;\r
351           this.ptd.setId(res._id);\r
352           this.ptd.setName(res.testName);\r
353           this.ptd.setDescription(res.testDescription);\r
354           this.ptd.setGroupId(res.groupId);\r
355           this.ptd.setVersion(res.bpmnInstances.length + 1);\r
356           //this.ptd.bpmnInstances = [];\r
357 \r
358           for(let i = 0; i < res.bpmnInstances.length; i++){\r
359             this.ptd.addBpmnInstance(res.bpmnInstances[i]);\r
360           }\r
361 \r
362           \r
363           //this.ptd.addBpmnInstance (res.bpmnInstances);\r
364         //}\r
365       }else{\r
366         this.existingTd = false;\r
367         this.ptd.setId(null);\r
368         this.ptd.setName('');\r
369         this.ptd.setDescription('');\r
370         this.ptd.setGroupId('');\r
371         this.ptd.setVersion(1);\r
372       }\r
373 \r
374       if(!this.ptd.currentInstance.version){\r
375         this.ptd.setNewVersion();\r
376       }\r
377 \r
378     });\r
379   }\r
380 \r
381   validateFile() {\r
382 \r
383     this.isUploading = true\r
384     this.fetchFileContents(val => {\r
385       //\r
386       this.ptd.currentInstance.bpmnXml = val;\r
387       if (!this.ptd.currentInstance.bpmnXml) {\r
388         this.isUploading = false;\r
389         this.dialog.open(AlertModalComponent, {\r
390           width: '250px',\r
391           data: {\r
392             type: 'Alert',\r
393             message: 'File was not selected. Please try again.'\r
394           }\r
395         });\r
396         return null;\r
397       }\r
398       \r
399       this.testDefinition.validate(this.ptd.getAll())\r
400         .subscribe(\r
401           result => {\r
402             this.handleResponse(result);\r
403             //\r
404             this.isUploading = false;\r
405             this.ptd.currentInstance.bpmnHasChanged = true;\r
406             this.loadDiagram();\r
407           },\r
408           err => {\r
409             this.dialog.open(AlertModalComponent, {\r
410               width: '250px',\r
411               data: {\r
412                 type: 'Alert',\r
413                 message: 'Something went wrong. Please try again'\r
414               }\r
415             });\r
416             this.isUploading = false;\r
417           }\r
418         );\r
419     });\r
420 \r
421 \r
422   }\r
423 \r
424   showHasNotBeenSaved(){\r
425     this.dialog.open(AlertModalComponent, {\r
426       width: '250px',\r
427       data: {\r
428         type: 'Alert',\r
429         message: 'There was a problem with saving the test definition.'\r
430       }\r
431     });\r
432     this.inProgress = false;\r
433   }\r
434 \r
435   showHasBeenSaved(){\r
436     this.snack.openFromComponent(AlertSnackbarComponent, {\r
437       duration: 1500,\r
438       data: {\r
439         message: 'Test Definition Saved!'\r
440       }\r
441     });\r
442     //this.switchVersion();\r
443     this.ptd.switchVersion();\r
444     this.hasBeenSaved = true;\r
445     this.saved = true;\r
446     this.form.form.markAsPristine();\r
447     this.inProgress = false;\r
448   }\r
449 \r
450   showHasBeenUpdated(){\r
451     this.snack.openFromComponent(AlertSnackbarComponent, {\r
452       duration: 1500,\r
453       data: {\r
454         message: 'Test Definition Updated!'\r
455       }\r
456     });\r
457     //this.switchVersion();\r
458     this.ptd.switchVersion(this.ptd.currentInstance.version);\r
459     this.saved = true;\r
460     this.form.form.markAsPristine();\r
461     this.ptd.currentInstance.bpmnHasChanged = false;\r
462     this.inProgress = false;\r
463   }\r
464 \r
465   showHasNotBeenUpdated(error = null){\r
466     this.dialog.open(AlertModalComponent, {\r
467       width: '250px',\r
468       data: {\r
469         type: 'Alert',\r
470         message: JSON.stringify(error)\r
471       }\r
472     });\r
473     this.inProgress = false;\r
474   }\r
475 \r
476   setTestDefinition(data = null){\r
477     //new\r
478     if(data){\r
479       \r
480       this.ptd.setAll(data);\r
481     }else{\r
482       this.ptd.setAll(JSON.parse(JSON.stringify(this.formData)));\r
483     }\r
484 \r
485     this.switchVersion();\r
486 \r
487     //console.log(this.ptd);\r
488     \r
489   }\r
490 \r
491   clearQueue(){\r
492     this.uploader.clearQueue();\r
493     if(this.scripts){\r
494       this.scripts.nativeElement.value = null;\r
495     }\r
496   }\r
497 \r
498   switchVersion(versionName = null){\r
499     this.ptd.switchVersion(versionName);\r
500     this.checkTestDataTemplate();\r
501 \r
502     this.clearQueue();\r
503     this.bpmnFileInput.nativeElement.value = null;\r
504 \r
505     //Get bpmn file contents\r
506     this.fileTransfer.get(this.ptd.currentInstance.bpmnFileId).subscribe(\r
507       result => {\r
508         result = new Buffer(result as Buffer);\r
509         this.ptd.currentInstance.bpmnXml = result.toString();\r
510         this.loadDiagram();\r
511       }\r
512     );\r
513 \r
514     //get info on resource file\r
515     if(this.ptd.currentInstance.resourceFileId){\r
516       this.fileService.get(this.ptd.currentInstance.resourceFileId).subscribe(\r
517         result => {\r
518           this.ptd.currentInstance.resourceFileName = result['filename'];\r
519         }\r
520       )\r
521     }\r
522 \r
523     if(this.ptd.currentInstance.testHeads){\r
524       this.ptd.currentInstance.dataTestHeads = [];\r
525       this.ptd.currentInstance.testHeads.forEach((elem, val) => {\r
526         //Find test head info\r
527         const e = elem;\r
528         this.testHead.get(e.testHeadId).subscribe(\r
529           result => {\r
530             this.ptd.currentInstance.dataTestHeads.push({\r
531               testHeadId: e.testHeadId,\r
532               bpmnVthTaskId: e.bpmnVthTaskId,\r
533               testHead: JSON.parse(JSON.stringify(result))\r
534             });\r
535           },\r
536           err => {\r
537             this.ptd.currentInstance.dataTestHeads.push({\r
538               testHeadId: e.testHeadId,\r
539               bpmnVthTaskId: e.bpmnVthTaskId,\r
540               testHead: { _id: e.testHeadId, testHeadName: 'No Access' }\r
541             });\r
542           }\r
543         );\r
544       });\r
545     }\r
546   }\r
547 \r
548   gatherTestDefinition(bpmnFile = null) {\r
549 \r
550     if(bpmnFile){\r
551       this.ptd.currentInstance.bpmnFileId = bpmnFile._id;\r
552     }\r
553 \r
554     this.ptd.currentInstance.testHeads = [];\r
555     this.ptd.currentInstance.dataTestHeads.forEach((elem, val) => {\r
556       this.ptd.currentInstance.testHeads.push({\r
557         testHeadId: elem.testHead._id,\r
558         bpmnVthTaskId: elem.bpmnVthTaskId\r
559       });\r
560     });\r
561 \r
562     return this.ptd.getAll();\r
563   \r
564   }\r
565 \r
566   saveDeploy() {\r
567     let version = JSON.parse(JSON.stringify(this.ptd.currentInstance.version));\r
568     console.log(version)\r
569     this.save().then(x => {\r
570       this.deploy(version);\r
571     });\r
572   }\r
573 \r
574   updateDeploy() {\r
575     let version = JSON.parse(JSON.stringify(this.ptd.currentInstance.version));\r
576     this.update().then(x => {\r
577       this.deploy(version);\r
578     });\r
579   }S\r
580 \r
581   handleResponse(result) {\r
582     this.successUpload = true;\r
583     this.processDefinitionKey = false;\r
584     //this.validateResponse = result;\r
585     if (result['body']['errors']) {\r
586 \r
587 \r
588       if (result['body']['errors']['processDefinitionKey']) {\r
589         this.openProcessDefinitionKeyModal();\r
590         this.pStatus = 'notUnique';\r
591         this.ptd.setProcessDefinitionKey(result['body'].errors.processDefinitionKey.key)\r
592         //this.td.processDefinitionKey = result['body']['errors']['processDefinitionKey']['key'];\r
593         this.processDefinitionKey = true;\r
594       }\r
595       if (result['body']['errors']['notFound']) {\r
596         this.dialog.open(AlertModalComponent, {\r
597           width: '250px',\r
598           data: { type: 'alert', message: result['body']['errors']['notFound']['error'] }\r
599         });\r
600         this.successUpload = false;\r
601       }\r
602       if (result['body']['errors']['startEvent']) {\r
603         this.dialog.open(AlertModalComponent, {\r
604           width: '250px',\r
605           data: { type: 'alert', message: result['body']['errors']['startEvent']['error'] }\r
606         });\r
607         this.successUpload = false;\r
608       }\r
609       if (result['body']['errors']['required']) {\r
610         this.dialog.open(AlertModalComponent, {\r
611           width: '250px',\r
612           data: { type: 'alert', message: result['body']['errors']['required']['error'] }\r
613         });\r
614         this.successUpload = false;\r
615       }\r
616       if (result['body']['errors']['permissions']) {\r
617         let mess = '';\r
618         result['body']['errors']['permissions'].forEach(elem => {\r
619           mess += elem.error + '\n';\r
620         })\r
621         this.dialog.open(AlertModalComponent, {\r
622           width: '250px',\r
623           data: { type: 'alert', message: mess }\r
624         });\r
625         this.successUpload = false;\r
626       }\r
627 \r
628     }else{\r
629       this.markAsDirty();\r
630     }\r
631     // Update list of test heads\r
632     if (result['body']['bpmnVthTaskIds']) {\r
633       this.ptd.currentInstance.dataTestHeads = result['body'].bpmnVthTaskIds;\r
634       this.ptd.currentInstance.testHeads = [];\r
635       //this.definitionInstance.testHeads = result['body']['bpmnVthTaskIds'];\r
636     }\r
637 \r
638     //Update plfos list\r
639     if(result['body']['bpmnPfloTaskIds']){\r
640       this.ptd.currentInstance.pflos = result['body'].bpmnPfloTaskIds;\r
641     }\r
642 \r
643     if (result['body']['processDefinitionKey']) {\r
644       this.ptd.setProcessDefinitionKey(result['body'].processDefinitionKey);\r
645       //this.td.processDefinitionKey = result['body']['processDefinitionKey'];\r
646       this.checkProcessDefinitionKey()\r
647     }\r
648   }\r
649 \r
650   markAsDirty() {\r
651     this.form.control.markAsDirty();\r
652   }\r
653 \r
654   //returns promise for file object \r
655   saveBpmnFile() {\r
656     return new Promise((resolve, reject) => {\r
657 \r
658       //check for bpmnXml\r
659       if (!this.ptd.currentInstance.bpmnXml) {\r
660         this.dialog.open(AlertModalComponent, {\r
661           width: '250px',\r
662           data: {\r
663             type: 'Alert',\r
664             message: 'No File found. Please select a file to upload'\r
665           }\r
666         });\r
667         reject();\r
668       }\r
669 \r
670       if(this.ptd.currentInstance.bpmnHasChanged){\r
671         // Upload\r
672         console.log('validate save call')\r
673         this.testDefinition.validateSave(this.ptd).subscribe(\r
674           result => {\r
675             resolve(JSON.parse(result.toString())[0]);\r
676           }\r
677         );\r
678       }else{\r
679         //bpmn has not changed, so did not save it.\r
680         resolve(null);\r
681       }\r
682     });\r
683   }\r
684 \r
685   saveVersion(data){\r
686     return new Promise((resolve, reject) => {\r
687 \r
688       let newBpmnInsance = JSON.parse(JSON.stringify(data.bpmnInstances[0]));\r
689       delete data.bpmnInstances;\r
690       data['$push'] = {\r
691         bpmnInstances: newBpmnInsance\r
692       }\r
693 \r
694       console.log(data)\r
695 \r
696       this.testDefinition.patch(data).subscribe(\r
697         result => {\r
698           this.uploadResources(result).then(\r
699             res => {\r
700               resolve(res);\r
701             }\r
702           )\r
703         },\r
704         err => {\r
705           reject(err);\r
706         }\r
707       )\r
708     });\r
709   }\r
710 \r
711   uploadResources(td){\r
712     return new Promise((resolve, reject) => {\r
713       if(this.uploader.queue.length > 0){\r
714         //console.log('has file');\r
715         this.uploader.uploadAll();\r
716         this.uploader.onCompleteItem = (item: FileItem, response: string, status: Number, headers: ParsedResponseHeaders) => {\r
717           this.scriptFiles.push(JSON.parse(response)[0]);\r
718           //console.log('in file')\r
719         }\r
720         this.uploader.onCompleteAll = () => {\r
721           //console.log('complete')\r
722           let scriptFilesId = [];\r
723           for (let i = 0; i < this.scriptFiles.length; i++) {\r
724             scriptFilesId.push(this.scriptFiles[i]['_id']);\r
725           }\r
726           td['bpmnInstances'][this.ptd.currentVersion]['resourceFileId'] = scriptFilesId[0];\r
727           //console.log(td);\r
728           this.testDefinition.patch(td).subscribe(\r
729             res => {\r
730               //console.log(res);\r
731               resolve(res);\r
732             },\r
733             err => {\r
734               reject(err);\r
735             }\r
736           );\r
737         }\r
738       }else{\r
739         resolve(td);\r
740       }\r
741     });\r
742   }\r
743 \r
744   checkTestDataTemplate() {\r
745     if (this.ptd.currentInstance.testDataTemplate == null || this.ptd.currentInstance.testDataTemplate == '') {\r
746       delete this.ptd.currentInstance.testDataTemplate;\r
747     }\r
748     // if (this.definitionInstance.testDataTemplate == null || this.definitionInstance.testDataTemplate == '') {\r
749     //   delete this.definitionInstance.testDataTemplate;\r
750     // }\r
751   }\r
752 \r
753   async loadDiagram() {\r
754     if (this.ptd.currentInstance.bpmnXml) {\r
755       //render xml and display\r
756       this.viewer.setBpmnXml(this.ptd.currentInstance.bpmnXml);\r
757       // if (!this.viewer) {\r
758       //   this.viewer = new Modeler({\r
759       //     container: this.canvas.nativeElement\r
760       //   });\r
761       // }\r
762 \r
763       // this.viewer.importXML(this.ptd.currentInstance.bpmnXml, (err) => {\r
764       //   if (!err) {\r
765       //     this.viewer.get('canvas').zoom('fit-viewport');\r
766       //   } else {\r
767       //     //\r
768       //   }\r
769       // });\r
770 \r
771     }\r
772   }\r
773 \r
774   enlargeBpmn(){\r
775     this.dialog.open(ViewWorkflowModalComponent, {\r
776       data: {\r
777         xml: this.ptd.currentInstance.bpmnXml\r
778       },\r
779       width: '100%',\r
780       height: '100%'\r
781     })\r
782   }\r
783 \r
784   fetchFileContents(callback) {\r
785     var val = "x";\r
786     var fileToLoad = (document.getElementById('file'))['files'][0];\r
787     var fileReader = new FileReader();\r
788     if (!fileToLoad) {\r
789       return null;\r
790     }\r
791     fileReader.onload = function (event) {\r
792       //\r
793       val = event.target['result'] as string;\r
794 \r
795       //\r
796       callback(val);\r
797     }\r
798     fileReader.readAsText(fileToLoad);\r
799   }\r
800 \r
801   openProcessDefinitionKeyModal() {\r
802     const dialogRef = this.dialog.open(AlertModalComponent, {\r
803       width: '250px',\r
804       data: { type: 'warning', message: 'You cannot use this process definition key. Please change it.' }\r
805     });\r
806   }\r
807 \r
808   checkVersionUnique(){\r
809     let exists = false;\r
810     this.ptd.bpmnInstances.forEach(elem => {\r
811       if(elem != this.ptd.currentInstance && elem.version == this.ptd.currentInstance.version){\r
812         exists = true;\r
813       }\r
814     });\r
815 \r
816     if(exists){\r
817       this.form.controls['version'].setErrors({error: 'Version Already Exists'});\r
818     }else{\r
819       this.form.controls['version'].setErrors(null);\r
820     }\r
821   }\r
822 \r
823 }