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

cdc.issues.checks.io.WorkbookCheckStatsIo Maven / Gradle / Ivy

package cdc.issues.checks.io;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import cdc.issues.checks.CheckResult;
import cdc.issues.checks.CheckStats;
import cdc.issues.rules.RuleId;
import cdc.office.ss.SheetLoader;
import cdc.office.ss.WorkbookWriter;
import cdc.office.ss.WorkbookWriterFactory;
import cdc.office.ss.WorkbookWriterFeatures;
import cdc.office.tables.Row;
import cdc.office.tables.TableSection;

/**
 * Class used to save {@link CheckStats} to a Workbook.
 *
 * @author Damien Carbonne
 *
 * @param  The type of the checked or ignored item.
 */
public class WorkbookCheckStatsIo {
    private final Function itemToString;

    private static final String DATA = "Data";
    private static final String STATS = "Stats";
    private static final String TOTAL = "Total";

    /**
     * Creates a new instance of {@link WorkbookCheckStatsIo}.
     *
     * @param itemToString The function used to convert an item to a String.
     */
    public WorkbookCheckStatsIo(Function itemToString) {
        this.itemToString = itemToString;
    }

    /**
     * Save {@link CheckStats} to a workbook file.
     *
     * @param stats The {@link CheckStats}.
     * @param file The workbook file to generate.
     * @param features The {@link WorkbookWriterFeatures features}.
     * @throws IOException When an IO error occurs.
     */
    public void save(CheckStats stats,
                     File file,
                     WorkbookWriterFeatures features) throws IOException {
        final WorkbookWriterFactory factory = new WorkbookWriterFactory();
        factory.setEnabled(WorkbookWriterFactory.Hint.POI_STREAMING, true);
        try (final WorkbookWriter writer = factory.create(file, features)) {
            saveData(stats, writer);
            saveStats(stats, writer);
        }
    }

    private void saveData(CheckStats stats,
                          WorkbookWriter writer) throws IOException {
        final CheckResult[] values = CheckResult.values();
        writer.beginSheet(DATA);
        writer.addRow(TableSection.HEADER, "Item", "Rule", "Result");
        for (final I item : stats.getItems()) {
            for (final CheckResult result : values) {
                for (final RuleId ruleId : stats.getRuleIds(item, result)) {
                    writer.addRow(TableSection.DATA,
                                  itemToString.apply(item),
                                  ruleId.toString(),
                                  result.name());
                }
            }
        }
    }

    private void saveStats(CheckStats stats,
                           WorkbookWriter writer) throws IOException {
        final CheckResult[] values = CheckResult.values();
        writer.beginSheet(STATS);
        writer.beginRow(TableSection.HEADER);
        writer.addCell("Rule");
        for (final CheckResult result : values) {
            writer.addCell(result);
        }
        writer.addCell(TOTAL);
        for (final RuleId ruleId : stats.getRuleIds().stream()
                                        .sorted()
                                        .toList()) {
            writer.beginRow(TableSection.DATA);
            writer.addCell(ruleId.toString());
            for (final CheckResult result : values) {
                writer.addCell(stats.getCounts(ruleId, result));
            }
            writer.addCell(stats.getCounts(ruleId));
        }
        writer.beginRow(TableSection.DATA);
        writer.addCell(TOTAL);
        for (final CheckResult result : values) {
            writer.addCell(stats.getCounts(result));
        }
        writer.addCell(stats.getCounts());
    }

    /**
     * Merges several workbook stats files into one.
     *
     * @param output The generated file.
     * @param inputs The input files.
* They should have been generated with {@link #save(CheckStats, File, WorkbookWriterFeatures)}. * @throws IOException When an IO error occurs. */ public static void merge(File output, List inputs) throws IOException { final WorkbookWriterFactory factory = new WorkbookWriterFactory(); factory.setEnabled(WorkbookWriterFactory.Hint.POI_STREAMING, true); try (final WorkbookWriter writer = factory.create(output, WorkbookWriterFeatures.STANDARD_FAST)) { mergeData(writer, inputs); mergeStats(writer, inputs); writer.flush(); } } private static void mergeData(WorkbookWriter writer, List inputs) throws IOException { final SheetLoader loader = new SheetLoader(); writer.beginSheet(DATA); boolean first = true; for (final File input : inputs) { final List rows = loader.load(input, null, DATA); if (first) { writer.addRow(TableSection.HEADER, rows.get(0)); } for (final Row row : rows.subList(1, rows.size())) { writer.addRow(TableSection.DATA, row); } first = false; } } private static void mergeStats(WorkbookWriter writer, List inputs) throws IOException { final int num = 5; final Map map = new HashMap<>(); final SheetLoader loader = new SheetLoader(); for (final File input : inputs) { final List rows = loader.load(input, null, STATS); rows.remove(0); rows.remove(rows.size() - 1); for (final Row row : rows) { final String key = row.getValue(0); final int[] counts = map.computeIfAbsent(key, k -> new int[num]); for (int i = 0; i < num; i++) { counts[i] += row.getValueAsInteger(1 + i, 0); } } } writer.beginSheet(STATS); writer.beginRow(TableSection.HEADER); writer.addCell("Rule"); for (final CheckResult result : CheckResult.values()) { writer.addCell(result); } writer.addCell(TOTAL); final int[] totals = new int[num]; for (final String key : map.keySet() .stream() .sorted() .toList()) { writer.beginRow(TableSection.DATA); writer.addCell(key); final int[] counts = map.get(key); for (int i = 0; i < num; i++) { writer.addCell(counts[i]); totals[i] += counts[i]; } } writer.beginRow(TableSection.DATA); writer.addCell(TOTAL); for (final int total : totals) { writer.addCell(total); } } }