import { Component, OnInit, Output, EventEmitter } from '@angular/core'; import { DatasetListingModel } from '@app/core/model/dataset/dataset-listing'; import { DatasetService } from '@app/core/services/dataset/dataset.service'; import { DataTableRequest } from '@app/core/model/data-table/data-table-request'; import { DatasetCriteria } from '@app/core/query/dataset/dataset-criteria'; import { AuthService } from '@app/core/services/auth/auth.service'; import { BaseComponent } from '@common/base/base.component'; import { Principal } from '@app/core/model/auth/principal'; import { TranslateService } from '@ngx-translate/core'; import { EnumUtils } from '@app/core/services/utilities/enum-utils.service'; import { FormControl } from '@angular/forms'; import { DatasetCopyDialogueComponent } from '@app/ui/dataset/dataset-wizard/dataset-copy-dialogue/dataset-copy-dialogue.component'; import { MatDialog } from '@angular/material'; import { takeUntil } from 'rxjs/operators'; import { Router } from '@angular/router'; import { DatasetWizardService } from '@app/core/services/dataset-wizard/dataset-wizard.service'; import * as FileSaver from 'file-saver'; import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component'; import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model'; import { UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { SnackBarNotificationLevel } from '@common/modules/notification/ui-notification-service'; import { DatasetStatus } from '@app/core/common/enum/dataset-status'; import { DmpInvitationDialogComponent } from '@app/ui/dmp/invitation/dmp-invitation.component'; @Component({ selector: 'app-recent-edited-dataset-activity', templateUrl: './recent-edited-dataset-activity.component.html', styleUrls: ['./recent-edited-dataset-activity.component.scss'] }) export class RecentEditedDatasetActivityComponent extends BaseComponent implements OnInit { @Output() totalCountDatasets: EventEmitter = new EventEmitter(); datasetActivities: DatasetListingModel[]; totalCount: number; startIndex: number = 4; pageSize: number = 5; // publicMode = false; constructor( private authentication: AuthService, private datasetService: DatasetService, private language: TranslateService, public enumUtils: EnumUtils, public dialog: MatDialog, public router: Router, private datasetWizardService: DatasetWizardService, private uiNotificationService: UiNotificationService ) { super(); } ngOnInit() { if (this.isAuthenticated()) { const fields: Array = ["-modified"]; const datasetDataTableRequest: DataTableRequest = new DataTableRequest(0, this.pageSize, { fields: fields }); datasetDataTableRequest.criteria = new DatasetCriteria(); datasetDataTableRequest.criteria.like = ""; this.datasetService .getPaged(datasetDataTableRequest) .subscribe(response => { this.datasetActivities = response.data; this.totalCount = response.totalCount; this.totalCountDatasets.emit(this.totalCount); }); } } public loadMore() { const fields: Array = ["-modified"]; const request = new DataTableRequest(this.startIndex, this.pageSize, { fields: fields }); request.criteria = new DatasetCriteria(); request.criteria.like = ""; this.datasetService.getPaged(request).pipe(takeUntil(this._destroyed)).subscribe(result => { if (!result) { return []; } this.datasetActivities = this.datasetActivities.concat(result.data); }); this.startIndex = this.startIndex + this.pageSize; } public isAuthenticated(): boolean { return !!this.authentication.current(); } 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'); } } openDmpSearchDialogue(dataset: DatasetListingModel) { 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) ); } }); } openShareDialog(dmpRowId: any, dmpRowName: any, activity: any) { const dialogRef = this.dialog.open(DmpInvitationDialogComponent, { // height: '250px', // width: '700px', restoreFocus: false, data: { dmpId: dmpRowId, dmpName: dmpRowName } }); } 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; } 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); }); } onCallbackSuccess(id?: String): void { this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success); id ? this.router.navigate(['/reload']).then(() => { this.router.navigate(['/datasets', 'edit', id]); }) : this.router.navigate(['/datasets']); } onCallbackError(error: any) { this.setErrorModel(error.error); } openUpdateDatasetProfileDialogue(id: string) { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { restoreFocus: false, data: { message: this.language.instant('DATASET-EDITOR.VERSION-DIALOG.QUESTION'), confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'), cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'), isDeleteConfirmation: false } }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { this.uiNotificationService.snackBarNotification(this.language.instant('DATASET-WIZARD.MESSAGES.SUCCESS-UPDATE-DATASET-PROFILE'), SnackBarNotificationLevel.Success); this.router.navigate(['/datasets/profileupdate/' + id]); } }); } public setErrorModel(validationErrorModel: ValidationErrorModel) { } needsUpdate(activity: DatasetListingModel) { if (activity.isProfileLatestVersion || (activity.status === DatasetStatus.Finalized) || (activity.isProfileLatestVersion == undefined && activity.status == undefined)) { return false; } else { return true; } } }