mil.army.usace.data.dataquery.utility.DefaultConverter.java Source code

Java tutorial

Introduction

Here is the source code for mil.army.usace.data.dataquery.utility.DefaultConverter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mil.army.usace.data.dataquery.utility;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.commons.convert.ConversionException;
import org.apache.commons.convert.Converter;
import org.apache.commons.convert.Converters;

/**
 *
 * @author k3endrsg
 */
public class DefaultConverter implements DbConverter {

    public <S> Object convertType(S source, Class target) throws ClassNotFoundException, ConversionException {
        if (source == null)
            return null;
        else {
            Converter<S, ?> converter = (Converter<S, ?>) Converters.getConverter(source.getClass(), target);
            return converter.convert(source);
        }
    }

    //@TODO..should handle some basic object to primitive cases...e.g. BigDecimal to double or int for oracle
    @Override
    public Object convertType(Object obj, Field field) throws ClassNotFoundException, ConversionException {
        Class fieldParam = field.getType();
        if (obj == null)
            return null;
        else {
            if (fieldParam.isPrimitive()) {
                return obj;
            } else {
                return convertType(obj, fieldParam);
            }
        }
    }

    @Override
    public Object convertType(Object obj, Method method) throws ClassNotFoundException, ConversionException {
        Class methodParam = method.getParameterTypes()[0];
        if (obj == null)
            return null;
        else {
            if (methodParam.isPrimitive()) {
                return obj;
            } else {
                return convertType(obj, methodParam);
            }
        }
    }

}