/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library 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 any later version.
 * 
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): Helene Joanin
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas_ejb.genic;


import org.objectweb.jonas_ejb.lib.JavaType;


/**
 * This class is the "Velocity context" for a parameter of a where clause
 * used in the Velocity Templates.
 */

public class VcParamWhere {

    private String  mName;
    private String  mTypeName;
    private String  mSqlTypeName;
    private String  mSqlSetMethod;
    private boolean hasNotPrimitiveType;
    private boolean hasBigIntegerType;
    private boolean hasSerializableType;
    private boolean hasJavaLangTypeExceptString;

    VcParamWhere(Class type, int position) {

	mName = new String("p"+position);
	mTypeName = JavaType.getName(type);
	mSqlTypeName = JavaType.getSQLType(type);
	mSqlSetMethod = JavaType.getSQLSetMethod(type);
	if (mSqlSetMethod == null) {
	    throw new Error("Cannot container persistence manage the type '"
			    +type.getName()+"'");
	}
	hasNotPrimitiveType = ! type.isPrimitive();
	hasBigIntegerType = type.equals(java.math.BigInteger.class);
	hasSerializableType = "setSerializable".equals(mSqlSetMethod);
	hasJavaLangTypeExceptString = false;
	if (type.getPackage() != null) {
	    if ("java.lang".equals(type.getPackage().getName())
		&& ! java.lang.String.class.equals(type)) {
		hasJavaLangTypeExceptString = true;
	    }
	}

	// System.out.print("VcParamWhere: "+type.toString());
	// System.out.println(toString());
    }

    
    public String getName() {
	return mName;
    }public String getTypeName() {
	return mTypeName;
    }
    public String getSqlTypeName() {
	return mSqlTypeName;
    }
    public String getSqlSetMethod() {
	return mSqlSetMethod;
    }
    public boolean hasNotPrimitiveType() {
	return hasNotPrimitiveType;
    }
    public boolean hasBigIntegerType() {
	return hasBigIntegerType;
    }
    public boolean hasSerializableType() {
	return hasSerializableType;
    }
    public boolean hasJavaLangTypeExceptString() {
	return hasJavaLangTypeExceptString;
    }

    public String toString() {
	StringBuffer ret = new StringBuffer();
	ret.append("\n    Name                 = "+getName());
	ret.append("\n    TypeName             = "+getTypeName());
	ret.append("\n    SqlTypeName          = "+getSqlTypeName());
	ret.append("\n    SqlSetMethod         = "+getSqlSetMethod());
	ret.append("\n    hasNotPrimitiveType  = "+hasNotPrimitiveType());
	ret.append("\n    hasBigIntegerType    = "+hasBigIntegerType());
	ret.append("\n    hasSerializableType  = "+hasSerializableType());
	ret.append("\n    hasJavaLangTypeExceptString = "+hasJavaLangTypeExceptString());
	return(ret.toString());
    }

}
