argos/dmp-frontend/src/app/ui/admin/dmp-profile/editor/dmp-profile-editor.componen...

261 lines
8.6 KiB
TypeScript
Raw Normal View History

2019-09-23 10:17:03 +02:00
import {of as observableOf, Observable } from 'rxjs';
import {map, takeUntil } from 'rxjs/operators';
2019-06-05 16:07:36 +02:00
import { AfterViewInit, Component, OnInit } from '@angular/core';
2018-11-27 15:13:56 +01:00
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
import { ActivatedRoute, Params, Router } from '@angular/router';
2018-10-05 17:00:54 +02:00
import { TranslateService } from '@ngx-translate/core';
2019-02-25 17:53:36 +01:00
import * as FileSaver from 'file-saver';
2019-01-18 18:03:45 +01:00
import { environment } from '../../../../../environments/environment';
import { ValidationErrorModel } from '../../../../common/forms/validation/error-model/validation-error-model';
2019-01-18 18:03:45 +01:00
import { BaseComponent } from '../../../../core/common/base/base.component';
import { DmpProfileFieldDataType } from '../../../../core/common/enum/dmp-profile-field-type';
2019-02-25 17:53:36 +01:00
import { DmpProfileStatus } from '../../../../core/common/enum/dmp-profile-status';
2019-01-18 18:03:45 +01:00
import { DmpProfileType } from '../../../../core/common/enum/dmp-profile-type';
import { DmpProfile } from '../../../../core/model/dmp-profile/dmp-profile';
import { DmpProfileService } from '../../../../core/services/dmp/dmp-profile.service';
import { SnackBarNotificationLevel, UiNotificationService } from '../../../../core/services/notification/ui-notification-service';
2019-01-18 18:03:45 +01:00
import { EnumUtils } from '../../../../core/services/utilities/enum-utils.service';
import { DmpProfileEditorModel, DmpProfileFieldEditorModel } from './dmp-profile-editor.model';
2019-05-23 11:40:24 +02:00
import { BreadcrumbItem } from '../../../misc/breadcrumb/definition/breadcrumb-item';
import { DmpProfileExternalAutoCompleteFieldDataEditorModel } from './external-autocomplete/dmp-profile-external-autocomplete-field-editor.model';
2018-03-28 15:24:47 +02:00
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-dmp-profile-editor-component',
templateUrl: 'dmp-profile-editor.component.html',
2019-01-18 18:03:45 +01:00
styleUrls: ['./dmp-profile-editor.component.scss']
2018-03-28 15:24:47 +02:00
})
2019-01-18 18:03:45 +01:00
export class DmpProfileEditorComponent extends BaseComponent implements AfterViewInit {
2018-03-28 15:24:47 +02:00
2018-10-05 17:00:54 +02:00
isNew = true;
2019-02-25 17:53:36 +01:00
viewOnly = false;
2019-01-18 18:03:45 +01:00
dmpProfileModel: DmpProfileEditorModel;
2018-10-05 17:00:54 +02:00
formGroup: FormGroup = null;
2018-11-27 15:13:56 +01:00
host = environment.Server;
2019-02-25 17:53:36 +01:00
dmpProfileId: string;
2019-05-23 11:40:24 +02:00
breadCrumbs: Observable<BreadcrumbItem[]>;
2019-01-18 18:03:45 +01:00
2018-10-05 17:00:54 +02:00
constructor(
2019-01-18 18:03:45 +01:00
private dmpProfileService: DmpProfileService,
2018-10-05 17:00:54 +02:00
private route: ActivatedRoute,
2019-01-18 18:03:45 +01:00
private router: Router,
private language: TranslateService,
private enumUtils: EnumUtils,
private uiNotificationService: UiNotificationService
2018-10-05 17:00:54 +02:00
) {
2018-11-27 18:33:17 +01:00
super();
2018-10-05 17:00:54 +02:00
}
ngAfterViewInit() {
2018-11-27 18:33:17 +01:00
this.route.params
.pipe(takeUntil(this._destroyed))
.subscribe((params: Params) => {
2019-02-25 17:53:36 +01:00
this.dmpProfileId = params['id'];
2018-11-27 18:33:17 +01:00
2019-02-25 17:53:36 +01:00
if (this.dmpProfileId != null) {
2018-11-27 18:33:17 +01:00
this.isNew = false;
2019-09-23 10:17:03 +02:00
this.dmpProfileService.getSingle(this.dmpProfileId).pipe(map(data => data as DmpProfile))
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
2019-01-18 18:03:45 +01:00
this.dmpProfileModel = new DmpProfileEditorModel().fromModel(data);
this.formGroup = this.dmpProfileModel.buildForm();
2019-02-25 17:53:36 +01:00
if (this.dmpProfileModel.status == DmpProfileStatus.Finalized) {
this.formGroup.disable();
this.viewOnly = true
}
2019-09-23 10:17:03 +02:00
this.breadCrumbs = observableOf([{
2019-05-23 11:40:24 +02:00
parentComponentName: 'DmpProfileListingComponent',
label: this.language.instant('NAV-BAR.TEMPLATE'),
url: '/dmp-profiles/' + this.dmpProfileId
}]);
2018-11-27 18:33:17 +01:00
});
} else {
2019-01-18 18:03:45 +01:00
this.dmpProfileModel = new DmpProfileEditorModel();
2018-11-27 18:33:17 +01:00
setTimeout(() => {
2019-01-18 18:03:45 +01:00
this.formGroup = this.dmpProfileModel.buildForm();
2019-06-05 16:07:36 +02:00
this.addField();
2018-10-05 17:00:54 +02:00
});
2019-09-23 10:17:03 +02:00
this.breadCrumbs = observableOf([{
2019-05-23 11:40:24 +02:00
parentComponentName: 'DmpProfileListingComponent',
label: this.language.instant('NAV-BAR.TEMPLATE'),
url: '/dmp-profiles/' + this.dmpProfileId
}]);
2018-11-27 18:33:17 +01:00
}
});
2018-10-05 17:00:54 +02:00
}
formSubmit(): void {
this.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) { return; }
this.onSubmit();
}
public isFormValid() {
return this.formGroup.valid;
}
onSubmit(): void {
2019-01-18 18:03:45 +01:00
this.dmpProfileService.createDmp(this.formGroup.value)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
2018-11-27 18:33:17 +01:00
);
2018-10-05 17:00:54 +02:00
}
onCallbackSuccess(): void {
this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
2018-10-05 17:00:54 +02:00
this.router.navigate(['/dmp-profiles']);
}
onCallbackError(errorResponse: any) {
this.setErrorModel(errorResponse.error);
this.validateAllFormFields(this.formGroup);
}
public setErrorModel(validationErrorModel: ValidationErrorModel) {
Object.keys(validationErrorModel).forEach(item => {
(<any>this.dmpProfileModel.validationErrorModel)[item] = (<any>validationErrorModel)[item];
});
2018-10-05 17:00:54 +02:00
}
public cancel(): void {
this.router.navigate(['/dmp-profiles']);
}
public touchAllFormFields(formControl: AbstractControl) {
if (formControl instanceof FormControl) {
formControl.markAsTouched();
} else if (formControl instanceof FormGroup) {
Object.keys(formControl.controls).forEach(item => {
const control = formControl.get(item);
this.touchAllFormFields(control);
});
} else if (formControl instanceof FormArray) {
formControl.controls.forEach(item => {
this.touchAllFormFields(item);
});
}
}
public validateAllFormFields(formControl: AbstractControl) {
if (formControl instanceof FormControl) {
formControl.updateValueAndValidity({ emitEvent: false });
} else if (formControl instanceof FormGroup) {
Object.keys(formControl.controls).forEach(item => {
const control = formControl.get(item);
this.validateAllFormFields(control);
});
} else if (formControl instanceof FormArray) {
formControl.controls.forEach(item => {
this.validateAllFormFields(item);
});
}
}
addField() {
2019-01-18 18:03:45 +01:00
(<FormArray>this.formGroup.get('definition').get('fields')).push(new DmpProfileFieldEditorModel().buildForm());
}
removeField(index: number) {
(<FormArray>this.formGroup.get('definition').get('fields')).controls.splice(index, 1);
2018-10-05 17:00:54 +02:00
}
getDMPProfileFieldDataTypeValues(): Number[] {
2019-01-18 18:03:45 +01:00
let keys: string[] = Object.keys(DmpProfileFieldDataType);
2018-10-05 17:00:54 +02:00
keys = keys.slice(0, keys.length / 2);
const values: Number[] = keys.map(Number);
return values;
}
2019-01-18 18:03:45 +01:00
getDMPProfileFieldDataTypeWithLanguage(fieldType: DmpProfileFieldDataType): string {
2018-10-05 17:00:54 +02:00
let result = '';
2019-01-18 18:03:45 +01:00
this.language.get(this.enumUtils.toDmpProfileFieldDataTypeString(fieldType))
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe((value: string) => {
result = value;
});
2018-10-05 17:00:54 +02:00
return result;
}
getDMPProfileFieldTypeValues(): Number[] {
2019-01-18 18:03:45 +01:00
let keys: string[] = Object.keys(DmpProfileType);
2018-10-05 17:00:54 +02:00
keys = keys.slice(0, keys.length / 2);
const values: Number[] = keys.map(Number);
return values;
}
2019-01-18 18:03:45 +01:00
getDMPProfileFieldTypeWithLanguage(profileType: DmpProfileType): string {
2018-10-05 17:00:54 +02:00
let result = '';
2019-01-18 18:03:45 +01:00
this.language.get(this.enumUtils.toDmpProfileTypeString(profileType))
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe((value: string) => {
result = value;
});
2018-10-05 17:00:54 +02:00
return result;
}
2019-01-18 18:03:45 +01:00
delete() {
2019-02-13 16:51:16 +01:00
this.formGroup.get('status').setValue(DmpProfileStatus.Deleted);
this.dmpProfileService.createDmp(this.formGroup.value)
2019-02-25 17:53:36 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
2019-02-25 17:53:36 +01:00
);
}
finalize() {
//const data = this.form.value;
this.formGroup.get('status').setValue(DmpProfileStatus.Finalized);
this.onSubmit();
}
downloadXML(): void {
this.dmpProfileService.downloadXML(this.dmpProfileId)
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'application/xml' });
const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
FileSaver.saveAs(blob, filename);
});
}
getFilenameFromContentDispositionHeader(header: string): string {
const regex: RegExp = new RegExp(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/g);
const matches = header.match(regex);
let filename: string;
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
if (match.includes('filename="')) {
filename = match.substring(10, match.length - 1);
break;
} else if (match.includes('filename=')) {
filename = match.substring(9);
break;
}
}
return filename;
2019-01-18 18:03:45 +01:00
}
isExternalAutocomplete(formGroup: FormGroup) {
if (formGroup.get('dataType').value == DmpProfileFieldDataType.ExternalAutocomplete) {
this.addControl(formGroup);
return true;
} else {
return false;
}
}
addControl(formGroup: FormGroup) {
if (formGroup.get('dataType').value == 3)
formGroup.addControl('externalAutocomplete', new DmpProfileExternalAutoCompleteFieldDataEditorModel().buildForm());
}
2019-05-23 11:40:24 +02:00
}