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

io.github.millij.poi.ss.handler.RowContentsAsMapHandler Maven / Gradle / Ivy

The newest version!
package io.github.millij.poi.ss.handler;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.millij.poi.util.Spreadsheet;


/**
 * SheetContentsHandler impl for reading row as {@link Map}
 * 
 * @since 3.1.0
 */
public class RowContentsAsMapHandler extends AbstractSheetContentsHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(RowContentsAsMapHandler.class);

    private final RowListener> listener;

    private final int headerRowNum;
    private final Map headerCellRefsMap;

    private final int lastRowNum;


    // Constructors
    // ------------------------------------------------------------------------

    public RowContentsAsMapHandler(RowListener> listener, int headerRowNum, int lastRowNum) {
        super();

        // init
        this.listener = listener;

        this.headerRowNum = headerRowNum;
        this.headerCellRefsMap = new HashMap<>();

        this.lastRowNum = lastRowNum;
    }


    // AbstractSheetContentsHandler Methods
    // ------------------------------------------------------------------------

    @Override
    void beforeRowStart(final int rowNum) {
        try {
            // Row Callback
            listener.beforeRow(rowNum);
        } catch (Exception ex) {
            String errMsg = String.format("Error calling #beforeRow callback  row - %d", rowNum);
            LOGGER.error(errMsg, ex);
        }
    }


    @Override
    void afterRowEnd(final int rowNum, final Map rowDataMap) {
        // Sanity Checks
        if (Objects.isNull(rowDataMap) || rowDataMap.isEmpty()) {
            LOGGER.debug("INVALID Row data Passed - Row #{}", rowNum);
            return;
        }

        // Skip rows before Header ROW and after Last ROW
        if (rowNum < headerRowNum || rowNum > lastRowNum) {
            return;
        }

        // Process Header ROW
        if (rowNum == headerRowNum) {
            final Map headerCellRefs = this.asHeaderNameToCellRefMap(rowDataMap);
            headerCellRefsMap.putAll(headerCellRefs);
            return;
        }

        // Check for Column Definitions before processing NON-Header ROWs

        // Row As Bean
        final Map rowBean = Spreadsheet.rowAsMap(headerCellRefsMap, rowDataMap);
        if (Objects.isNull(rowBean)) {
            LOGGER.debug("Unable to construct Row data Bean object - Row #{}", rowNum);
            return;
        }

        // Row Callback
        try {
            listener.row(rowNum, rowBean);
        } catch (Exception ex) {
            String errMsg = String.format("Error calling #row callback  row - %d, bean - %s", rowNum, rowBean);
            LOGGER.error(errMsg, ex);
        }
    }


    // Private Methods
    // ------------------------------------------------------------------------

    private Map asHeaderNameToCellRefMap(final Map headerRowData) {
        // Sanity checks
        if (Objects.isNull(headerRowData) || headerRowData.isEmpty()) {
            return new HashMap<>();
        }

        // Get Bean Column definitions
        final Map headerCellRefs = new HashMap();
        for (final String colRef : headerRowData.keySet()) {
            final Object header = headerRowData.get(colRef);

            final String headerName = Objects.isNull(header) ? "" : String.valueOf(header);
            headerCellRefs.put(headerName, colRef);
        }

        LOGGER.debug("Header Name to Cell Refs : {}", headerCellRefs);
        return headerCellRefs;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy