2019-01-18 18:03:45 +01:00
|
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
|
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
|
|
|
|
import { FormArray, FormControl, FormGroup } from '@angular/forms';
|
2019-02-06 12:10:44 +01:00
|
|
|
|
import { MatHorizontalStepper, MatDialog } from '@angular/material';
|
2019-01-18 18:03:45 +01:00
|
|
|
|
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { map, takeUntil } from 'rxjs/operators';
|
|
|
|
|
import { BaseComponent } from '../../../../core/common/base/base.component';
|
|
|
|
|
import { DatasetProfile } from '../../../../core/model/admin/dataset-profile/dataset-profile';
|
|
|
|
|
import { DatasetWizardModel } from '../../../../core/model/dataset/dataset-wizard';
|
|
|
|
|
import { DatasetProfileService } from '../../../../core/services/dataset-profile/dataset-profile.service';
|
|
|
|
|
import { LoggingService } from '../../../../core/services/logging/logging-service';
|
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '../../../../core/services/notification/ui-notification-service';
|
|
|
|
|
import { PageEditorModel } from '../admin/page-editor-model';
|
|
|
|
|
import { SectionEditorModel } from '../admin/section-editor-model';
|
|
|
|
|
import { DatasetProfileEditorModel } from './dataset-profile-editor-model';
|
2019-02-06 12:10:44 +01:00
|
|
|
|
import { ConfirmationDialogComponent } from '../../../../library/confirmation-dialog/confirmation-dialog.component';
|
2019-02-11 13:04:45 +01:00
|
|
|
|
import { DatasetProfileEnum } from '../../../../core/common/enum/dataset-profile';
|
2019-01-18 18:03:45 +01:00
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-dataset-profile-editor-component',
|
|
|
|
|
templateUrl: './dataset-profile-editor.component.html',
|
|
|
|
|
styleUrls: ['./dataset-profile-editor.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class DatasetProfileEditorComponent extends BaseComponent implements OnInit {
|
|
|
|
|
|
|
|
|
|
isNew = true;
|
|
|
|
|
isDeleted = false;
|
|
|
|
|
dataModel: DatasetProfileEditorModel;
|
|
|
|
|
form: FormGroup;
|
|
|
|
|
previewerFormGroup: FormGroup;
|
|
|
|
|
private datasetProfileId: string;
|
|
|
|
|
dataWizardModel: DatasetWizardModel;
|
|
|
|
|
@ViewChild('stepper') stepper: MatHorizontalStepper;
|
2019-02-11 13:04:45 +01:00
|
|
|
|
viewOnly = false;
|
2019-02-06 12:10:44 +01:00
|
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
|
constructor(
|
|
|
|
|
private datasetProfileService: DatasetProfileService,
|
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
|
private router: Router,
|
|
|
|
|
private logger: LoggingService,
|
|
|
|
|
private uiNotificationService: UiNotificationService,
|
2019-02-06 12:10:44 +01:00
|
|
|
|
private language: TranslateService,
|
|
|
|
|
private dialog: MatDialog
|
2019-01-18 18:03:45 +01:00
|
|
|
|
) {
|
|
|
|
|
super();
|
|
|
|
|
// this.profileID = route.snapshot.params['id'];
|
|
|
|
|
// this.cloneId = route.snapshot.params['cloneid'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.route.paramMap.pipe(takeUntil(this._destroyed)).subscribe((paramMap: ParamMap) => {
|
|
|
|
|
this.datasetProfileId = paramMap.get('id');
|
|
|
|
|
const cloneId = paramMap.get('cloneid');
|
|
|
|
|
|
|
|
|
|
if (this.datasetProfileId != null) {
|
|
|
|
|
this.isNew = false;
|
|
|
|
|
this.datasetProfileService.getDatasetProfileById(this.datasetProfileId)
|
|
|
|
|
.pipe(map(data => data as DatasetProfile), takeUntil(this._destroyed))
|
|
|
|
|
.subscribe(
|
2019-02-11 13:04:45 +01:00
|
|
|
|
data => {
|
|
|
|
|
try {
|
|
|
|
|
this.dataModel = new DatasetProfileEditorModel().fromModel(data);
|
|
|
|
|
// this.isDeleted = this.masterItem.isActive === IsActive.Inactive;
|
|
|
|
|
this.form = this.dataModel.buildForm();
|
|
|
|
|
if (this.dataModel.status === DatasetProfileEnum.FINALIZED) {
|
|
|
|
|
this.form.disable();
|
|
|
|
|
this.viewOnly = true;
|
2019-01-18 18:03:45 +01:00
|
|
|
|
}
|
2019-02-11 13:04:45 +01:00
|
|
|
|
this.prepareForm();
|
|
|
|
|
} catch {
|
|
|
|
|
this.logger.error('Could not parse MasterItem: ' + data);
|
|
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('NOTIFICATIONS.DEFAULT.ERROR'), SnackBarNotificationLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
error => this.onCallbackError(error)
|
2019-01-18 18:03:45 +01:00
|
|
|
|
);
|
|
|
|
|
} else if (cloneId != null) {
|
|
|
|
|
this.datasetProfileService.clone(cloneId)
|
|
|
|
|
.pipe(map(data => data as DatasetProfile), takeUntil(this._destroyed))
|
|
|
|
|
.subscribe(
|
2019-02-11 13:04:45 +01:00
|
|
|
|
data => {
|
|
|
|
|
try {
|
|
|
|
|
this.dataModel = new DatasetProfileEditorModel().fromModel(data);
|
|
|
|
|
// this.isDeleted = this.masterItem.isActive === IsActive.Inactive;
|
|
|
|
|
this.dataModel.status = DatasetProfileEnum.SAVED;
|
|
|
|
|
this.form = this.dataModel.buildForm();
|
|
|
|
|
this.prepareForm();
|
|
|
|
|
} catch {
|
|
|
|
|
this.logger.error('Could not parse MasterItem: ' + data);
|
|
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('NOTIFICATIONS.DEFAULT.ERROR'), SnackBarNotificationLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
error => this.onCallbackError(error)
|
2019-01-18 18:03:45 +01:00
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this.dataModel = new DatasetProfileEditorModel();
|
|
|
|
|
this.form = this.dataModel.buildForm();
|
2019-02-11 13:04:45 +01:00
|
|
|
|
if (this.dataModel.status === DatasetProfileEnum.FINALIZED) {
|
|
|
|
|
this.form.disable();
|
|
|
|
|
this.viewOnly = true;
|
|
|
|
|
}
|
2019-01-18 18:03:45 +01:00
|
|
|
|
this.addSection();
|
|
|
|
|
this.addPage();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prepareForm() {
|
|
|
|
|
this.form.valueChanges
|
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
|
.subscribe(change => {
|
|
|
|
|
// this.datasetProfileService.preview(this.form.value)
|
|
|
|
|
// .pipe(takeUntil(this._destroyed))
|
|
|
|
|
// .subscribe(dataset => {
|
|
|
|
|
// const datasetModel = new DatasetWizardModel();
|
|
|
|
|
// datasetModel.datasetProfileDefinition = JsonSerializer.fromJSONObject(dataset, DatasetProfileDefinitionModel);
|
|
|
|
|
// this.dataWizardModel = datasetModel;
|
|
|
|
|
// this.previewerFormGroup = <FormGroup>this.dataWizardModel.buildForm().get('datasetProfileDefinition');
|
|
|
|
|
// });
|
|
|
|
|
});
|
|
|
|
|
this.form.updateValueAndValidity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onIsMultiplicityEnabledChange(isMultiplicityEnabled: boolean) {
|
|
|
|
|
if (!isMultiplicityEnabled) {
|
|
|
|
|
(<FormControl>this.form.get('multiplicity').get('min')).setValue(0);
|
|
|
|
|
(<FormControl>this.form.get('multiplicity').get('max')).setValue(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addSection() {
|
|
|
|
|
const section: SectionEditorModel = new SectionEditorModel();
|
|
|
|
|
this.dataModel.sections.push(section);
|
|
|
|
|
(<FormArray>this.form.get('sections')).push(section.buildForm());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addPage() {
|
|
|
|
|
const page: PageEditorModel = new PageEditorModel(this.dataModel.pages.length);
|
|
|
|
|
this.dataModel.pages.push(page);
|
|
|
|
|
(<FormArray>this.form.get('pages')).push(page.buildForm());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeleteSection(index) {
|
|
|
|
|
this.dataModel.sections.splice(index, 1);
|
|
|
|
|
(<FormArray>this.form.get('sections')).removeAt(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSubmit() {
|
2019-02-15 11:26:14 +01:00
|
|
|
|
let data = this.form.value;
|
2019-01-18 18:03:45 +01:00
|
|
|
|
|
|
|
|
|
if (this.datasetProfileId) {
|
|
|
|
|
this.datasetProfileService.updateForm(this.datasetProfileId, data)
|
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
|
.subscribe(() => {
|
|
|
|
|
this.router.navigate(['/dataset-profiles']);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2019-02-15 11:26:14 +01:00
|
|
|
|
this.form.get('status').setValue(0);
|
|
|
|
|
data = this.form.value;
|
2019-01-18 18:03:45 +01:00
|
|
|
|
this.datasetProfileService.createForm(data)
|
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
|
.subscribe(() => {
|
|
|
|
|
this.router.navigate(['/dataset-profiles']);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 13:04:45 +01:00
|
|
|
|
finalize() {
|
|
|
|
|
//const data = this.form.value;
|
|
|
|
|
this.form.get('status').setValue(DatasetProfileEnum.FINALIZED);
|
|
|
|
|
|
|
|
|
|
this.onSubmit();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
|
isStepActive(step: number) {
|
|
|
|
|
return this.stepper && this.stepper.selectedIndex === step;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
this.router.navigate(['/master-items']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: HttpErrorResponse) {
|
|
|
|
|
// const error: HttpError = this.httpErrorHandlingService.getError(errorResponse);
|
|
|
|
|
// if (error.statusCode === 400) {
|
|
|
|
|
// this.masterItem.validationErrorModel.fromJSONObject(errorResponse.error);
|
|
|
|
|
// this.formService.validateAllFormFields(this.formGroup);
|
|
|
|
|
// } else {
|
|
|
|
|
this.uiNotificationService.snackBarNotification(errorResponse.message, SnackBarNotificationLevel.Warning);
|
|
|
|
|
// }
|
|
|
|
|
}
|
2019-02-06 12:10:44 +01:00
|
|
|
|
|
|
|
|
|
// DELETE Function
|
|
|
|
|
public delete(): void {
|
|
|
|
|
if (this.datasetProfileId && !this.isNew) {
|
|
|
|
|
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
|
|
|
|
|
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) {
|
2019-02-15 12:11:11 +01:00
|
|
|
|
this.form.get('status').setValue(DatasetProfileEnum.DELETED);
|
|
|
|
|
this.datasetProfileService.delete(this.datasetProfileId,this.form.value)
|
2019-02-06 12:10:44 +01:00
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
|
.subscribe(
|
2019-02-11 13:04:45 +01:00
|
|
|
|
complete => {
|
|
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-DATASET-PROFILE-DELETE'), SnackBarNotificationLevel.Success);
|
|
|
|
|
this.router.navigate(['/dataset-profiles']);
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
this.onCallbackError(error);
|
|
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DATASET-PROFILE-DELETE'), SnackBarNotificationLevel.Success);
|
|
|
|
|
}
|
2019-02-06 12:10:44 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-18 18:03:45 +01:00
|
|
|
|
}
|