001/*
002 * Copyright 2008 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.sys;
017
018import org.directwebremoting.ConversionException;
019import org.directwebremoting.extend.Converter;
020import org.directwebremoting.extend.ConverterManager;
021import org.directwebremoting.extend.InboundVariable;
022import org.directwebremoting.extend.OutboundContext;
023import org.kuali.rice.core.api.util.type.KualiDecimal;
024import org.kuali.rice.core.api.util.type.KualiInteger;
025
026import uk.ltd.getahead.dwr.compat.BaseV10Converter;
027
028/**
029 * Converter for all Kuali Numbers (KualiDecimal & KualiInteger)
030 * 
031 * @see org.kuali.rice.core.api.util.type.KualiDecimal
032 * @see org.kuali.rice.core.api.util.type.KualiInteger
033 */
034public class KualiNumberConverter extends BaseV10Converter implements Converter {
035    /**
036     * @see uk.ltd.getahead.dwr.Converter#init(uk.ltd.getahead.dwr.DefaultConfiguration)
037     */
038    public void setConverterManager(ConverterManager config) {
039    }
040
041    /**
042     * @see uk.ltd.getahead.dwr.Converter#convertInbound(java.lang.Class, java.util.List, uk.ltd.getahead.dwr.InboundVariable,
043     *      uk.ltd.getahead.dwr.InboundContext)
044     */
045    public Object convertInbound(Class paramType, InboundVariable iv) throws ConversionException {
046        String value = iv.getValue();
047        try {
048            if (paramType == KualiDecimal.class) {
049                return new KualiDecimal(value.trim());
050            }
051
052            if (paramType == KualiInteger.class) {
053                return new KualiInteger(value.trim());
054            }
055            String message = MessageBuilder.buildMessage("BigNumberConverter.NonPrimitive", paramType.getName()).getMessage();
056            throw new ConversionException(paramType,message);
057        }
058        catch (NumberFormatException ex) {
059            String message = MessageBuilder.buildMessage("BigNumberConverter.FormatError", paramType.getName()).getMessage();
060            throw new ConversionException(paramType, message, ex);
061        }
062    }
063
064    /*
065     * (non-Javadoc)
066     * 
067     * @see uk.ltd.getahead.dwr.Converter#convertOutbound(java.lang.Object, java.lang.String, uk.ltd.getahead.dwr.OutboundContext)
068     */
069    public String convertOutbound(Object object, String varname, OutboundContext outctx) {
070        return "var " + varname + "=" + object.toString() + ';'; //$NON-NLS-1$ //$NON-NLS-2$
071    }
072}