/*
 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source 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, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.amber.type;

import java.util.HashMap;

import com.caucho.bytecode.JClass;

import com.caucho.util.L10N;

import com.caucho.config.ConfigException;
  
/**
 * The manages the types known to the Amber instnce.
 */
public class TypeManager {
  private static final L10N L = new L10N(TypeManager.class);

  private static HashMap<String,Type> _builtinTypes;
  private static HashMap<String,Type> _primitiveTypes;

  private HashMap<String,Type> _typeMap;

  public TypeManager()
  {
    _typeMap = new HashMap<String,Type>();

    _typeMap.putAll(_builtinTypes);
  }
  
  /**
   * Returns the type.
   */
  public Type create(String name)
    throws ConfigException
  {
    Type type = _typeMap.get(name);
    //System.out.println("\nXXX TypeManager.create(name)");
    //System.out.println("type: " + type);
    //System.out.println("name: " + name);

    if (type != null)
      return type;

    throw new ConfigException(L.l("'{0}' is an unknown type", name));
  }
  
  /**
   * Returns the type.
   */
  public Type create(JClass cl)
    throws ConfigException
  {
    Type type = _primitiveTypes.get(cl.getName());

    if (type != null)
      return type;
    
    type = _typeMap.get(cl.getName());

    return type;
  }
  
  /**
   * Returns the type.
   */
  public Type get(String name)
  {
    return _typeMap.get(name);
  }
  
  /**
   * Returns the type.
   */
  public EntityType getEntityByInstanceClass(String name)
  {
    for (Type type : _typeMap.values()) {
      if (type instanceof EntityType) {
	EntityType entityType = (EntityType) type;

	if (name.equals(entityType.getInstanceClassName()))
	  return entityType;
      }
    }

    return null;
  }

  /**
   * Adds a type.
   */
  public void put(String name, Type type)
  {
    Type oldType = _typeMap.get(name);

    if (oldType != null && oldType != type)
      throw new IllegalStateException(L.l("'{0}' is a duplicate type",
					  name));
    
    _typeMap.put(name, type);
  }

  static {
    _builtinTypes = new HashMap<String,Type>();

    _builtinTypes.put("boolean", BooleanType.create());
    _builtinTypes.put("java.lang.Boolean", BooleanType.create());
    _builtinTypes.put("yes_no", YesNoType.create());
    _builtinTypes.put("true_false", TrueFalseType.create());

    _builtinTypes.put("byte", ByteType.create());
    _builtinTypes.put("java.lang.Byte", ByteType.create());
    
    _builtinTypes.put("character", CharacterType.create());
    _builtinTypes.put("java.lang.Character", CharacterType.create());
    
    _builtinTypes.put("short", ShortType.create());
    _builtinTypes.put("java.lang.Short", ShortType.create());
    
    _builtinTypes.put("integer", IntegerType.create());
    _builtinTypes.put("java.lang.Integer", IntegerType.create());
    
    _builtinTypes.put("long", LongType.create());
    _builtinTypes.put("java.lang.Long", LongType.create());
    
    _builtinTypes.put("float", FloatType.create());
    _builtinTypes.put("java.lang.Float", FloatType.create());

    _builtinTypes.put("double", DoubleType.create());
    _builtinTypes.put("java.lang.Double", DoubleType.create());

    _builtinTypes.put("string", StringType.create());
    _builtinTypes.put("java.lang.String", StringType.create());
    
    _builtinTypes.put("date", SqlDateType.create());
    _builtinTypes.put("java.sql.Date", SqlDateType.create());
    
    _builtinTypes.put("time", SqlTimeType.create());
    _builtinTypes.put("java.sql.Time", SqlTimeType.create());
    
    _builtinTypes.put("timestamp", SqlTimestampType.create());
    _builtinTypes.put("java.sql.Timestamp", SqlTimestampType.create());
    
    _builtinTypes.put("java.util.Date", UtilDateType.create());
    _builtinTypes.put("java.util.Calendar", CalendarType.create());

    //XXX Need test case for timestamp
    _builtinTypes.put("timestamp", BigDecimalType.create());
    _builtinTypes.put("java.math.BigDecimal", BigDecimalType.create());
    
    _builtinTypes.put("blob", BlobType.create());
    _builtinTypes.put("java.sql.Blob", BlobType.create());
    _builtinTypes.put("clob", BlobType.create());
    _builtinTypes.put("java.sql.Clob", ClobType.create());
    
    _builtinTypes.put("[B", ByteArrayType.create());
    _builtinTypes.put("[byte", ByteArrayType.create());
    
    _builtinTypes.put("class", ClassType.create());
    _builtinTypes.put("java.lang.Class", ClassType.create());
    
    _primitiveTypes = new HashMap<String,Type>();

    _primitiveTypes.put("boolean", PrimitiveBooleanType.create());
    _primitiveTypes.put("char", PrimitiveCharType.create());
    _primitiveTypes.put("byte", PrimitiveByteType.create());
    _primitiveTypes.put("short", PrimitiveShortType.create());
    _primitiveTypes.put("int", PrimitiveIntType.create());
    _primitiveTypes.put("long", PrimitiveLongType.create());
    _primitiveTypes.put("float", PrimitiveFloatType.create());
    _primitiveTypes.put("double", PrimitiveDoubleType.create());
  }
}
