package loxia.support.excel.convertor;
import java.math.BigDecimal;
import loxia.support.excel.definition.ExcelCell;
import loxia.support.excel.exception.ErrorCode;
import loxia.support.excel.exception.ExcelManipulateException;
public class BigDecimalConvertor implements DataConvertor<BigDecimal> {
public BigDecimal convert(Object value, int sheetNo, String cellIndex,
ExcelCell cellDefinition) throws ExcelManipulateException {
if(value == null && cellDefinition.isMandatory())
throw new ExcelManipulateException(ErrorCode.WRONG_DATA_NULL,
new Object[]{sheetNo + 1, cellIndex,
null,cellDefinition.getPattern(),
cellDefinition.getChoiceString()});
if(value == null) return null;
if(value instanceof String){
String str = (String) value;
str = str.trim();
if(str.length() == 0){
if(cellDefinition.isMandatory())
throw new ExcelManipulateException(ErrorCode.WRONG_DATA_NULL,
new Object[]{sheetNo + 1, cellIndex,
null,cellDefinition.getPattern(),
cellDefinition.getChoiceString()});
else
return null;
}else{
try {
return new BigDecimal((String)value);
} catch (NumberFormatException e) {
throw new ExcelManipulateException(ErrorCode.WRONG_DATA_TYPE_NUMBER,
new Object[]{sheetNo + 1, cellIndex,
value,cellDefinition.getPattern(),
cellDefinition.getChoiceString()});
}
}
}else if(value instanceof Double){
return new BigDecimal((Double)value);
}else
throw new ExcelManipulateException(ErrorCode.WRONG_DATA_TYPE_NUMBER,
new Object[]{sheetNo + 1, cellIndex,
value,cellDefinition.getPattern(),
cellDefinition.getChoiceString()});
}
public String getDataTypeAbbr() {
return "bigdecimal";
}
public Class<BigDecimal> supportClass() {
return BigDecimal.class;
}
}