All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jpattern.orm.generator.ResultSetMethodHelper Maven / Gradle / Ivy

There is a newer version: 3.5.1
Show newest version
package com.jpattern.orm.generator;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.RowId;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Map;
import java.util.HashMap;

import com.jpattern.orm.exception.OrmConfigurationException;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.generator.wrapper.ByteToBigDecimalWrapper;
import com.jpattern.orm.generator.wrapper.DoubleToBigDecimalWrapper;
import com.jpattern.orm.generator.wrapper.FloatToBigDecimalWrapper;
import com.jpattern.orm.generator.wrapper.IntegerToBigDecimalWrapper;
import com.jpattern.orm.generator.wrapper.JodaDateMidnightToSqlDateWrapper;
import com.jpattern.orm.generator.wrapper.LongToBigDecimalWrapper;
import com.jpattern.orm.generator.wrapper.ShortToBigDecimalWrapper;
import com.jpattern.orm.generator.wrapper.TypeWrapper;
import com.jpattern.orm.generator.wrapper.JodaDateTimeToSqlTimestampWrapper;
import com.jpattern.orm.generator.wrapper.JodaLocalDateTimeToSqlTimestampWrapper;
import com.jpattern.orm.generator.wrapper.JodaLocalDateToSqlTimestampWrapper;
import com.jpattern.orm.generator.wrapper.UtilDateToSqlTimestampWrapper;

/**
 * 
 * @author Francesco Cina
 * 
 *         06/giu/2011
 */
public class ResultSetMethodHelper {

	private Map, Method> RESULTSET_GETTER_FOR_INT = new HashMap, Method>();
	private Map, Method> RESULTSET_SETTER_FOR_STRING = new HashMap, Method>();
	private Map, TypeWrapper> WRAPPER_METHODS = new HashMap, TypeWrapper>();

	public ResultSetMethodHelper() {
		try {
			this.addGetter(Array.class, "getArray");
			this.addGetter(BigDecimal.class, "getBigDecimal");
			this.addGetter(InputStream.class, "getBinaryStream");
			this.addGetter(Blob.class, "getBlob");
			this.addGetter(Boolean.TYPE, "getBoolean");
			this.addGetter(Byte.TYPE, "getByte");
			this.addGetter(byte[].class, "getBytes");
			this.addGetter(Reader.class, "getCharacterStream");
			this.addGetter(Clob.class, "getClob");
			this.addGetter(Date.class, "getDate");
			this.addGetter(Double.TYPE, "getDouble");
			this.addGetter(Float.TYPE, "getFloat");
			this.addGetter(Integer.TYPE, "getInt");
			this.addGetter(Long.TYPE, "getLong");
			this.addGetter(NClob.class, "getNClob");
			this.addGetter(Object.class, "getObject");
			this.addGetter(Ref.class, "getRef");
			this.addGetter(RowId.class, "getRowId");
			this.addGetter(Short.TYPE, "getShort");
			this.addGetter(SQLXML.class, "getSQLXML");
			this.addGetter(String.class, "getString");
			this.addGetter(Time.class, "getTime");
			this.addGetter(Timestamp.class, "getTimestamp");
			this.addGetter(URL.class, "getURL");

			//After initialization
			this.addWrappers(new UtilDateToSqlTimestampWrapper());
			this.addWrappers(new ByteToBigDecimalWrapper());
			this.addWrappers(new DoubleToBigDecimalWrapper());
			this.addWrappers(new FloatToBigDecimalWrapper());
			this.addWrappers(new IntegerToBigDecimalWrapper());
			this.addWrappers(new LongToBigDecimalWrapper());
			this.addWrappers(new ShortToBigDecimalWrapper());
			try {
				this.addWrappers(new JodaDateTimeToSqlTimestampWrapper());
				this.addWrappers(new JodaDateMidnightToSqlDateWrapper());
				this.addWrappers(new JodaLocalDateTimeToSqlTimestampWrapper());
				this.addWrappers(new JodaLocalDateToSqlTimestampWrapper());
			} catch (final Throwable e) {}

		} catch (Exception e) {
			throw new OrmException(e);
		}

	}

	public final Method findResultSetGetterForString(final Class type) throws SecurityException, NoSuchMethodException, OrmConfigurationException {
		if (this.RESULTSET_SETTER_FOR_STRING.containsKey(type)) {
			return this.RESULTSET_SETTER_FOR_STRING.get(type);
		}
		throw new OrmConfigurationException("Type [" + type + "] is not supported at this time");
	}

	public final Method findResultSetGetterForInt(final Class type) throws SecurityException, NoSuchMethodException, OrmConfigurationException {
		if (this.RESULTSET_GETTER_FOR_INT.containsKey(type)) {
			return this.RESULTSET_GETTER_FOR_INT.get(type);
		}
		throw new OrmConfigurationException("Type [" + type + "] is not supported at this time");
	}

	private void addGetter(final Class key, final String resultSetMethodName) throws SecurityException, NoSuchMethodException {
		this.RESULTSET_GETTER_FOR_INT.put(key, ResultSet.class.getMethod(resultSetMethodName, new Class[] { int.class }));
		this.RESULTSET_SETTER_FOR_STRING.put(key, ResultSet.class.getMethod(resultSetMethodName, new Class[] { String.class }));
	}

	public  void addWrappers(final TypeWrapper wrapper) throws SecurityException, OrmConfigurationException, NoSuchMethodException {
		this.WRAPPER_METHODS.put(wrapper.wrapperClass(), wrapper);
		this.addGetter( wrapper.wrapperClass() , this.findResultSetGetterForInt( wrapper.resultSetTypeToUse() ).getName() );
	}

	@SuppressWarnings("unchecked")
	public  TypeWrapper getWrapper(final Class clazz) throws SecurityException, NoSuchMethodException {
		if (this.WRAPPER_METHODS.containsKey(clazz)) {
			return (TypeWrapper) this.WRAPPER_METHODS.get(clazz);
		}
		throw new OrmConfigurationException("Type [" + clazz + "] has no wrapper associated");
	}

	public boolean isWrappedType(final Class clazz) throws SecurityException, NoSuchMethodException {
		return this.WRAPPER_METHODS.containsKey(clazz);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy