All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cdc.impex.core.ActiveExporterImpl Maven / Gradle / Ivy

package cdc.impex.core;

import java.io.File;
import java.io.IOException;
import java.util.List;

import cdc.impex.ImpExFactory;
import cdc.impex.ImpExFormat;
import cdc.impex.exports.ActiveExporter;
import cdc.impex.exports.ExportDriver;
import cdc.impex.exports.ExportIssue;
import cdc.impex.exports.ExportIssueType;
import cdc.impex.exports.ExportRow;
import cdc.impex.templates.SheetTemplate;
import cdc.issues.IssueSeverity;
import cdc.issues.IssuesHandler;
import cdc.issues.locations.WorkbookLocation;
import cdc.util.events.ProgressController;
import cdc.util.events.ProgressSupplier;
import cdc.util.time.Chronometer;

public class ActiveExporterImpl implements ActiveExporter {
    private final ImpExFactory factory;
    private ExportDriver driver;
    private IssuesHandler issuesHandler;
    private String systemId;
    private SheetTemplate template;
    private int sheetNumber = 0;
    private String sheetName;
    private int maxRowsPerSheet = -1;
    private int rowsInSheet = 0;
    private long remaining = -1L;
    private final Chronometer workbookChrono = new Chronometer();
    private final Chronometer sheetChrono = new Chronometer();
    private ExportRowImpl row;
    private final ProgressSupplier progress = new ProgressSupplier();

    public ActiveExporterImpl(ImpExFactory factory) {
        this.factory = factory;
    }

    private void beginDriverSheet() throws IOException {
        sheetChrono.start();

        // Number of rows in this physical sheet
        final long numberOfRows;
        if (remaining < 0) {
            numberOfRows = -1L;
        } else if (maxRowsPerSheet < 0) {
            // Add 1 to remaining to take into account header
            // One sheet will do the job
            numberOfRows = remaining + 1L;
        } else {
            // Add 1 to remaining to take into account header
            numberOfRows = Math.min(maxRowsPerSheet, remaining + 1L);
        }

        sheetNumber++;
        sheetName = template.getName() + "#" + sheetNumber;
        progress.reset(numberOfRows, "Generate sheet " + sheetName);

        row = new ExportRowImpl(template, sheetName, systemId);

        issue(ExportIssueType.GENERATE_SHEET,
              "Generate '" + sheetName + "' sheet with '" + template.getQName() + "' template.");
        driver.beginSheet(template, sheetName, numberOfRows);
        row.setNumber(1); // Count header
        rowsInSheet = 1;
        progress.incrementValue();
    }

    private void endDriverSheet() throws IOException {
        driver.endSheet();
        sheetChrono.suspend();
        issue(ExportIssueType.GENERATED_SHEET,
              "Generated '" + sheetName + "' sheet with '" + template.getQName() + "' template in " + sheetChrono + ".");
    }

    private boolean isFull() {
        return maxRowsPerSheet > 0 && rowsInSheet == maxRowsPerSheet;
    }

    private void beginDriverSheetIfFull() throws IOException {
        if (isFull()) {
            endDriverSheet();
            beginDriverSheet();
        }
    }

    @Override
    public void beginExport(File file,
                            IssuesHandler issuesHandler,
                            ProgressController controller) throws IOException {
        workbookChrono.start();
        this.progress.setController(controller);
        this.driver = factory.createExportDriver(file, issuesHandler);
        this.issuesHandler = issuesHandler;
        this.systemId = file.getPath();
        this.sheetName = null;
        issue(ExportIssueType.GENERATE_WORKBOOK,
              "Generate '" + systemId + "' workbook.");
        this.driver.beginExport(file);
        final ImpExFormat format = ImpExFormat.from(file);
        this.maxRowsPerSheet = format.getMaxRows();
    }

    @Override
    public void beginSheet(SheetTemplate template,
                           long numberOfRows) throws IOException {
        this.template = template;
        this.sheetNumber = 0;
        this.remaining = numberOfRows;
        beginDriverSheet();
    }

    @Override
    public ExportRow nextRow() throws IOException {
        beginDriverSheetIfFull();
        // if (isFull()) {
        // row.setNumber(1);
        // }
        row.incrementNumber();
        row.clear();
        return row;
    }

    @Override
    public void addRow() throws IOException {
        remaining--;
        rowsInSheet++;
        final List issues = row.getIssues();
        issuesHandler.issues(issues);
        driver.addRow(row);
        progress.incrementValue();
    }

    @Override
    public void endSheet() throws IOException {
        endDriverSheet();
    }

    @Override
    public void endExport() throws IOException {
        driver.endExport();
        workbookChrono.suspend();
        issue(ExportIssueType.GENERATED_WORKBOOK,
              "Generated '" + systemId + "' workbook in " + workbookChrono + ".");
    }

    @Override
    public IssuesHandler getIssuesHandler() {
        return issuesHandler;
    }

    @Override
    public void issue(ExportIssueType type,
                      IssueSeverity severity,
                      String description) {
        issuesHandler.issue(ExportIssue.builder()
                                       .name(type)
                                       .severity(severity)
                                       .description(description)
                                       .addLocation(WorkbookLocation.builder()
                                                                    .systemId(systemId)
                                                                    .sheetName(sheetName)
                                                                    .build())
                                       .build());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy