import { Component, OnInit } from '@angular/core'; import { FormArray, UntypedFormGroup } from '@angular/forms'; import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Router } from '@angular/router'; import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { ReferenceTypeService } from '@app/core/services/reference-type/reference-type.service'; import { EnumUtils } from '@app/core/services/utilities/enum-utils.service'; // import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item'; import { DatePipe } from '@angular/common'; import { IsActive } from '@app/core/common/enum/is-active.enum'; import { AppPermission } from '@app/core/common/enum/permission.enum'; import { ReferenceFieldDataType } from '@app/core/common/enum/reference-field-data-type'; import { ReferenceTypeExternalApiHTTPMethodType } from '@app/core/common/enum/reference-type-external-api-http-method-type'; import { ReferenceTypeSourceType } from '@app/core/common/enum/reference-type-source-type'; import { ReferenceType, ReferenceTypeDefinition, ReferenceTypePersist } from '@app/core/model/reference-type/reference-type'; import { AuthService } from '@app/core/services/auth/auth.service'; import { LoggingService } from '@app/core/services/logging/logging-service'; import { QueryParamsService } from '@app/core/services/utilities/query-params.service'; import { BaseEditor } from '@common/base/base-editor'; import { FormService } from '@common/forms/form-service'; import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component'; import { HttpErrorHandlingService } from '@common/modules/errors/error-handling/http-error-handling.service'; import { FilterService } from '@common/modules/text-filter/filter-service'; import { Guid } from '@common/types/guid'; import { TranslateService } from '@ngx-translate/core'; import { map, takeUntil } from 'rxjs/operators'; import { DependencyPropertyEditorModel, QueryConfigEditorModel, ReferenceTypeDefinitionEditorModel, ReferenceTypeEditorModel, ReferenceTypeFieldEditorModel, ReferenceTypeSourceBaseConfigurationEditorModel, ReferenceTypeSourceBaseDependencyEditorModel, ReferenceTypeStaticOptionEditorModel, ResultFieldsMappingConfigurationEditorModel } from './reference-type-editor.model'; import { ReferenceTypeEditorResolver } from './reference-type-editor.resolver'; import { ReferenceTypeEditorService } from './reference-type-editor.service'; @Component({ selector: 'app-reference-type-editor-component', templateUrl: 'reference-type-editor.component.html', styleUrls: ['./reference-type-editor.component.scss'], providers: [ReferenceTypeEditorService] }) export class ReferenceTypeEditorComponent extends BaseEditor implements OnInit { isNew = true; isDeleted = false; formGroup: UntypedFormGroup = null; showInactiveDetails = false; referenceTypeSourceType = ReferenceTypeSourceType; referenceTypeExternalApiHTTPMethodType = ReferenceTypeExternalApiHTTPMethodType; public referenceTypeSourceTypeEnum = this.enumUtils.getEnumValues(ReferenceTypeSourceType); public referenceFieldDataTypeEnum = this.enumUtils.getEnumValues(ReferenceFieldDataType); public referenceTypeExternalApiHTTPMethodTypeEnum = this.enumUtils.getEnumValues(ReferenceTypeExternalApiHTTPMethodType); referenceTypes: ReferenceType[] = null; sourceKeys: string[] = []; sourceKeysMap: Map = new Map(); propertyCodes: string[] = []; targetPropertyCodes: string[] = []; targetPropertyCodesMap: Map = new Map(); protected get canDelete(): boolean { return !this.isDeleted && !this.isNew && this.hasPermission(this.authService.permissionEnum.DeleteReferenceType); } protected get canSave(): boolean { return !this.isDeleted && this.hasPermission(this.authService.permissionEnum.EditReferenceType); } protected get canFinalize(): boolean { return !this.isDeleted && this.hasPermission(this.authService.permissionEnum.EditReferenceType); } private hasPermission(permission: AppPermission): boolean { return this.authService.hasPermission(permission) || this.editorModel?.permissions?.includes(permission); } constructor( // BaseFormEditor injected dependencies protected dialog: MatDialog, protected language: TranslateService, protected formService: FormService, protected router: Router, protected uiNotificationService: UiNotificationService, protected httpErrorHandlingService: HttpErrorHandlingService, protected filterService: FilterService, protected datePipe: DatePipe, protected route: ActivatedRoute, protected queryParamsService: QueryParamsService, // Rest dependencies. Inject any other needed deps here: public authService: AuthService, public enumUtils: EnumUtils, private referenceTypeService: ReferenceTypeService, private logger: LoggingService, private referenceTypeEditorService: ReferenceTypeEditorService ) { super(dialog, language, formService, router, uiNotificationService, httpErrorHandlingService, filterService, datePipe, route, queryParamsService); } ngOnInit(): void { super.ngOnInit(); if ((this.formGroup.get('definition').get('sources') as FormArray).length == 0) { this.addSource(); } } getItem(itemId: Guid, successFunction: (item: ReferenceType) => void) { this.referenceTypeService.getSingle(itemId, ReferenceTypeEditorResolver.lookupFields()) .pipe(map(data => data as ReferenceType), takeUntil(this._destroyed)) .subscribe( data => successFunction(data), error => this.onCallbackError(error) ); } prepareForm(data: ReferenceType) { try { this.editorModel = data ? new ReferenceTypeEditorModel().fromModel(data) : new ReferenceTypeEditorModel(); this.getReferenceTypes(this.editorModel.id); this.propertyCodes = this.referenceTypeService.getSystemFields([]); if (data) { this.editorModel.definition.sources.forEach(source => source.dependencies.forEach(dependency => { this.selectedReferenceTypeChanged(dependency.referenceTypeCode); })); this.editorModel.definition.fields.forEach(field => { if (!this.propertyCodes.includes(field.code)) { this.propertyCodes.push(field.code); } }); } this.isDeleted = data ? data.isActive === IsActive.Inactive : false; this.buildForm(); } catch (error) { this.logger.error('Could not parse referenceType item: ' + data + error); this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.ERRORS.DEFAULT'), SnackBarNotificationLevel.Error); } } buildForm() { this.formGroup = this.editorModel.buildForm(null, this.isDeleted || !this.authService.hasPermission(AppPermission.EditReferenceType)); this.referenceTypeEditorService.setValidationErrorModel(this.editorModel.validationErrorModel); } refreshData(): void { this.getItem(this.editorModel.id, (data: ReferenceType) => this.prepareForm(data)); } refreshOnNavigateToData(id?: Guid): void { this.formGroup.markAsPristine(); let route = []; if (id === null) { route.push('../..'); } else if (this.isNew) { route.push('../' + id); } else { route.push('..'); } this.router.navigate(route, { queryParams: { 'lookup': this.queryParamsService.serializeLookup(this.lookupParams), 'lv': ++this.lv }, replaceUrl: true, relativeTo: this.route }); } persistEntity(onSuccess?: (response) => void): void { const formData = this.formService.getValue(this.formGroup.getRawValue()) as ReferenceTypePersist; console.log(formData); this.referenceTypeService.persist(formData) .pipe(takeUntil(this._destroyed)).subscribe( complete => onSuccess ? onSuccess(complete) : this.onCallbackSuccess(complete), error => this.onCallbackError(error) ); } formSubmit(): void { this.formService.touchAllFormFields(this.formGroup); // if (!this.isFormValid()) { // return; // } this.persistEntity(); } public delete() { const value = this.formGroup.value; if (value.id) { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '300px', data: { message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'), confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'), cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL') } }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { this.referenceTypeService.delete(value.id).pipe(takeUntil(this._destroyed)) .subscribe( complete => this.onCallbackSuccess(), error => this.onCallbackError(error) ); } }); } } clearErrorModel() { this.editorModel.validationErrorModel.clear(); this.formService.validateAllFormFields(this.formGroup); } // // // fields // // addField(): void { (this.formGroup.get('definition').get('fields') as FormArray).push(this.editorModel.createChildField((this.formGroup.get('definition').get('fields') as FormArray).length)); } removeField(fieldIndex: number): void { const fieldCode = (this.formGroup.get('definition').get('fields') as FormArray).at(fieldIndex).get('code').value if (this.propertyCodes.length > 0) { if (this.propertyCodes.includes(fieldCode)) { this.propertyCodes.splice(this.propertyCodes.indexOf(fieldCode), 1); } } (this.formGroup.get('definition').get('fields') as FormArray).removeAt(fieldIndex); const sourceFormArray = ((this.formGroup.get('definition').get('sources') as FormArray)); for (let i = 0; i < sourceFormArray.length; i++) { const optionsFormArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(i).get('options') as FormArray); for (let j = 0; j < optionsFormArray.length; j++) { if (fieldCode == ((this.formGroup.get('definition').get('sources') as FormArray).at(i).get('options') as FormArray).at(j).get('code').getRawValue()) { this.removeOption(i, j); } } const fieldMappingFormArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(i).get('results').get('fieldsMapping') as FormArray); for (let j = 0; j < fieldMappingFormArray.length; j++) { if (fieldCode == ((this.formGroup.get('definition').get('sources') as FormArray).at(i).get('results').get('fieldsMapping') as FormArray).at(j).get('code').getRawValue()) { this.removeFieldMapping(i, j); } } } } submitFields(): void { const fieldsFormArray = (this.formGroup.get('definition').get('fields') as FormArray); if (fieldsFormArray.valid) { const sourcesFormArray = (this.formGroup.get('definition').get('sources') as FormArray); if (fieldsFormArray.length > 0) { for (let j = 0; j < sourcesFormArray.length; j++) { for (let i = 0; i < fieldsFormArray.length; i++) { this.addFieldMapping(j, fieldsFormArray.at(i).get('code').value); this.addOption(j, fieldsFormArray.at(i).get('code').value); if (!this.propertyCodes.includes(fieldsFormArray.at(i).get('code').value)) { this.propertyCodes.push(fieldsFormArray.at(i).get('code').value); } } } } } } addSource(): void { const source: ReferenceTypeSourceBaseConfigurationEditorModel = new ReferenceTypeSourceBaseConfigurationEditorModel(); (this.formGroup.get('definition').get('sources') as FormArray).push(source.buildForm()); const sourceIndex = (this.formGroup.get('definition').get('sources') as FormArray).length - 1; this.addFieldMapping(sourceIndex, "reference_id"); this.addFieldMapping(sourceIndex, "label"); this.addFieldMapping(sourceIndex, "description"); this.addOption(sourceIndex, "reference_id"); this.addOption(sourceIndex, "label"); this.addOption(sourceIndex, "description"); const fieldsFormArray = (this.formGroup.get('definition').get('fields') as FormArray); if (fieldsFormArray && fieldsFormArray.length > 0) { for (let i = 0; i < fieldsFormArray.length; i++) { this.addFieldMapping(sourceIndex, fieldsFormArray.at(i).get('code').value); this.addOption(sourceIndex, fieldsFormArray.at(i).get('code').value); } } } removeSource(sourceIndex: number): void { (this.formGroup.get('definition').get('sources') as FormArray).removeAt(sourceIndex); } // // // resultFieldsMapping // // addFieldMapping(sourceIndex: number, code: string): void { const fieldMapping: ResultFieldsMappingConfigurationEditorModel = new ResultFieldsMappingConfigurationEditorModel(); const fieldMappingSize = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('results').get('fieldsMapping') as FormArray).length; if (fieldMappingSize > 0) { for (let i = 0; i < fieldMappingSize; i++) { if (((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('results').get('fieldsMapping') as FormArray).at(i).get('code').getRawValue() == code) { return; } } } ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('results').get('fieldsMapping') as FormArray).push(fieldMapping.buildForm()); ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('results').get('fieldsMapping') as FormArray).at(fieldMappingSize).get('code').patchValue(code); } removeFieldMapping(sourceIndex: number, fieldMappingIndex: number): void { const formArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex) as FormArray); (formArray.get('results').get('fieldsMapping') as FormArray).removeAt(fieldMappingIndex); } // // // queries // // addQuery(sourceIndex: number): void { const query: QueryConfigEditorModel = new QueryConfigEditorModel(); ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('queries') as FormArray).push(query.buildForm()); } removeQuery(sourceIndex: number, queryIndex: number): void { const formArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('queries') as FormArray); formArray.removeAt(queryIndex); } // Options addOption(sourceIndex: number, code: string): void { const options: ReferenceTypeStaticOptionEditorModel = new ReferenceTypeStaticOptionEditorModel(); const optionsSize = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('options') as FormArray).length; if (optionsSize > 0) { for (let i = 0; i < optionsSize; i++) { if (((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('options') as FormArray).at(i).get('code').getRawValue() == code) { return; } } } ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('options') as FormArray).push(options.buildForm()); ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('options') as FormArray).at(optionsSize).get('code').patchValue(code); } removeOption(sourceIndex: number, optionIndex: number): void { const formArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex) as FormArray); (formArray.get('options') as FormArray).removeAt(optionIndex); } // Dependencies addDependency(sourceIndex: number): void { const dependency: ReferenceTypeSourceBaseDependencyEditorModel = new ReferenceTypeSourceBaseDependencyEditorModel(); const formArray = (this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('dependencies') as FormArray; formArray.push(dependency.buildForm()); this.addProperty(sourceIndex, (formArray.length - 1)); } removeDependency(sourceIndex: number, dependencyIndex: number): void { const formArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('dependencies') as FormArray); formArray.removeAt(dependencyIndex); } private getReferenceTypes(excludedId?: Guid): void { const lookup = ReferenceTypeService.DefaultReferenceTypeLookup(); if (excludedId) lookup.excludedIds = [excludedId]; this.referenceTypeService.query(lookup) .pipe(takeUntil(this._destroyed)) .subscribe(response => { this.referenceTypes = response.items as ReferenceType[]; this.referenceTypes.forEach(referenceType => { this.sourceKeysMap.set(referenceType.code, []); this.targetPropertyCodesMap.set(referenceType.code, []); }) }); } selectedReferenceTypeChanged(code: string): void { this.sourceKeys = []; this.targetPropertyCodes = []; this.referenceTypeService.getSingleWithCode(code, ReferenceTypeEditorResolver.lookupFields()) .pipe(takeUntil(this._destroyed)) .subscribe(data => { const referenceType = data as ReferenceType; // source keys referenceType.definition.sources.forEach(source => { if (!this.sourceKeys.includes(source.key)) this.sourceKeys.push(source.key) }); if (this.sourceKeysMap.has(code) && this.sourceKeysMap.get(code).length == 0) { this.sourceKeysMap.set(code, this.sourceKeys); } // targetPropertyCodes let fields = []; if (referenceType.definition.fields) { fields = this.referenceTypeService.getSystemFields(referenceType.definition.fields.map(x => x.code)); } else { fields = this.referenceTypeService.getSystemFields(fields); } fields.forEach(field => { if (!this.targetPropertyCodes.includes(field)) this.targetPropertyCodes.push(field) }) if (this.targetPropertyCodesMap.has(code) && this.targetPropertyCodesMap.get(code).length == 0) { this.targetPropertyCodesMap.set(code, this.targetPropertyCodes); } }); } // Properties addProperty(sourceIndex: number, dependencyIndex: number): void { if (((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('dependencies') as FormArray).at(dependencyIndex).get('referenceTypeCode').value == null) { return; } const property: DependencyPropertyEditorModel = new DependencyPropertyEditorModel(); (((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('dependencies') as FormArray).at(dependencyIndex).get("properties") as FormArray).push(property.buildForm()); } removeProperty(sourceIndex: number, dependencyIndex: number, propertyIndex: number): void { const formArray = (((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('dependencies') as FormArray).at(dependencyIndex).get("properties") as FormArray); formArray.removeAt(propertyIndex); } }