argos/dmp-frontend/src/app/ui/dataset/dataset-wizard/dataset-wizard.component.ts

601 lines
22 KiB
TypeScript
Raw Normal View History

2019-01-18 18:03:45 +01:00
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { MatDialog, MatSnackBar, MatStepper } from '@angular/material';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import * as FileSaver from 'file-saver';
import { Observable } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { ValidationErrorModel } from '../../../common/forms/validation/error-model/validation-error-model';
import { BaseComponent } from '../../../core/common/base/base.component';
import { DatasetStatus } from '../../../core/common/enum/dataset-status';
import { DatasetProfileModel } from '../../../core/model/dataset/dataset-profile';
import { DmpModel } from '../../../core/model/dmp/dmp';
import { DmpListingModel } from '../../../core/model/dmp/dmp-listing';
import { DatasetProfileCriteria } from '../../../core/query/dataset-profile/dataset-profile-criteria';
import { DmpCriteria } from '../../../core/query/dmp/dmp-criteria';
import { RequestItem } from '../../../core/query/request-item';
import { DatasetWizardService } from '../../../core/services/dataset-wizard/dataset-wizard.service';
import { DmpService } from '../../../core/services/dmp/dmp.service';
import { ExternalSourcesConfigurationService } from '../../../core/services/external-sources/external-sources-configuration.service';
import { ExternalSourcesService } from '../../../core/services/external-sources/external-sources.service';
import { SingleAutoCompleteConfiguration } from '../../../library/auto-complete/single/single-auto-complete-configuration';
import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item';
import { IBreadCrumbComponent } from '../../misc/breadcrumb/definition/IBreadCrumbComponent';
import { DatasetDescriptionFormEditorModel } from '../../misc/dataset-description-form/dataset-description-form.model';
import { DatasetWizardEditorModel } from './dataset-wizard-editor.model';
import { SnackBarNotificationLevel, UiNotificationService, SnackBarNotification } from '../../../core/services/notification/ui-notification-service';
2019-01-29 08:56:46 +01:00
import { ConfirmationDialogComponent } from '../../../library/confirmation-dialog/confirmation-dialog.component';
import { DataTableRequest } from '../../../core/model/data-table/data-table-request';
import { DatasetCopyDialogueComponent } from './dataset-copy-dialogue/dataset-copy-dialogue.component';
import { FormControl } from '@angular/forms';
import { error } from 'selenium-webdriver';
2019-01-18 18:03:45 +01:00
@Component({
selector: 'app-dataset-wizard-component',
templateUrl: 'dataset-wizard.component.html',
styleUrls: ['./dataset-wizard.component.scss']
})
export class DatasetWizardComponent extends BaseComponent implements OnInit, IBreadCrumbComponent {
2019-01-18 18:03:45 +01:00
breadCrumbs: Observable<BreadcrumbItem[]>;
viewOnly = false;
@ViewChild('stepper') stepper: MatStepper;
editMode = false;
publicMode = false;
2019-01-18 18:03:45 +01:00
DatasetStatus = DatasetStatus;
dmpAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
datasetWizardModel: DatasetWizardEditorModel;
isNew = true;
isCopy = false;
2019-01-18 18:03:45 +01:00
formGroup: FormGroup;
datasetProfileDefinitionModel: DatasetDescriptionFormEditorModel;
availableProfiles: DatasetProfileModel[] = [];
itemId: string;
publicId: string;
profileUpdateId: string;
downloadDocumentId: string;
2019-01-18 18:03:45 +01:00
isLinear = false;
constructor(
private datasetWizardService: DatasetWizardService,
private route: ActivatedRoute,
public snackBar: MatSnackBar,
public router: Router,
public language: TranslateService,
public externalSourcesService: ExternalSourcesService,
public dmpService: DmpService,
public dialog: MatDialog,
public externalSourcesConfigurationService: ExternalSourcesConfigurationService,
private uiNotificationService: UiNotificationService
2019-01-18 18:03:45 +01:00
) {
super();
}
ngOnInit() {
this.route
.data
.pipe(takeUntil(this._destroyed))
.subscribe(v => this.viewOnly = v['public']);
const dmpRequestItem: RequestItem<DmpCriteria> = new RequestItem();
dmpRequestItem.criteria = new DmpCriteria();
// const externalDatasetRequestItem: RequestItem<ExternalDatasetCriteria> = new RequestItem();
// externalDatasetRequestItem.criteria = new ExternalDatasetCriteria();
// this.externalDatasetAutoCompleteConfiguration = new AutoCompleteConfiguration(this.externalSourcesService.searchDatasetSExternalDatasetservice.bind(this.externalSourcesService),
// externalDatasetRequestItem, new BehaviorSubject(false));
// const registriesRequestItem: RequestItem<RegistryCriteria> = new RequestItem();
// registriesRequestItem.criteria = new RegistryCriteria();
// this.registriesAutoCompleteConfiguration = new AutoCompleteConfiguration(this.externalSourcesService.searchDatasetRegistry.bind(this.externalSourcesService),
// registriesRequestItem, new BehaviorSubject(false));
// const dataRepositoriesRequestItem: RequestItem<DataRepositoryCriteria> = new RequestItem();
// dataRepositoriesRequestItem.criteria = new DataRepositoryCriteria();
// this.dataRepositoriesAutoCompleteConfiguration = new AutoCompleteConfiguration(this.externalSourcesService.searchDatasetRepository.bind(this.externalSourcesService),
// dataRepositoriesRequestItem, new BehaviorSubject(false));
// const servicesRequestItem: RequestItem<ServiceCriteria> = new RequestItem();
// servicesRequestItem.criteria = new ServiceCriteria();
// this.servicesAutoCompleteConfiguration = new AutoCompleteConfiguration(this.externalSourcesService.searchDatasetService.bind(this.externalSourcesService),
// servicesRequestItem, new BehaviorSubject(false));
// const tagsRequestItem: RequestItem<TagCriteria> = new RequestItem();
// tagsRequestItem.criteria = new TagCriteria();
// this.tagsAutoCompleteConfiguration = new AutoCompleteConfiguration(this.externalSourcesService.searchDatasetTags.bind(this.externalSourcesService),
// tagsRequestItem, new BehaviorSubject(false));
this.dmpAutoCompleteConfiguration = {
filterFn: this.searchDmp.bind(this),
initialItems: (extraData) => this.searchDmp(''),
displayFn: (item) => item['label'],
titleFn: (item) => item['label']
2019-01-18 18:03:45 +01:00
};
const params = this.route.snapshot.params;
const queryParams = this.route.snapshot.queryParams;
2019-01-18 18:03:45 +01:00
this.itemId = params['id'];
const dmpId = params['dmpId'];
const newDmpId = queryParams['newDmpId'];
this.publicId = params['publicId'];
this.profileUpdateId = params['updateId'];
this.itemId ? this.downloadDocumentId = this.itemId : this.downloadDocumentId = this.publicId
if (this.itemId != null && newDmpId == null) {
2019-01-18 18:03:45 +01:00
this.isNew = false;
this.datasetWizardService.getSingle(this.itemId)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data);
this.needsUpdate();
2019-01-18 18:03:45 +01:00
this.breadCrumbs = Observable.of([
{
parentComponentName: null,
label: 'Datasets',
url: '/datasets',
notFoundResolver: [
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.project.label,
url: '/projects/edit/' + this.datasetWizardModel.dmp.project.id
},
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.label,
url: '/plans/edit/' + this.datasetWizardModel.dmp.id,
},
]
}]);
this.formGroup = this.datasetWizardModel.buildForm();
this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
if (this.datasetWizardModel.status === 1) {
this.formGroup.disable();
this.viewOnly = true;
}
// if (this.viewOnly) { this.formGroup.disable(); } // For future use, to make Dataset edit like DMP.
2019-01-18 18:03:45 +01:00
this.loadDatasetProfiles();
});
} else if (dmpId != null) {
this.isNew = true;
this.dmpService.getSingle(dmpId).map(data => data as DmpModel)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.datasetWizardModel = new DatasetWizardEditorModel();
setTimeout(() => {
this.datasetWizardModel.dmp = data;
this.formGroup = this.datasetWizardModel.buildForm();
this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
if (this.datasetWizardModel.status === 1) {
this.formGroup.disable();
this.viewOnly = true;
}
this.loadDatasetProfiles();
this.breadCrumbs = Observable.of([
{
parentComponentName: null,
label: 'Datasets',
url: '/datasets',
notFoundResolver: [
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.project.label,
url: '/projects/edit/' + this.datasetWizardModel.dmp.project.id
},
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.label,
url: '/plans/edit/' + this.datasetWizardModel.dmp.id,
}]
}]);
});
});
} else if (newDmpId != null) {
this.isNew = false;
this.isCopy = true;
this.datasetWizardService.getSingle(this.itemId)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data);
this.formGroup = this.datasetWizardModel.buildForm();
this.formGroup.get('id').setValue(null);
this.dmpService.getSingle(newDmpId).map(data => data as DmpModel)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
setTimeout(() => {
this.datasetWizardModel.dmp = data;
this.formGroup.get('dmp').setValue(this.datasetWizardModel.dmp);
this.loadDatasetProfiles();
this.breadCrumbs = Observable.of([
{
parentComponentName: null,
label: 'Datasets',
url: '/datasets',
notFoundResolver: [
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.project.label,
url: '/projects/edit/' + this.datasetWizardModel.dmp.project.id
},
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.label,
url: '/plans/edit/' + this.datasetWizardModel.dmp.id,
}]
}]);
});
});
this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
if (this.datasetWizardModel.status === 1) {
this.formGroup.disable();
this.viewOnly = true;
}
// if (this.viewOnly) { this.formGroup.disable(); } // For future use, to make Dataset edit like DMP.
this.loadDatasetProfiles();
});
} else if (this.publicId != null) {
this.datasetWizardService.getSinglePublic(this.publicId)
.pipe(takeUntil(this._destroyed))
.catch((error: any) => {
this.uiNotificationService.snackBarNotification(error.error.message, SnackBarNotificationLevel.Error);
this.router.navigate(['/explore']);
return Observable.of(null);
})
.subscribe(data => {
if (data) {
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data);
this.formGroup = this.datasetWizardModel.buildForm();
this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
if (this.datasetWizardModel.status === 1) {
this.formGroup.disable();
this.viewOnly = true;
}
this.formGroup.get('dmp').setValue(this.datasetWizardModel.dmp);
this.loadDatasetProfiles();
this.breadCrumbs = Observable.of([
{
parentComponentName: null,
label: 'Datasets',
url: '/datasets',
notFoundResolver: [
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.project.label,
url: '/projects/edit/' + this.datasetWizardModel.dmp.project.id
},
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.label,
url: '/plans/edit/' + this.datasetWizardModel.dmp.id,
}]
}]);
}
});
this.publicMode = true;
} else if (this.profileUpdateId != null) {
this.datasetWizardService.updateDatasetProfile(this.profileUpdateId)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data);
this.needsUpdate();
this.breadCrumbs = Observable.of([
{
parentComponentName: null,
label: 'Datasets',
url: '/datasets',
notFoundResolver: [
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.project.label,
url: '/projects/edit/' + this.datasetWizardModel.dmp.project.id
},
{
parentComponentName: null,
label: this.datasetWizardModel.dmp.label,
url: '/plans/edit/' + this.datasetWizardModel.dmp.id,
},
]
}]);
this.formGroup = this.datasetWizardModel.buildForm();
this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
if (this.datasetWizardModel.status === 1) {
this.formGroup.disable();
this.viewOnly = true;
}
// if (this.viewOnly) { this.formGroup.disable(); } // For future use, to make Dataset edit like DMP.
this.loadDatasetProfiles();
});
} else {
2019-01-18 18:03:45 +01:00
this.datasetWizardModel = new DatasetWizardEditorModel();
this.formGroup = this.datasetWizardModel.buildForm();
this.editMode = this.datasetWizardModel.status === DatasetStatus.Draft;
if (this.datasetWizardModel.status === 1) {
this.formGroup.disable();
this.viewOnly = true;
}
//if (this.viewOnly) { this.formGroup.disable(); } // For future use, to make Dataset edit like DMP.
2019-01-18 18:03:45 +01:00
this.formGroup.get('dmp').valueChanges
.pipe(takeUntil(this._destroyed))
.subscribe(x => {
if (x) { this.loadDatasetProfiles(); }
2019-01-29 08:56:46 +01:00
else {
this.availableProfiles = [];
this.formGroup.get('profile').reset();
}
2019-01-18 18:03:45 +01:00
});
}
this.route.params
.pipe(takeUntil(this._destroyed))
.subscribe((params: Params) => {
const itemId = params['id'];
if (itemId != null) { setTimeout(() => this.stepper.selectedIndex = 2); }
2019-01-18 18:03:45 +01:00
});
}
searchDmp(query: string): Observable<DmpListingModel[]> {
const fields: Array<string> = new Array<string>();
fields.push('asc');
const dmpDataTableRequest: DataTableRequest<DmpCriteria> = new DataTableRequest(0, null, { fields: fields });
dmpDataTableRequest.criteria = new DmpCriteria();
dmpDataTableRequest.criteria.like = query;
return this.dmpService.getPaged(dmpDataTableRequest, "autocomplete").map(x => x.data);
2019-01-18 18:03:45 +01:00
}
loadDatasetProfiles() {
const datasetProfileRequestItem: RequestItem<DatasetProfileCriteria> = new RequestItem();
datasetProfileRequestItem.criteria = new DatasetProfileCriteria();
datasetProfileRequestItem.criteria.id = this.formGroup.get('dmp').value.id;
if (datasetProfileRequestItem.criteria.id) {
this.datasetWizardService.getAvailableProfiles(datasetProfileRequestItem)
.pipe(takeUntil(this._destroyed))
.subscribe(items => {
this.availableProfiles = items;
});
}
}
public cancel(): void {
//this.router.navigate(['/plans']);
}
getDefinition() {
if (this.formGroup.invalid) { setTimeout(() => this.stepper.selectedIndex = 0); return; }
2019-01-18 18:03:45 +01:00
if (this.isNew) {
this.datasetWizardService.getDefinition(this.formGroup.get('profile').value)
.pipe(takeUntil(this._destroyed))
.subscribe(item => {
this.datasetWizardModel.datasetProfileDefinition = new DatasetDescriptionFormEditorModel().fromModel(item);
this.datasetProfileDefinitionModel = this.datasetWizardModel.datasetProfileDefinition;
this.formGroup.addControl('datasetProfileDefinition', this.datasetProfileDefinitionModel.buildForm());
});
}
}
formSubmit(): void {
if (!this.isFormValid()) { return; }
this.onSubmit();
}
public isFormValid() {
return this.formGroup.valid;
}
onSubmit(): void {
this.datasetWizardService.createDataset(this.formGroup.value)
.pipe(takeUntil(this._destroyed))
.subscribe(
2019-01-29 08:56:46 +01:00
complete => {
this.datasetWizardService.getSingle(complete.id)
.pipe(takeUntil(this._destroyed))
.subscribe(
result => {
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(result);
}
);
this.onCallbackSuccess();
},
error => this.onCallbackError(error)
2019-01-18 18:03:45 +01:00
);
}
submit() {
this.datasetWizardService.createDataset(this.formGroup.value)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.router.navigateByUrl('/datasets/dmp/' + this.formGroup.get('dmp').value.id);
});
}
save() {
//if (!this.isFormValid()) { return; }
2019-01-18 18:03:45 +01:00
this.formGroup.get('status').setValue('0');
this.submit();
}
saveFinalize() {
if (!this.isFormValid()) { return; }
this.formGroup.get('status').setValue('1');
this.submit();
}
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);
2019-01-18 18:03:45 +01:00
}
onCallbackError(error: any) {
this.setErrorModel(error.error);
}
public setErrorModel(validationErrorModel: ValidationErrorModel) {
Object.keys(validationErrorModel).forEach(item => {
(<any>this.datasetWizardModel.validationErrorModel)[item] = (<any>validationErrorModel)[item];
2019-01-18 18:03:45 +01:00
});
}
downloadPDF(): void {
this.datasetWizardService.downloadPDF(this.downloadDocumentId)
2019-01-18 18:03:45 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'application/pdf' });
const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
FileSaver.saveAs(blob, filename);
});
}
downloadDOCX(): void {
this.datasetWizardService.downloadDOCX(this.downloadDocumentId)
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'application/msword' });
const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
FileSaver.saveAs(blob, filename);
});
}
2019-01-18 18:03:45 +01:00
downloadXML(): void {
this.datasetWizardService.downloadXML(this.downloadDocumentId)
2019-01-18 18:03:45 +01:00
.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;
}
public redirectToProject() {
this.router.navigate(['projects/edit/' + this.datasetWizardModel.dmp.project.id]);
}
public redirectToDmp() {
this.router.navigate(['plans/edit/' + this.datasetWizardModel.dmp.id]);
}
public enableForm() {
if (this.formGroup.get('status').value !== DatasetStatus.Finalized) {
this.editMode = true;
this.viewOnly = false;
this.formGroup.enable();
} else {
this.datasetWizardService.unlock(this.formGroup.get('id').value)
.pipe(takeUntil(this._destroyed))
.subscribe(x => {
this.editMode = true;
this.viewOnly = false;
this.datasetWizardModel.status = DatasetStatus.Draft;
this.formGroup.get('status').patchValue(DatasetStatus.Draft);
this.formGroup.enable();
});
}
}
public disableForm() {
this.editMode = false;
//this.viewOnly = true;
2019-01-18 18:03:45 +01:00
this.formGroup.disable();
}
isActiveStep(index: number) {
return this.stepper.selectedIndex === index;
}
openConfirm(dmpLabel, id): void {
2019-01-29 08:56:46 +01:00
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
//maxWidth: '300px',
2019-01-29 08:56:46 +01:00
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.datasetWizardService.delete(id)
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => { this.onCallbackSuccess(); this.router.navigateByUrl('/datasets') },
2019-01-29 08:56:46 +01:00
error => this.onCallbackError(error)
);
}
});
2019-01-18 18:03:45 +01:00
}
openDmpSearchDialogue() {
const formControl = new FormControl();
const dialogRef = this.dialog.open(DatasetCopyDialogueComponent, {
data: {
formControl: formControl,
datasetId: this.formGroup.value.id,
datasetProfileId: this.formGroup.value.profile,
datasetProfileExist: false,
2019-04-16 15:47:02 +02:00
confirmButton: this.language.instant('DATASET-WIZARD.DIALOGUE.COPY'),
cancelButton: this.language.instant('DATASET-WIZARD.DIALOGUE.CANCEL')
}
});
dialogRef.afterClosed().pipe(takeUntil(this._destroyed))
.subscribe(result => {
if (result && result.datasetProfileExist) {
const newDmpId = result.formControl.value.id
this.router.navigate(['/datasets/copy/' + result.datasetId], { queryParams: { newDmpId: newDmpId } });
}
});
}
needsUpdate() {
if (this.datasetWizardModel.isProfileLatestVersion || (this.datasetWizardModel.status === 1)
|| (this.datasetWizardModel.isProfileLatestVersion == undefined && this.datasetWizardModel.status == undefined)) {
return false;
}
else {
return true
}
}
openUpdateDatasetProfileDialogue() {
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
message: this.language.instant('DATASET-EDITOR.VERSION-DIALOG.QUESTION'),
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.profileUpdateId = this.itemId;
this.uiNotificationService.snackBarNotification("Profile changed, WOW!", SnackBarNotificationLevel.Success);
this.router.navigate(['/datasets/profileupdate/' + this.profileUpdateId]);
}
});
}
2019-01-18 18:03:45 +01:00
}