/*
 * 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 java.lang.reflect.Field;

import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
import org.objectweb.jonas_ejb.deployment.api.FieldDesc;
import org.objectweb.jonas_ejb.deployment.api.FieldJdbcDesc;
import org.objectweb.jonas_ejb.lib.JavaType;


/**
 * This class is the "Velocity context" for a container managed persistence bean's field
 * used in the Velocity Template.
 */

public class VcField {

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

    VcField(Field field, FieldDesc fd) {

	mName = field.getName();
	Class fType = field.getType();
	mTypeName = JavaType.getName(fType);
	mDefaultValue = JavaType.getDefaultValue(fType);
	mSqlTypeName = JavaType.getSQLType(fType);
	mSqlGetMethod = JavaType.getSQLGetMethod(fType);
	mSqlSetMethod = JavaType.getSQLSetMethod(fType);
	if ((mSqlGetMethod == null) || (mSqlSetMethod == null)) {
	    throw new Error("Cannot container persistence manage the field "+mName
			    +"' with type '"+fType.getName()+"'");
	}
	hasNotPrimitiveType = ! fType.isPrimitive();
	hasBigIntegerType = fType.equals(java.math.BigInteger.class);
	hasSerializableType = "setSerializable".equals(mSqlSetMethod)
	    && "getSerializable".equals(mSqlGetMethod);
	hasJavaLangTypeExceptString = false;
	if (fType.getPackage() != null) {
	    if ("java.lang".equals(fType.getPackage().getName())
		&& ! java.lang.String.class.equals(fType)) {
		hasJavaLangTypeExceptString = true;
	    }
	}

	// System.out.print("VcField: "+field.toString());
	// System.out.println(toString());
    }

    public String getName() {
	return mName;
    }
    public String getTypeName() {
	return mTypeName;
    }
    public String getDefaultValue() {
	return mDefaultValue;
    }
    public String getSqlTypeName() {
	return mSqlTypeName;
    }
    public String getSqlGetMethod() {
	return mSqlGetMethod;
    }
    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    DefaultValue         = "+getDefaultValue());
	ret.append("\n    SqlTypeName          = "+getSqlTypeName());
	ret.append("\n    SqlGetMethod         = "+getSqlGetMethod());
	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());
    }

}
