/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.dom.vehicle;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.starobjects.jpa.applib.usertypes.ImmutableUserType;


public class RegistrationNumberType extends ImmutableUserType {

    public Class<RegistrationNumber> returnedClass() {
        return RegistrationNumber.class;
    }

    public Object nullSafeGet(
    		final ResultSet rs, 
    		final String[] names, 
    		final Object owner) throws SQLException {
        final String value = rs.getString(names[0]);
        return rs.wasNull() ? null : new RegistrationNumber(value);
    }

    public void nullSafeSet(
    			final PreparedStatement st, 
    			final Object value, 
    			final int index) throws HibernateException,
            SQLException {
        if (value == null) {
            st.setNull(index, Hibernate.STRING.sqlType());
        } else {
            RegistrationNumber registrationNumber = (RegistrationNumber) value;
			st.setString(index, registrationNumber.getValue());
        }
    }

    public int[] sqlTypes() {
        return new int[] { Hibernate.STRING.sqlType() };
    }
}
// Copyright (c) Naked Objects Group Ltd.
