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

com.welemski.wrench.poi.util.DefaultSpreadsheetQuery Maven / Gradle / Ivy

/*
 * The MIT License
 *
 * Copyright 2016 welemski.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.welemski.wrench.poi.util;

import com.welemski.wrench.delegate.Delegable;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 *
 * @author Lemuel Raganas
 */
public class DefaultSpreadsheetQuery extends Delegable implements SpreadsheetQuery{
    
    private String kCondition = "condition";
    private String kStructure = "structure";
    private Workbook workbook;
    private String sheetName;
    private int sheetIndex = 0;
    
    public DefaultSpreadsheetQuery(){}
    public DefaultSpreadsheetQuery(Workbook workbook){
        this.workbook = workbook;
    }


    protected List findAll(Workbook workbook,String sheetName, Integer sheetIndex, Integer limit){
        List rows = new ArrayList<>();
        Sheet sheet = null;
        if(workbook == null){
            return new ArrayList<>();
        }
        if(sheetName != null && !"".equals(sheetName)){
            sheet = workbook.getSheet(sheetName);
        } else if(sheetIndex != null ){
            sheet = workbook.getSheetAt(sheetIndex);
        }

        if(sheet == null){
            return new ArrayList<>();
        }
        
        if( null == limit ){
            rows = this.getAllRows(sheet);
        } else if( limit > 0 ){
            rows = this.getAllRows(sheet,limit);
        } 
        return rows;
    }
    
    protected List getAllRows(Sheet sheet) {
        List rows = new ArrayList<>();
        sheet.forEach((Row row)->{
            boolean skip = false;
            SpreadsheetRow sr = new SpreadsheetRow(row);

            if(this.hasDelegate(kCondition)){
                skip = !((SpreadsheetQueryCondition)this.getDelegate(kCondition)).where(this, sr);
            }

            if(!skip){
                rows.add(sr);
            }             
        });
        
        return rows;
    }
    
    protected List getAllRows(Sheet sheet, int limit){
        List rows = new ArrayList<>();

        for(int i=0 ; i= limit ) {
                return rows;
            }
            
            Row row = sheet.getRow(i);
            SpreadsheetRow sr = new SpreadsheetRow(row);
            boolean skip = false;
            if(this.hasDelegate(kCondition)){
                skip = !((SpreadsheetQueryCondition)this.getDelegate(kCondition)).where(this, sr);
            }
            
            if(!skip){
                rows.add(sr);
            }
        }
        
        return rows;
    }
    

    @Override
    public SpreadsheetQuery where(SpreadsheetQueryCondition condition) {
        this.addDelegate(kCondition, condition);
        return this;
    }

    @Override
    public SpreadsheetQuery from(String sheetName) {
        this.sheetName = sheetName;
        return this;
    }

    @Override
    public SpreadsheetQuery from(int sheetIndex) {
        this.sheetIndex = sheetIndex;
        return this;
    }

    @Override
    public List findAll() {
       return this.findAll(workbook, sheetName, sheetIndex, null);
    }

    @Override
    public List findAll(int limit) {
        return this.findAll(workbook, sheetName, sheetIndex, limit);
    }
    
    @Override
    public SpreadsheetRow get() {
        List list = this.findAll(1);
        if(list.isEmpty()){
            return null;
        }
        
        return list.get(0);
    }


    @Override
    public SpreadsheetRow get(int atIndex) {
        Sheet sheet = null;
        if(workbook == null){
            return null;
        }
        if(sheetName != null || !"".equals(sheetName)){
            sheet = workbook.getSheet(sheetName);
        }
        
        if(sheet == null){
            return null;
        }
        
        sheet = workbook.getSheetAt(sheetIndex);
        
        if(sheet == null){
            return null;
        }
        
        if(sheet.getPhysicalNumberOfRows() <= atIndex ){
            return null;
        }
        
        Row row = sheet.getRow(atIndex);
        SpreadsheetRow sr = new SpreadsheetRow(row);
        return sr;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy