import { Component, Inject, OnInit } from "@angular/core"; import { UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms"; import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from "@angular/material/dialog"; import { PrefillingSearchRequest, DescriptionProfilingRequest } from "@app/core/model/description-profiling-request/description-profiling-request"; import { DescriptionTemplate } from "@app/core/model/description-template/description-template"; import { Dmp } from "@app/core/model/dmp/dmp"; import { Prefilling } from "@app/core/model/prefilling-source/prefilling-source"; import { DmpBlueprintService } from "@app/core/services/dmp/dmp-blueprint.service"; import { PrefillingSourceService } from "@app/core/services/prefilling-source/prefilling-source.service"; import { ProgressIndicationService } from "@app/core/services/progress-indication/progress-indication-service"; import { SingleAutoCompleteConfiguration } from "@app/library/auto-complete/single/single-auto-complete-configuration"; import { DescriptionTemplateEditorResolver } from "@app/ui/admin/description-template/editor/description-template-editor.resolver"; import { BaseComponent } from "@common/base/base.component"; import { FormService } from "@common/forms/form-service"; import { Guid } from "@common/types/guid"; import { TranslateService } from "@ngx-translate/core"; import { UUID } from "crypto"; import { Observable } from "rxjs"; import { map, takeUntil } from "rxjs/operators"; @Component({ selector: 'prefill-description-component', templateUrl: 'prefill-description.component.html', styleUrls: ['prefill-description.component.scss'] }) export class PrefillDescriptionDialogComponent extends BaseComponent implements OnInit { progressIndication = false; prefillAutoCompleteConfiguration: SingleAutoCompleteConfiguration; prefillSelected: boolean = false; prefillForm: UntypedFormGroup; dmp: Dmp; dmpSectionId: Guid; availableDescriptionTemplates: DescriptionTemplate[] = []; constructor(public dialogRef: MatDialogRef, // private dmpBlueprintService: DmpBlueprintService, // private dialog: MatDialog, // private language: TranslateService, private progressIndicationService: ProgressIndicationService, private fb: UntypedFormBuilder, public prefillingSourceService: PrefillingSourceService, private formService: FormService, @Inject(MAT_DIALOG_DATA) public data: any) { super(); this.dmp = data.dmp; this.dmpSectionId = data.dmpSectionId; this.availableDescriptionTemplates = this.dmp.dmpDescriptionTemplates.filter(x => x.sectionId == this.dmpSectionId).map(x => x.currentDescriptionTemplate); } ngOnInit() { this.progressIndicationService.getProgressIndicationObservable().pipe(takeUntil(this._destroyed)).subscribe(x => { setTimeout(() => { this.progressIndication = x; }); }); this.prefillForm = this.fb.group({ type: this.fb.control(false), descriptionTemplateId: this.fb.control(null, Validators.required), prefillingSourceId: this.fb.control(null, Validators.required), prefillId: this.fb.control(null, Validators.required), }) // if (this.data.availableProfiles && this.data.availableProfiles.length === 1) { // this.addProfileIfUsedLessThanMax(this.data.availableProfiles[0]); // } this.prefillAutoCompleteConfiguration = { filterFn: this.searchDescriptions.bind(this), loadDataOnStart: false, displayFn: (item) => (item['label'].length > 60) ? (item['label'].substr(0, 60) + "...") : item['label'], titleFn: (item) => item['label'], subtitleFn: (item) => item['id'], valueAssign: (item) => item['id'], }; } // addProfileIfUsedLessThanMax(profile: DescriptionProfileModel) { // const dmpSectionIndex = this.data.datasetFormGroup.get('dmpSectionIndex').value; // const blueprintId = this.data.datasetFormGroup.get('dmp').value.profile.id; // this.dmpBlueprintService.getSingle(blueprintId, this.getBlueprintDefinitionFields()) // .pipe(takeUntil(this._destroyed)) // .subscribe(result => { // const section = result.definition.sections[dmpSectionIndex]; // if (section.hasTemplates) { // const foundTemplate = section.descriptionTemplates.find(template => template.descriptionTemplateId === Guid.parse(profile.id)); // if (foundTemplate !== undefined) { // let count = 0; // if (this.data.datasetFormGroup.get('dmp').value.datasets != null) { // for (let dataset of this.data.datasetFormGroup.get('dmp').value.datasets) { // if (dataset.dmpSectionIndex === dmpSectionIndex && dataset.profile.id === foundTemplate.descriptionTemplateId) { // count++; // } // } // if (count < foundTemplate.maxMultiplicity) { // this.prefillForm.get('profile').patchValue(profile); // } // } // } // else { // this.prefillForm.get('profile').patchValue(profile); // } // } // else { // this.prefillForm.get('profile').patchValue(profile); // } // }); // } // checkMinMax(event, profile: DescriptionProfileModel) { // event.stopPropagation(); // const dmpSectionIndex = this.data.datasetFormGroup.get('dmpSectionIndex').value; // const blueprintId = this.data.datasetFormGroup.get('dmp').value.profile.id; // this.dmpBlueprintService.getSingle(blueprintId, this.getBlueprintDefinitionFields()) // .pipe(takeUntil(this._destroyed)) // .subscribe(result => { // const section = result.definition.sections[dmpSectionIndex]; // if (section.hasTemplates) { // const foundTemplate = section.descriptionTemplates.find(template => template.descriptionTemplateId === Guid.parse(profile.id)); // if (foundTemplate !== undefined) { // let count = 0; // if (this.data.datasetFormGroup.get('dmp').value.datasets != null) { // for (let dataset of this.data.datasetFormGroup.get('dmp').value.datasets) { // if (dataset.dmpSectionIndex === dmpSectionIndex && dataset.profile.id === foundTemplate.descriptionTemplateId) { // count++; // } // } // if (count === foundTemplate.maxMultiplicity) { // this.dialog.open(PopupNotificationDialogComponent, { // data: { // title: this.language.instant('DATASET-EDITOR.MAX-DESCRIPTION-DIALOG.TITLE'), // message: this.language.instant('DATASET-EDITOR.MAX-DESCRIPTION-DIALOG.MESSAGE') // }, maxWidth: '30em' // }); // } // else { // this.prefillForm.get('profile').setValue(profile); // } // } // } // else { // this.prefillForm.get('profile').setValue(profile); // } // } // else { // this.prefillForm.get('profile').setValue(profile); // } // }); // } // private getBlueprintDefinitionFields() { // return [ // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.id)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.label)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.description)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.ordinal)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.hasTemplates)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.id)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.category)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.dataType)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.systemFieldType)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.label)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.placeholder)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.description)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.required)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.ordinal)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.id)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.descriptionTemplateId)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.label)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.minMultiplicity)].join('.'), // [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.maxMultiplicity)].join('.'), // ] // } public compareWith(object1: any, object2: any) { return object1 && object2 && object1.id === object2.id; } searchDescriptions(query: string): Observable { const request: PrefillingSearchRequest= { like: query, prefillingSourceId: this.prefillForm.get('prefillingSourceId').value }; return this.prefillingSourceService.search(request).pipe(map(prefilling => prefilling.sort((a, b) => { if (a.label > b.label) { return 1; } else if (a.label < b.label) { return -1; } else { return 0; } }))); } next() { // if (this.isPrefilled) { // if (this.prefillForm.get('prefill').value.data == null) { // this.prefillingService.getPrefillingDescription(this.prefillForm.get('prefill').value.pid, this.prefillForm.get('profile').value.id, this.prefillForm.get('prefill').value.key).subscribe(wizard => { // wizard.profile = this.prefillForm.get('profile').value; // this.closeDialog(wizard); // }); // } // else { // this.prefillingService.getPrefillingDescriptionUsingData(this.prefillForm.get('prefill').value.data, this.prefillForm.get('profile').value.id, this.prefillForm.get('prefill').value.key).subscribe(wizard => { // wizard.profile = this.prefillForm.get('profile').value; // this.closeDialog(wizard); // }); // } // } else { // this.closeDialog(); // } const formData = this.formService.getValue(this.prefillForm.value) as DescriptionProfilingRequest; this.prefillingSourceService.generate(formData, DescriptionTemplateEditorResolver.lookupFields()) .pipe(takeUntil(this._destroyed)).subscribe(description => { if (description) { this.closeDialog(description); } else { this.closeDialog(); } } ); } closeDialog(result = null): void { this.dialogRef.close(result); } }