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

io.github.relbraun.poi.ss.handler.RowContentsHandler Maven / Gradle / Ivy

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

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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


public class RowContentsHandler extends AbstractSheetContentsHandler {

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

    private final Class beanClz;

    private final int headerRow;
    private final Map cellPropertyMap;

    private final RowListener rowListener;


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

    public RowContentsHandler(Class beanClz, RowListener rowListener) {
        this(beanClz, rowListener, 0);
    }

    public RowContentsHandler(Class beanClz, RowListener rowListener, int headerRow) {
        super();

        this.beanClz = beanClz;

        this.headerRow = headerRow;
        this.cellPropertyMap = new HashMap();

        this.rowListener = rowListener;
    }


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

    @Override
    void beforeRowStart(int rowNum) {
        // Row Callback
        // rowListener.beforeRow(rowNum);
    }


    @Override
    void afterRowEnd(int rowNum, Map rowDataMap) {
        // Sanity Checks
        if (rowDataMap == null || rowDataMap.isEmpty()) {
            return;
        }

        // Skip rows before header row
        if (rowNum < headerRow) {
            return;
        }

        if (rowNum == headerRow) {
            final Map headerMap = this.prepareHeaderMap(rowNum, rowDataMap);
            cellPropertyMap.putAll(headerMap);
            return;
        }

        // Row As Bean
        T rowBean = Spreadsheet.rowAsBean(beanClz, cellPropertyMap, rowDataMap);

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


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

    private Map prepareHeaderMap(int rowNo, Map rowDataMap) {
        // Sanity checks
        if (rowDataMap == null || rowDataMap.isEmpty()) {
            String errMsg = String.format("Invalid Header data found - Row #%d", rowNo);
            throw new RuntimeException(errMsg);
        }

        final Map colToBeanPropMap = Spreadsheet.getColumnToPropertyMap(beanClz);

        final Map headerMap = new HashMap();
        for (String collRef : rowDataMap.keySet()) {
            String colName = String.valueOf(rowDataMap.get(collRef));
            String propName = colToBeanPropMap.get(colName);
            if (StringUtils.isNotEmpty(propName)) {
                headerMap.put(collRef, String.valueOf(propName));
            }
        }

        LOGGER.debug("Header DataMap prepared : {}", headerMap);
        return headerMap;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy