import { JsonPipe } from '@angular/common'; import { Component, Inject, OnInit, SecurityContext } from '@angular/core'; import { FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms'; import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { MatSelectChange } from '@angular/material/select'; import { ActivatedRoute, Router } from '@angular/router'; import { KeyValue, SimpleResource, WfConf, WfParam, WfSection } from '../common/is.model'; import { ISService } from '../common/is.service'; import { ResMetadataDialog } from '../resources/resources.component'; @Component({ selector: 'app-wf-confs', templateUrl: './wf-confs.component.html', styleUrls: ['./wf-confs.component.css'] }) export class WfConfsComponent implements OnInit { section?: WfSection; confs: KeyValue[] = []; conf?: WfConf; constructor(public service: ISService, public route: ActivatedRoute, public router: Router, public dialog: MatDialog) { } ngOnInit() { this.route.params.subscribe(params => { let sectionId = params['section']; let confId = params['conf']; if (confId) { this.service.loadWfConfiguration(confId, (conf: WfConf) => { this.conf = conf; if (conf.section) { this.service.loadWfSections((data: WfSection[]) => this.section = data.find(s => s.id == conf?.section)); this.service.loadWfConfigurations(conf.section, (data: KeyValue[]) => this.confs = data); } }); } else if (sectionId) { this.service.loadWfSections((data: WfSection[]) => this.section = data.find(s => s.id == sectionId)); this.service.loadWfConfigurations(sectionId, (data: KeyValue[]) => { this.confs = data; if (data.length > 0) { this.router.navigate(['/wfs/conf', data[0].k]); } }); } else { console.log("One of the following parameters is missing: sectionId or confId"); } }); } openAddWfConfDialog(): void { const dialogRef = this.dialog.open(WfConfDialog, { data: { id: '', name: '', section: this.section?.id, enabled: true, priority: 75, workflow: '', schedulingEnabled: false, cronExpression: '0 30 12 1/1 * ?', cronMinInterval: 9600, details: new Map, configured: true, systemParams: new Map, userParams: new Map }, width: '80%' }); dialogRef.afterClosed().subscribe(result => { if (result) this.router.navigate(['/wfs/conf', result.id]);; }); } } @Component({ selector: 'wf-conf-dialog', templateUrl: 'wf-conf-dialog.html', styleUrls: ['./wf-confs.component.css'] }) export class WfConfDialog implements OnInit { wfTemplates: SimpleResource[] = []; wfParameters: WfParam[] = []; wfConfFormStep1 = new FormGroup({ workflow: new FormControl('', [Validators.required]), }); wfConfFormStep2 = new FormGroup({ name: new FormControl('', [Validators.required]), //details: Map, enabled: new FormControl(true, [Validators.required]), priority: new FormControl(75, [Validators.required, Validators.min(1), Validators.max(100)]), }); wfConfFormStep3 = new FormGroup({ //systemParams: Map, //userParams: Map }); wfConfFormStep4 = new FormGroup({ schedulingEnabled: new FormControl(false, [Validators.required]), cronExpression: new FormControl("", [Validators.required]), cronMinInterval: new FormControl("", [Validators.required]), }); wfConfFormFinal = new FormGroup({}); constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) { this.wfConfFormStep1.get('workflow')?.setValue(data.workflow); this.wfConfFormStep2.get('name')?.setValue(data.name); this.wfConfFormStep2.get('enabled')?.setValue(data.enabled); this.wfConfFormStep2.get('priority')?.setValue(data.priority); //details //systemParams, //userParams this.wfConfFormStep4.get('schedulingEnabled')?.setValue(data.schedulingEnabled); this.wfConfFormStep4.get('cronExpression')?.setValue(data.cronExpression); this.wfConfFormStep4.get('cronMinInterval')?.setValue(data.cronMinInterval); } ngOnInit(): void { this.service.loadSimpleResources("wf_template", (data: SimpleResource[]) => this.wfTemplates = data); } onChangeWfTemplate(e: MatSelectChange): void { this.service.loadSimpleResourceContent(e.value, (data: any) => { console.log(data); if (data.parameters) { this.wfParameters = data.parameters; } else { this.wfParameters = JSON.parse(data).parameters; } console.log(this.wfParameters); this.wfConfFormStep3.controls = {}; this.wfParameters.forEach(p => { let validations: ValidatorFn[] = []; if (p.required) { validations.push(Validators.required); } if (p.type == 'number') { validations.push(Validators.pattern('^[0-9]*$')); } this.wfConfFormStep3.addControl(p.name, new FormControl('', validations)); }) }); } onSubmit(): void { const conf = Object.assign({}, this.data, this.wfConfFormStep1.value, this.wfConfFormStep2.value, this.wfConfFormStep3.value, this.wfConfFormStep4.value); this.service.saveWfConfiguration(conf, (data: void) => this.dialogRef.close(1), this.wfConfFormFinal); } onNoClick(): void { this.dialogRef.close(); } }