mil.army.usace.data.dataquery.rdbms.implementations.OracleConverter.java Source code

Java tutorial

Introduction

Here is the source code for mil.army.usace.data.dataquery.rdbms.implementations.OracleConverter.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.rdbms.implementations;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import mil.army.usace.data.dataquery.utility.DbConverter;
import oracle.jdbc.OracleConnection;
import org.apache.commons.convert.ConversionException;
import mil.army.usace.data.dataquery.utility.ConversionUtility;

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

    private final OracleConnection oc;

    public OracleConverter(Connection conn) {
        try {
            this.oc = conn.unwrap(OracleConnection.class);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public Object convertType(Object obj, Method method)
            throws ClassNotFoundException, ConversionException, SQLException {
        Class methodParam = method.getParameterTypes()[0];
        if (obj == null)
            return null;
        else if (obj instanceof oracle.sql.TIMESTAMPTZ) {
            java.sql.Date sqlDate = ((oracle.sql.TIMESTAMPTZ) obj).dateValue(oc);
            return ConversionUtility.convertType(sqlDate, methodParam);
        } else {
            if (methodParam.isPrimitive()) {
                return obj;
            } else {
                return ConversionUtility.convertType(obj, methodParam);
            }
        }
    }

    @Override
    public Object convertType(Object obj, Field field)
            throws ClassNotFoundException, ConversionException, SQLException {
        Class fieldParam = field.getType();
        if (obj == null)
            return null;
        else if (obj instanceof oracle.sql.TIMESTAMPTZ) {
            java.sql.Date sqlDate = ((oracle.sql.TIMESTAMPTZ) obj).dateValue(oc);
            return ConversionUtility.convertType(sqlDate, fieldParam);
        } else {
            if (fieldParam.isPrimitive()) {
                return obj;
            } else {
                return ConversionUtility.convertType(obj, fieldParam);
            }
        }
    }

}