/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ejb.plugins.jaws.metadata;

import java.sql.Types;

import org.w3c.dom.Element;

import org.jboss.deployment.DeploymentException;
import org.jboss.logging.Logger;
import org.jboss.metadata.MetaData;
import org.jboss.metadata.XmlLoadable;

/**
 *  <description>
 *
 *  @see <related>
 *  @author <a href="sebastien.alborini@m4x.org">Sebastien Alborini</a>
 *  @version $Revision: 1.6.4.1 $
 */
public class MappingMetaData extends MetaData implements XmlLoadable
{
    // Constants -----------------------------------------------------

    // Attributes ----------------------------------------------------
    protected static Logger log = Logger.getLogger(MappingMetaData.class);
    private String javaType;
    
    private int jdbcType;
    
    private String sqlType;
    
    
    // Static --------------------------------------------------------
    
    public static int getJdbcTypeFromName(String name) throws DeploymentException {     
        if (name == null) {
            throw new DeploymentException("jdbc-type cannot be null");
        }
        
        try {
            Integer constant = (Integer)Types.class.getField(name).get(null);
            return constant.intValue();
        
        } catch (Exception e) {
            log.warn("Unrecognized jdbc-type: " + name + ", using Types.OTHER", e);
            return Types.OTHER;
        }
    }
    
    
    // Constructors --------------------------------------------------

    // Public --------------------------------------------------------

    public String getJavaType() { return javaType; }
    
    public int getJdbcType() { return jdbcType; }
    
    public String getSqlType() { return sqlType; }
    
    
    // XmlLoadable implementation ------------------------------------
    
    public void importXml(Element element) throws DeploymentException {
    
        javaType = getElementContent(getUniqueChild(element, "java-type"));
        
        jdbcType = getJdbcTypeFromName(getElementContent(getUniqueChild(element, "jdbc-type")));
        
        sqlType = getElementContent(getUniqueChild(element, "sql-type"));
    }   
    
    // Package protected ---------------------------------------------

    // Protected -----------------------------------------------------

    // Private -------------------------------------------------------

    // Inner classes -------------------------------------------------
}
