org.vulpe.controller.struts.commons.beans.converter.DecimalConverter.java Source code

Java tutorial

Introduction

Here is the source code for org.vulpe.controller.struts.commons.beans.converter.DecimalConverter.java

Source

/**
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 *
 * Este programa  software livre; voc pode redistribu-lo e/ou
 * modific-lo sob os termos da Licena Pblica Geral GNU, conforme
 * publicada pela Free Software Foundation; tanto a verso 2 da
 * Licena como (a seu critrio) qualquer verso mais nova.
 *
 * Este programa  distribudo na expectativa de ser til, mas SEM
 * QUALQUER GARANTIA; sem mesmo a garantia implcita de
 * COMERCIALIZAO ou de ADEQUAO A QUALQUER PROPSITO EM
 * PARTICULAR. Consulte a Licena Pblica Geral GNU para obter mais
 * detalhes.
 *
 * Voc deve ter recebido uma cpia da Licena Pblica Geral GNU
 * junto com este programa; se no, escreva para a Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.vulpe.controller.struts.commons.beans.converter;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import ognl.TypeConverter;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vulpe.commons.helper.VulpeConfigHelper;
import org.vulpe.config.annotations.VulpeApplication;

import com.opensymphony.xwork2.conversion.TypeConversionException;

@SuppressWarnings({ "rawtypes" })
public class DecimalConverter extends AbstractVulpeBaseTypeConverter implements TypeConverter {
    private static final Logger LOG = LoggerFactory.getLogger(DecimalConverter.class);

    public Object convert(final Class type, final Object value) {
        try {
            final VulpeApplication vulpeApplication = VulpeConfigHelper.getApplicationConfiguration();
            final String currencySymbol = vulpeApplication.view().showCurrencySymbol()
                    ? vulpeApplication.view().currencySymbol()
                    : "";
            final String locale[] = vulpeApplication.localeCode().split("_");
            final DecimalFormat valueFormat = new DecimalFormat(currencySymbol + "#,##0.00",
                    new DecimalFormatSymbols(new Locale(locale[0], locale[1])));
            if (value instanceof String) {
                if (StringUtils.isNotBlank(value.toString())) {
                    String newValue = StringUtils.replace(value.toString(), currencySymbol, "");
                    newValue = StringUtils.replace(newValue, " ", "");
                    newValue = StringUtils.replace(newValue, ".", "");
                    newValue = StringUtils.replace(newValue, ",", ".");
                    return new BigDecimal(newValue);
                }
            } else if (value instanceof Double && String.class.equals(type)) {
                return valueFormat.format(value);
            } else if (value instanceof Double) {
                return value;
            }
        } catch (Exception e) {
            LOG.error("Error on convert decimal value: " + value);
            throw new TypeConversionException("Error on convert decimal value: " + value, e);
        }
        return null;
    }

}