br.com.objectos.way.io.TableColumnBody Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of way-io Show documentation
Show all versions of way-io Show documentation
CSV, XLS and fixed parsers
The newest version!
/*
* Copyright 2013 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.io;
import org.apache.poi.ss.usermodel.Cell;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
/**
* @author [email protected] (Marcio Endo)
*/
abstract class TableColumnBody {
private final int index;
private final TableMethodWrapper method;
private TableStyle style;
private TableFormat format;
public TableColumnBody(int index, TableMethodWrapper method) {
this.index = index;
this.method = method;
}
protected static TableColumnBody forBoolean(int index, TableMethodWrapper method,
String valueTrue,
String valueFalse) {
return new BooleanPojo(index, method, valueTrue, valueFalse);
}
public static TableColumnBody forDouble(int index, TableMethodWrapper method) {
return new DoublePojo(index, method);
}
public static TableColumnBody forInteger(int index, TableMethodWrapper method) {
return new IntegerPojo(index, method);
}
public static TableColumnBody forLocalDate(int index, TableMethodWrapper method) {
return new LocalDatePojo(index, method);
}
public static TableColumnBody forLocalDateTime(int index, TableMethodWrapper method) {
return new LocalDateTimePojo(index, method);
}
public static TableColumnBody forString(int index, TableMethodWrapper method) {
return new StringPojo(index, method);
}
public TableColumnBody style(TableStyle style, TableFormat format) {
this.style = style;
this.format = format;
return this;
}
void apachePOI(POIRow row, Object entity) {
POICell cell = row.createCell(index);
cell.setBodyStyle(style, format);
Object value = method.call(entity);
apachePOI(cell, value);
}
abstract void apachePOI(POICell cell, Object entity);
private static class DoublePojo extends TableColumnBody {
public DoublePojo(int index, TableMethodWrapper method) {
super(index, method);
}
@Override
void apachePOI(POICell cell, Object value) {
if (value != null) {
Double val = (Double) value;
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(val.doubleValue());
}
}
}
private static class BooleanPojo extends TableColumnBody {
String valueTrue;
String valueFalse;
public BooleanPojo(int index, TableMethodWrapper method, String valueTrue, String valueFalse) {
super(index, method);
this.valueTrue = valueTrue;
this.valueFalse = valueFalse;
}
@Override
void apachePOI(POICell cell, Object value) {
if (value != null) {
Boolean val = (Boolean) value;
cell.setCellType(Cell.CELL_TYPE_STRING);
if (val.booleanValue()) {
cell.setCellValue(valueTrue);
} else {
cell.setCellValue(valueFalse);
}
}
}
}
private static class IntegerPojo extends TableColumnBody {
public IntegerPojo(int index, TableMethodWrapper method) {
super(index, method);
}
@Override
void apachePOI(POICell cell, Object value) {
if (value != null) {
Integer val = (Integer) value;
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(val.doubleValue());
}
}
}
private static class LocalDatePojo extends TableColumnBody {
public LocalDatePojo(int index, TableMethodWrapper method) {
super(index, method);
}
@Override
void apachePOI(POICell cell, Object value) {
if (value != null) {
LocalDate val = (LocalDate) value;
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(val.toDate());
}
}
}
private static class LocalDateTimePojo extends TableColumnBody {
public LocalDateTimePojo(int index, TableMethodWrapper method) {
super(index, method);
}
@Override
void apachePOI(POICell cell, Object value) {
if (value != null) {
LocalDateTime val = (LocalDateTime) value;
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(val.toDate());
}
}
}
private static class StringPojo extends TableColumnBody {
public StringPojo(int index, TableMethodWrapper method) {
super(index, method);
}
@Override
void apachePOI(POICell cell, Object value) {
if (value != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(value.toString());
}
}
}
}