Report module

The Report module allows you to generate a data export in the preferred format.

To activate a section with a Report module, the settings in the relevant data section must be configured, specifying the form and the class of implementation of the methods.

 

 

The module requires you to set up a class inherited from ModuleReport in which the form validation methods of the export settings (optional), the report name, the data format (optional) and the actual generation of the report must be implemented.

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using DataWeb.Newsletter;
using DataWeb.Data.Exchange;
using System.Threading;

namespace DataWeb.Structure.Modules
{
    public class ModuleSubscriberReport(Section section, NavigationContext navigationContext, IServiceProvider serviceProvider) : ModuleReport(section, navigationContext, serviceProvider)
    {
        private readonly IExcelService excelService = serviceProvider.GetService<IExcelService>();
        private readonly INewsletterSubscriberService newsletterSubscriberService = serviceProvider.GetService<INewsletterSubscriberService>();

        public override Task<string> GetReportNameAsync(Item item, Dictionary<string, object> moduleData, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(string.Format("Subscribers_{0:yyyy-MM-dd}.xlsx", DateTime.Now));
        }

        public override async Task<Stream> ProcessReportAsync(Item item, Dictionary<string, object> moduleData, CancellationToken cancellationToken = default)
        {
            var subscriberInfos = new List<ModuleSubscriberReportInfo>();

            var subscribers = await newsletterSubscriberService.GetSubscribersAsync(new NewsletterSubscriberFilter(), cancellationToken);

            foreach (var subscriber in subscribers)
            {
                subscriberInfos.Add(new ModuleSubscriberReportInfo()
                {
                    Email = subscriber.Email,
                    Status = Convert.ToString(subscriber.Status),
                    SubscriptionDate = subscriber.SubscriptionDate,
                    AdditionalValues = newsletterSubscriberService.SerializeAdditionalValues(subscriber.AdditionalValues),
                    Culture = subscriber.Culture,
                    Country = subscriber.Country,
                    Zone = subscriber.Zone,
                    Interests = string.Join(",", subscriber.Interests),
                    Groups = string.Join(",", subscriber.Groups),
                    Source = subscriber.Source
                });
            }

            return await excelService.ListToStreamAsync(subscriberInfos, "Subscribers", cancellationToken);
        }
    }
}

 

In this example, we're generating an Excel file with newsletter subscribers.

 

This table can be used as a reference to identify areas of the ModuleForm base class that can be customized by creating a derived class.

 

MethodDescriptionPossible customization
ValidateReportDataAsyncValidate the data needed to generate a report for a specific item. Verifies that the values provided meet the requirements defined on the associated form.Overwritable to add custom validation rules based on report structure or application-specific conditions.
GetReportNameAsyncRetrieves the file name for the generated report.Overridable to set dynamic file names based on item-specific data or custom parameters (e.g., including date or item identifiers).
GetContentTypeAsyncRetrieves the content type (MIME type) of the generated report.Overwritable to define specific content types (e.g. application/pdf, text/csv, etc.) based on the format of the generated report.
ProcessReportAsyncGenerate report content as a stream. This method processes the data associated with the item and produces the report as a file.Overwritable to implement report generation logic, such as complex data processing, formatting in PDF, Excel, CSV, or other required formats.