import { Component, OnInit } from '@angular/core'; import { Params, ActivatedRoute, Router } from '@angular/router'; import { BaseComponent } from '../../../core/common/base/base.component'; import { DmpService } from '../../../core/services/dmp/dmp.service'; import { takeUntil } from 'rxjs/operators'; import { DmpOverviewModel } from '../../../core/model/dmp/dmp-overview'; import { TranslateService } from '@ngx-translate/core'; import { Principal } from '../../../core/model/auth/Principal'; import { AuthService } from '../../../core/services/auth/auth.service'; import { UserInfoListingModel } from '../../../core/model/user/user-info-listing'; import { DatasetOverviewModel } from '../../../core/model/dataset/dataset-overview'; import { MatDialog } from '@angular/material'; import { ConfirmationDialogComponent } from '../../../library/confirmation-dialog/confirmation-dialog.component'; import { UiNotificationService, SnackBarNotificationLevel } from '../../../core/services/notification/ui-notification-service'; import * as FileSaver from 'file-saver'; import { ExportMethodDialogComponent } from '../../../library/export-method-dialog/export-method-dialog.component'; import { Observable } from 'rxjs'; import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item'; @Component({ selector: 'app-dmp-overview', templateUrl: './dmp-overview.component.html', styleUrls: ['./dmp-overview.component.scss'] }) export class DmpOverviewComponent extends BaseComponent implements OnInit { dmp: DmpOverviewModel; isNew = true; breadCrumbs: Observable = Observable.of(); constructor( private route: ActivatedRoute, private router: Router, private dmpService: DmpService, private translate: TranslateService, private authentication: AuthService, private dialog: MatDialog, private language: TranslateService, private uiNotificationService: UiNotificationService ) { super(); } ngOnInit() { // Gets dmp data using parameter id this.route.params .pipe(takeUntil(this._destroyed)) .subscribe((params: Params) => { const itemId = params['id']; if (itemId != null) { this.isNew = false; this.dmpService.getOverviewSingle(itemId) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.dmp = data; const breadCrumbs = []; breadCrumbs.push({ parentComponentName: null, label: 'DMPs', url: "/plans" }); breadCrumbs.push({ parentComponentName: 'DmpListingComponent', label: this.dmp.label, url: '/overview/' + this.dmp.id }); this.breadCrumbs = Observable.of(breadCrumbs); }) } }); } editClicked(dmp: DmpOverviewModel) { this.router.navigate(['/plans/edit/' + dmp.id]); } cloneClicked(dmp: DmpOverviewModel) { this.router.navigate(['/plans/clone/' + dmp.id]); } projectClicked(projectId: String) { this.router.navigate(['/projects/edit/' + projectId]); } datasetClicked(datasetId: String) { this.router.navigate(['/datasets/edit/' + datasetId]); } datasetsClicked(dmpId: String) { this.router.navigate(['/datasets'], { queryParams: { dmpId: dmpId } }); } goToUri(uri: string) { window.open(uri, "_blank"); } deleteClicked() { 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.DELETE'), 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.onDeleteCallbackError(error) ); } }); } advancedClicked() { const dialogRef = this.dialog.open(ExportMethodDialogComponent, { maxWidth: '400px', data: { message: "Download as:", XMLButton: "XML", documentButton: "Document", pdfButton: "PDF" } }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result == "pdf") { this.downloadPDF(this.dmp.id); } else if (result == "xml") { this.downloadXml(this.dmp.id); } else if (result == "doc") { this.downloadDocx(this.dmp.id); } }); } 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(['/plans']); } onDeleteCallbackError(error) { this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DELETE'), SnackBarNotificationLevel.Error); } downloadXml(id: string) { this.dmpService.downloadXML(id) .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); }); } downloadDocx(id: string) { this.dmpService.downloadDocx(id) .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); }); } downloadPDF(id: string) { this.dmpService.downloadPDF(id) .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); }); } 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; } roleDisplay(value: UserInfoListingModel[]) { const principal: Principal = this.authentication.current(); let role: number; if (principal) { value.forEach(element => { if (principal.id === element.id) { role = element.role; } }); } if (role === 0) { return this.translate.instant('DMP-LISTING.OWNER'); } else if (role === 1) { return this.translate.instant('DMP-LISTING.MEMBER'); } else { return this.translate.instant('DMP-LISTING.OWNER'); } } isDraft(dataset: DatasetOverviewModel) { if (dataset.status == 0) { return true } else { return false } } }