import { Component, OnInit } from '@angular/core'; import { BaseComponent } from '@common/base/base.component'; import { DatasetOverviewModel } from '@app/core/model/dataset/dataset-overview'; import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item'; import { Observable, of as observableOf, interval } from 'rxjs'; import { ActivatedRoute, Router, Params } from '@angular/router'; import { DatasetService } from '@app/core/services/dataset/dataset.service'; import { TranslateService } from '@ngx-translate/core'; import { AuthService } from '@app/core/services/auth/auth.service'; import { MatDialog } from '@angular/material'; import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { ConfigurationService } from '@app/core/services/configuration/configuration.service'; import { Oauth2DialogService } from '@app/ui/misc/oauth2-dialog/service/oauth2-dialog.service'; import { UserService } from '@app/core/services/user/user.service'; import { takeUntil } from 'rxjs/operators'; import { Principal } from '@app/core/model/auth/principal'; import { Role } from '@app/core/common/enum/role'; import { Location } from '@angular/common'; import { UserInfoListingModel } from '@app/core/model/user/user-info-listing'; import { DatasetStatus } from '@app/core/common/enum/dataset-status'; import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component'; import * as FileSaver from 'file-saver'; import { DmpInvitationDialogComponent } from '@app/ui/dmp/invitation/dmp-invitation.component'; import { DatasetWizardEditorModel } from '../dataset-wizard/dataset-wizard-editor.model'; import { DatasetWizardService } from '@app/core/services/dataset-wizard/dataset-wizard.service'; import { FormControl } from '@angular/forms'; import { DatasetCopyDialogueComponent } from '../dataset-wizard/dataset-copy-dialogue/dataset-copy-dialogue.component'; import { DmpService } from '@app/core/services/dmp/dmp.service'; import { ResearcherModel } from '@app/core/model/researcher/researcher'; import { LockService } from '@app/core/services/lock/lock.service'; @Component({ selector: 'app-dataset-overview', templateUrl: './dataset-overview.component.html', styleUrls: ['./dataset-overview.component.scss'] }) export class DatasetOverviewComponent extends BaseComponent implements OnInit { dataset: DatasetOverviewModel; datasetWizardModel: DatasetWizardEditorModel; isNew = true; isFinalized = false; isPublicView = true; hasPublishButton: boolean = true; breadCrumbs: Observable = observableOf(); isUserOwner: boolean; expand = false; hasDOIToken = false; researchers: ResearcherModel[]; users: UserInfoListingModel[]; lockStatus = false; // lockStatus: Boolean; constructor( private route: ActivatedRoute, private router: Router, private datasetService: DatasetService, private translate: TranslateService, private authentication: AuthService, private dialog: MatDialog, private language: TranslateService, private uiNotificationService: UiNotificationService, private configurationService: ConfigurationService, private oauth2DialogService: Oauth2DialogService, private userService: UserService, private dmpService: DmpService, private location: Location, private datasetWizardService: DatasetWizardService, private lockService: LockService ) { super(); } ngOnInit() { // Gets dataset data using parameter id this.route.params .pipe(takeUntil(this._destroyed)) .subscribe((params: Params) => { const itemId = params['id']; const publicId = params['publicId']; if (itemId != null) { this.isNew = false; this.isPublicView = false; this.datasetService.getOverviewSingle(itemId) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.dataset = data; this.getDmpResearchers(); this.getDmpUsers(); this.datasetWizardService.getSingle(this.dataset.id).pipe(takeUntil(this._destroyed)) .subscribe(data => { this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data); }); // this.checkLockStatus(this.dataset.id); this.setIsUserOwner(); const breadCrumbs = []; breadCrumbs.push({ parentComponentName: null, label: this.language.instant('NAV-BAR.MY-DATASET-DESCRIPTIONS'), url: "/datasets" }); breadCrumbs.push({ parentComponentName: 'DatasetListingComponent', label: this.dataset.label, url: '/datasets/overview/' + this.dataset.id }); this.breadCrumbs = observableOf(breadCrumbs); }, (error: any) => { if (error.status === 404) { return this.onFetchingDeletedCallbackError('/datasets/'); } if (error.status === 403) { return this.onFetchingForbiddenCallbackError('/datasets/'); } }); } else if (publicId != null) { this.isNew = false; this.isFinalized = true; this.isPublicView = true; this.datasetService.getOverviewSinglePublic(publicId) .pipe(takeUntil(this._destroyed)) .subscribe(data => { this.dataset = data; this.getDmpResearchers(); this.getDmpUsers(); this.datasetWizardService.getSingle(this.dataset.id).pipe(takeUntil(this._destroyed)) .subscribe(data => { this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data); }); // this.checkLockStatus(this.dataset.id); this.setIsUserOwner(); const breadCrumbs = []; breadCrumbs.push({ parentComponentName: null, label: this.language.instant('NAV-BAR.PUBLIC DATASETS'), url: "/explore" }); breadCrumbs.push({ parentComponentName: 'DatasetListingComponent', label: this.dataset.label, url: '/datasets/publicOverview/' + this.dataset.id }); this.breadCrumbs = observableOf(breadCrumbs); }, (error: any) => { if (error.status === 404) { return this.onFetchingDeletedCallbackError('/explore'); } if (error.status === 403) { return this.onFetchingForbiddenCallbackError('/explore'); } }); } }); } checkLockStatus(id: string) { this.lockService.checkLockStatus(id).pipe(takeUntil(this._destroyed)) .subscribe(lockStatus => this.lockStatus = lockStatus); } onFetchingDeletedCallbackError(redirectRoot: string) { this.uiNotificationService.snackBarNotification(this.language.instant('DATASET-OVERVIEW.ERROR.DELETED-DATASET'), SnackBarNotificationLevel.Error); this.router.navigate([redirectRoot]); } onFetchingForbiddenCallbackError(redirectRoot: string) { this.uiNotificationService.snackBarNotification(this.language.instant('DATASET-OVERVIEW.ERROR.FORBIDEN-DATASET'), SnackBarNotificationLevel.Error); this.router.navigate([redirectRoot]); } goBack(): void { this.location.back(); } reloadComponent(): void { this.router.navigateByUrl('/datasets', { skipLocationChange: true }).then(() => { this.router.navigate([`/datasets/overview/${this.dataset.id}`]); }); } getDmpResearchers() { this.dmpService.getSingle(this.dataset.dmp.id).pipe(takeUntil(this._destroyed)) .subscribe(data => { this.researchers = data.researchers; }); } getDmpUsers() { this.dmpService.getSingle(this.dataset.dmp.id).pipe(takeUntil(this._destroyed)) .subscribe(data => { this.users = data.users; }); } setIsUserOwner() { if (this.dataset) { const principal: Principal = this.authentication.current(); if (principal) this.isUserOwner = principal.id === this.dataset.users.find(x => x.role === Role.Owner).id; } } isUserAuthor(userId: string): boolean { const principal: Principal = this.authentication.current(); return userId === principal.id; } isUserDatasetRelated() { const principal: Principal = this.authentication.current(); let isRelated: boolean = false; if (this.dataset && principal) { this.dataset.users.forEach(element => { if (element.id === principal.id) { isRelated = true; } }) } return isRelated; } roleDisplay(value: UserInfoListingModel) { if (value.role === Role.Owner) { return this.translate.instant('DMP-LISTING.OWNER'); } else if (value.role === Role.Member) { return this.translate.instant('DMP-LISTING.MEMBER'); } else { return this.translate.instant('DMP-LISTING.OWNER'); } } roleDisplayFromList(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 === Role.Owner) { return this.translate.instant('DMP-LISTING.OWNER'); } else if (role === Role.Member) { return this.translate.instant('DMP-LISTING.MEMBER'); } else { return this.translate.instant('DMP-LISTING.OWNER'); } } openShareDialog(rowId: any, rowName: any) { const dialogRef = this.dialog.open(DmpInvitationDialogComponent, { restoreFocus: false, data: { dmpId: rowId, dmpName: rowName } }); } public isAuthenticated(): boolean { return !(!this.authentication.current()); } isDraftDataset(dataset: DatasetOverviewModel) { return dataset.status == DatasetStatus.Draft; } isFinalizedDataset(dataset: DatasetOverviewModel) { return dataset.status == DatasetStatus.Finalized; } editClicked(dataset: DatasetOverviewModel) { if (dataset.public) { this.router.navigate(['/datasets/publicEdit/' + dataset.id]); } else { this.router.navigate(['/datasets/edit/' + dataset.id]); } } 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'), isDeleteConfirmation: true } }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { this.datasetService.delete(this.dataset.id) .pipe(takeUntil(this._destroyed)) .subscribe( complete => { this.onCallbackSuccess(); this.router.navigate(['/datasets']); }, error => this.onDeleteCallbackError(error) ); } }); } dmpClicked(dmpId: String) { this.router.navigate(['/plans/overview/' + dmpId]); } 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); } onDeleteCallbackError(error) { this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DELETE'), SnackBarNotificationLevel.Error); } public getOrcidPath(): string { return this.configurationService.orcidPath; } downloadPDF(id: string) { this.datasetService.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); }); } downloadDocx(id: string) { this.datasetService.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); }); } downloadXml(id: string) { this.datasetService.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); }); } downloadJson(id: string) { this.datasetService.downloadJson(id) .pipe(takeUntil(this._destroyed)) .subscribe(response => { const blob = new Blob([response.body], { type: 'application/json' }); 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; } openDmpSearchDialogue() { const formControl = new FormControl(); const dialogRef = this.dialog.open(DatasetCopyDialogueComponent, { width: '500px', restoreFocus: false, data: { formControl: formControl, datasetId: this.dataset.id, datasetProfileId: this.datasetWizardModel.profile, datasetProfileExist: false, 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 } }); } }); } updateUsers() { return this.dmpService.updateUsers(this.dataset.dmp.id, this.users).pipe(takeUntil(this._destroyed)) .subscribe( complete => { this.onCallbackSuccess(); this.reloadComponent(); }, error => this.onDeleteCallbackError(error) ); } removeUserFromDmp(user: UserInfoListingModel) { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { data: { message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-USER'), confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.REMOVE'), cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'), isDeleteConfirmation: false } }); dialogRef.afterClosed().subscribe(result => { if (result) { const index = this.users.findIndex(x => x.id === user.id); if (index > -1) { this.users.splice(index, 1); } this.updateUsers(); } }); } }