argos/dmp-frontend/src/app/ui/dmp/editor/dmp-editor.component.ts

433 lines
15 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, ViewContainerRef } from '@angular/core';
2018-11-27 18:33:17 +01:00
import { FormControl, FormGroup } from '@angular/forms';
import { MatDialog, MatSnackBar } from '@angular/material';
import { ActivatedRoute, Params, Router } from '@angular/router';
2018-10-05 17:00:54 +02:00
import { TranslateService } from '@ngx-translate/core';
2018-11-27 18:33:17 +01:00
import * as FileSaver from 'file-saver';
import { Observable } from 'rxjs';
2018-11-27 18:33:17 +01:00
import { takeUntil } from 'rxjs/operators';
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 { DmpStatus } from '../../../core/common/enum/dmp-status';
import { Status } from '../../../core/common/enum/Status';
import { DatasetListingModel } from '../../../core/model/dataset/dataset-listing';
import { DatasetProfileModel } from '../../../core/model/dataset/dataset-profile';
import { DmpModel } from '../../../core/model/dmp/dmp';
import { ExternalSourceItemModel } from '../../../core/model/external-sources/external-source-item';
import { ProjectModel } from '../../../core/model/project/project';
2019-01-18 18:03:45 +01:00
import { UserModel } from '../../../core/model/user/user';
import { BaseCriteria } from '../../../core/query/base-criteria';
import { DatasetProfileCriteria } from '../../../core/query/dataset-profile/dataset-profile-criteria';
import { ProjectCriteria } from '../../../core/query/project/project-criteria';
import { RequestItem } from '../../../core/query/request-item';
2019-01-18 18:03:45 +01:00
import { DmpProfileService } from '../../../core/services/dmp/dmp-profile.service';
import { DmpService } from '../../../core/services/dmp/dmp.service';
import { ExternalSourcesService } from '../../../core/services/external-sources/external-sources.service';
import { SnackBarNotificationLevel, UiNotificationService } from '../../../core/services/notification/ui-notification-service';
2019-01-18 18:03:45 +01:00
import { ProjectService } from '../../../core/services/project/project.service';
import { MultipleAutoCompleteConfiguration } from '../../../library/auto-complete/multiple/multiple-auto-complete-configuration';
import { SingleAutoCompleteConfiguration } from '../../../library/auto-complete/single/single-auto-complete-configuration';
import { LanguageResolverService } from '../../../services/language-resolver/language-resolver.service';
import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item';
import { IBreadCrumbComponent } from '../../misc/breadcrumb/definition/IBreadCrumbComponent';
import { AddResearcherComponent } from './add-researcher/add-researcher.component';
import { AvailableProfilesComponent } from './available-profiles/available-profiles.component';
import { DmpEditorModel } from './dmp-editor.model';
import { DmpFinalizeDialogComponent } from './dmp-finalize-dialog/dmp-finalize-dialog.component';
import { ConfirmationDialogComponent } from '../../../library/confirmation-dialog/confirmation-dialog.component';
2017-12-14 18:13:28 +01:00
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-dmp-editor-component',
templateUrl: 'dmp-editor.component.html',
2019-01-18 18:03:45 +01:00
styleUrls: ['./dmp-editor.component.scss']
2017-12-14 18:13:28 +01:00
})
export class DmpEditorComponent extends BaseComponent implements OnInit, IBreadCrumbComponent {
2017-12-18 11:01:22 +01:00
2018-11-29 13:26:18 +01:00
editMode = false;
2017-12-21 10:16:46 +01:00
2018-10-05 17:00:54 +02:00
breadCrumbs: Observable<BreadcrumbItem[]>;
isNew = true;
textCtrl = new FormControl();
2019-01-18 18:03:45 +01:00
dmp: DmpEditorModel;
2018-10-05 17:00:54 +02:00
formGroup: FormGroup = null;
filteringOrganisationsAsync = false;
filteringResearchersAsync = false;
filteredProfilesAsync = false;
2019-01-18 18:03:45 +01:00
filteredOrganisations: ExternalSourceItemModel[];
filteredResearchers: ExternalSourceItemModel[];
2018-10-05 17:00:54 +02:00
filteredProfiles: DatasetProfileModel[];
projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
profilesAutoCompleteConfiguration: MultipleAutoCompleteConfiguration;
organisationsAutoCompleteConfiguration: MultipleAutoCompleteConfiguration;
researchersAutoCompleteConfiguration: MultipleAutoCompleteConfiguration;
createNewVersion;
2019-01-18 18:03:45 +01:00
associatedUsers: Array<UserModel>;
// filteredOptions: Observable<DmpProfileListing[]>;
2018-10-05 17:00:54 +02:00
constructor(
2019-01-18 18:03:45 +01:00
private dmpProfileService: DmpProfileService,
private dmpService: DmpService,
2018-10-05 17:00:54 +02:00
private projectService: ProjectService,
private externalSourcesService: ExternalSourcesService,
private route: ActivatedRoute,
2019-01-18 18:03:45 +01:00
private snackBar: MatSnackBar,
private router: Router,
private language: TranslateService,
private _service: DmpService,
private dialog: MatDialog,
2018-10-05 17:00:54 +02:00
private _viewContainerRef: ViewContainerRef,
public languageResolverService: LanguageResolverService,
private uiNotificationService: UiNotificationService
2018-10-05 17:00:54 +02:00
) {
2018-11-27 18:33:17 +01:00
super();
2019-01-18 18:03:45 +01:00
// this.filteredOptions = dmpProfileService.getAll({ criteria: new DmpProfileCriteria() });
2018-10-05 17:00:54 +02:00
}
ngOnInit() {
2018-11-27 18:33:17 +01:00
this.route.params
.pipe(takeUntil(this._destroyed))
.subscribe((params: Params) => {
const itemId = params['id'];
const projectId = params['projectId'];
2018-11-27 18:33:17 +01:00
const projectRequestItem: RequestItem<ProjectCriteria> = new RequestItem();
projectRequestItem.criteria = new ProjectCriteria();
const organisationRequestItem: RequestItem<BaseCriteria> = new RequestItem();
organisationRequestItem.criteria = new BaseCriteria();
this.projectAutoCompleteConfiguration = {
filterFn: this.searchProject.bind(this),
initialItems: (extraData) => this.searchProject(''),
2018-11-27 18:33:17 +01:00
displayFn: (item) => item['label'],
titleFn: (item) => item['label']
2018-11-27 18:33:17 +01:00
};
this.profilesAutoCompleteConfiguration = {
filterFn: this.filterProfiles.bind(this),
initialItems: (excludedItems: any[]) => this.filterProfiles('').map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)),
displayFn: (item) => item['label'],
titleFn: (item) => item['label']
2018-11-27 18:33:17 +01:00
};
this.organisationsAutoCompleteConfiguration = {
filterFn: this.filterOrganisations.bind(this),
initialItems: (excludedItems: any[]) => this.filterOrganisations('').map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)),
displayFn: (item) => item['name'],
titleFn: (item) => item['name']
2018-11-27 18:33:17 +01:00
};
this.researchersAutoCompleteConfiguration = {
filterFn: this.filterResearchers.bind(this),
initialItems: (excludedItems: any[]) => this.filterResearchers('').map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)),
displayFn: (item) => item['name'],
titleFn: (item) => item['name']
2018-11-27 18:33:17 +01:00
};
if (itemId != null) {
this.isNew = false;
2019-01-18 18:03:45 +01:00
this.dmpService.getSingle(itemId).map(data => data as DmpModel)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(async data => {
2019-01-18 18:03:45 +01:00
this.dmp = new DmpEditorModel().fromModel(data);
this.formGroup = this.dmp.buildForm();
if (!this.editMode || this.dmp.status === Status.Inactive) { this.formGroup.disable(); }
2018-11-27 18:33:17 +01:00
if (this.formGroup.get('profile') && this.formGroup.get('profile').value) {
this.textCtrl.patchValue(this.formGroup.get('profile').value);
}
this.breadCrumbs = Observable.of([
{
2019-01-18 18:03:45 +01:00
parentComponentName: 'DmpListingComponent',
2018-11-27 18:33:17 +01:00
label: 'DMPs',
url: 'dmps',
2019-01-18 18:03:45 +01:00
notFoundResolver: [await this.projectService.getSingle(this.dmp.project.id).map(x => ({ label: x.label, url: '/projects/edit/' + x.id }) as BreadcrumbItem).toPromise()]
2018-11-27 18:33:17 +01:00
}]
);
this.associatedUsers = data.associatedUsers;
});
} else if (projectId != null) {
this.isNew = true;
this.projectService.getSingle(projectId).map(data => data as ProjectModel)
.pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.dmp = new DmpEditorModel();
this.dmp.project = data;
this.formGroup = this.dmp.buildForm();
this.formGroup.get('project').disable();
this.registerFormEventsForNewItem();
});
2018-11-27 18:33:17 +01:00
} else {
2019-01-18 18:03:45 +01:00
this.dmp = new DmpEditorModel();
this.formGroup = this.dmp.buildForm();
this.registerFormEventsForNewItem();
2018-11-27 18:33:17 +01:00
}
});
2018-10-05 17:00:54 +02:00
this.route
.queryParams
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
2018-10-05 17:00:54 +02:00
.subscribe(params => {
this.createNewVersion = params['clone'];
});
}
registerFormEventsForNewItem() {
if (this.formGroup.get('profile') && this.formGroup.get('profile').value) {
this.textCtrl.patchValue(this.formGroup.get('profile').value);
}
this.breadCrumbs = Observable.of([
{
parentComponentName: 'DmpListingComponent',
label: 'DMPs',
url: 'dmps',
}
]);
}
2018-10-05 17:00:54 +02:00
searchProject(query: string) {
const projectRequestItem: RequestItem<ProjectCriteria> = new RequestItem();
projectRequestItem.criteria = new ProjectCriteria();
projectRequestItem.criteria.like = query;
return this.projectService.getWithExternal(projectRequestItem);
}
formSubmit(): void {
//this.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) { return; }
this.onSubmit();
}
public isFormValid() {
return this.formGroup.valid;
}
onSubmit(): void {
this.dmpService.createDmp(this.formGroup.getRawValue())
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
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);
2019-01-18 18:03:45 +01:00
this.router.navigate(['/plans']);
2018-10-05 17:00:54 +02:00
}
onCallbackError(error: any) {
this.setErrorModel(error.error);
//this.validateAllFormFields(this.formGroup);
}
public setErrorModel(validationErrorModel: ValidationErrorModel) {
Object.keys(validationErrorModel).forEach(item => {
(<any>this.dmp.validationErrorModel)[item] = (<any>validationErrorModel)[item];
2018-10-05 17:00:54 +02:00
});
}
public cancel(): void {
2019-01-18 18:03:45 +01:00
this.router.navigate(['/plans']);
2018-10-05 17:00:54 +02:00
}
public invite(): void {
2019-01-18 18:03:45 +01:00
this.router.navigate(['/invite/' + this.dmp.id]);
2018-10-05 17:00:54 +02:00
}
2019-01-18 18:03:45 +01:00
filterOrganisations(value: string): Observable<ExternalSourceItemModel[]> {
2018-10-05 17:00:54 +02:00
this.filteredOrganisations = undefined;
this.filteringOrganisationsAsync = true;
return this.externalSourcesService.searchDMPOrganizations(value);
}
2019-01-18 18:03:45 +01:00
filterResearchers(value: string): Observable<ExternalSourceItemModel[]> {
2018-10-05 17:00:54 +02:00
this.filteredResearchers = undefined;
this.filteringResearchersAsync = true;
return this.externalSourcesService.searchDMPResearchers({ criteria: { name: value, like: null } });
}
filterProfiles(value: string): Observable<DatasetProfileModel[]> {
this.filteredProfiles = undefined;
this.filteredProfilesAsync = true;
const request = new RequestItem<DatasetProfileCriteria>();
const criteria = new DatasetProfileCriteria();
criteria.like = value;
request.criteria = criteria;
return this._service.searchDMPProfiles(request);
}
addResearcher(rowId: any, rowName: any) {
2019-01-18 18:03:45 +01:00
const dialogRef = this.dialog.open(AddResearcherComponent, {
2018-10-05 17:00:54 +02:00
data: {
dmpId: rowId,
dmpName: rowName
}
});
}
availableProfiles() {
const dialogRef = this.dialog.open(AvailableProfilesComponent, {
2018-11-29 15:19:08 +01:00
// height: '355px',
// width: '700px',
2018-10-05 17:00:54 +02:00
data: {
profiles: this.formGroup.get('profiles')
}
});
return false;
}
public delete(): void {
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.dmpService.delete(this.dmp.id)
.pipe(takeUntil(this._destroyed))
.subscribe(
complete => { this.onCallbackSuccess() },
error => this.onCallbackError(error)
);
}
});
2018-10-05 17:00:54 +02:00
}
selectOption(option: any) {
2019-01-18 18:03:45 +01:00
this.dmp.definition = null;
2018-10-05 17:00:54 +02:00
this.formGroup.get('profile').patchValue(option, { emitEvent: false });
2018-11-27 18:33:17 +01:00
this.dmpProfileService.getSingle(option.id)
.pipe(takeUntil(this._destroyed))
.subscribe(result => {
2019-01-18 18:03:45 +01:00
this.dmp.definition = result.definition;
2018-11-27 18:33:17 +01:00
});
2018-10-05 17:00:54 +02:00
}
displayWith(item: any) {
if (!item) { return null; }
return item['label'];
}
redirectToProject() {
2019-01-18 18:03:45 +01:00
this.router.navigate(['projects/edit/' + this.dmp.project.id]);
2018-10-05 17:00:54 +02:00
}
redirectToDatasets() {
2019-01-18 18:03:45 +01:00
this.router.navigate(['datasets/dmp/' + this.dmp.id]);
2018-10-05 17:00:54 +02:00
}
newVersion(id: String, label: String) {
2019-01-18 18:03:45 +01:00
this.router.navigate(['/plans/new_version/' + id, { dmpLabel: label }]);
2018-10-05 17:00:54 +02:00
}
clone(id: String) {
2019-01-18 18:03:45 +01:00
this.router.navigate(['/plans/clone/' + id]);
2018-10-05 17:00:54 +02:00
}
2018-10-05 17:00:54 +02:00
viewVersions(rowId: String, rowLabel: String) {
2019-01-18 18:03:45 +01:00
this.router.navigate(['/plans/versions/' + rowId], { queryParams: { groupLabel: rowLabel } });
2018-10-05 17:00:54 +02:00
}
2018-10-09 10:28:44 +02:00
downloadXml(id: string) {
2019-01-18 18:03:45 +01:00
this.dmpService.downloadXML(id)
2018-11-27 18:33:17 +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'));
2018-10-09 10:28:44 +02:00
2018-11-27 18:33:17 +01:00
FileSaver.saveAs(blob, filename);
});
2018-10-09 10:28:44 +02:00
}
downloadDocx(id: string) {
2019-01-18 18:03:45 +01:00
this.dmpService.downloadDocx(id)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'application/octet-stream' });
const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
2018-11-27 18:33:17 +01:00
FileSaver.saveAs(blob, filename);
});
}
downloadPDF(id: string) {
2019-01-18 18:03:45 +01:00
this.dmpService.downloadPDF(id)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'application/octet-stream' });
const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
2018-11-27 18:33:17 +01:00
FileSaver.saveAs(blob, filename);
});
}
2018-10-09 10:28:44 +02:00
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 enableForm() {
2019-01-18 18:03:45 +01:00
if (this.formGroup.get('status').value !== DmpStatus.Finalized) {
this.editMode = true;
this.formGroup.enable();
} else {
2019-01-18 18:03:45 +01:00
this.dmpService.unlock(this.formGroup.get('id').value)
2018-11-27 18:33:17 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(x => {
this.editMode = true;
2019-01-18 18:03:45 +01:00
this.formGroup.get('status').patchValue(DmpStatus.Draft);
2018-11-27 18:33:17 +01:00
this.formGroup.enable();
});
}
}
public disableForm() {
this.editMode = false;
this.formGroup.disable();
}
saveAndFinalize() {
2019-01-18 18:03:45 +01:00
const dialogRef = this.dialog.open(DmpFinalizeDialogComponent, {
data: {
submitFunction: (items: DatasetListingModel[]) => {
this.formGroup.get('status').setValue('1');
this.formGroup.addControl('datasets', new FormControl(items));
this.formSubmit();
dialogRef.close();
},
2019-01-18 18:03:45 +01:00
dmp: this.dmp
}
});
}
2018-05-28 11:50:42 +02:00
}