import { Component, OnInit, Output, EventEmitter } from '@angular/core'; import { MatDialog } from '@angular/material'; import { Router } from '@angular/router'; import { RecentActivityType } from '@app/core/common/enum/recent-activity-type'; import { Principal } from '@app/core/model/auth/principal'; import { DataTableRequest, DataTableMultiTypeRequest } from '@app/core/model/data-table/data-table-request'; import { DmpListingModel } from '@app/core/model/dmp/dmp-listing'; import { DmpCriteria } from '@app/core/query/dmp/dmp-criteria'; import { AuthService } from '@app/core/services/auth/auth.service'; import { DmpService } from '@app/core/services/dmp/dmp.service'; import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { EnumUtils } from '@app/core/services/utilities/enum-utils.service'; import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component'; import { BaseComponent } from '@common/base/base.component'; import { TranslateService } from '@ngx-translate/core'; import * as FileSaver from 'file-saver'; import { takeUntil } from 'rxjs/operators'; import { DmpInvitationDialogComponent } from '@app/ui/dmp/invitation/dmp-invitation-dialog.component'; import { DmpStatus } from '@app/core/common/enum/dmp-status'; import { DatasetService } from '@app/core/services/dataset/dataset.service'; import { DatasetListingModel } from '@app/core/model/dataset/dataset-listing'; import { Role } from '@app/core/common/enum/role'; import { RecentActivityModel } from '@app/core/model/recent-activity/recent-activity.model'; import { DashboardService } from '@app/core/services/dashboard/dashboard.service'; import { RecentActivityCriteria } from '@app/core/query/recent-activity/recent-activity-criteria'; import { RecentDmpModel } from '@app/core/model/recent-activity/recent-dmp-activity.model'; import { RecentDatasetModel } from '@app/core/model/recent-activity/recent-dataset-activity.model'; import { UserInfoListingModel } from '@app/core/model/user/user-info-listing'; import { DatasetWizardService } from '@app/core/services/dataset-wizard/dataset-wizard.service'; import { FormControl, FormBuilder } from '@angular/forms'; import { DatasetCopyDialogueComponent } from '@app/ui/dataset/dataset-wizard/dataset-copy-dialogue/dataset-copy-dialogue.component'; import { RecentActivityOrder } from '@app/core/common/enum/recent-activity-order'; @Component({ selector: 'app-recent-edited-activity', templateUrl: './recent-edited-activity.component.html', styleUrls: ['./recent-edited-activity.component.css'] }) export class RecentEditedActivityComponent extends BaseComponent implements OnInit { @Output() totalCountRecentEdited: EventEmitter = new EventEmitter(); allRecentActivities: RecentActivityModel[]; recentActivityTypeEnum = RecentActivityType; isDraft: boolean; totalCount: number; startIndex: number = 0; dmpOffset: number = 0; datasetOffset: number = 0; pageSize: number = 5; public formGroup = new FormBuilder().group({ like: new FormControl(), order: new FormControl() }); order = RecentActivityOrder; constructor( private router: Router, public enumUtils: EnumUtils, private authentication: AuthService, private dmpService: DmpService, private dashboardService: DashboardService, private language: TranslateService, private dialog: MatDialog, private uiNotificationService: UiNotificationService, private datasetWizardService: DatasetWizardService ) { super(); } ngOnInit() { if (this.isAuthenticated()) { const fields: Array = []; // const fields: Array = ["-modified"]; const allDataTableRequest: DataTableMultiTypeRequest = new DataTableMultiTypeRequest(0, 0, 5, { fields: fields }); allDataTableRequest.criteria = new RecentActivityCriteria(); allDataTableRequest.criteria.like = ""; this.formGroup.get('order').setValue(this.order.MODIFIED); allDataTableRequest.criteria.order = this.formGroup.get('order').value; this.dashboardService .getRecentActivity(allDataTableRequest) .subscribe(response => { this.allRecentActivities = response; this.allRecentActivities.forEach(recentActivity => { if (recentActivity.type === RecentActivityType.Dataset) { this.datasetOffset = this.datasetOffset + 1; } else if (recentActivity.type === RecentActivityType.Dmp) { this.dmpOffset = this.dmpOffset + 1; } }); this.totalCountRecentEdited.emit(this.allRecentActivities.length); }); this.formGroup.get('like').valueChanges .pipe(takeUntil(this._destroyed)) .subscribe(x => this.refresh()); this.formGroup.get('order').valueChanges .pipe(takeUntil(this._destroyed)) .subscribe(x => this.refresh()); } } getDatasets(activity: RecentDmpModel): RecentDatasetModel[] { return activity.datasets; } getGroupId(activity: RecentDmpModel): string { return activity.groupId; } getDmp(activity: RecentDatasetModel): String { return activity.dmp; } getDmpId(activity: RecentDatasetModel): String { return activity.dmpId; } // getPublic(activity: RecentDmpModel): boolean { // return activity.isPublic; // } // getUsers(activity: RecentDmpModel): UserInfoListingModel[] { // return activity.users; // } public isAuthenticated(): boolean { return !!this.authentication.current(); } isUserOwner(activity: DmpListingModel): boolean { const principal: Principal = this.authentication.current(); if (principal) return principal.id === activity.users.find(x => x.role === Role.Owner).id; } editClicked(dmp: DmpListingModel) { this.router.navigate(['/plans/edit/' + dmp.id]); } cloneClicked(dmp: DmpListingModel) { let url = this.router.createUrlTree(['/plans/clone/', dmp.id]); window.open(url.toString(), '_blank'); } deleteClicked(dmp: DmpListingModel) { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '300px', restoreFocus: false, 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.dmpService.delete(dmp.id) .pipe(takeUntil(this._destroyed)) .subscribe( complete => { this.onCallbackSuccess() }, error => this.onDeleteCallbackError(error) ); } }); } openShareDialog(rowId: any, rowName: any) { const dialogRef = this.dialog.open(DmpInvitationDialogComponent, { // height: '250px', // width: '700px', restoreFocus: false, data: { dmpId: rowId, dmpName: rowName } }); } isDraftDmp(activity: DmpListingModel) { return activity.status == DmpStatus.Draft; } onCallbackSuccess(): void { this.uiNotificationService.snackBarNotification(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); } redirect(id: string, type: RecentActivityType) { switch (type) { case RecentActivityType.Grant: { this.router.navigate(["grants/edit/" + id]); return; } case RecentActivityType.Dataset: { this.router.navigate(["datasets/edit/" + id]); return; } case RecentActivityType.Dmp: { this.router.navigate(["plans/overview/" + id]); return; } default: throw new Error("Unsupported Activity Type "); } } // navigateToUrl() { // this.router.navigate(["plans/"]); // } roleDisplay(value: any) { 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.language.instant('DMP-LISTING.OWNER'); } else if (role === 1) { return this.language.instant('DMP-LISTING.MEMBER'); } else { return this.language.instant('DMP-LISTING.OWNER'); } } // dmpProfileDisplay(value: any) { // if (value != null) { // return value; // } // else { // return "--"; // } // } 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); }); } downloadJson(id: string) { this.dmpService.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); }) } downloadPDF(dataset: DatasetListingModel): void { this.datasetWizardService.downloadPDF(dataset.id as string) .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(dataset: DatasetListingModel): void { this.datasetWizardService.downloadDOCX(dataset.id as string) .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(dataset: DatasetListingModel): void { this.datasetWizardService.downloadXML(dataset.id as string) .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; } addDataset(activityId: String) { this.router.navigate(['/datasets/new/' + activityId]); } newVersion(id: String, label: String) { this.router.navigate(['/plans/new_version/' + id, { dmpLabel: label }]); } viewVersions(rowId: String, rowLabel: String, activity: DmpListingModel) { if (activity.public && !this.isUserOwner) { this.router.navigate(['/explore-plans/versions/' + rowId], { queryParams: { groupLabel: rowLabel } }); } else { this.router.navigate(['/plans/versions/' + rowId], { queryParams: { groupLabel: rowLabel } }); } } openDmpSearchDialogue(dataset: RecentDatasetModel) { const formControl = new FormControl(); const dialogRef = this.dialog.open(DatasetCopyDialogueComponent, { width: '500px', restoreFocus: false, data: { formControl: formControl, datasetId: dataset.id, datasetProfileId: dataset.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 } }); } }); } openConfirm(id: string): void { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '300px', restoreFocus: false, 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.datasetWizardService.delete(id) .pipe(takeUntil(this._destroyed)) .subscribe( complete => this.onCallbackSuccess(), // error => this.onCallbackError(error) ); } }); } refresh(): void { const fields: Array = []; // const fields: Array = ["-modified"]; this.startIndex = 0; const allDataTableRequest: DataTableMultiTypeRequest = new DataTableMultiTypeRequest(0, 0, this.pageSize, { fields: fields }); allDataTableRequest.criteria = new RecentActivityCriteria(); allDataTableRequest.criteria.like = this.formGroup.get("like").value; allDataTableRequest.criteria.order = this.formGroup.get("order").value; this.dashboardService .getRecentActivity(allDataTableRequest) .subscribe(response => { this.allRecentActivities = response; this.allRecentActivities.forEach(recentActivity => { if (recentActivity.type === RecentActivityType.Dataset) { this.datasetOffset = this.datasetOffset + 1; } else if (recentActivity.type === RecentActivityType.Dmp) { this.dmpOffset = this.dmpOffset + 1; } }); this.totalCountRecentEdited.emit(this.allRecentActivities.length); }); } public loadMore() { const fields: Array = []; // const fields: Array = ["-modified"]; const request = new DataTableMultiTypeRequest(this.dmpOffset, this.datasetOffset, this.pageSize, { fields: fields }); request.criteria = new RecentActivityCriteria(); request.criteria.like = this.formGroup.get("like").value ? this.formGroup.get("like").value : ""; request.criteria.order = this.formGroup.get("order").value; this.dashboardService.getRecentActivity(request).pipe(takeUntil(this._destroyed)).subscribe(result => { if (!result) { return []; } result.forEach(recentActivity => { if (recentActivity.type === RecentActivityType.Dataset) { this.datasetOffset = this.datasetOffset + 1; } else if (recentActivity.type === RecentActivityType.Dmp) { this.dmpOffset = this.dmpOffset + 1; } }); this.allRecentActivities = this.allRecentActivities.concat(result); this.totalCountRecentEdited.emit(this.allRecentActivities.length); }); } // advancedClicked(dmp: DmpListingModel) { // const dialogRef = this.dialog.open(ExportMethodDialogComponent, { // maxWidth: '500px', // data: { // message: "Download as:", // XMLButton: "XML", // documentButton: "Document", // pdfButton: "PDF", // jsonButton: "JSON" // } // }); // dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { // if (result == "pdf") { // this.downloadPDF(dmp.id); // } else if (result == "xml") { // this.downloadXml(dmp.id); // } else if (result == "doc") { // this.downloadDocx(dmp.id); // } else if (result == "json") { // this.downloadJson(dmp.id) // } // }); // } }