package org.bigk.invoices.converters;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.util.StrutsTypeConverter;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.conversion.TypeConversionException;
public abstract class BigDecimalConverter extends StrutsTypeConverter {
public static final Log logger = LogFactory.getLog(BigDecimalConverter.class);
@SuppressWarnings("unchecked")
@Override
public Object convertFromString(Map map, String[] as, Class class1) {
if (logger.isDebugEnabled()) {
logger.debug("convertFromString(Map map=" + map + ", String[] as=" + as + ", Class class1=" + class1 + ") - start");
}
if (as == null || as.length != 1)
return performFallbackConversion(map, as, class1);
BigDecimal res = null;
String mask = null;
if (StringUtils.isNotEmpty(as[0])) {
try {
ActionSupport action =
(ActionSupport) ActionContext.getContext().getActionInvocation().getAction();
// najpierw RegEx
String regEx = this.getRegEx(action);
if (StringUtils.isNotEmpty(regEx)) {
if (!Pattern.matches(regEx, as[0])) {
logger.warn("convertFromString(Map, String[], Class) - " + as[0] + " nie pasuje do RegEx-a:: " + regEx);
throw new TypeConversionException(as[0] + " nie pasuje do RegEx-a:: " + regEx);
}
}
mask = this.getMask(action);
DecimalFormat nf = new DecimalFormat(mask);
Number number = nf.parse(as[0]);
res = BigDecimal.valueOf(number.doubleValue());
if (logger.isDebugEnabled())
logger.debug("convertFromString(Map, String[], Class) - " + as[0] + " -> " + res);
} catch (ParseException ex) {
logger.warn("convertFromString(Map, String[], Class) - " + as[0] + ", mask: " + mask);
throw new TypeConversionException("No nie da sie tego przekonwertowac ciolku!", ex);
}
}
if (logger.isDebugEnabled()) {
logger.debug("convertFromString(Map, String[], Class) - end - return value=" + res);
}
return res;
}
@SuppressWarnings("unchecked")
@Override
public String convertToString(Map map, Object obj) {
if (logger.isDebugEnabled()) {
logger.debug("convertToString(Map map=" + map + ", Object obj=" + obj + ") - start");
}
String res = obj != null ? obj.toString() : StringUtils.EMPTY;
if (obj != null && obj instanceof BigDecimal) {
ActionSupport action =
(ActionSupport) ActionContext.getContext().getActionInvocation().getAction();
String mask = this.getMask(action);
DecimalFormat nf = new DecimalFormat(mask);
res = nf.format((BigDecimal) obj);
}
if (logger.isDebugEnabled()) {
logger.debug("convertToString(Map, Object) - end - return value=" + res);
}
return res;
}
protected abstract String getMask(ActionSupport action);
protected String getRegEx(ActionSupport action) {
return null;
}
public static void main(String[] args) throws Exception {
// {0,number,,##0.00}
String str = "123456";
DecimalFormat nf = new DecimalFormat("#");
Number number = nf.parse(str);
BigDecimal res = BigDecimal.valueOf(number.doubleValue());
System.out.println(res);
System.out.println(nf.format(new BigDecimal(123456d)));
}
}