argos/dmp-frontend/src/app/ui/description/editor/prefill-description/prefill-description.compone...

230 lines
12 KiB
TypeScript
Raw Normal View History

2023-11-29 14:26:40 +01:00
import { Component, Inject, OnInit } from "@angular/core";
import { UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms";
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from "@angular/material/dialog";
2024-03-05 12:41:58 +01:00
import { PrefillingSearchRequest, DescriptionProfilingRequest } from "@app/core/model/description-profiling-request/description-profiling-request";
2024-02-28 14:41:36 +01:00
import { DescriptionTemplate } from "@app/core/model/description-template/description-template";
import { Dmp } from "@app/core/model/dmp/dmp";
2024-03-05 12:41:58 +01:00
import { Prefilling } from "@app/core/model/prefilling-source/prefilling-source";
2023-11-29 14:26:40 +01:00
import { DmpBlueprintService } from "@app/core/services/dmp/dmp-blueprint.service";
2024-03-01 17:45:32 +01:00
import { PrefillingSourceService } from "@app/core/services/prefilling-source/prefilling-source.service";
2023-11-29 14:26:40 +01:00
import { ProgressIndicationService } from "@app/core/services/progress-indication/progress-indication-service";
2024-03-05 12:41:58 +01:00
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";
2023-11-29 14:26:40 +01:00
import { BaseComponent } from "@common/base/base.component";
2024-03-01 17:45:32 +01:00
import { FormService } from "@common/forms/form-service";
2023-11-29 14:26:40 +01:00
import { Guid } from "@common/types/guid";
import { TranslateService } from "@ngx-translate/core";
2024-02-28 14:41:36 +01:00
import { UUID } from "crypto";
2024-03-05 12:41:58 +01:00
import { Observable } from "rxjs";
import { map, takeUntil } from "rxjs/operators";
2023-11-29 14:26:40 +01:00
@Component({
2024-02-28 14:41:36 +01:00
selector: 'prefill-description-component',
templateUrl: 'prefill-description.component.html',
styleUrls: ['prefill-description.component.scss']
2023-11-29 14:26:40 +01:00
})
2024-02-28 14:41:36 +01:00
export class PrefillDescriptionDialogComponent extends BaseComponent implements OnInit {
2023-11-29 14:26:40 +01:00
progressIndication = false;
2024-03-05 12:41:58 +01:00
prefillAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
2024-02-28 14:41:36 +01:00
prefillSelected: boolean = false;
2023-11-29 14:26:40 +01:00
prefillForm: UntypedFormGroup;
2024-02-28 14:41:36 +01:00
dmp: Dmp;
dmpSectionId: Guid;
2024-03-01 17:45:32 +01:00
availableDescriptionTemplates: DescriptionTemplate[] = [];
2024-02-28 14:41:36 +01:00
constructor(public dialogRef: MatDialogRef<PrefillDescriptionDialogComponent>,
// private dmpBlueprintService: DmpBlueprintService,
// private dialog: MatDialog,
// private language: TranslateService,
2023-11-29 14:26:40 +01:00
private progressIndicationService: ProgressIndicationService,
private fb: UntypedFormBuilder,
2024-03-01 17:45:32 +01:00
public prefillingSourceService: PrefillingSourceService,
private formService: FormService,
2023-11-29 14:26:40 +01:00
@Inject(MAT_DIALOG_DATA) public data: any) {
super();
2024-02-28 14:41:36 +01:00
this.dmp = data.dmp;
this.dmpSectionId = data.dmpSectionId;
2024-03-01 17:45:32 +01:00
this.availableDescriptionTemplates = this.dmp.dmpDescriptionTemplates.filter(x => x.sectionId == this.dmpSectionId).map(x => x.currentDescriptionTemplate);
2023-11-29 14:26:40 +01:00
}
ngOnInit() {
this.progressIndicationService.getProgressIndicationObservable().pipe(takeUntil(this._destroyed)).subscribe(x => {
setTimeout(() => { this.progressIndication = x; });
});
this.prefillForm = this.fb.group({
type: this.fb.control(false),
2024-03-01 17:45:32 +01:00
descriptionTemplateId: this.fb.control(null, Validators.required),
2024-03-05 12:41:58 +01:00
prefillingSourceId: this.fb.control(null, Validators.required),
prefillId: this.fb.control(null, Validators.required),
2023-11-29 14:26:40 +01:00
})
2024-02-28 14:41:36 +01:00
// if (this.data.availableProfiles && this.data.availableProfiles.length === 1) {
// this.addProfileIfUsedLessThanMax(this.data.availableProfiles[0]);
// }
2024-03-05 12:41:58 +01:00
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'],
};
2023-11-29 14:26:40 +01:00
}
2024-02-28 14:41:36 +01:00
// 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);
// }
// });
// }
2023-11-29 14:26:40 +01:00
2024-02-28 14:41:36 +01:00
// 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);
// }
// });
// }
2023-11-29 14:26:40 +01:00
2024-02-28 14:41:36 +01:00
// private getBlueprintDefinitionFields() {
// return [
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.id)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.label)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.description)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.ordinal)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.hasTemplates)].join('.'),
2023-11-29 14:26:40 +01:00
2024-02-28 14:41:36 +01:00
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.id)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.category)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.dataType)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.systemFieldType)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.label)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.placeholder)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.description)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.required)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.fields), nameof<FieldInSection>(x => x.ordinal)].join('.'),
2023-11-29 14:26:40 +01:00
2024-02-28 14:41:36 +01:00
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.descriptionTemplates), nameof<DescriptionTemplatesInSection>(x => x.id)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.descriptionTemplates), nameof<DescriptionTemplatesInSection>(x => x.descriptionTemplateId)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.descriptionTemplates), nameof<DescriptionTemplatesInSection>(x => x.label)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.descriptionTemplates), nameof<DescriptionTemplatesInSection>(x => x.minMultiplicity)].join('.'),
// [nameof<DmpBlueprint>(x => x.definition), nameof<DmpBlueprintDefinition>(x => x.sections), nameof<DmpBlueprintDefinitionSection>(x => x.descriptionTemplates), nameof<DescriptionTemplatesInSection>(x => x.maxMultiplicity)].join('.'),
// ]
// }
2023-11-29 14:26:40 +01:00
public compareWith(object1: any, object2: any) {
return object1 && object2 && object1.id === object2.id;
}
2024-03-05 12:41:58 +01:00
searchDescriptions(query: string): Observable<Prefilling[]> {
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;
}
})));
}
2023-11-29 14:26:40 +01:00
next() {
2024-02-28 14:41:36 +01:00
// 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();
// }
2024-03-01 17:45:32 +01:00
const formData = this.formService.getValue(this.prefillForm.value) as DescriptionProfilingRequest;
2024-03-05 12:41:58 +01:00
this.prefillingSourceService.generate(formData, DescriptionTemplateEditorResolver.lookupFields())
.pipe(takeUntil(this._destroyed)).subscribe(description => {
if (description) {
this.closeDialog(description);
} else {
this.closeDialog();
}
}
2024-03-01 17:45:32 +01:00
);
2023-11-29 14:26:40 +01:00
}
closeDialog(result = null): void {
this.dialogRef.close(result);
}
}