UNPKG

@adaptabletools/adaptable

Version:

Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

285 lines (284 loc) 12.6 kB
import * as ExportRedux from '../../Redux/ActionsReducers/ExportRedux'; import { getScheduledReports } from '../../Utilities/Helpers/Scheduling/ScheduledReportHelper'; import { ApiBase } from './ApiBase'; import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants'; import { ExportInternalApi } from '../Internal/ExportInternalApi'; import { ALL_DATA_REPORT, CLIPBOARD_EXPORT_DESTINATION, DOWNLOAD_EXPORT_DESTINATION, EMPTY_STRING, SYSTEM_EXPORT_DESTINATIONS, SYSTEM_REPORT_FORMATS, SYSTEM_REPORT_NAMES, } from '../../Utilities/Constants/GeneralConstants'; export class ExportApiImpl extends ApiBase { internalApi; constructor(_adaptable) { super(_adaptable); this.internalApi = new ExportInternalApi(_adaptable); } getExportState() { return this.getAdaptableState().Export; } getCurrentReportName() { return this.getExportState().CurrentReport; } getCurrentReportFormat() { return this.getExportState().CurrentFormat; } getCurrentReport() { const reportName = this.getCurrentReportName(); return this.getReportByName(reportName); } getReportByName(reportName) { if (this.internalApi.isSystemReport(reportName)) { return this.internalApi.createSystemReport(reportName); } const customReport = this.getCustomReports().find((r) => r.Name == reportName); if (customReport) { return customReport; } } getReportById(id) { return this.getAllReports()?.find((report) => report.Uuid === id); } getDestinationByName(destinationName) { if (this.internalApi.isSystemDestination(destinationName)) { return destinationName; } return this.getAvailableCustomDestinations().find((destination) => destination.name === destinationName); } getAvailableSystemReports() { const systemReportNamesOption = this.getExportOptions().systemReportNames; const reportNames = typeof systemReportNamesOption === 'function' ? systemReportNamesOption({ ...this.getAdaptableInternalApi().buildBaseContext(), defaultSystemReportNames: SYSTEM_REPORT_NAMES, currentLayoutName: this.getLayoutApi().getCurrentLayoutName(), }) : systemReportNamesOption; return reportNames.filter((s) => this.internalApi.isSystemReportActive(s)); } getAvailableSystemFormats() { const systemReportFormatsOption = this.getExportOptions().systemReportFormats; const reportFormats = typeof systemReportFormatsOption === 'function' ? systemReportFormatsOption({ ...this.getAdaptableInternalApi().buildBaseContext(), defaultSystemReportFormats: SYSTEM_REPORT_FORMATS, currentLayoutName: this.getLayoutApi().getCurrentLayoutName(), currentReportName: this.getExportApi().getCurrentReportName(), }) : systemReportFormatsOption; return reportFormats.filter((format) => { if (this.getGridApi().isMasterDetailGrid()) { return format !== 'JSON'; } if (this.getExportApi().getCurrentReportName() === ALL_DATA_REPORT && this.getLayoutApi().isCurrentLayoutPivot()) { return format !== 'VisualExcel'; } return true; }); } getAvailableSystemDestinations() { const systemExportDestinationsOption = this.getExportOptions().systemExportDestinations; const systemExportDestinations = typeof systemExportDestinationsOption === 'function' ? systemExportDestinationsOption({ ...this.getAdaptableInternalApi().buildBaseContext(), defaultSystemExportDestinations: SYSTEM_EXPORT_DESTINATIONS, currentLayoutName: this.getLayoutApi().getCurrentLayoutName(), currentReportName: this.getExportApi().getCurrentReportName(), currentReportFormat: this.getExportApi().getCurrentReportFormat(), }) : systemExportDestinationsOption; return systemExportDestinations; } getAvailableCustomDestinations() { const customDestinationsOption = this.getExportOptions().customDestinations; const customDestinations = typeof customDestinationsOption === 'function' ? customDestinationsOption({ ...this.getAdaptableInternalApi().buildBaseContext(), currentLayoutName: this.getLayoutApi().getCurrentLayoutName(), currentReportName: this.getExportApi().getCurrentReportName(), currentReportFormat: this.getExportApi().getCurrentReportFormat(), }) : customDestinationsOption; return customDestinations ?? []; } isExportDestinationSystem(destinationName) { return (destinationName == DOWNLOAD_EXPORT_DESTINATION || destinationName == CLIPBOARD_EXPORT_DESTINATION); } getAllExportDestinations() { const destinationItems = [ ...this.getAvailableSystemDestinations(), ...this.getAvailableCustomDestinations().map((destination) => destination.name), ]; return destinationItems; } getAllFormats() { return this.getAvailableSystemFormats(); } getAllReports() { const reports = this.getAvailableSystemReports() .map((s) => this.getReportByName(s)) .concat(this.getExportState().Reports); return reports; } selectReport(reportName) { this.dispatchAction(ExportRedux.ReportSelect(reportName)); } clearReport() { this.selectReport(EMPTY_STRING); } selectFormat(reportFormat) { this.dispatchAction(ExportRedux.FormatSelect(reportFormat)); } clearFormat() { this.selectFormat(null); } getExportDestinationForm(destinationName) { return this.getAvailableCustomDestinations().find((destination) => destination.name === destinationName)?.form; } canExportToExcel() { return this._adaptable.canExportToExcel(); } canExportToCsv() { return this._adaptable.canExportToCsv(); } openExportSettingsPanel() { this.showModulePopup(ModuleConstants.ExportModuleId); } updateReport(report) { this.dispatchAction(ExportRedux.ReportEdit(report)); return this.getReportById(report.Uuid); } updateReports(reports) { reports.forEach((report) => { this.updateReport(report); }); return reports?.map((report) => this.getReportById(report.Uuid)); } getCustomReports() { return this.getExportState().Reports ?? []; } ensureScheduledReportUuids() { this.dispatchAction(ExportRedux.ExportEnsureScheduleUuids()); } getScheduledReports(_config) { return getScheduledReports(this.getExportState()); } getActiveScheduledReports(_config) { return getScheduledReports(this.getExportState()).filter((d) => !d.IsSuspended); } getScheduledReportById(reportScheduleId, config) { return this.getScheduledReports(config).find((d) => d.Uuid === reportScheduleId); } addScheduledReport(reportSchedule) { this.dispatchAction(ExportRedux.ScheduledReportAdd(reportSchedule)); return this.getScheduledReportById(reportSchedule.Uuid); } editScheduledReport(reportSchedule) { this.dispatchAction(ExportRedux.ScheduledReportEdit(reportSchedule)); return this.getScheduledReportById(reportSchedule.Uuid); } deleteScheduledReport(reportSchedule) { this.dispatchAction(ExportRedux.ScheduledReportDelete(reportSchedule)); } suspendScheduledReport(reportSchedule) { this.dispatchAction(ExportRedux.ScheduledReportSuspend(reportSchedule)); return this.getScheduledReportById(reportSchedule.Uuid); } unSuspendScheduledReport(reportSchedule) { this.dispatchAction(ExportRedux.ScheduledReportUnSuspend(reportSchedule)); return this.getScheduledReportById(reportSchedule.Uuid); } applyScheduledReport(reportSchedule) { this.exportReport(reportSchedule.ReportName, reportSchedule.ReportFormat, reportSchedule.ExportDestination ?? 'Download'); this.dispatchAction(ExportRedux.ScheduledReportJobRun(reportSchedule)); this.getAdaptableApi().eventApi.internalApi.fireScheduledReportRanEvent(reportSchedule); } isColumnExportable(adaptableColumn) { const isExportableFn = this.getExportOptions().isColumnExportable; if (typeof isExportableFn === 'function') { const adaptableColumnContext = { ...this.getAdaptableInternalApi().buildBaseContext(), column: adaptableColumn, }; return isExportableFn(adaptableColumnContext); } return true; } getSupportedExportDestinations(reportFormat) { if (reportFormat == undefined) { return []; } return this.getExportApi() .getAllExportDestinations() .filter((destination) => { if (reportFormat === 'Excel' || reportFormat === 'VisualExcel') { return destination !== 'Clipboard'; } return true; }); } async exportReport(reportName, format, destination = 'Download', exportConfig) { let report = this.getReportByName(reportName); if (!this.checkItemExists(report, reportName, 'Report')) { return; } this.logInfo(`Start Export of ${reportName} in format ${format} to ${destination}`); const processExportResult = await this.processExport(report, format, destination); if (!processExportResult) { this.logInfo(`Cancel Export of ${reportName}`); return; } if (processExportResult === true) { const exportedReport = await this._adaptable.agGridExportAdapter.exportData({ report, format, destination, showProgressIndicator: exportConfig?.showProgressIndicator === false ? false : true, customExportParams: exportConfig?.exportParams, }); if (!exportedReport) { return; } this.internalApi.sendReportToDestination(exportedReport, report, format, destination); } else { const userProcessedExportResult = processExportResult; if (!userProcessedExportResult) { this.logWarn(`Processing of Export ${reportName} to ${destination} returned null or undefined`); return; } this.internalApi.sendReportToDestination(userProcessedExportResult, report, format, destination); } this.logInfo(`Finished Export of ${reportName} in format ${format} to ${destination}`); } async getReportData(reportName, format, config) { let report = this.getReportByName(reportName); if (!this.checkItemExists(report, reportName, 'Report')) { return; } this.logInfo(`Start getting Report Data for ${reportName} in format ${format}`); const processExportResult = await this.processExport(report, format, 'Clipboard'); let reportData; if (typeof processExportResult?.type === 'string') { reportData = processExportResult; } else { reportData = await this._adaptable.agGridExportAdapter.exportData({ report, format, destination: 'Clipboard', showProgressIndicator: config?.showProgressIndicator === false ? false : true, customExportParams: config?.exportParams, }); } this.logInfo(`Finished getting Report Data for ${reportName} in format ${format}`); return reportData; } async processExport(report, format, destination) { const processExportFn = this.getExportOptions().processExport; if (!processExportFn) { return true; } this.logInfo(`Process Export of ${report.Name} in format ${format} to ${destination}`); const processExportContext = this.internalApi.buildProcessExportContext(report, format, destination); return processExportFn(processExportContext); } }