
com.welemski.wrench.poi.util.SpreadsheetQuery Maven / Gradle / Ivy
The newest version!
/*
* The MIT License
*
* Copyright 2016 Lemuel Raganas.
*
* 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.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
/**
*
* @author Lemuel Raganas
*/
public class SpreadsheetQuery extends Delegable implements SheetQuery{
private int limit = 0;
private int atRowIndex = 0;
private final String kCondition = "condition";
private Workbook workbook;
private String sheetName;
private int sheetIndex = 0;
public SpreadsheetQuery(){}
public SpreadsheetQuery(Workbook workbook){
this.workbook = workbook;
}
protected List findAllBySheetName(Workbook workbook,String sheetName, int limit, int atRowIndex){
if(workbook == null){
return new ArrayList<>();
}
Sheet sheet = this.getSheetNamed(sheetName);
if(sheet == null){
return new ArrayList<>();
}
return this.getAllRows(sheet, limit, atRowIndex);
}
protected List findAllBySheetIndex(Workbook workbook, int sheetIndex, int limit, int atRowIndex){
if(workbook == null){
return new ArrayList<>();
}
Sheet sheet = this.getSheetAtIndex(sheetIndex);
if(sheet == null){
return new ArrayList<>();
}
return this.getAllRows(sheet, limit, atRowIndex);
}
protected Sheet getSheetNamed(String sheetName){
if(this.workbook == null){
return null;
}
if(sheetName == null){
return null;
}
return this.workbook.getSheet(sheetName);
}
protected Sheet getSheetAtIndex(int index){
if(this.workbook == null){
return null;
}
return this.workbook.getSheetAt(index);
}
protected List getAllRows(Sheet sheet, int limit, int atRowIndex){
List rows = new ArrayList<>();
if( atRowIndex > 0){
if ((atRowIndex + 1 ) > sheet.getPhysicalNumberOfRows()) {
return rows;
}
}
for(int i = atRowIndex ; i 0 && rows.size() >= 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 SheetQuery where(SpreadsheetQueryCondition condition) {
this.addDelegate(kCondition, condition);
return this;
}
@Override
public SheetQuery from(String sheetName) {
this.sheetName = sheetName;
return this;
}
@Override
public SheetQuery from(int sheetIndex) {
this.sheetIndex = sheetIndex;
return this;
}
@Override
public List findAll() {
if(this.sheetName == null){
return this.findAllBySheetIndex(workbook, sheetIndex, limit, atRowIndex);
}
return this.findAllBySheetName(workbook, sheetName, limit, atRowIndex);
}
@Override
public List findAll(int limit) {
this.limit = limit;
return this.findAll();
}
@Override
public SpreadsheetRow get() {
List list = this.findAll(1);
if(list.isEmpty()){
return null;
}
return list.get(0);
}
@Override
public SpreadsheetRow get(int atIndex) {
this.atRowIndex = atIndex;
Sheet sheet = this.sheetName == null ? this.getSheetAtIndex(this.sheetIndex) : this.getSheetNamed(this.sheetName);
if(sheet == null){
return null;
}
if(sheet.getPhysicalNumberOfRows() <= atRowIndex ){
return null;
}
Row row = sheet.getRow(atRowIndex);
SpreadsheetRow sr = new SpreadsheetRow(row);
return sr;
}
@Override
public int count() {
List findAll = this.findAll();
return findAll.size();
}
@Override
public SheetQuery setLimit(int limit) {
this.limit = limit;
return this;
}
@Override
public SheetQuery atRow(int rowIndex) {
this.atRowIndex = rowIndex;
return this;
}
@Override
public Workbook getWorkbook() {
return this.workbook;
}
@Override
public SpreadsheetCell getCell(String cellAddress) {
CellReference cellRef = new CellReference(cellAddress);
Sheet sheet = this.sheetName == null ? this.getSheetAtIndex(this.sheetIndex) : this.getSheetNamed(this.sheetName);
if(sheet == null){
// Incase no sheet was specified and sheet reference exists in the cell address
sheet = this.getSheetNamed(cellRef.getSheetName());
}
if(sheet == null){
return null;
}
Row row = sheet.getRow(cellRef.getRow());
if(row == null){
return null;
}
Cell cell = row.getCell(cellRef.getCol());
if(cell == null){
return null;
}
return new SpreadsheetCell(cell);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy