/*
 * 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): ____________________________________.
 * Contributor(s): ______________________________________.
 * 
 *    JOnAS 2.2.1 Joe Gittings has proposed to code method signature in order to avoid
 *                same signature for inherited methods.
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas_ejb.lib; 

import java.io.File;
import java.util.*;
import java.lang.reflect.Method;

import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
import org.objectweb.jonas_ejb.deployment.api.EntityCmpDesc;


/**
 * This class is made for hiding Naming convention in all JOnAS framework.
 * <br>
 * (Among other things, names of the implementation classes of
 * the Enterprise Bean's Home and Enterprise Bean Remote interfaces
 * generated by the GenIC tools.)
 * <br>
 * It is used by the EJB generation tools and framework
 */
public class BeanNaming {
    
    private String fullWrpRemoteName = null;
    private String wrpRemoteName = null;

    private String fullWrpHomeName = null;
    private String wrpHomeName = null;

    private String fullWrpLocalName = null;
    private String wrpLocalName = null;

    private String fullWrpLocalHomeName = null;
    private String wrpLocalHomeName = null;

    private String fullWrpHandleName = null;
    private String wrpHandleName = null;

    private String fullDerivedBeanName = null;
    private String derivedBeanName = null;

    /**
     * Create a BeanNaming Object from the Deployment Descriptor.
     * <br>
     * (Generally speaking FullXName means packageName + Name X)
     */
    public BeanNaming(BeanDesc dd) {

	String packageName;

	// Build a String that identifies the bean from its name.
	String ejbName = dd.getEjbName();
	char[] tabChar = new char[ejbName.length()];
	for (int i = 0; i < ejbName.length(); i++) {
	    if (!Character.isJavaIdentifierPart(ejbName.charAt(i))) {
		tabChar[i] = '_';
	    } else {
		tabChar[i] = ejbName.charAt(i);
	    }
	}
	String ejbNameLike = new String(tabChar); 

	if (dd.getRemoteClass() != null) {
	    packageName = getPackageName(dd.getRemoteClass().getName());
	    wrpRemoteName = new String("JOnAS"+ejbNameLike+"Remote");
	    wrpHandleName = new String("JOnAS"+ejbNameLike+"Handle");
	    if (packageName.equals("")) {
		fullWrpRemoteName = new String(wrpRemoteName);
		fullWrpHandleName = new String(wrpHandleName);
	    } else {
		fullWrpRemoteName = new String(packageName+"."+wrpRemoteName);
		fullWrpHandleName = new String(packageName+"."+wrpHandleName);
	    }
	}

	if (dd.getLocalClass() != null) {
	    packageName = getPackageName(dd.getLocalClass().getName());
	    wrpLocalName = new String("JOnAS"+ejbNameLike+"Local");
	    if (packageName.equals("")) {
		fullWrpLocalName = new String(wrpLocalName);
	    } else {
		fullWrpLocalName = new String(packageName+"."+wrpLocalName);
	    }
	}

	if (dd.getHomeClass() != null) {
	    packageName = getPackageName(dd.getHomeClass().getName());
	    wrpHomeName = new String("JOnAS"+ejbNameLike+"Home");
	    if (packageName.equals("")) {
		fullWrpHomeName = new String(wrpHomeName);
	    } else {
		fullWrpHomeName = new String(packageName+"."+wrpHomeName);
	    }
	}

	if (dd.getLocalHomeClass() != null) {
	    packageName = getPackageName(dd.getLocalHomeClass().getName());
	    wrpLocalHomeName = new String("JOnAS"+ejbNameLike+"LocalHome");
	    if (packageName.equals("")) {
		fullWrpLocalHomeName = new String(wrpLocalHomeName);
	    } else {
		fullWrpLocalHomeName = new String(packageName+"."+wrpLocalHomeName);
	    }
	}

	if (dd instanceof EntityCmpDesc) {
	    packageName = getPackageName(dd.getEjbClass().getName());
	    derivedBeanName = new String("JOnAS"+ejbNameLike+"Bean");
	    if (packageName.equals("")) {
		fullDerivedBeanName = new String(derivedBeanName);
	    } else {
		fullDerivedBeanName = new String(packageName+"."+derivedBeanName);
	    }
	} else {
	    fullDerivedBeanName = new String(dd.getEjbClass().getName());
	    derivedBeanName     = new String(getBaseName(dd.getEjbClass().getName()));
	}
    }

    /**
     * returns the name of the package of the given full name
     */
    public static String getPackageName(String fullName) {
	String pkg = new String();
	StringTokenizer stk = new StringTokenizer(fullName, ".");
	int nb = stk.countTokens();
	for (int i = 0; i < nb - 1; i++) {
	    pkg = pkg.concat(stk.nextToken());
	    if (i < nb-2) {
		pkg = pkg.concat(".");
	    }
	}	
	return pkg;
    }

    /**
     * returns the full path of the file name.
     * mainly replace '.' by '/'.
     */
    public static String getPath(String fullName) {
	String pkg = new String();
	StringTokenizer stk = new StringTokenizer(fullName, ".");
	int nb = stk.countTokens();
	for (int i = 0; i < nb; i++) {
	    pkg = pkg.concat(stk.nextToken());
	    if (i < nb - 1) {
		pkg = pkg + File.separatorChar;
	    }
	}
	return pkg;
    }

    /**
     * returns the basename of the given full name
     */
    public static String getBaseName(String fullName) {
	String baseName;
	StringTokenizer stk = new StringTokenizer(fullName, ".");
	int nb = stk.countTokens();
	String pkg = new String();
	for (int i = 0; i < nb - 1; i++) {
	    pkg = pkg.concat(stk.nextToken());
	    if (i < nb - 2) {
		pkg = pkg.concat(".");
	    }
	}
	if (nb > 0) {
	    baseName = new String(stk.nextToken());
	} else {
	    baseName = new String();
	}
	return baseName;
    }

    /**
     * returns the given string with the first character converted to upper case
     */
    public static String firstToUpperCase(String s) {
	String value;
	if (s.length() > 0) {
	    char c = Character.toUpperCase(s.charAt(0));
	    value = new String(c+s.substring(1));
	} else {
	    value = new String();
	}
	return(value);
    }

    /**
     * returns the full name of the wrapper class for the bean
     */
    public String getFullWrpRemoteName() {
	return fullWrpRemoteName;
    }

    /**
     * returns the full name of the wrapper class for the bean
     */
    public String getFullWrpLocalName() {
	return fullWrpLocalName;
    }

    /**
     * returns the name of the wrapper class for the bean
     */
    public String getWrpRemoteName() {
	return wrpRemoteName;
    }

    /**
     * returns the name of the wrapper class for the bean
     */
    public String getWrpLocalName() {
	return wrpLocalName;
    }

    /**
     * returns the full name of the Handle class for the bean
     */
    public String getFullWrpHandleName() {
	return fullWrpHandleName;
    }

    /**
     * returns the name of the Handle class for the bean
     */
    public String getWrpHandleName() {
	return wrpHandleName;
    }

    /**
     * returns the full name of the wrapper home class for the bean
     */
    public String getFullWrpHomeName() {
	return fullWrpHomeName;
    }

    /**
     * returns the full name of the wrapper local home class for the bean
     */
    public String getFullWrpLocalHomeName() {
	return fullWrpLocalHomeName;
    }

    /**
     * returns the name of the wrapper home class for the bean
     */
    public String getWrpHomeName() {
	return wrpHomeName;
    }

    /**
     * returns the name of the wrapper local home class for the bean
     */
    public String getWrpLocalHomeName() {
	return wrpLocalHomeName;
    }

    /**
     * returns the full name of the derived class of the bean
     */
    public String getFullDerivedBeanName() {
	return fullDerivedBeanName;
    }

    /**
     * returns the name of the derived class of the bean
     */
    public String getDerivedBeanName() {
	return derivedBeanName;
    }


    /**
     * returns the name of the JOnAS specific deployment descriptor file's name
     * corresponding to the given standard deployment descriptor file's name.
     * (ex: returns "jonas-XXX.xml" for "XXX.xml" and "../../jonas-XXX.xml" for "../../XXX.xml")
     */
    public static String getJonasXmlName(String stdXmlName) {

	String jonasXmlName = new String();
	File f = new File(stdXmlName);
	String p = f.getParent();
	String n = f.getName();
	if (p != null) {
	    jonasXmlName = jonasXmlName.concat(p);
	    jonasXmlName = jonasXmlName.concat(File.separator);
	}
	jonasXmlName = jonasXmlName.concat("jonas-");
	jonasXmlName = jonasXmlName.concat(n);

	return(jonasXmlName);
    }

    /**
     * returns a string that represents the signature of a method
     *
     */
    public static String getSignature(String prefix, Method method) {

	String signature = new String();
	String classname = method.getDeclaringClass().getName();
	String methodname = method.getName();
	Class [] types = method.getParameterTypes();
	String parameters = new String("_"+types.length+":");
	for (int i=0; i< types.length; i++) {
	    parameters += JavaType.getName(types[i])+" ";
	}
	signature = prefix +":"+ classname+"."+methodname+parameters;
	return signature;
    }
}

