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

org.sfm.poi.impl.DynamicSheetMapper Maven / Gradle / Ivy

package org.sfm.poi.impl;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.sfm.csv.CsvColumnKey;
import org.sfm.map.*;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.MapperKey;
import org.sfm.map.mapper.MapperCache;
import org.sfm.map.mapper.MapperKeyComparator;
import org.sfm.poi.SheetMapper;
import org.sfm.poi.SheetMapperBuilder;
import org.sfm.reflect.meta.ClassMeta;
import org.sfm.utils.RowHandler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
//IFJAVA8_START
import java.util.stream.Stream;
//IFJAVA8_END


public class DynamicSheetMapper implements SheetMapper {

    private final int startRow = 0;


    private final MapperCache> mapperCache =
            new MapperCache>(MapperKeyComparator.csvColumnKeyComparator());
    private final ClassMeta classMeta;
    private final MapperConfig> mapperConfig;
    private final GetterFactory getterFactory;

    public DynamicSheetMapper(
            ClassMeta classMeta,
            MapperConfig> mapperConfig,
            GetterFactory getterFactory) {
        this.classMeta = classMeta;
        this.mapperConfig = mapperConfig;
        this.getterFactory = getterFactory;
    }


    @Override
    public Iterator iterator(Sheet sheet) {
        return iterator(startRow, sheet);
    }

    @Override
    public Iterator iterator(int startRow, Sheet sheet) {
        return getPoiMapper(startRow, sheet).iterator(startRow + 1 , sheet);
    }

    @Override
    public > RH forEach(Sheet sheet, RH rowHandler) {
        return forEach(startRow, sheet, rowHandler);
    }

    @Override
    public > RH forEach(int startRow, Sheet sheet, RH rowHandler) {
        return getPoiMapper(startRow, sheet).forEach(startRow + 1, sheet, rowHandler);
    }

    //IFJAVA8_START
    @Override
    public Stream stream(Sheet sheet) {
        return stream(startRow, sheet);
    }

    @Override
    public Stream stream(int startRow, Sheet sheet) {
        return getPoiMapper(startRow, sheet).stream(startRow + 1, sheet);
    }
    //IFJAVA8_END

    private SheetMapper getPoiMapper(int startRow, Sheet sheet) {
        Row row = sheet.getRow(startRow);

        List keys = new ArrayList(row.getLastCellNum() - row.getFirstCellNum());
        for(short i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
                keys.add(new CsvColumnKey(cell.getStringCellValue(), i));
            }
        }

        return getPoiMapper(new MapperKey(keys.toArray(new CsvColumnKey[0])));


    }

    private SheetMapper getPoiMapper(MapperKey key) {
        SheetMapper mapper = mapperCache.get(key);

        if (mapper == null) {
            final SheetMapperBuilder builder =
                    new SheetMapperBuilder(
                            classMeta,
                            mapperConfig,
                            getterFactory);

            for(CsvColumnKey k : key.getColumns()) {
                builder.addMapping(k, FieldMapperColumnDefinition.identity());
            }

            mapper = builder.mapper();

            mapperCache.add(key, mapper);
        }
        return mapper;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy