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

424 lines
16 KiB
TypeScript
Raw Normal View History

2020-07-03 15:58:13 +02:00
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';
2020-07-06 12:21:11 +02:00
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';
2020-07-06 17:42:54 +02:00
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';
2020-07-03 15:58:13 +02:00
@Component({
selector: 'app-dataset-overview',
templateUrl: './dataset-overview.component.html',
styleUrls: ['./dataset-overview.component.scss']
})
export class DatasetOverviewComponent extends BaseComponent implements OnInit {
dataset: DatasetOverviewModel;
2020-07-06 12:21:11 +02:00
datasetWizardModel: DatasetWizardEditorModel;
2020-07-03 15:58:13 +02:00
isNew = true;
isFinalized = false;
isPublicView = true;
hasPublishButton: boolean = true;
breadCrumbs: Observable<BreadcrumbItem[]> = observableOf();
isUserOwner: boolean;
expand = false;
hasDOIToken = false;
2020-07-06 17:42:54 +02:00
researchers: ResearcherModel[];
users: UserInfoListingModel[];
lockStatus: Boolean;
2020-07-03 15:58:13 +02:00
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,
2020-07-06 17:42:54 +02:00
private dmpService: DmpService,
2020-07-06 12:21:11 +02:00
private location: Location,
2020-07-06 17:42:54 +02:00
private datasetWizardService: DatasetWizardService,
private lockService: LockService
2020-07-03 15:58:13 +02:00
) {
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;
2020-07-06 17:42:54 +02:00
this.getDmpResearchers();
this.getDmpUsers();
2020-07-06 12:21:11 +02:00
this.datasetWizardService.getSingle(this.dataset.id).pipe(takeUntil(this._destroyed))
2020-07-06 17:42:54 +02:00
.subscribe(data => {
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data);
});
this.checkLockStatus(this.dataset.id);
2020-07-03 15:58:13 +02:00
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;
2020-07-06 17:42:54 +02:00
this.getDmpResearchers();
this.getDmpUsers();
2020-07-06 12:21:11 +02:00
this.datasetWizardService.getSingle(this.dataset.id).pipe(takeUntil(this._destroyed))
2020-07-06 17:42:54 +02:00
.subscribe(data => {
this.datasetWizardModel = new DatasetWizardEditorModel().fromModel(data);
});
this.checkLockStatus(this.dataset.id);
2020-07-03 15:58:13 +02:00
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);
}
2020-07-03 15:58:13 +02:00
2020-07-06 17:42:54 +02:00
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]);
2020-07-03 15:58:13 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-03 15:58:13 +02:00
goBack(): void {
2020-07-06 17:42:54 +02:00
this.location.back();
}
reloadPage(): void {
this.router.navigateByUrl('/datasets', { skipLocationChange: true }).then(() => {
this.router.navigate([`/datasets/overview/${this.dataset.id}`]);
});
}
2020-07-06 17:42:54 +02:00
getDmpResearchers() {
this.dmpService.getSingle(this.dataset.dmp.id).pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.researchers = data.researchers;
});
}
2020-07-03 15:58:13 +02:00
getDmpUsers() {
this.dmpService.getSingle(this.dataset.dmp.id).pipe(takeUntil(this._destroyed))
.subscribe(data => {
this.users = data.users;
});
}
2020-07-03 15:58:13 +02:00
setIsUserOwner() {
2020-07-06 17:42:54 +02:00
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;
}
2020-07-03 15:58:13 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-06 12:21:11 +02:00
isUserAuthor(userId: string): boolean {
2020-07-06 17:42:54 +02:00
const principal: Principal = this.authentication.current();
return userId === principal.id;
2020-07-06 12:21:11 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-03 15:58:13 +02:00
isUserDatasetRelated() {
2020-07-06 17:42:54 +02:00
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;
2020-07-03 15:58:13 +02:00
}
2020-07-06 12:21:11 +02:00
roleDisplay(value: UserInfoListingModel) {
2020-07-06 17:42:54 +02:00
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');
}
2020-07-06 12:21:11 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-03 15:58:13 +02:00
roleDisplayFromList(value: UserInfoListingModel[]) {
2020-07-06 17:42:54 +02:00
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');
}
2020-07-03 15:58:13 +02:00
}
2020-07-06 12:21:11 +02:00
openShareDialog(rowId: any, rowName: any) {
2020-07-06 17:42:54 +02:00
const dialogRef = this.dialog.open(DmpInvitationDialogComponent, {
restoreFocus: false,
data: {
dmpId: rowId,
dmpName: rowName
}
});
}
2020-07-03 15:58:13 +02:00
public isAuthenticated(): boolean {
2020-07-06 17:42:54 +02:00
return !(!this.authentication.current());
2020-07-03 15:58:13 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-03 15:58:13 +02:00
isDraftDataset(dataset: DatasetOverviewModel) {
2020-07-06 17:42:54 +02:00
return dataset.status == DatasetStatus.Draft;
2020-07-03 15:58:13 +02:00
}
2020-07-06 12:21:11 +02:00
isFinalizedDataset(dataset: DatasetOverviewModel) {
2020-07-06 17:42:54 +02:00
return dataset.status == DatasetStatus.Finalized;
}
2020-07-03 15:58:13 +02:00
editClicked(dataset: DatasetOverviewModel) {
2020-07-06 12:21:11 +02:00
if (dataset.public) {
2020-07-06 17:42:54 +02:00
this.router.navigate(['/datasets/publicEdit/' + dataset.id]);
} else {
this.router.navigate(['/datasets/edit/' + dataset.id]);
}
2020-07-03 15:58:13 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-03 15:58:13 +02:00
deleteClicked() {
2020-07-06 17:42:54 +02:00
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']);
},
2020-07-06 17:42:54 +02:00
error => this.onDeleteCallbackError(error)
);
}
});
2020-07-03 15:58:13 +02:00
}
2020-07-06 12:21:11 +02:00
dmpClicked(dmpId: String) {
2020-07-06 17:42:54 +02:00
this.router.navigate(['/plans/overview/' + dmpId]);
}
2020-07-03 15:58:13 +02:00
onCallbackSuccess(): void {
2020-07-06 17:42:54 +02:00
this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
}
2020-07-03 15:58:13 +02:00
2020-07-06 17:42:54 +02:00
onDeleteCallbackError(error) {
this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DELETE'), SnackBarNotificationLevel.Error);
2020-07-03 15:58:13 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-03 15:58:13 +02:00
public getOrcidPath(): string {
2020-07-06 17:42:54 +02:00
return this.configurationService.orcidPath;
2020-07-03 17:31:35 +02:00
}
2020-07-06 12:21:11 +02:00
downloadPDF(id: string) {
2020-07-06 17:42:54 +02:00
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);
});
2020-07-06 12:21:11 +02:00
}
downloadDocx(id: string) {
2020-07-06 17:42:54 +02:00
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);
});
2020-07-06 12:21:11 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-06 12:21:11 +02:00
downloadXml(id: string) {
2020-07-06 17:42:54 +02:00
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);
});
2020-07-06 12:21:11 +02:00
}
2020-07-06 17:42:54 +02:00
2020-07-06 12:21:11 +02:00
downloadJson(id: string) {
2020-07-06 17:42:54 +02:00
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);
})
2020-07-06 12:21:11 +02:00
}
2020-07-06 17:42:54 +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;
2020-07-06 12:21:11 +02:00
}
openDmpSearchDialogue() {
2020-07-06 17:42:54 +02:00
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.reloadPage();
},
error => this.onDeleteCallbackError(error)
);
}
2020-07-07 17:30:25 +02:00
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();
}
});
}
2020-07-06 17:42:54 +02:00
2020-07-03 15:58:13 +02:00
}